Merge pull request #77 from gfleury/create_gh_actions

Enable github actions and other improvements
This commit is contained in:
George Fleury 2023-08-30 12:50:21 +02:00 committed by GitHub
commit d09bc7e3bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 424 additions and 337 deletions

33
.github/workflows/pr.yaml vendored Normal file
View File

@ -0,0 +1,33 @@
name: Go
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.21.x', 'stable' ]
steps:
- uses: actions/checkout@v3
- name: Setup Go ${{ matrix.go-version }}
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: Display Go version
run: go version
- name: Format
run: go fmt . && git diff --exit-code
- name: Build
run: go build -v ./...
- name: Test
run: go test -v ./...
- name: Test Integration
run: INTEGRATION=TRUE go test -v ./...

View File

@ -1,23 +0,0 @@
# Swagger Codegen Ignore
# Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen
# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.
# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line:
#ApiClient.cs
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md

View File

@ -1 +0,0 @@
2.3.0

View File

@ -1,26 +0,0 @@
language: go
go:
- 1.11.x
- 1.13.x
- 1.14.x
- master
env:
- GO111MODULE=on
GOLANGCI_LINT_VERSION=1.35.2
services:
- docker
before_install:
- docker pull gfleury/bb-demo:demo
- docker run -e BITBUCKET_HOME=/mnt --rm --name="bitbucket" -d -p 7990:7990 -p 7999:7999 gfleury/bb-demo:demo
- docker ps -a
- curl http://localhost:7990/mvc/login?nextUrl=/dashboard |grep "Log in"; while [ $? != 0 ]; do sleep 2; curl http://localhost:7990/mvc/login?nextUrl=/dashboard |grep "Log in"; done
script:
- go test -coverprofile=coverage.txt -covermode=atomic
- INTEGRATION=TRUE go test
- go build -v ./
- if [[ "$(go version)" =~ "go version go1.11" ]]; then exit 0; else curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin "v${GOLANGCI_LINT_VERSION}" && golangci-lint run -v; fi
after_success:
- bash <(curl -s https://codecov.io/bash)

View File

@ -717,7 +717,7 @@ func NewBitbucketAPIResponse(r *http.Response) (*APIResponse, error) {
// NewRawAPIResponse create new API response from http.response with raw data
func NewRawAPIResponse(r *http.Response) (*APIResponse, error) {
response := &APIResponse{Response: r}
raw, err := ioutil.ReadAll(r.Body)
raw, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}

File diff suppressed because it is too large Load Diff

View File

@ -6,6 +6,7 @@ package bitbucketv1
import (
"io"
"log"
"net/http"
"net/http/httptest"
"os"
@ -14,12 +15,21 @@ import (
"time"
"golang.org/x/net/context"
sw "github.com/gfleury/go-bitbucket-v1/test/bb-mock-server/go"
)
var runIntegrationTests bool
func TestAAAAA(t *testing.T) {
runIntegrationTests = os.Getenv("INTEGRATION") == "TRUE"
if runIntegrationTests {
go func() {
log.Fatal(sw.RunServer(7990))
}()
time.Sleep(2 * time.Second)
}
}
func generateContextCanceled() context.Context {
@ -437,10 +447,10 @@ func TestDefaultApiService_Create(t *testing.T) {
{"networkErrorContextExceeded", fields{client: generateConfigFake()}, args{}, &APIResponse{Message: "Post https://stash.domain.com/rest/api/1.0/projects//repos//pull-requests: context canceled"}, true, false},
{"InvalidRequest", fields{client: generateConfigRealLocalServer()},
args{projectKey: "PROJ",
repositorySlug: "repo1",
repositorySlug: "repo1_test1",
localVarOptionals: map[string]interface{}{"values": "values"}},
&APIResponse{
Message: "Status: 400 , Body: {errors:[{context:null,message:title must be supplied for this request,exceptionName:null}]}",
Message: "Status: 400 Bad Request, Body: {errors:[{context:null,exceptionName:null,message:title must be supplied for this request}]}",
Values: map[string]interface{}{
"errors": []interface{}{
map[string]interface{}{
@ -454,7 +464,7 @@ func TestDefaultApiService_Create(t *testing.T) {
true, true},
{"ValidRequestNoBranch", fields{client: generateConfigRealLocalServer()},
args{projectKey: "PROJ",
repositorySlug: "repo1",
repositorySlug: "repo1_test2",
localVarOptionals: map[string]interface{}{
"title": "test PR",
"description": "test Desc",
@ -483,7 +493,7 @@ func TestDefaultApiService_Create(t *testing.T) {
},
},
&APIResponse{
Message: `Status: 404 , Body: {errors:[{context:null,message:Repository \repo1\ of project with key \PROJ\ has no branch \refs/heads/feature\,exceptionName:com.atlassian.bitbucket.commit.NoSuchCommitException}]}`,
Message: `Status: 404 Not Found, Body: {errors:[{context:null,exceptionName:com.atlassian.bitbucket.commit.NoSuchCommitException,message:Repository \repo1\ of project with key \PROJ\ has no branch \refs/heads/feature\}]}`,
Values: map[string]interface{}{
"errors": []interface{}{
map[string]interface{}{
@ -538,11 +548,11 @@ func TestDefaultApiService_CreatePullRequest(t *testing.T) {
{"networkErrorContextExceeded", fields{client: generateConfigFake()}, args{}, &APIResponse{Message: "Post https://stash.domain.com/rest/api/1.0/projects//repos//pull-requests: context canceled"}, true, false},
{"InvalidRequest", fields{client: generateConfigRealLocalServer()},
args{projectKey: "PROJ",
repositorySlug: "repo1",
repositorySlug: "repo1_test1",
localVarOptionals: PullRequest{},
},
&APIResponse{
Message: "Status: 400 , Body: {errors:[{context:null,message:title must be supplied for this request,exceptionName:null}]}",
Message: "Status: 400 Bad Request, Body: {errors:[{context:null,exceptionName:null,message:title must be supplied for this request}]}",
Values: map[string]interface{}{
"errors": []interface{}{
map[string]interface{}{
@ -556,7 +566,7 @@ func TestDefaultApiService_CreatePullRequest(t *testing.T) {
true, true},
{"ValidRequestNoBranch", fields{client: generateConfigRealLocalServer()},
args{projectKey: "PROJ",
repositorySlug: "repo1",
repositorySlug: "repo1_test2",
localVarOptionals: PullRequest{
Title: "test PR",
Description: "test Desc",
@ -584,7 +594,7 @@ func TestDefaultApiService_CreatePullRequest(t *testing.T) {
Locked: false,
},
},
&APIResponse{Message: `Status: 404 , Body: {errors:[{context:null,message:Repository \repo1\ of project with key \PROJ\ has no branch \refs/heads/feature\,exceptionName:com.atlassian.bitbucket.commit.NoSuchCommitException}]}`,
&APIResponse{Message: `Status: 404 Not Found, Body: {errors:[{context:null,exceptionName:com.atlassian.bitbucket.commit.NoSuchCommitException,message:Repository \repo1\ of project with key \PROJ\ has no branch \refs/heads/feature\}]}`,
Values: map[string]interface{}{
"errors": []interface{}{
map[string]interface{}{
@ -763,7 +773,7 @@ func TestDefaultApiService_CreateCommentWithComment(t *testing.T) {
commitId: "657f55ce41710f9bfde15c374837136728fae9d9e0eca0b97cb7bfea5095af30",
comment: Comment{Text: "Simple comment"},
},
&APIResponse{Message: `Status: 404 , Body: {errors:[{context:null,message:Commit '657f55ce41710f9bfde15c374837136728fae9d9e0eca0b97cb7bfea5095af30' does not exist in repository 'repo1'.,exceptionName:com.atlassian.bitbucket.commit.NoSuchCommitException}]}`,
&APIResponse{Message: `Status: 404 Not Found, Body: {errors:[{context:null,exceptionName:com.atlassian.bitbucket.commit.NoSuchCommitException,message:Commit '657f55ce41710f9bfde15c374837136728fae9d9e0eca0b97cb7bfea5095af30' does not exist in repository 'repo1'.}]}`,
Values: map[string]interface{}{
"errors": []interface{}{
map[string]interface{}{
@ -826,7 +836,7 @@ func TestDefaultApiService_CreatePullRequestComment(t *testing.T) {
comment: Comment{Text: "Simple comment"},
localVarHTTPContentTypes: []string{"application/json"},
},
&APIResponse{Message: `Status: 404 , Body: {errors:[{context:null,message:Pull request 1 does not exist in PROJ/repo1.,exceptionName:com.atlassian.bitbucket.pull.NoSuchPullRequestException}]}`,
&APIResponse{Message: `Status: 404 Not Found, Body: {errors:[{context:null,exceptionName:com.atlassian.bitbucket.pull.NoSuchPullRequestException,message:Pull request 1 does not exist in PROJ/repo1.}]}`,
Values: map[string]interface{}{
"errors": []interface{}{
map[string]interface{}{
@ -1260,7 +1270,7 @@ func TestDefaultApiService_Delete(t *testing.T) {
pullRequestID: -1,
},
&APIResponse{
Message: "Status: 404 , Body: {errors:[{context:null,message:No pull request exists with ID -1 for this repository 1,exceptionName:com.atlassian.bitbucket.pull.NoSuchPullRequestException}]}",
Message: "Status: 404 Not Found, Body: {errors:[{context:null,exceptionName:com.atlassian.bitbucket.pull.NoSuchPullRequestException,message:No pull request exists with ID -1 for this repository 1}]}",
Values: map[string]interface{}{
"errors": []interface{}{
map[string]interface{}{
@ -4907,11 +4917,17 @@ func TestDefaultApiService_GetSSHKeys(t *testing.T) {
{"networkErrorContextExceeded", fields{client: generateConfigFake()}, args{}, &APIResponse{Message: "Get https://stash.domain.com/rest/ssh/1.0/keys: context canceled"}, true, false},
{"realLocalServer", fields{client: generateConfigRealLocalServer()}, args{},
&APIResponse{Values: map[string]interface{}{
"size": float64(0),
"size": float64(1),
"limit": float64(25),
"isLastPage": true,
"values": []interface{}{},
"start": float64(0),
"values": []interface{}{
map[string]interface{}{
"id": float64(1),
"text": "ssh-rsa AAAAB3... me@127.0.0.1",
"label": "me@127.0.0.1",
},
},
"start": float64(0),
}},
false, true},
}
@ -6077,6 +6093,7 @@ func TestDefaultApiService_SetDefaultBranch(t *testing.T) {
type args struct {
projectKey string
repositorySlug string
branchRef string
}
tests := []struct {
name string
@ -6086,6 +6103,7 @@ func TestDefaultApiService_SetDefaultBranch(t *testing.T) {
wantErr, integrationTest bool
}{
{"networkErrorContextExceeded", fields{client: generateConfigFake()}, args{}, &APIResponse{Message: "Put https://stash.domain.com/rest/api/1.0/projects//repos//branches/default: context canceled"}, true, false},
{"goodRequest", fields{client: generateConfigRealLocalServer()}, args{"PRJ", "repo1", "ref/branch/master"}, &APIResponse{Values: map[string]interface{}{}}, false, true},
}
for _, tt := range tests {
if tt.integrationTest != runIntegrationTests {
@ -6095,11 +6113,12 @@ func TestDefaultApiService_SetDefaultBranch(t *testing.T) {
a := &DefaultApiService{
client: tt.fields.client,
}
got, err := a.SetDefaultBranch(tt.args.projectKey, tt.args.repositorySlug)
got, err := a.SetDefaultBranch(tt.args.projectKey, tt.args.repositorySlug, tt.args.branchRef)
if (err != nil) != tt.wantErr {
t.Errorf("DefaultApiService.SetDefaultBranch() error = %v, wantErr %v", err, tt.wantErr)
return
}
got.Response = nil
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("DefaultApiService.SetDefaultBranch() = %v, want %v", got, tt.want)
}

View File

@ -1,52 +0,0 @@
#!/bin/sh
# ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/
#
# Usage example: /bin/sh ./git_push.sh wing328 swagger-petstore-perl "minor update"
git_user_id=$1
git_repo_id=$2
release_note=$3
if [ "$git_user_id" = "" ]; then
git_user_id="GIT_USER_ID"
echo "[INFO] No command line input provided. Set \$git_user_id to $git_user_id"
fi
if [ "$git_repo_id" = "" ]; then
git_repo_id="GIT_REPO_ID"
echo "[INFO] No command line input provided. Set \$git_repo_id to $git_repo_id"
fi
if [ "$release_note" = "" ]; then
release_note="Minor update"
echo "[INFO] No command line input provided. Set \$release_note to $release_note"
fi
# Initialize the local directory as a Git repository
git init
# Adds the files in the local repository and stages them for commit.
git add .
# Commits the tracked changes and prepares them to be pushed to a remote repository.
git commit -m "$release_note"
# Sets the new remote
git_remote=`git remote`
if [ "$git_remote" = "" ]; then # git remote not defined
if [ "$GIT_TOKEN" = "" ]; then
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git credential in your environment."
git remote add origin https://github.com/${git_user_id}/${git_repo_id}.git
else
git remote add origin https://${git_user_id}:${GIT_TOKEN}@github.com/${git_user_id}/${git_repo_id}.git
fi
fi
git pull origin master
# Pushes (Forces) the changes in the local repository up to the remote repository
echo "Git pushing to https://github.com/${git_user_id}/${git_repo_id}.git"
git push origin master 2>&1 | grep -v 'To https'

3
go.mod
View File

@ -2,7 +2,10 @@ module github.com/gfleury/go-bitbucket-v1
go 1.14
replace github.com/gfleury/go-bitbucket-v1/test/bb-mock-server => ./test/bb-mock-server
require (
github.com/gfleury/go-bitbucket-v1/test/bb-mock-server v0.0.0-20230825095122-9bc1711434ab
github.com/mitchellh/mapstructure v1.1.2
golang.org/x/net v0.7.0
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45

2
go.sum
View File

@ -1,6 +1,8 @@
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=

View File

@ -10,7 +10,12 @@
package swagger
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"github.com/gorilla/mux"
)
func AddGroupToUser(w http.ResponseWriter, r *http.Request) {
@ -54,6 +59,27 @@ func CountPullRequestTasks(w http.ResponseWriter, r *http.Request) {
}
func Create(w http.ResponseWriter, r *http.Request) {
repositorySlug := mux.Vars(r)["repositorySlug"]
if strings.Compare(repositorySlug, "repo1_test1") == 0 {
HandleError(w, http.StatusBadRequest, map[string]interface{}{"errors": []interface{}{
map[string]interface{}{
"context": nil,
"message": "title must be supplied for this request",
"exceptionName": nil,
},
}})
return
} else if strings.Compare(repositorySlug, "repo1_test2") == 0 {
HandleError(w, http.StatusNotFound, map[string]interface{}{"errors": []interface{}{
map[string]interface{}{
"context": nil,
"message": "Repository \"repo1\" of project with key \"PROJ\" has no branch \"refs/heads/feature\"",
"exceptionName": "com.atlassian.bitbucket.commit.NoSuchCommitException",
},
}})
return
}
HandleRequest(w, r)
}
@ -62,10 +88,37 @@ func CreateBranch(w http.ResponseWriter, r *http.Request) {
}
func CreateComment(w http.ResponseWriter, r *http.Request) {
commit_id := mux.Vars(r)["commitId"]
if strings.Compare(commit_id, "657f55ce41710f9bfde15c374837136728fae9d9e0eca0b97cb7bfea5095af30") == 0 {
HandleError(w, http.StatusNotFound, map[string]interface{}{"errors": []interface{}{
map[string]interface{}{
"context": nil,
"message": "Commit '657f55ce41710f9bfde15c374837136728fae9d9e0eca0b97cb7bfea5095af30' does not exist in repository 'repo1'.",
"exceptionName": "com.atlassian.bitbucket.commit.NoSuchCommitException",
},
}})
return
}
HandleRequest(w, r)
}
func CreateCommentCommit(w http.ResponseWriter, r *http.Request) {
pr_id := mux.Vars(r)["pullRequestId"]
if id, err := strconv.Atoi(pr_id); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
} else if id == 1 {
HandleError(w, http.StatusNotFound, map[string]interface{}{"errors": []interface{}{
map[string]interface{}{
"context": nil,
"message": "Pull request 1 does not exist in PROJ/repo1.",
"exceptionName": "com.atlassian.bitbucket.pull.NoSuchPullRequestException",
},
}})
return
}
HandleRequest(w, r)
}
@ -102,9 +155,32 @@ func Decline(w http.ResponseWriter, r *http.Request) {
}
func Delete(w http.ResponseWriter, r *http.Request) {
pr_id := mux.Vars(r)["pullRequestId"]
if id, err := strconv.Atoi(pr_id); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
} else if id < 0 {
HandleError(w, http.StatusNotFound, map[string]interface{}{"errors": []interface{}{
map[string]interface{}{
"context": nil,
"message": "No pull request exists with ID -1 for this repository 1",
"exceptionName": "com.atlassian.bitbucket.pull.NoSuchPullRequestException",
},
}})
return
}
HandleRequest(w, r)
}
func HandleError(w http.ResponseWriter, httpStatus int, body interface{}) {
w.WriteHeader(httpStatus)
bodyData, err := json.Marshal(body)
if err == nil {
w.Write(bodyData)
}
}
func DeleteAvatar(w http.ResponseWriter, r *http.Request) {
HandleRequest(w, r)
}
@ -798,3 +874,7 @@ func SetCommitStatus(w http.ResponseWriter, r *http.Request) {
func SearchCode(w http.ResponseWriter, r *http.Request) {
HandleRequest(w, r)
}
func GetSSHKeys(w http.ResponseWriter, r *http.Request) {
HandleRequest(w, r)
}

View File

@ -1426,4 +1426,11 @@ var routes = Routes{
"/rest/search/latest/search",
SearchCode,
},
Route{
"GetSSHKeys",
strings.ToUpper("Get"),
"/rest/ssh/1.0/keys",
GetSSHKeys,
},
}

View File

@ -2,7 +2,6 @@ package swagger
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
@ -19,14 +18,9 @@ func HandleRequest(w http.ResponseWriter, r *http.Request) {
if routeName != "" {
handlerName = routeName
}
path, err := route.GetPathTemplate()
if err == nil {
fmt.Println(path)
}
}
fmt.Println(handlerName)
fileName := fmt.Sprintf("mocked_responses/%s.json", handlerName)
response, err := ioutil.ReadFile(fileName)
fileName := fmt.Sprintf("test/bb-mock-server/mocked_responses/%s.json", handlerName)
response, err := os.ReadFile(fileName)
if err == nil {
w.Write(response)
} else {

View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1,13 @@
{
"size": 1,
"limit": 25,
"isLastPage": true,
"values": [
{
"id": 1,
"text": "ssh-rsa AAAAB3... me@127.0.0.1",
"label": "me@127.0.0.1"
}
],
"start": 0
}

View File

@ -1 +1,16 @@
{}
{
"name": "admin",
"emailAddress": "admin@example.com",
"id": 1,
"displayName": "admin",
"active": true,
"slug": "admin",
"type": "NORMAL",
"links": {
"self": [
{
"href": "http://localhost:7990/users/admin"
}
]
}
}

View File

@ -0,0 +1,16 @@
{
"query": {
"substituted": false
},
"code": {
"category": "primary",
"count": 0,
"isLastPage": true,
"nextStart": 10,
"start": 0,
"values": []
},
"scope": {
"type": "GLOBAL"
}
}

View File

@ -0,0 +1 @@
{}