diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 000000000..a00b8fc4d --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,121 @@ +linters-settings: + depguard: + list-type: blacklist + packages: + - github.com/jenkins-x/jx/v2/pkg/log/ + - github.com/satori/go.uuid + - github.com/pborman/uuid + packages-with-error-message: + - github.com/jenkins-x/jx/v2/pkg/log/: "use jenkins-x/jx-logging instead" + - github.com/satori/go.uuid: "use github.com/google/uuid instead" + - github.com/pborman/uuid: "use github.com/google/uuid instead" + dupl: + threshold: 100 + exhaustive: + default-signifies-exhaustive: false + funlen: + lines: 200 + statements: 150 + goconst: + min-len: 3 + min-occurrences: 3 + gocritic: + enabled-tags: + - diagnostic + - experimental + - opinionated + - performance + - style + disabled-checks: + - dupImport # https://github.com/go-critic/go-critic/issues/845 + - ifElseChain + - octalLiteral + - whyNoLint + - wrapperFunc + - importShadow # not important for now + - unnamedResult # not important + gocyclo: + min-complexity: 15 + goimports: {} + golint: + min-confidence: 0 + gofmt: + simplify: true + gomnd: + settings: + mnd: + # don't include the "operation" and "assign" + checks: [argument, case, condition, return] + govet: + check-shadowing: true + settings: + printf: + funcs: + - (github.com/jenkins-x/jx-logging/v3/pkg/log/Logger()).Debugf + - (github.com/jenkins-x/jx-logging/v3/pkg/log/Logger()).Infof + - (github.com/jenkins-x/jx-logging/v3/pkg/log/Logger()).Warnf + - (github.com/jenkins-x/jx-logging/v3/pkg/log/Logger()).Errorf + - (github.com/jenkins-x/jx-logging/v3/pkg/log/Logger()).Fatalf + lll: + line-length: 140 + maligned: + suggest-new: true + misspell: {} + nolintlint: + allow-leading-space: true # don't require machine-readable nolint directives (i.e. with no leading space) + allow-unused: false # report any unused nolint directives + require-explanation: false # don't require an explanation for nolint directives + require-specific: false # don't require nolint directives to be specific about which linter is being skipped +linters: + # please, do not use `enable-all`: it's deprecated and will be removed soon. + # inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint + disable-all: true + enable: + - asciicheck + - bodyclose + - deadcode + - depguard + - errcheck + - gofmt + - goimports + - goprintffuncname + - gosec + - gosimple + - ineffassign + - misspell + - nakedret + - rowserrcheck + - staticcheck + - structcheck + - typecheck + - unconvert + - unparam + - unused + - varcheck + - revive + - gocritic + - govet +issues: + # Excluding configuration per-path, per-linter, per-text and per-source + exclude-rules: + # - path: _test\.go + # linters: + # - gomnd + # https://github.com/go-critic/go-critic/issues/926 + - linters: + - gocritic + text: "unnecessaryDefer:" + exclude: + - 'shadow: declaration of "err" shadows declaration at' + max-same-issues: 0 + +run: + timeout: 30m + skip-dirs: + - cmd/docs +# golangci.com configuration +# https://github.com/golangci/golangci/wiki/Configuration +service: + golangci-lint-version: 1.42.x # use the fixed version to not introduce new linters unexpectedly + prepare: + - echo "here I can run custom commands, but no preparation needed for this repo" diff --git a/.lighthouse/jenkins-x/lint.yaml b/.lighthouse/jenkins-x/lint.yaml new file mode 100644 index 000000000..52102ea52 --- /dev/null +++ b/.lighthouse/jenkins-x/lint.yaml @@ -0,0 +1,30 @@ +apiVersion: tekton.dev/v1beta1 +kind: PipelineRun +metadata: + creationTimestamp: null + name: lint +spec: + pipelineSpec: + tasks: + - name: go-scm-lint + resources: {} + taskSpec: + metadata: {} + stepTemplate: + image: uses:jenkins-x/jx3-pipeline-catalog/tasks/go/pullrequest.yaml@versionStream + name: "" + resources: + requests: + cpu: 400m + memory: 600Mi + workingDir: /workspace/source + steps: + - image: uses:jenkins-x/jx3-pipeline-catalog/tasks/git-clone/git-clone-pr.yaml@versionStream + name: "" + resources: {} + - name: make-lint + resources: {} + podTemplate: {} + serviceAccountName: tekton-bot + timeout: 30m0s +status: {} diff --git a/.lighthouse/jenkins-x/triggers.yaml b/.lighthouse/jenkins-x/triggers.yaml index 81e3c039f..a188062be 100644 --- a/.lighthouse/jenkins-x/triggers.yaml +++ b/.lighthouse/jenkins-x/triggers.yaml @@ -6,9 +6,16 @@ spec: context: "pr" always_run: true optional: false - trigger: "/test" - rerun_command: "/retest" + trigger: (?m)^/test( all| pr),?(s+|$) + rerun_command: /test pr source: "pullrequest.yaml" + # - name: lint + # context: "lint" + # always_run: true + # optional: false + # trigger: (?m)^/test( all| lint),?(s+|$) + # rerun_command: /test lint + # source: "lint.yaml" postsubmits: - name: release context: "release" diff --git a/pkg/hmac/hmac_test.go b/pkg/hmac/hmac_test.go index 017e6d8c1..194fede61 100644 --- a/pkg/hmac/hmac_test.go +++ b/pkg/hmac/hmac_test.go @@ -5,7 +5,9 @@ package hmac import ( + // nolint "crypto/md5" + // nolint "crypto/sha1" "crypto/sha256" "hash" diff --git a/scm/const_test.go b/scm/const_test.go index ea1f2cf48..82966b101 100644 --- a/scm/const_test.go +++ b/scm/const_test.go @@ -7,7 +7,7 @@ import ( func TestStateJSON(t *testing.T) { for i := StateUnknown; i < StateExpected; i++ { - in := State(i) + in := i t.Run(in.String(), func(t *testing.T) { b, err := json.Marshal(in) if err != nil { diff --git a/scm/driver/bitbucket/bitbucket.go b/scm/driver/bitbucket/bitbucket.go index 8ed13c948..09b067235 100644 --- a/scm/driver/bitbucket/bitbucket.go +++ b/scm/driver/bitbucket/bitbucket.go @@ -30,7 +30,7 @@ func New(uri string) (*scm.Client, error) { return nil, err } if !strings.HasSuffix(base.Path, "/") { - base.Path = base.Path + "/" + base.Path += "/" } client := &wrapper{new(scm.Client)} client.BaseURL = base @@ -73,7 +73,10 @@ func (c *wrapper) do(ctx context.Context, method, path string, in, out interface // write it to the body of the request. if in != nil { buf := new(bytes.Buffer) - json.NewEncoder(buf).Encode(in) // #nosec + err := json.NewEncoder(buf).Encode(in) // #nosec + if err != nil { + return nil, err + } req.Header = map[string][]string{ "Content-Type": {"application/json"}, } diff --git a/scm/driver/bitbucket/content_test.go b/scm/driver/bitbucket/content_test.go index c5f8263bf..bbbccdc8a 100644 --- a/scm/driver/bitbucket/content_test.go +++ b/scm/driver/bitbucket/content_test.go @@ -33,7 +33,10 @@ func TestContentFind(t *testing.T) { want := new(scm.Content) raw, _ := ioutil.ReadFile("testdata/content.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/bitbucket/git.go b/scm/driver/bitbucket/git.go index 5c47a76d3..c41d187de 100644 --- a/scm/driver/bitbucket/git.go +++ b/scm/driver/bitbucket/git.go @@ -25,7 +25,7 @@ func (s *gitService) FindRef(ctx context.Context, repo, ref string) (string, *sc branchRef, res, err := s.FindBranch(ctx, repo, ref) if err == nil { - return branchRef.Sha, res, err + return branchRef.Sha, res, nil } return ref, res, err @@ -46,23 +46,6 @@ func (s *gitService) FindRef(ctx context.Context, repo, ref string) (string, *sc ref = ref[idx+1:] } return ref, nil, nil - - /* - path := fmt.Sprintf("2.0/repositories/%s/refs?%s", repo, encodeRefQueryOptions(ref)) - out := new(branches) - res, err := s.client.do(ctx, "GET", path, nil, out) - if debugDump { - var buf bytes.Buffer - res, err := s.client.do(ctx, "GET", path, nil, &buf) - fmt.Printf("%s\n", buf.String()) - return "", res, err - } - branches := convertBranchList(out) - if len(branches) == 0 { - return "", res, err - } - return branches[0].Name, res, err - */ } func (s *gitService) CreateRef(ctx context.Context, repo, ref, sha string) (*scm.Reference, *scm.Response, error) { diff --git a/scm/driver/bitbucket/git_test.go b/scm/driver/bitbucket/git_test.go index 290d7b25d..7fe02722c 100644 --- a/scm/driver/bitbucket/git_test.go +++ b/scm/driver/bitbucket/git_test.go @@ -33,7 +33,10 @@ func TestGitFindCommit(t *testing.T) { want := new(scm.Commit) raw, _ := ioutil.ReadFile("testdata/commit.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -58,7 +61,10 @@ func TestGitFindBranch(t *testing.T) { want := new(scm.Reference) raw, _ := ioutil.ReadFile("testdata/branch.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -83,7 +89,10 @@ func TestGitFindTag(t *testing.T) { want := new(scm.Reference) raw, _ := ioutil.ReadFile("testdata/tag.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -110,7 +119,10 @@ func TestGitListCommits(t *testing.T) { want := []*scm.Commit{} raw, _ := ioutil.ReadFile("testdata/commits.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -139,7 +151,10 @@ func TestGitListBranches(t *testing.T) { want := []*scm.Reference{} raw, _ := ioutil.ReadFile("testdata/branches.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -168,7 +183,10 @@ func TestGitListTags(t *testing.T) { want := []*scm.Reference{} raw, _ := ioutil.ReadFile("testdata/tags.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -197,7 +215,10 @@ func TestGitListChanges(t *testing.T) { want := []*scm.Change{} raw, _ := ioutil.ReadFile("testdata/diffstat.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -224,7 +245,10 @@ func TestGitCompareCommits(t *testing.T) { want := []*scm.Change{} raw, _ := ioutil.ReadFile("testdata/diffstat.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/bitbucket/issue.go b/scm/driver/bitbucket/issue.go index 772bfed5e..42c0960f6 100644 --- a/scm/driver/bitbucket/issue.go +++ b/scm/driver/bitbucket/issue.go @@ -145,7 +145,8 @@ func (s *issueService) DeleteComment(ctx context.Context, repo string, number, i return s.client.do(ctx, "DELETE", path, nil, nil) } -func (s *issueService) EditComment(ctx context.Context, repo string, number int, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { +// func (s *issueService) EditComment(ctx context.Context, repo string, number int, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { +func (s *issueService) EditComment(ctx context.Context, repo string, number, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } @@ -165,7 +166,7 @@ func (s *issueService) Unlock(ctx context.Context, repo string, number int) (*sc return nil, scm.ErrNotSupported } -func (s *issueService) SetMilestone(ctx context.Context, repo string, issueID int, number int) (*scm.Response, error) { +func (s *issueService) SetMilestone(ctx context.Context, repo string, issueID, number int) (*scm.Response, error) { return nil, scm.ErrNotSupported } diff --git a/scm/driver/bitbucket/org.go b/scm/driver/bitbucket/org.go index 74858a135..ac410602f 100644 --- a/scm/driver/bitbucket/org.go +++ b/scm/driver/bitbucket/org.go @@ -22,8 +22,7 @@ func (s *organizationService) Create(context.Context, *scm.OrganizationInput) (* func (s *organizationService) Delete(context.Context, string) (*scm.Response, error) { return nil, scm.ErrNotSupported } - -func (s *organizationService) IsMember(ctx context.Context, org string, user string) (bool, *scm.Response, error) { +func (s *organizationService) IsMember(ctx context.Context, org, user string) (bool, *scm.Response, error) { path := fmt.Sprintf("2.0/workspaces/%s/permissions?q=user.account_id=\"%s\"", org, user) result := new(organizationMemberships) res, err := s.client.do(ctx, "GET", path, nil, result) @@ -45,7 +44,7 @@ func (s *organizationService) IsMember(ctx context.Context, org string, user str return false, res, nil } -func (s *organizationService) IsAdmin(ctx context.Context, org string, user string) (bool, *scm.Response, error) { +func (s *organizationService) IsAdmin(ctx context.Context, org, user string) (bool, *scm.Response, error) { return false, nil, scm.ErrNotSupported } diff --git a/scm/driver/bitbucket/org_test.go b/scm/driver/bitbucket/org_test.go index bc18c89f0..6a028f218 100644 --- a/scm/driver/bitbucket/org_test.go +++ b/scm/driver/bitbucket/org_test.go @@ -10,21 +10,18 @@ import ( "io/ioutil" "testing" - "github.com/jenkins-x/go-scm/scm" - "github.com/google/go-cmp/cmp" + "github.com/jenkins-x/go-scm/scm" "gopkg.in/h2non/gock.v1" ) func TestOrganizationFind(t *testing.T) { defer gock.Off() - gock.New("https://api.bitbucket.org"). Get("/2.0/workspaces/atlassian"). Reply(200). Type("application/json"). File("testdata/workspace.json") - client, _ := New("https://api.bitbucket.org") got, _, err := client.Organizations.Find(context.Background(), "atlassian") if err != nil { @@ -33,17 +30,18 @@ func TestOrganizationFind(t *testing.T) { want := new(scm.Organization) raw, _ := ioutil.ReadFile("testdata/workspace.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") t.Log(diff) } } - func TestOrganizationList(t *testing.T) { defer gock.Off() - gock.New("https://api.bitbucket.org"). Get("/2.0/workspaces"). MatchParam("pagelen", "30"). @@ -51,7 +49,6 @@ func TestOrganizationList(t *testing.T) { Reply(200). Type("application/json"). File("testdata/workspaces.json") - client, _ := New("https://api.bitbucket.org") got, _, err := client.Organizations.List(context.Background(), scm.ListOptions{Size: 30, Page: 1}) if err != nil { @@ -60,7 +57,10 @@ func TestOrganizationList(t *testing.T) { want := []*scm.Organization{} raw, _ := ioutil.ReadFile("testdata/workspaces.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/bitbucket/pr.go b/scm/driver/bitbucket/pr.go index 7f9f3cfb6..46cd082cd 100644 --- a/scm/driver/bitbucket/pr.go +++ b/scm/driver/bitbucket/pr.go @@ -306,8 +306,7 @@ type prDestination struct { } type pullRequest struct { - ID int `json:"id"` - //Version int `json:"version"` + ID int `json:"id"` Title string `json:"title"` Description string `json:"description"` State string `json:"state"` @@ -345,7 +344,8 @@ func findRefs(from *pullRequest) (string, string) { } func convertPullRequest(from *pullRequest) *scm.PullRequest { fork := "false" - closed := strings.ToLower(from.State) != "open" + closed := !(strings.EqualFold(from.State, "open")) + baseRef, headRef := findRefs(from) return &scm.PullRequest{ Number: from.ID, diff --git a/scm/driver/bitbucket/pr_test.go b/scm/driver/bitbucket/pr_test.go index f2285b94a..c4b3b0279 100644 --- a/scm/driver/bitbucket/pr_test.go +++ b/scm/driver/bitbucket/pr_test.go @@ -30,8 +30,6 @@ func TestPullList(t *testing.T) { MatchParam("state", "all"). Reply(200). Type("application/json"). - //SetHeaders(mockHeaders). - //SetHeaders(mockPageHeaders). File("testdata/pulls.json") client := NewDefault() @@ -43,7 +41,10 @@ func TestPullList(t *testing.T) { want := []*scm.PullRequest{} raw, _ := ioutil.ReadFile("testdata/pulls.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -73,7 +74,10 @@ func TestPullListChanges(t *testing.T) { want := []*scm.Change{} raw, _ := ioutil.ReadFile("testdata/pr_diffstat.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -100,7 +104,6 @@ func TestPullCreate(t *testing.T) { Post("2.0/repositories/octocat/hello-world/pullrequests"). Reply(201). Type("application/json"). - //SetHeaders(mockHeaders). File("testdata/pr_create.json") input := &scm.PullRequestInput{ @@ -119,7 +122,13 @@ func TestPullCreate(t *testing.T) { want := new(scm.PullRequest) raw, err := ioutil.ReadFile("testdata/pr_create.json.golden") - json.Unmarshal(raw, want) + if err != nil { + t.Fatal(err) + } + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/bitbucket/repo.go b/scm/driver/bitbucket/repo.go index b14d2e070..92f5890f0 100644 --- a/scm/driver/bitbucket/repo.go +++ b/scm/driver/bitbucket/repo.go @@ -171,7 +171,7 @@ func (s *repositoryService) FindCombinedStatus(ctx context.Context, repo, ref st return combined, resp, nil } -func (s *repositoryService) FindUserPermission(ctx context.Context, repo string, user string) (string, *scm.Response, error) { +func (s *repositoryService) FindUserPermission(ctx context.Context, repo, user string) (string, *scm.Response, error) { return "", nil, scm.ErrNotSupported } @@ -237,7 +237,7 @@ func (s *repositoryService) Find(ctx context.Context, repo string) (*scm.Reposit } // FindHook returns a repository hook. -func (s *repositoryService) FindHook(ctx context.Context, repo string, id string) (*scm.Hook, *scm.Response, error) { +func (s *repositoryService) FindHook(ctx context.Context, repo, id string) (*scm.Hook, *scm.Response, error) { path := fmt.Sprintf("2.0/repositories/%s/hooks/%s", repo, id) out := new(hook) res, err := s.client.do(ctx, "GET", path, nil, out) @@ -321,6 +321,7 @@ func (s *repositoryService) CreateHook(ctx context.Context, repo string, input * if in.Description == "" { in.Description = "my webhook" } + // nolint in.Events = append( input.NativeEvents, convertHookEvents(input.Events)..., @@ -350,7 +351,7 @@ func (s *repositoryService) CreateStatus(ctx context.Context, repo, ref string, } // DeleteHook deletes a repository webhook. -func (s *repositoryService) DeleteHook(ctx context.Context, repo string, id string) (*scm.Response, error) { +func (s *repositoryService) DeleteHook(ctx context.Context, repo, id string) (*scm.Response, error) { path := fmt.Sprintf("2.0/repositories/%s/hooks/%s", repo, id) return s.client.do(ctx, "DELETE", path, nil, nil) } @@ -428,23 +429,13 @@ func convertHookEvents(from scm.HookEvents) []string { events = append(events, "repo:push") } if from.PullRequest { - events = append(events, "pullrequest:created") - events = append(events, "pullrequest:updated") - events = append(events, "pullrequest:changes_request_created") - events = append(events, "pullrequest:changes_request_removed") - events = append(events, "pullrequest:approved") - events = append(events, "pullrequest:unapproved") - events = append(events, "pullrequest:fulfilled") - events = append(events, "pullrequest:rejected") + events = append(events, "pullrequest:created", "pullrequest:updated", "pullrequest:changes_request_created", "pullrequest:changes_request_removed", "pullrequest:approved", "pullrequest:unapproved", "pullrequest:fulfilled", "pullrequest:rejected") } if from.PullRequestComment { - events = append(events, "pullrequest:comment_created") - events = append(events, "pullrequest:comment_updated") - events = append(events, "pullrequest:comment_deleted") + events = append(events, "pullrequest:comment_created", "pullrequest:comment_updated", "pullrequest:comment_deleted") } if from.Issue { - events = append(events, "issue:created") - events = append(events, "issue:updated") + events = append(events, "issue:created", "issue:updated") } if from.IssueComment { events = append(events, "issue:comment_created") diff --git a/scm/driver/bitbucket/repo_test.go b/scm/driver/bitbucket/repo_test.go index 0e80d79a8..eba80ccc7 100644 --- a/scm/driver/bitbucket/repo_test.go +++ b/scm/driver/bitbucket/repo_test.go @@ -34,7 +34,10 @@ func TestRepositoryFind(t *testing.T) { want := new(scm.Repository) raw, _ := ioutil.ReadFile("testdata/repo.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -80,7 +83,10 @@ func TestRepositoryPerms(t *testing.T) { want := new(scm.Perm) raw, _ := ioutil.ReadFile("testdata/perms.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -130,7 +136,10 @@ func TestRepositoryList(t *testing.T) { want := []*scm.Repository{} raw, _ := ioutil.ReadFile("testdata/repos.json.golden") - json.Unmarshal(raw, &want) + err := json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -158,7 +167,10 @@ func TestStatusList(t *testing.T) { want := []*scm.Status{} raw, _ := ioutil.ReadFile("testdata/statuses.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -195,7 +207,10 @@ func TestStatusCreate(t *testing.T) { want := new(scm.Status) raw, _ := ioutil.ReadFile("testdata/status.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -221,7 +236,10 @@ func TestRepositoryHookFind(t *testing.T) { want := new(scm.Hook) raw, _ := ioutil.ReadFile("testdata/hook.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -249,7 +267,10 @@ func TestRepositoryHookList(t *testing.T) { want := []*scm.Hook{} raw, _ := ioutil.ReadFile("testdata/hooks.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -289,7 +310,10 @@ func TestRepositoryHookCreate(t *testing.T) { want := new(scm.Hook) raw, _ := ioutil.ReadFile("testdata/hook.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/bitbucket/review.go b/scm/driver/bitbucket/review.go index 5769e8ba6..18fd00258 100644 --- a/scm/driver/bitbucket/review.go +++ b/scm/driver/bitbucket/review.go @@ -29,19 +29,17 @@ func (s *reviewService) Create(ctx context.Context, repo string, number int, inp func (s *reviewService) Delete(ctx context.Context, repo string, number, id int) (*scm.Response, error) { return nil, scm.ErrNotSupported } - -func (s *reviewService) ListComments(ctx context.Context, repo string, prID int, reviewID int, options scm.ListOptions) ([]*scm.ReviewComment, *scm.Response, error) { +func (s *reviewService) ListComments(ctx context.Context, repo string, prID, reviewID int, options scm.ListOptions) ([]*scm.ReviewComment, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } -func (s *reviewService) Update(ctx context.Context, repo string, prID int, reviewID int, body string) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Update(ctx context.Context, repo string, prID, reviewID int, body string) (*scm.Review, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } - -func (s *reviewService) Submit(ctx context.Context, repo string, prID int, reviewID int, input *scm.ReviewSubmitInput) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Submit(ctx context.Context, repo string, prID, reviewID int, input *scm.ReviewSubmitInput) (*scm.Review, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } -func (s *reviewService) Dismiss(ctx context.Context, repo string, prID int, reviewID int, msg string) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Dismiss(ctx context.Context, repo string, prID, reviewID int, msg string) (*scm.Review, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } diff --git a/scm/driver/bitbucket/user_test.go b/scm/driver/bitbucket/user_test.go index 5770be986..c9eae063a 100644 --- a/scm/driver/bitbucket/user_test.go +++ b/scm/driver/bitbucket/user_test.go @@ -33,7 +33,10 @@ func TestUserFind(t *testing.T) { want := new(scm.User) raw, _ := ioutil.ReadFile("testdata/user.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -58,7 +61,10 @@ func TestUserLoginFind(t *testing.T) { want := new(scm.User) raw, _ := ioutil.ReadFile("testdata/user.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/bitbucket/webhook.go b/scm/driver/bitbucket/webhook.go index ab18320ab..711b609de 100644 --- a/scm/driver/bitbucket/webhook.go +++ b/scm/driver/bitbucket/webhook.go @@ -46,24 +46,16 @@ func (s *webhookService) Parse(req *http.Request, fn scm.SecretFunc) (scm.Webhoo hook, err = s.parsePullRequestHook(data) case "pullrequest:updated": hook, err = s.parsePullRequestHook(data) - if hook != nil { - hook.(*scm.PullRequestHook).Action = scm.ActionSync - } + hook.(*scm.PullRequestHook).Action = scm.ActionSync case "pullrequest:fulfilled": hook, err = s.parsePullRequestHook(data) - if hook != nil { - hook.(*scm.PullRequestHook).Action = scm.ActionMerge - } + hook.(*scm.PullRequestHook).Action = scm.ActionMerge case "pullrequest:rejected": hook, err = s.parsePullRequestHook(data) - if hook != nil { - hook.(*scm.PullRequestHook).Action = scm.ActionClose - } + hook.(*scm.PullRequestHook).Action = scm.ActionClose case "pullrequest:comment_created", "pullrequest:comment_updated": hook, err = s.parsePullRequestCommentHook(data) - if hook != nil { - hook.(*scm.PullRequestCommentHook).Action = scm.ActionCreate - } + hook.(*scm.PullRequestCommentHook).Action = scm.ActionCreate } if err != nil { return nil, err @@ -101,11 +93,11 @@ func (s *webhookService) parsePushHook(data []byte, guid string) (scm.Webhook, e change := dst.Push.Changes[0] switch { // case change.New.Type == "branch" && change.Created: - // return convertBranchCreateHook(dst), nil + // return convertBranchCreateHook(dst), nil case change.Old.Type == "branch" && change.Closed: return convertBranchDeleteHook(dst), nil // case change.New.Type == "tag" && change.Created: - // return convertTagCreateHook(dst), nil + // return convertTagCreateHook(dst), nil case change.Old.Type == "tag" && change.Closed: return convertTagDeleteHook(dst), nil default: @@ -125,10 +117,7 @@ func (s *webhookService) parsePullRequestHook(data []byte) (*scm.PullRequestHook return nil, err } - switch { - default: - return s.convertPullRequestHook(dst) - } + return s.convertPullRequestHook(dst) } func (s *webhookService) parsePullRequestCommentHook(data []byte) (*scm.PullRequestCommentHook, error) { @@ -512,7 +501,7 @@ type ( type webhookPRComment struct { PullRequest *webhookPullRequest `json:"pullrequest"` - Comment *prComment `json:"comment"` //this struct definition is available in pr.go + Comment *prComment `json:"comment"` // this struct definition is available in pr.go Repository *webhookRepository `json:"repository"` Actor *webhookActor `json:"actor"` } @@ -645,11 +634,11 @@ func convertTagDeleteHook(src *pushHook) *scm.TagHook { // username is unavailable in response since 2.0 release // this hack may not be needed since other sources are assuming 2.0 API version // return account Id in case user name is empty -func validUser(acId string, userName string) string { +func validUser(acID, userName string) string { result := userName if userName == "" { - result = acId + result = acID } return result @@ -745,24 +734,24 @@ func (s *webhookService) convertPullRequestCommentHook(src *webhookPRComment) (* Link: src.Repository.Links.HTML.Href, } - feature_repo := scm.Repository{ + featureRepo := scm.Repository{ ID: src.PullRequest.Source.Repository.UUID, Namespace: namespace, Name: name, FullName: src.PullRequest.Source.Repository.FullName, Branch: src.PullRequest.Source.Branch.Name, - Private: true, //(TODO) Private value is set to default(true) as this value does not come with the PR Source Repo payload + Private: true, // (TODO) Private value is set to default(true) as this value does not come with the PR Source Repo payload Clone: fmt.Sprintf("https://bitbucket.org/%s.git", src.PullRequest.Source.Repository.FullName), CloneSSH: fmt.Sprintf("git@bitbucket.org:%s.git", src.PullRequest.Source.Repository.FullName), Link: src.PullRequest.Source.Repository.Links.HTML.Href, } - base_repo := scm.Repository{ + baseRepo := scm.Repository{ ID: src.PullRequest.Destination.Repository.UUID, Namespace: namespace, Name: name, FullName: src.PullRequest.Destination.Repository.FullName, Branch: src.PullRequest.Destination.Branch.Name, - Private: true, //(TODO) Private value is set to default(true) as this value does not come with the PR Destination Repo payload + Private: true, // (TODO) Private value is set to default(true) as this value does not come with the PR Destination Repo payload Clone: fmt.Sprintf("https://bitbucket.org/%s.git", src.PullRequest.Destination.Repository.FullName), CloneSSH: fmt.Sprintf("git@bitbucket.org:%s.git", src.PullRequest.Destination.Repository.FullName), Link: src.PullRequest.Destination.Repository.Links.HTML.Href, @@ -810,14 +799,14 @@ func (s *webhookService) convertPullRequestCommentHook(src *webhookPRComment) (* Updated: src.Comment.UpdatedOn, }, } - dst.PullRequest.Base.Repo = base_repo - dst.PullRequest.Head.Repo = feature_repo + dst.PullRequest.Base.Repo = baseRepo + dst.PullRequest.Head.Repo = featureRepo dst.PullRequest.Base.Ref = src.PullRequest.Destination.Branch.Name dst.PullRequest.Head.Ref = src.PullRequest.Source.Branch.Name if len(featureSha) <= 12 && featureSha != "" && s.client != nil { - //TODO - need to consider the forking scenario to determine which Repo whould be considered for mapping "repo" variable Full name - repo := feature_repo.FullName + // TODO - need to consider the forking scenario to determine which Repo whould be considered for mapping "repo" variable Full name + repo := featureRepo.FullName fullHash, _, err := s.client.Git.FindRef(context.TODO(), repo, featureSha) if err != nil { return nil, errors.Wrapf(err, "failed to resolve full hash %s", featureSha) @@ -829,7 +818,7 @@ func (s *webhookService) convertPullRequestCommentHook(src *webhookPRComment) (* } if dst.PullRequest.Head.Sha == "" && dst.PullRequest.Head.Ref != "" && s.client != nil { - repo := feature_repo.FullName + repo := featureRepo.FullName fullHash, _, err := s.client.Git.FindRef(context.TODO(), repo, dst.PullRequest.Head.Ref) if err != nil { return nil, errors.Wrapf(err, "failed to resolve sha for ref %s", dst.PullRequest.Head.Ref) @@ -846,7 +835,7 @@ func (s *webhookService) convertPullRequestCommentHook(src *webhookPRComment) (* masterSha := src.PullRequest.Destination.Commit.Hash if len(masterSha) <= 12 && masterSha != "" && s.client != nil { - repo := base_repo.FullName + repo := baseRepo.FullName fullHash, _, err := s.client.Git.FindRef(context.TODO(), repo, masterSha) if err != nil { return nil, errors.Wrapf(err, "failed to resolve full hash %s", masterSha) diff --git a/scm/driver/bitbucket/webhook_test.go b/scm/driver/bitbucket/webhook_test.go index 65cb51b5d..cde6829db 100644 --- a/scm/driver/bitbucket/webhook_test.go +++ b/scm/driver/bitbucket/webhook_test.go @@ -174,7 +174,10 @@ func TestWebhooks(t *testing.T) { t.Log(diff) // debug only. remove once implemented - json.NewEncoder(os.Stdout).Encode(o) + err := json.NewEncoder(os.Stdout).Encode(o) + if err != nil { + t.Fatal(err) + } } switch event := o.(type) { diff --git a/scm/driver/fake/content.go b/scm/driver/fake/content.go index 83c9176f8..ccfc393a7 100644 --- a/scm/driver/fake/content.go +++ b/scm/driver/fake/content.go @@ -108,7 +108,7 @@ func (c contentService) Delete(_ context.Context, repo, path, ref string) (*scm. return nil, nil } -func (c contentService) path(repo string, path string, ref string) (string, error) { +func (c contentService) path(repo, path, ref string) (string, error) { if c.data.ContentDir == "" { return "", errors.Errorf("no data.ContentDir configured") } diff --git a/scm/driver/fake/data.go b/scm/driver/fake/data.go index dde99d409..7f6891ed1 100644 --- a/scm/driver/fake/data.go +++ b/scm/driver/fake/data.go @@ -35,7 +35,7 @@ type Data struct { Deployments map[string][]*scm.Deployment DeploymentStatus map[string][]*scm.DeploymentStatus - //All Labels That Exist In The Repo + // All Labels That Exist In The Repo RepoLabelsExisting []string // org/repo#number:label IssueLabelsAdded []string diff --git a/scm/driver/fake/deploy.go b/scm/driver/fake/deploy.go index d09bd5601..fd63c7da3 100644 --- a/scm/driver/fake/deploy.go +++ b/scm/driver/fake/deploy.go @@ -13,7 +13,7 @@ type deploymentService struct { data *Data } -func (s *deploymentService) Find(ctx context.Context, repoFullName string, deploymentID string) (*scm.Deployment, *scm.Response, error) { +func (s *deploymentService) Find(ctx context.Context, repoFullName, deploymentID string) (*scm.Deployment, *scm.Response, error) { for _, d := range s.data.Deployments[repoFullName] { if d.ID == deploymentID { return d, nil, nil @@ -56,7 +56,7 @@ func (s *deploymentService) Create(ctx context.Context, repoFullName string, inp return d, nil, nil } -func (s *deploymentService) Delete(ctx context.Context, repoFullName string, deploymentID string) (*scm.Response, error) { +func (s *deploymentService) Delete(ctx context.Context, repoFullName, deploymentID string) (*scm.Response, error) { deployments := s.data.Deployments[repoFullName] for i, d := range deployments { if d.ID == deploymentID { @@ -71,7 +71,7 @@ func (s *deploymentService) Delete(ctx context.Context, repoFullName string, dep return nil, scm.ErrNotFound } -func (s *deploymentService) FindStatus(ctx context.Context, repoFullName string, deploymentID string, statusID string) (*scm.DeploymentStatus, *scm.Response, error) { +func (s *deploymentService) FindStatus(ctx context.Context, repoFullName, deploymentID, statusID string) (*scm.DeploymentStatus, *scm.Response, error) { key := scm.Join(repoFullName, deploymentID) for _, d := range s.data.DeploymentStatus[key] { if d.ID == statusID { @@ -81,12 +81,12 @@ func (s *deploymentService) FindStatus(ctx context.Context, repoFullName string, return nil, nil, scm.ErrNotFound } -func (s *deploymentService) ListStatus(ctx context.Context, repoFullName string, deploymentID string, opts scm.ListOptions) ([]*scm.DeploymentStatus, *scm.Response, error) { +func (s *deploymentService) ListStatus(ctx context.Context, repoFullName, deploymentID string, opts scm.ListOptions) ([]*scm.DeploymentStatus, *scm.Response, error) { key := scm.Join(repoFullName, deploymentID) return s.data.DeploymentStatus[key], nil, nil } -func (s *deploymentService) CreateStatus(ctx context.Context, repoFullName string, deploymentID string, input *scm.DeploymentStatusInput) (*scm.DeploymentStatus, *scm.Response, error) { +func (s *deploymentService) CreateStatus(ctx context.Context, repoFullName, deploymentID string, input *scm.DeploymentStatusInput) (*scm.DeploymentStatus, *scm.Response, error) { key := scm.Join(repoFullName, deploymentID) statuses := s.data.DeploymentStatus[key] diff --git a/scm/driver/fake/deploy_test.go b/scm/driver/fake/deploy_test.go index ac4db5c4d..f0ecb3d93 100644 --- a/scm/driver/fake/deploy_test.go +++ b/scm/driver/fake/deploy_test.go @@ -17,7 +17,7 @@ func TestDeploy(t *testing.T) { repo := "myorg/myrepo" - AssertDeploymentSize(t, ctx, client, 0, repo) + AssertDeploymentSize(ctx, t, client, 0, repo) // lets create a deployment input := &scm.DeploymentInput{ @@ -30,8 +30,8 @@ func TestDeploy(t *testing.T) { require.NoError(t, err, "failed to create deploy in repo %s", repo) require.NotNil(t, deploy, "should have created a deployment") - AssertDeploymentSize(t, ctx, client, 1, repo) - AssertDeploymentStatusSize(t, ctx, client, 0, repo, deploy.ID) + AssertDeploymentSize(ctx, t, client, 1, repo) + AssertDeploymentStatusSize(ctx, t, client, 0, repo, deploy.ID) deploy2, _, err := client.Deployments.Find(ctx, repo, deploy.ID) require.NoError(t, err, "failed to find deploy in repo %s for deployment %s ", repo, deploy.ID) @@ -48,7 +48,7 @@ func TestDeploy(t *testing.T) { require.NoError(t, err, "failed to create status in repo %s for status %s", repo, deploy.ID) require.NotNil(t, status, "should have created a status") - AssertDeploymentStatusSize(t, ctx, client, 1, repo, deploy.ID) + AssertDeploymentStatusSize(ctx, t, client, 1, repo, deploy.ID) status2, _, err := client.Deployments.FindStatus(ctx, repo, deploy.ID, status.ID) require.NoError(t, err, "failed to find status in repo %s for deployment %s status %s", repo, deploy.ID, status.ID) @@ -58,17 +58,17 @@ func TestDeploy(t *testing.T) { _, err = client.Deployments.Delete(ctx, repo, deploy.ID) require.NoError(t, err, "failed to delete deploy in repo %s", repo) - AssertDeploymentSize(t, ctx, client, 0, repo) + AssertDeploymentSize(ctx, t, client, 0, repo) } -func AssertDeploymentSize(t *testing.T, ctx context.Context, client *scm.Client, size int, repo string) []*scm.Deployment { +func AssertDeploymentSize(ctx context.Context, t *testing.T, client *scm.Client, size int, repo string) []*scm.Deployment { deploys, _, err := client.Deployments.List(ctx, repo, scm.ListOptions{}) require.NoError(t, err, "could not list deploys in repo %s", repo) require.Len(t, deploys, size, "deploy size") return deploys } -func AssertDeploymentStatusSize(t *testing.T, ctx context.Context, client *scm.Client, size int, repo, deploymentID string) []*scm.DeploymentStatus { +func AssertDeploymentStatusSize(ctx context.Context, t *testing.T, client *scm.Client, size int, repo, deploymentID string) []*scm.DeploymentStatus { statuses, _, err := client.Deployments.ListStatus(ctx, repo, deploymentID, scm.ListOptions{}) require.NoError(t, err, "could not list statuses in repo %s deploymentID %s", repo, deploymentID) require.Len(t, statuses, size, "status size") diff --git a/scm/driver/fake/fake.go b/scm/driver/fake/fake.go index 0e3cfc78d..5b5c76cc4 100644 --- a/scm/driver/fake/fake.go +++ b/scm/driver/fake/fake.go @@ -36,9 +36,7 @@ func NewDefault() (*scm.Client, *Data) { client.Username = data.CurrentUser.Login // TODO - /* - client.Webhooks = &webhookService{client} - */ + return client.Client, data } diff --git a/scm/driver/fake/git.go b/scm/driver/fake/git.go index 18a46cc40..07f64c703 100644 --- a/scm/driver/fake/git.go +++ b/scm/driver/fake/git.go @@ -38,9 +38,9 @@ func (s *gitService) FindBranch(ctx context.Context, repo, name string) (*scm.Re panic("implement me") } -func (s *gitService) FindCommit(ctx context.Context, repo, SHA string) (*scm.Commit, *scm.Response, error) { +func (s *gitService) FindCommit(ctx context.Context, repo, sha string) (*scm.Commit, *scm.Response, error) { f := s.data - return f.Commits[SHA], nil, nil + return f.Commits[sha], nil, nil } func (s *gitService) FindTag(ctx context.Context, repo, name string) (*scm.Reference, *scm.Response, error) { diff --git a/scm/driver/fake/issue.go b/scm/driver/fake/issue.go index f3a0ec297..ae7e10938 100644 --- a/scm/driver/fake/issue.go +++ b/scm/driver/fake/issue.go @@ -147,7 +147,7 @@ func (s *issueService) CreateComment(ctx context.Context, repo string, number in return answer, nil, nil } -func (s *issueService) DeleteComment(ctx context.Context, repo string, number int, id int) (*scm.Response, error) { +func (s *issueService) DeleteComment(ctx context.Context, repo string, number, id int) (*scm.Response, error) { f := s.data f.IssueCommentsDeleted = append(f.IssueCommentsDeleted, fmt.Sprintf("%s#%d", repo, id)) for num, ics := range f.IssueComments { @@ -161,7 +161,7 @@ func (s *issueService) DeleteComment(ctx context.Context, repo string, number in return nil, fmt.Errorf("could not find issue comment %d", id) } -func (s *issueService) EditComment(ctx context.Context, repo string, number int, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { +func (s *issueService) EditComment(ctx context.Context, repo string, number, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } @@ -181,7 +181,7 @@ func (s *issueService) Unlock(context.Context, string, int) (*scm.Response, erro panic("implement me") } -func (s *issueService) SetMilestone(ctx context.Context, repo string, issueID int, number int) (*scm.Response, error) { +func (s *issueService) SetMilestone(ctx context.Context, repo string, issueID, number int) (*scm.Response, error) { return nil, scm.ErrNotSupported } diff --git a/scm/driver/fake/org.go b/scm/driver/fake/org.go index 46723ee09..2b6e5d791 100644 --- a/scm/driver/fake/org.go +++ b/scm/driver/fake/org.go @@ -35,11 +35,11 @@ func (s *organizationService) Delete(context.Context, string) (*scm.Response, er return nil, scm.ErrNotSupported } -func (s *organizationService) IsMember(ctx context.Context, org string, user string) (bool, *scm.Response, error) { +func (s *organizationService) IsMember(ctx context.Context, org, user string) (bool, *scm.Response, error) { panic("implement me") } -func (s *organizationService) IsAdmin(ctx context.Context, org string, user string) (bool, *scm.Response, error) { +func (s *organizationService) IsAdmin(ctx context.Context, org, user string) (bool, *scm.Response, error) { return user == "adminUser", &scm.Response{}, nil } diff --git a/scm/driver/fake/pr.go b/scm/driver/fake/pr.go index 4956ae461..d2eccc4ac 100644 --- a/scm/driver/fake/pr.go +++ b/scm/driver/fake/pr.go @@ -19,7 +19,7 @@ func (s *pullService) Find(ctx context.Context, repo string, number int) (*scm.P f := s.data val, exists := f.PullRequests[number] if !exists { - return nil, nil, fmt.Errorf("Pull request number %d does not exit", number) + return nil, nil, fmt.Errorf("pull request number %d does not exit", number) } return val, nil, nil } @@ -158,7 +158,7 @@ func (s *pullService) CreateComment(ctx context.Context, repo string, number int return answer, nil, nil } -func (s *pullService) DeleteComment(ctx context.Context, repo string, number int, id int) (*scm.Response, error) { +func (s *pullService) DeleteComment(ctx context.Context, repo string, number, id int) (*scm.Response, error) { f := s.data f.PullRequestCommentsDeleted = append(f.PullRequestCommentsDeleted, fmt.Sprintf("%s#%d", repo, id)) for num, ics := range f.PullRequestComments { @@ -172,7 +172,7 @@ func (s *pullService) DeleteComment(ctx context.Context, repo string, number int return nil, fmt.Errorf("could not find issue comment %d", id) } -func (s *pullService) EditComment(ctx context.Context, repo string, number int, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { +func (s *pullService) EditComment(ctx context.Context, repo string, number, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } @@ -235,7 +235,7 @@ func (s *pullService) Create(_ context.Context, fullName string, input *scm.Pull return answer, nil, nil } -func (s *pullService) SetMilestone(ctx context.Context, repo string, prID int, number int) (*scm.Response, error) { +func (s *pullService) SetMilestone(ctx context.Context, repo string, prID, number int) (*scm.Response, error) { return nil, scm.ErrNotSupported } diff --git a/scm/driver/fake/release.go b/scm/driver/fake/release.go index a690f2f98..305cecadf 100644 --- a/scm/driver/fake/release.go +++ b/scm/driver/fake/release.go @@ -33,7 +33,7 @@ func (r *releaseService) releaseMap(repo string) map[int]*scm.Release { return releaseMap } -func (r *releaseService) FindByTag(_ context.Context, repo string, tag string) (*scm.Release, *scm.Response, error) { +func (r *releaseService) FindByTag(_ context.Context, repo, tag string) (*scm.Release, *scm.Response, error) { for _, rel := range r.releaseMap(repo) { if rel.Tag == tag { return rel, nil, nil @@ -95,7 +95,7 @@ func (r *releaseService) Update(_ context.Context, repo string, number int, inpu return nil, nil, nil } -func (r *releaseService) UpdateByTag(ctx context.Context, repo string, tag string, input *scm.ReleaseInput) (*scm.Release, *scm.Response, error) { +func (r *releaseService) UpdateByTag(ctx context.Context, repo, tag string, input *scm.ReleaseInput) (*scm.Release, *scm.Response, error) { rel, _, _ := r.FindByTag(ctx, repo, tag) return r.Update(ctx, repo, rel.ID, input) } @@ -106,7 +106,7 @@ func (r *releaseService) Delete(_ context.Context, repo string, number int) (*sc return nil, nil } -func (r *releaseService) DeleteByTag(ctx context.Context, repo string, tag string) (*scm.Response, error) { +func (r *releaseService) DeleteByTag(ctx context.Context, repo, tag string) (*scm.Response, error) { rel, _, _ := r.FindByTag(ctx, repo, tag) return r.Delete(ctx, repo, rel.ID) } diff --git a/scm/driver/fake/repo.go b/scm/driver/fake/repo.go index a8f8c02a3..18c217cd1 100644 --- a/scm/driver/fake/repo.go +++ b/scm/driver/fake/repo.go @@ -26,7 +26,7 @@ func (s *repositoryService) FindCombinedStatus(ctx context.Context, repo, ref st }, nil, nil } -func (s *repositoryService) FindUserPermission(ctx context.Context, repo string, user string) (string, *scm.Response, error) { +func (s *repositoryService) FindUserPermission(ctx context.Context, repo, user string) (string, *scm.Response, error) { f := s.data m := f.UserPermissions[repo] perm := "" @@ -142,7 +142,7 @@ func (s *repositoryService) ListLabels(context.Context, string, scm.ListOptions) return la, nil, nil } -func (s *repositoryService) ListStatus(ctx context.Context, repo string, ref string, opt scm.ListOptions) ([]*scm.Status, *scm.Response, error) { +func (s *repositoryService) ListStatus(ctx context.Context, repo, ref string, opt scm.ListOptions) ([]*scm.Status, *scm.Response, error) { f := s.data result := make([]*scm.Status, 0, len(f.Statuses)) result = append(result, f.Statuses[ref]...) @@ -194,7 +194,7 @@ func (s *repositoryService) UpdateHook(ctx context.Context, repo string, input * return nil, nil, scm.ErrNotSupported } -func (s *repositoryService) DeleteHook(ctx context.Context, fullName string, hookID string) (*scm.Response, error) { +func (s *repositoryService) DeleteHook(ctx context.Context, fullName, hookID string) (*scm.Response, error) { hooks := s.data.Hooks[fullName] for i, h := range hooks { if h.ID == hookID { @@ -206,7 +206,7 @@ func (s *repositoryService) DeleteHook(ctx context.Context, fullName string, hoo return nil, nil } -func (s *repositoryService) CreateStatus(ctx context.Context, repo string, ref string, in *scm.StatusInput) (*scm.Status, *scm.Response, error) { +func (s *repositoryService) CreateStatus(ctx context.Context, repo, ref string, in *scm.StatusInput) (*scm.Status, *scm.Response, error) { statuses := s.data.Statuses[ref] if statuses == nil { statuses = []*scm.Status{} diff --git a/scm/driver/fake/repo_test.go b/scm/driver/fake/repo_test.go index 581c6f7a8..fb7c38de6 100644 --- a/scm/driver/fake/repo_test.go +++ b/scm/driver/fake/repo_test.go @@ -2,10 +2,11 @@ package fake_test import ( "context" + "testing" + "github.com/jenkins-x/go-scm/scm/driver/fake" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "testing" "github.com/google/go-cmp/cmp" "github.com/jenkins-x/go-scm/scm" @@ -73,8 +74,8 @@ func TestForkRepository(t *testing.T) { ctx := context.TODO() - fake.AssertNoRepoExists(t, ctx, client, fullName) - fake.AssertNoRepoExists(t, ctx, client, forkFullName) + fake.AssertNoRepoExists(ctx, t, client, fullName) + fake.AssertNoRepoExists(ctx, t, client, forkFullName) repo, _, err := client.Repositories.Create(ctx, &scm.RepositoryInput{ Namespace: org, @@ -83,12 +84,15 @@ func TestForkRepository(t *testing.T) { require.NoError(t, err, "failed to create repo %s", fullName) require.NotNil(t, repo, "no repo returned for create repo %s", fullName) - fake.AssertRepoExists(t, ctx, client, fullName) + fake.AssertRepoExists(ctx, t, client, fullName) - repo, _, err = client.Repositories.Fork(ctx, &scm.RepositoryInput{ + _, _, err = client.Repositories.Fork(ctx, &scm.RepositoryInput{ Name: repoName, }, repoName) + if err != nil { + t.Error(err) + } - repository := fake.AssertRepoExists(t, ctx, client, forkFullName) + repository := fake.AssertRepoExists(ctx, t, client, forkFullName) assert.Equal(t, expectedGitURL, repository.Clone, "forked repository clone URL") } diff --git a/scm/driver/fake/review.go b/scm/driver/fake/review.go index ce555f0e5..e40643408 100644 --- a/scm/driver/fake/review.go +++ b/scm/driver/fake/review.go @@ -11,7 +11,7 @@ type reviewService struct { data *Data } -func (s *reviewService) Find(ctx context.Context, repo string, number int, reviewID int) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Find(ctx context.Context, repo string, number, reviewID int) (*scm.Review, *scm.Response, error) { reviews, r, err := s.List(ctx, repo, number, scm.ListOptions{}) if err != nil { return nil, r, err @@ -45,18 +45,18 @@ func (s *reviewService) Delete(context.Context, string, int, int) (*scm.Response panic("implement me") } -func (s *reviewService) ListComments(ctx context.Context, repo string, prID int, reviewID int, options scm.ListOptions) ([]*scm.ReviewComment, *scm.Response, error) { +func (s *reviewService) ListComments(ctx context.Context, repo string, prID, reviewID int, options scm.ListOptions) ([]*scm.ReviewComment, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } -func (s *reviewService) Update(ctx context.Context, repo string, prID int, reviewID int, body string) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Update(ctx context.Context, repo string, prID, reviewID int, body string) (*scm.Review, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } -func (s *reviewService) Submit(ctx context.Context, repo string, prID int, reviewID int, input *scm.ReviewSubmitInput) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Submit(ctx context.Context, repo string, prID, reviewID int, input *scm.ReviewSubmitInput) (*scm.Review, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } -func (s *reviewService) Dismiss(ctx context.Context, repo string, prID int, reviewID int, msg string) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Dismiss(ctx context.Context, repo string, prID, reviewID int, msg string) (*scm.Review, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } diff --git a/scm/driver/fake/test_helpers.go b/scm/driver/fake/test_helpers.go index e68e9aaaa..dfe92bc8b 100644 --- a/scm/driver/fake/test_helpers.go +++ b/scm/driver/fake/test_helpers.go @@ -2,13 +2,14 @@ package fake import ( "context" + "testing" + "github.com/jenkins-x/go-scm/scm" "github.com/stretchr/testify/require" - "testing" ) // AssertRepoExists asserts that the repository exists -func AssertRepoExists(t *testing.T, ctx context.Context, client *scm.Client, repo string) *scm.Repository { +func AssertRepoExists(ctx context.Context, t *testing.T, client *scm.Client, repo string) *scm.Repository { require.NotEmpty(t, repo, "no repository name") require.NotNil(t, client, "no scm client") require.NotNil(t, client.Repositories, "scm client does not support Repositories") @@ -24,7 +25,7 @@ func AssertRepoExists(t *testing.T, ctx context.Context, client *scm.Client, rep } // AssertNoRepoExists asserts that the repository does not exist -func AssertNoRepoExists(t *testing.T, ctx context.Context, client *scm.Client, repo string) { +func AssertNoRepoExists(ctx context.Context, t *testing.T, client *scm.Client, repo string) { require.NotEmpty(t, repo, "no repository name") require.NotNil(t, client, "no scm client") require.NotNil(t, client.Repositories, "scm client does not support Repositories") diff --git a/scm/driver/fake/user_test.go b/scm/driver/fake/user_test.go index 659f54cea..2c5a4b919 100644 --- a/scm/driver/fake/user_test.go +++ b/scm/driver/fake/user_test.go @@ -25,6 +25,9 @@ func TestInviteLogic(t *testing.T) { require.Empty(t, invitations, "should not have any invitations") addedFlag, alreadyExisted, _, err := client.Repositories.AddCollaborator(ctx, fullName, user, permission) + if err != nil { + t.Fatal(err) + } assert.True(t, addedFlag, "should have added a collaborator") assert.False(t, alreadyExisted, "the collaborator %s should not already exist", user) diff --git a/scm/driver/gitea/content.go b/scm/driver/gitea/content.go index 345c39345..2ca75ede7 100644 --- a/scm/driver/gitea/content.go +++ b/scm/driver/gitea/content.go @@ -31,7 +31,7 @@ func (s *contentService) Find(ctx context.Context, repo, path, ref string) (*scm return &scm.Content{ Path: path, - Data: []byte(raw), + Data: raw, Sha: out.SHA, }, toSCMResponse(resp), err } diff --git a/scm/driver/gitea/content_test.go b/scm/driver/gitea/content_test.go index 93c514df9..a4a0ca3e4 100644 --- a/scm/driver/gitea/content_test.go +++ b/scm/driver/gitea/content_test.go @@ -185,5 +185,5 @@ func TestContentDelete(t *testing.T) { } func encode(b []byte) string { - return base64.StdEncoding.EncodeToString([]byte(b)) + return base64.StdEncoding.EncodeToString(b) } diff --git a/scm/driver/gitea/git.go b/scm/driver/gitea/git.go index 1fa62686d..c3f314ed0 100644 --- a/scm/driver/gitea/git.go +++ b/scm/driver/gitea/git.go @@ -42,13 +42,11 @@ func (s *gitService) CreateRef(ctx context.Context, repo, ref, sha string) (*scm func (s *gitService) DeleteRef(ctx context.Context, repo, ref string) (*scm.Response, error) { namespace, name := scm.Split(repo) - if strings.HasPrefix(ref, "heads/") { - ref = strings.TrimPrefix(ref, "heads/") - } + ref = strings.TrimPrefix(ref, "heads/") out, giteaResp, err := s.client.GiteaClient.DeleteRepoBranch(namespace, name, ref) resp := toSCMResponse(giteaResp) if !out { - return resp, errors.New("Failed to delete branch") + return resp, errors.New("failed to delete branch") } return resp, err } diff --git a/scm/driver/gitea/git_test.go b/scm/driver/gitea/git_test.go index 82bb21c50..79619f160 100644 --- a/scm/driver/gitea/git_test.go +++ b/scm/driver/gitea/git_test.go @@ -44,7 +44,10 @@ func TestCommitFind(t *testing.T) { want := new(scm.Commit) raw, _ := ioutil.ReadFile("testdata/commit.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -119,7 +122,10 @@ func TestBranchFind(t *testing.T) { want := new(scm.Reference) raw, _ := ioutil.ReadFile("testdata/branch.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -147,7 +153,10 @@ func TestBranchList(t *testing.T) { want := []*scm.Reference{} raw, _ := ioutil.ReadFile("testdata/branches.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -189,7 +198,10 @@ func TestTagList(t *testing.T) { want := []*scm.Reference{} raw, _ := ioutil.ReadFile("testdata/tags.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/gitea/gitea.go b/scm/driver/gitea/gitea.go index f82458431..6516efea4 100644 --- a/scm/driver/gitea/gitea.go +++ b/scm/driver/gitea/gitea.go @@ -30,13 +30,13 @@ func New(uri string) (*scm.Client, error) { } // NewWithToken returns a new Gitea API client with the token set. -func NewWithToken(uri string, token string) (*scm.Client, error) { +func NewWithToken(uri, token string) (*scm.Client, error) { base, err := url.Parse(uri) if err != nil { return nil, err } if !strings.HasSuffix(base.Path, "/") { - base.Path = base.Path + "/" + base.Path += "/" } client := &wrapper{Client: new(scm.Client)} client.GiteaClient, err = gitea.NewClient(base.String(), gitea.SetToken(token)) @@ -62,13 +62,13 @@ func NewWithToken(uri string, token string) (*scm.Client, error) { } // NewWithBasicAuth returns a new Gitea API client with the basic auth set. -func NewWithBasicAuth(uri string, user, password string) (*scm.Client, error) { +func NewWithBasicAuth(uri, user, password string) (*scm.Client, error) { base, err := url.Parse(uri) if err != nil { return nil, err } if !strings.HasSuffix(base.Path, "/") { - base.Path = base.Path + "/" + base.Path += "/" } client := &wrapper{Client: new(scm.Client)} client.GiteaClient, err = gitea.NewClient(base.String(), gitea.SetBasicAuth(user, password)) @@ -110,7 +110,10 @@ func (c *wrapper) do(ctx context.Context, method, path string, in, out interface // write it to the body of the request. if in != nil { buf := new(bytes.Buffer) - json.NewEncoder(buf).Encode(in) + err := json.NewEncoder(buf).Encode(in) // #nosec + if err != nil { + return nil, err + } req.Header = map[string][]string{ "Content-Type": {"application/json"}, } @@ -139,7 +142,10 @@ func (c *wrapper) do(ctx context.Context, method, path string, in, out interface // if raw output is expected, copy to the provided // buffer and exit. if w, ok := out.(io.Writer); ok { - io.Copy(w, res.Body) + _, err := io.Copy(w, res.Body) + if err != nil { + return res, err + } return res, nil } diff --git a/scm/driver/gitea/issue.go b/scm/driver/gitea/issue.go index 66ae581ce..73888f4c0 100644 --- a/scm/driver/gitea/issue.go +++ b/scm/driver/gitea/issue.go @@ -78,7 +78,7 @@ func (s *issueService) ListLabels(ctx context.Context, repo string, number int, return convertLabels(out), toSCMResponse(resp), err } -func (s *issueService) lookupLabel(ctx context.Context, repo string, lbl string) (int64, *scm.Response, error) { +func (s *issueService) lookupLabel(ctx context.Context, repo, lbl string) (int64, *scm.Response, error) { var labelID int64 labelID = -1 var repoLabels []*scm.Label @@ -226,7 +226,7 @@ func (s *issueService) DeleteComment(ctx context.Context, repo string, index, id return toSCMResponse(resp), err } -func (s *issueService) EditComment(ctx context.Context, repo string, number int, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { +func (s *issueService) EditComment(ctx context.Context, repo string, number, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { namespace, name := scm.Split(repo) in := gitea.EditIssueCommentOption{Body: input.Body} out, resp, err := s.client.GiteaClient.EditIssueComment(namespace, name, int64(id), in) @@ -261,7 +261,7 @@ func (s *issueService) Unlock(ctx context.Context, repo string, number int) (*sc return nil, scm.ErrNotSupported } -func (s *issueService) SetMilestone(ctx context.Context, repo string, issueID int, number int) (*scm.Response, error) { +func (s *issueService) SetMilestone(ctx context.Context, repo string, issueID, number int) (*scm.Response, error) { namespace, name := scm.Split(repo) num64 := int64(number) in := gitea.EditIssueOption{ diff --git a/scm/driver/gitea/issue_test.go b/scm/driver/gitea/issue_test.go index c6cc7bf9b..e4ebca701 100644 --- a/scm/driver/gitea/issue_test.go +++ b/scm/driver/gitea/issue_test.go @@ -38,7 +38,10 @@ func TestIssueFind(t *testing.T) { want := new(scm.Issue) raw, _ := ioutil.ReadFile("testdata/issue.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -67,7 +70,10 @@ func TestIssueList(t *testing.T) { want := []*scm.Issue{} raw, _ := ioutil.ReadFile("testdata/issues.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -101,7 +107,10 @@ func TestIssueCreate(t *testing.T) { want := new(scm.Issue) raw, _ := ioutil.ReadFile("testdata/issue.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -186,7 +195,10 @@ func TestIssueCommentFind(t *testing.T) { want := new(scm.Comment) raw, _ := ioutil.ReadFile("testdata/comment.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -214,7 +226,10 @@ func TestIssueCommentList(t *testing.T) { want := []*scm.Comment{} raw, _ := ioutil.ReadFile("testdata/comments.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -243,7 +258,10 @@ func TestIssueCommentCreate(t *testing.T) { want := new(scm.Comment) raw, _ := ioutil.ReadFile("testdata/comment.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -292,7 +310,10 @@ func TestIssueListLabels(t *testing.T) { want := []*scm.Label{} raw, _ := ioutil.ReadFile("testdata/issue_labels.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/gitea/org.go b/scm/driver/gitea/org.go index b12299b3c..b34e3a015 100644 --- a/scm/driver/gitea/org.go +++ b/scm/driver/gitea/org.go @@ -35,12 +35,12 @@ func (s *organizationService) Delete(_ context.Context, org string) (*scm.Respon return toSCMResponse(resp), err } -func (s *organizationService) IsMember(ctx context.Context, org string, user string) (bool, *scm.Response, error) { +func (s *organizationService) IsMember(ctx context.Context, org, user string) (bool, *scm.Response, error) { isMember, resp, err := s.client.GiteaClient.CheckOrgMembership(org, user) return isMember, toSCMResponse(resp), err } -func (s *organizationService) IsAdmin(ctx context.Context, org string, user string) (bool, *scm.Response, error) { +func (s *organizationService) IsAdmin(ctx context.Context, org, user string) (bool, *scm.Response, error) { var members []*scm.TeamMember var res *scm.Response var membersPage []*scm.TeamMember diff --git a/scm/driver/gitea/org_test.go b/scm/driver/gitea/org_test.go index 2c6237701..7e0973647 100644 --- a/scm/driver/gitea/org_test.go +++ b/scm/driver/gitea/org_test.go @@ -35,7 +35,10 @@ func TestOrgFind(t *testing.T) { want := new(scm.Organization) raw, _ := ioutil.ReadFile("testdata/organization.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -63,7 +66,10 @@ func TestOrgList(t *testing.T) { want := []*scm.Organization{} raw, _ := ioutil.ReadFile("testdata/organizations.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/gitea/pr_test.go b/scm/driver/gitea/pr_test.go index ab305e6e0..580d541c3 100644 --- a/scm/driver/gitea/pr_test.go +++ b/scm/driver/gitea/pr_test.go @@ -40,7 +40,10 @@ func TestPullRequestFind(t *testing.T) { want := new(scm.PullRequest) raw, _ := ioutil.ReadFile("testdata/pr.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -68,7 +71,10 @@ func TestPullRequestList(t *testing.T) { want := []*scm.PullRequest{} raw, _ := ioutil.ReadFile("testdata/prs.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -192,7 +198,10 @@ func TestPullCreate(t *testing.T) { want := new(scm.PullRequest) raw, _ := ioutil.ReadFile("testdata/pr.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/gitea/release.go b/scm/driver/gitea/release.go index 97d335d23..579d888f2 100644 --- a/scm/driver/gitea/release.go +++ b/scm/driver/gitea/release.go @@ -20,7 +20,7 @@ func (s *releaseService) Find(ctx context.Context, repo string, id int) (*scm.Re return convertRelease(out), toSCMResponse(resp), err } -func (s *releaseService) FindByTag(ctx context.Context, repo string, tag string) (*scm.Release, *scm.Response, error) { +func (s *releaseService) FindByTag(ctx context.Context, repo, tag string) (*scm.Release, *scm.Response, error) { namespace, name := scm.Split(repo) @@ -57,13 +57,13 @@ func (s *releaseService) FindByTag(ctx context.Context, repo string, tag string) return nil, nil, scm.ErrNotFound } for _, r := range releases { - if strings.ToLower(r.TagName) == strings.ToLower(tag) { + if strings.EqualFold(strings.ToLower(r.TagName), strings.ToLower(tag)) { return convertRelease(r), nil, nil } } opts.Page++ } - return nil, nil, fmt.Errorf("Gave up scanning for release after %v pages, upgrade gitea to >= 1.13.2", scanPages) + return nil, nil, fmt.Errorf("gave up scanning for release after %v pages, upgrade gitea to >= 1.13.2", scanPages) } func (s *releaseService) List(ctx context.Context, repo string, opts scm.ReleaseListOptions) ([]*scm.Release, *scm.Response, error) { @@ -91,7 +91,7 @@ func (s *releaseService) Delete(ctx context.Context, repo string, id int) (*scm. return toSCMResponse(resp), err } -func (s *releaseService) DeleteByTag(ctx context.Context, repo string, tag string) (*scm.Response, error) { +func (s *releaseService) DeleteByTag(ctx context.Context, repo, tag string) (*scm.Response, error) { rel, _, err := s.FindByTag(ctx, repo, tag) if err != nil { return nil, err @@ -112,7 +112,7 @@ func (s *releaseService) Update(ctx context.Context, repo string, id int, input return convertRelease(out), toSCMResponse(resp), err } -func (s *releaseService) UpdateByTag(ctx context.Context, repo string, tag string, input *scm.ReleaseInput) (*scm.Release, *scm.Response, error) { +func (s *releaseService) UpdateByTag(ctx context.Context, repo, tag string, input *scm.ReleaseInput) (*scm.Release, *scm.Response, error) { rel, _, err := s.FindByTag(ctx, repo, tag) if err != nil { return nil, nil, err @@ -129,7 +129,7 @@ func convertReleaseList(from []*gitea.Release) []*scm.Release { } // ConvertAPIURLToHTMLURL converts an release API endpoint into a html endpoint -func ConvertAPIURLToHTMLURL(apiURL string, tagName string) string { +func ConvertAPIURLToHTMLURL(apiURL, tagName string) string { // "url": "https://try.gitea.com/api/v1/repos/octocat/Hello-World/123", // "html_url": "https://try.gitea.com/octocat/Hello-World/releases/tag/v1.0.0", // the url field is the API url, not the html url, so until go-sdk v0.13.3, build it ourselves diff --git a/scm/driver/gitea/release_test.go b/scm/driver/gitea/release_test.go index 199fd206c..186690643 100644 --- a/scm/driver/gitea/release_test.go +++ b/scm/driver/gitea/release_test.go @@ -23,9 +23,8 @@ func TestConvertAPIURLToHTMLURL(t *testing.T) { t.Log(diff) t.Log("got:") - t.Log(string(got)) + t.Log(got) } - } func TestConvertAPIURLToHTMLURLEmptyLinkWhenURLParseFails(t *testing.T) { @@ -41,10 +40,9 @@ func TestConvertAPIURLToHTMLURLEmptyLinkWhenURLParseFails(t *testing.T) { t.Log(diff) t.Log("got:") - t.Log(string(got)) + t.Log(got) } } - } func TestReleaseFind(t *testing.T) { defer gock.Off() @@ -226,6 +224,9 @@ func TestReleaseDelete(t *testing.T) { Type("application/json") client, err := New("https://try.gitea.io") + if err != nil { + t.Error(err) + } _, err = client.Releases.Delete(context.Background(), "octocat/hello-world", 1) if err != nil { t.Error(err) diff --git a/scm/driver/gitea/repo.go b/scm/driver/gitea/repo.go index 357514af7..52ee934bf 100644 --- a/scm/driver/gitea/repo.go +++ b/scm/driver/gitea/repo.go @@ -55,7 +55,7 @@ func (s *repositoryService) FindCombinedStatus(_ context.Context, repo, ref stri }, toSCMResponse(resp), nil } -func (s *repositoryService) FindUserPermission(ctx context.Context, repo string, user string) (string, *scm.Response, error) { +func (s *repositoryService) FindUserPermission(ctx context.Context, repo, user string) (string, *scm.Response, error) { namespace, _ := scm.Split(repo) if user == namespace { return scm.AdminPermission, nil, nil @@ -124,7 +124,7 @@ func (s *repositoryService) Find(_ context.Context, repo string) (*scm.Repositor return convertRepository(out), toSCMResponse(resp), err } -func (s *repositoryService) FindHook(_ context.Context, repo string, id string) (*scm.Hook, *scm.Response, error) { +func (s *repositoryService) FindHook(_ context.Context, repo, id string) (*scm.Hook, *scm.Response, error) { namespace, name := scm.Split(repo) idInt, err := strconv.ParseInt(id, 10, 64) if err != nil { @@ -163,7 +163,7 @@ func (s *repositoryService) ListHooks(_ context.Context, repo string, opts scm.L return convertHookList(out), toSCMResponse(resp), err } -func (s *repositoryService) ListStatus(_ context.Context, repo string, ref string, opts scm.ListOptions) ([]*scm.Status, *scm.Response, error) { +func (s *repositoryService) ListStatus(_ context.Context, repo, ref string, opts scm.ListOptions) ([]*scm.Status, *scm.Response, error) { namespace, name := scm.Split(repo) out, resp, err := s.client.GiteaClient.ListStatuses(namespace, name, ref, gitea.ListStatusesOption{ListOptions: toGiteaListOptions(opts)}) return convertStatusList(out), toSCMResponse(resp), err @@ -200,7 +200,7 @@ func (s *repositoryService) UpdateHook(ctx context.Context, repo string, input * return nil, nil, scm.ErrNotSupported } -func (s *repositoryService) CreateStatus(_ context.Context, repo string, ref string, input *scm.StatusInput) (*scm.Status, *scm.Response, error) { +func (s *repositoryService) CreateStatus(_ context.Context, repo, ref string, input *scm.StatusInput) (*scm.Status, *scm.Response, error) { namespace, name := scm.Split(repo) in := gitea.CreateStatusOption{ State: convertFromState(input.State), @@ -212,7 +212,7 @@ func (s *repositoryService) CreateStatus(_ context.Context, repo string, ref str return convertStatus(out), toSCMResponse(resp), err } -func (s *repositoryService) DeleteHook(_ context.Context, repo string, id string) (*scm.Response, error) { +func (s *repositoryService) DeleteHook(_ context.Context, repo, id string) (*scm.Response, error) { namespace, name := scm.Split(repo) idInt, err := strconv.ParseInt(id, 10, 64) if err != nil { @@ -306,8 +306,7 @@ func convertHookEvent(from scm.HookEvents) []string { events = append(events, "issue_comment") } if from.Branch || from.Tag { - events = append(events, "create") - events = append(events, "delete") + events = append(events, "create", "delete") } if from.Push { events = append(events, "push") diff --git a/scm/driver/gitea/repo_test.go b/scm/driver/gitea/repo_test.go index 7b0ed3a04..176d2b5c7 100644 --- a/scm/driver/gitea/repo_test.go +++ b/scm/driver/gitea/repo_test.go @@ -38,7 +38,10 @@ func TestRepoFind(t *testing.T) { want := new(scm.Repository) raw, _ := ioutil.ReadFile("testdata/repo.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -65,7 +68,10 @@ func TestRepoFindPerm(t *testing.T) { want := new(scm.Repository) raw, _ := ioutil.ReadFile("testdata/repo.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want.Perm); diff != "" { t.Errorf("Unexpected Results") @@ -93,7 +99,10 @@ func TestRepoList(t *testing.T) { want := []*scm.Repository{} raw, _ := ioutil.ReadFile("testdata/repos.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -145,7 +154,10 @@ func TestHookFind(t *testing.T) { want := new(scm.Hook) raw, _ := ioutil.ReadFile("testdata/hook.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -173,7 +185,10 @@ func TestHookList(t *testing.T) { want := []*scm.Hook{} raw, _ := ioutil.ReadFile("testdata/hooks.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -202,7 +217,10 @@ func TestHookCreate(t *testing.T) { want := new(scm.Hook) raw, _ := ioutil.ReadFile("testdata/hook.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -313,7 +331,10 @@ func TestStatusList(t *testing.T) { want := []*scm.Status{} raw, _ := ioutil.ReadFile("testdata/statuses.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -347,7 +368,10 @@ func TestStatusCreate(t *testing.T) { want := new(scm.Status) raw, _ := ioutil.ReadFile("testdata/status.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/gitea/review.go b/scm/driver/gitea/review.go index 1fa05fc74..aac98c475 100644 --- a/scm/driver/gitea/review.go +++ b/scm/driver/gitea/review.go @@ -47,13 +47,13 @@ func (s *reviewService) Delete(ctx context.Context, repo string, number, id int) return toSCMResponse(resp), err } -func (s *reviewService) ListComments(ctx context.Context, repo string, prID int, reviewID int, options scm.ListOptions) ([]*scm.ReviewComment, *scm.Response, error) { +func (s *reviewService) ListComments(ctx context.Context, repo string, prID, reviewID int, options scm.ListOptions) ([]*scm.ReviewComment, *scm.Response, error) { namespace, name := scm.Split(repo) comments, resp, err := s.client.GiteaClient.ListPullReviewComments(namespace, name, int64(prID), int64(reviewID)) return convertReviewCommentList(comments), toSCMResponse(resp), err } -func (s *reviewService) Update(ctx context.Context, repo string, prID int, reviewID int, body string) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Update(ctx context.Context, repo string, prID, reviewID int, body string) (*scm.Review, *scm.Response, error) { namespace, name := scm.Split(repo) in := gitea.SubmitPullReviewOptions{ Body: body, @@ -62,7 +62,7 @@ func (s *reviewService) Update(ctx context.Context, repo string, prID int, revie return convertReview(review), toSCMResponse(resp), err } -func (s *reviewService) Submit(ctx context.Context, repo string, prID int, reviewID int, input *scm.ReviewSubmitInput) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Submit(ctx context.Context, repo string, prID, reviewID int, input *scm.ReviewSubmitInput) (*scm.Review, *scm.Response, error) { namespace, name := scm.Split(repo) in := gitea.SubmitPullReviewOptions{ State: toGiteaState(input.Event), @@ -73,7 +73,7 @@ func (s *reviewService) Submit(ctx context.Context, repo string, prID int, revie } // TODO: Figure out whether this actually is a _thing_ exactly in Gitea. I don't think it is. -func (s *reviewService) Dismiss(ctx context.Context, repo string, prID int, reviewID int, msg string) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Dismiss(ctx context.Context, repo string, prID, reviewID int, msg string) (*scm.Review, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } diff --git a/scm/driver/gitea/user.go b/scm/driver/gitea/user.go index fc276277a..6fe3d24d7 100644 --- a/scm/driver/gitea/user.go +++ b/scm/driver/gitea/user.go @@ -15,7 +15,7 @@ type userService struct { client *wrapper } -func (s *userService) CreateToken(_ context.Context, user string, name string) (*scm.UserToken, *scm.Response, error) { +func (s *userService) CreateToken(_ context.Context, user, name string) (*scm.UserToken, *scm.Response, error) { out, resp, err := s.client.GiteaClient.CreateAccessToken(gitea.CreateAccessTokenOption{ Name: name, }) diff --git a/scm/driver/gitea/user_test.go b/scm/driver/gitea/user_test.go index 6149fd9af..7e720e9e0 100644 --- a/scm/driver/gitea/user_test.go +++ b/scm/driver/gitea/user_test.go @@ -35,7 +35,10 @@ func TestUserFind(t *testing.T) { want := new(scm.User) raw, _ := ioutil.ReadFile("testdata/user.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -62,7 +65,10 @@ func TestUserLoginFind(t *testing.T) { want := new(scm.User) raw, _ := ioutil.ReadFile("testdata/user.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/gitea/webhook_test.go b/scm/driver/gitea/webhook_test.go index b789ec792..9e5b90860 100644 --- a/scm/driver/gitea/webhook_test.go +++ b/scm/driver/gitea/webhook_test.go @@ -187,7 +187,10 @@ func TestWebhooks(t *testing.T) { t.Errorf("Error unmarshaling %s", test.before) t.Log(diff) - json.NewEncoder(os.Stdout).Encode(o) + err := json.NewEncoder(os.Stdout).Encode(o) + if err != nil { + t.Error(err) + } } switch event := o.(type) { diff --git a/scm/driver/github/app_test.go b/scm/driver/github/app_test.go index b63baf792..66903ce01 100644 --- a/scm/driver/github/app_test.go +++ b/scm/driver/github/app_test.go @@ -37,7 +37,10 @@ func TestAppRepositoryInstallation(t *testing.T) { want := new(scm.Installation) raw, _ := ioutil.ReadFile("testdata/app_repo_install.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/github/content_test.go b/scm/driver/github/content_test.go index eb4fbd2f7..8c7f7abeb 100644 --- a/scm/driver/github/content_test.go +++ b/scm/driver/github/content_test.go @@ -41,7 +41,10 @@ func TestContentFind(t *testing.T) { want := new(scm.Content) raw, _ := ioutil.ReadFile("testdata/content.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -77,7 +80,10 @@ func TestContentList(t *testing.T) { want := []*scm.FileEntry{} raw, _ := ioutil.ReadFile("testdata/content_list.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -161,5 +167,5 @@ func TestContentDelete(t *testing.T) { } func encode(b []byte) string { - return base64.StdEncoding.EncodeToString([]byte(b)) + return base64.StdEncoding.EncodeToString(b) } diff --git a/scm/driver/github/deploy.go b/scm/driver/github/deploy.go index 69873a0db..cb5a8a148 100644 --- a/scm/driver/github/deploy.go +++ b/scm/driver/github/deploy.go @@ -77,7 +77,7 @@ type deploymentStatusInput struct { AutoInactive bool `json:"auto_inactive"` } -func (s *deploymentService) Find(ctx context.Context, repoFullName string, deploymentID string) (*scm.Deployment, *scm.Response, error) { +func (s *deploymentService) Find(ctx context.Context, repoFullName, deploymentID string) (*scm.Deployment, *scm.Response, error) { path := fmt.Sprintf("repos/%s/deployments/%s", repoFullName, deploymentID) out := new(deployment) res, err := s.client.do(ctx, "GET", path, nil, out) @@ -99,26 +99,26 @@ func (s *deploymentService) Create(ctx context.Context, repoFullName string, dep return convertDeployment(out, repoFullName), res, wrapError(res, err) } -func (s *deploymentService) Delete(ctx context.Context, repoFullName string, deploymentID string) (*scm.Response, error) { +func (s *deploymentService) Delete(ctx context.Context, repoFullName, deploymentID string) (*scm.Response, error) { path := fmt.Sprintf("repos/%s/deployments/%s", repoFullName, deploymentID) return s.client.do(ctx, "DELETE", path, nil, nil) } -func (s *deploymentService) FindStatus(ctx context.Context, repoFullName string, deploymentID string, statusID string) (*scm.DeploymentStatus, *scm.Response, error) { +func (s *deploymentService) FindStatus(ctx context.Context, repoFullName, deploymentID, statusID string) (*scm.DeploymentStatus, *scm.Response, error) { path := fmt.Sprintf("repos/%s/deployments/%s/statuses/%s", repoFullName, deploymentID, statusID) out := new(deploymentStatus) res, err := s.client.do(ctx, "GET", path, nil, out) return convertDeploymentStatus(out), res, wrapError(res, err) } -func (s *deploymentService) ListStatus(ctx context.Context, repoFullName string, deploymentID string, opts scm.ListOptions) ([]*scm.DeploymentStatus, *scm.Response, error) { +func (s *deploymentService) ListStatus(ctx context.Context, repoFullName, deploymentID string, opts scm.ListOptions) ([]*scm.DeploymentStatus, *scm.Response, error) { path := fmt.Sprintf("repos/%s/deployments/%s/statuses?%s", repoFullName, deploymentID, encodeListOptions(opts)) out := []*deploymentStatus{} res, err := s.client.do(ctx, "GET", path, nil, &out) return convertDeploymentStatusList(out), res, wrapError(res, err) } -func (s *deploymentService) CreateStatus(ctx context.Context, repoFullName string, deploymentID string, deploymentStatusInput *scm.DeploymentStatusInput) (*scm.DeploymentStatus, *scm.Response, error) { +func (s *deploymentService) CreateStatus(ctx context.Context, repoFullName, deploymentID string, deploymentStatusInput *scm.DeploymentStatusInput) (*scm.DeploymentStatus, *scm.Response, error) { path := fmt.Sprintf("repos/%s/deployments/%s/statuses", repoFullName, deploymentID) in := convertToDeploymentStatusInput(deploymentStatusInput) out := new(deploymentStatus) diff --git a/scm/driver/github/deploy_test.go b/scm/driver/github/deploy_test.go index f01898efc..17231b373 100644 --- a/scm/driver/github/deploy_test.go +++ b/scm/driver/github/deploy_test.go @@ -34,7 +34,10 @@ func TestDeploymentFind(t *testing.T) { want := new(scm.Deployment) raw, _ := ioutil.ReadFile("testdata/deploy.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -90,7 +93,10 @@ func TestDeploymentList(t *testing.T) { want := []*scm.Deployment{} raw, _ := ioutil.ReadFile("testdata/deploys.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -125,7 +131,10 @@ func TestDeploymentCreate(t *testing.T) { want := new(scm.Deployment) raw, _ := ioutil.ReadFile("testdata/deploy_create.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -160,7 +169,10 @@ func TestDeploymentStatusList(t *testing.T) { want := []*scm.DeploymentStatus{} raw, _ := ioutil.ReadFile("testdata/deploy_statuses.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -193,7 +205,10 @@ func TestDeploymentStatusFind(t *testing.T) { want := new(scm.DeploymentStatus) raw, _ := ioutil.ReadFile("testdata/deploy_status.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -227,7 +242,10 @@ func TestDeploymentStatusCreate(t *testing.T) { want := new(scm.DeploymentStatus) raw, _ := ioutil.ReadFile("testdata/deploy_status_create.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/github/git_test.go b/scm/driver/github/git_test.go index 37b75351b..56d638731 100644 --- a/scm/driver/github/git_test.go +++ b/scm/driver/github/git_test.go @@ -36,7 +36,10 @@ func TestGitFindCommit(t *testing.T) { want := new(scm.Commit) raw, _ := ioutil.ReadFile("testdata/commit.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -66,7 +69,10 @@ func TestGitFindBranch(t *testing.T) { want := new(scm.Reference) raw, _ := ioutil.ReadFile("testdata/branch.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -108,7 +114,10 @@ func TestGitListCommits(t *testing.T) { want := []*scm.Commit{} raw, _ := ioutil.ReadFile("testdata/commits.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -142,7 +151,10 @@ func TestGitListBranches(t *testing.T) { want := []*scm.Reference{} raw, _ := ioutil.ReadFile("testdata/branches.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -176,7 +188,10 @@ func TestGitListTags(t *testing.T) { want := []*scm.Reference{} raw, _ := ioutil.ReadFile("testdata/tags.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -207,7 +222,10 @@ func TestGitListChanges(t *testing.T) { want := []*scm.Change{} raw, _ := ioutil.ReadFile("testdata/changes.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -237,7 +255,10 @@ func TestGitCompareCommits(t *testing.T) { want := []*scm.Change{} raw, _ := ioutil.ReadFile("testdata/changes.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -267,7 +288,10 @@ func TestGitCreateRef(t *testing.T) { want := &scm.Reference{} raw, _ := ioutil.ReadFile("testdata/ref.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/github/github.go b/scm/driver/github/github.go index f0f23b94f..7b950b520 100644 --- a/scm/driver/github/github.go +++ b/scm/driver/github/github.go @@ -38,7 +38,7 @@ func New(uri string) (*scm.Client, error) { return nil, err } if !strings.HasSuffix(base.Path, "/") { - base.Path = base.Path + "/" + base.Path += "/" } client := &wrapper{new(scm.Client)} client.BaseURL = base @@ -123,7 +123,10 @@ func (c *wrapper) doRequest(ctx context.Context, req *scm.Request, in, out inter // write it to the body of the request. if in != nil { buf := new(bytes.Buffer) - json.NewEncoder(buf).Encode(in) + err := json.NewEncoder(buf).Encode(in) // #nosec + if err != nil { + return nil, err + } if req.Header == nil { req.Header = map[string][]string{} } diff --git a/scm/driver/github/integration/issue_test.go b/scm/driver/github/integration/issue_test.go index d976392b6..6638f0815 100644 --- a/scm/driver/github/integration/issue_test.go +++ b/scm/driver/github/integration/issue_test.go @@ -136,13 +136,10 @@ func testIssueComment(comment *scm.Comment) func(t *testing.T) { if got, want := comment.Body, "A shiny new comment! :tada:"; got != want { t.Errorf("Want issue comment Body %q, got %q", want, got) } - if got, want := comment.Author.Login, "defualt"; got != want { + if got, want := comment.Author.Login, "default"; got != want { t.Errorf("Want issue comment Author Login %q, got %q", want, got) } // TODO: Avatar check seems to have become unreliable. Reenable in the future. (apb) - //if got, want := comment.Author.Avatar, "https://avatars2.githubusercontent.com/u/399135?v=4"; got != want { - // t.Errorf("Want issue comment Author Name %q, got %q", want, got) - //} if got, want := comment.Created.Unix(), int64(1495732818); got != want { t.Errorf("Want issue comment Created %d, got %d", want, got) } diff --git a/scm/driver/github/integration/pr_test.go b/scm/driver/github/integration/pr_test.go index 1d100068f..2d0ab9598 100644 --- a/scm/driver/github/integration/pr_test.go +++ b/scm/driver/github/integration/pr_test.go @@ -193,9 +193,6 @@ func testPullRequestComment(comment *scm.Comment) func(t *testing.T) { t.Errorf("Want pr comment Author Name %q, got %q", want, got) } // TODO: Avatar check has become unreliable. Fix in the future. (apb) - //if got, want := comment.Author.Avatar, "https://avatars3.githubusercontent.com/u/7744744?v=4"; got != want { - // t.Errorf("Want pr comment Author Avatar %q, got %q", want, got) - //} if got, want := comment.Created.Unix(), int64(1414224391); got != want { t.Errorf("Want pr comment Created %d, got %d", want, got) } diff --git a/scm/driver/github/issue.go b/scm/driver/github/issue.go index 37321f0bd..82b0f06fd 100644 --- a/scm/driver/github/issue.go +++ b/scm/driver/github/issue.go @@ -184,7 +184,7 @@ func (s *issueService) DeleteComment(ctx context.Context, repo string, number, i return s.client.do(ctx, "DELETE", path, nil, nil) } -func (s *issueService) EditComment(ctx context.Context, repo string, number int, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { +func (s *issueService) EditComment(ctx context.Context, repo string, number, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { path := fmt.Sprintf("repos/%s/issues/comments/%d", repo, id) in := &issueCommentInput{ Body: input.Body, @@ -222,7 +222,7 @@ func (s *issueService) Unlock(ctx context.Context, repo string, number int) (*sc return res, err } -func (s *issueService) SetMilestone(ctx context.Context, repo string, issueID int, number int) (*scm.Response, error) { +func (s *issueService) SetMilestone(ctx context.Context, repo string, issueID, number int) (*scm.Response, error) { path := fmt.Sprintf("repos/%s/issues/%d", repo, issueID) in := &struct { Milestone int `json:"milestone"` diff --git a/scm/driver/github/issue_test.go b/scm/driver/github/issue_test.go index 9f88a76d2..376268d37 100644 --- a/scm/driver/github/issue_test.go +++ b/scm/driver/github/issue_test.go @@ -35,7 +35,10 @@ func TestIssueFind(t *testing.T) { want := new(scm.Issue) raw, _ := ioutil.ReadFile("testdata/issue.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -65,7 +68,10 @@ func TestIssueCommentFind(t *testing.T) { want := new(scm.Comment) raw, _ := ioutil.ReadFile("testdata/issue_comment.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -99,7 +105,10 @@ func TestIssueList(t *testing.T) { want := []*scm.Issue{} raw, _ := ioutil.ReadFile("testdata/issues.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -116,9 +125,6 @@ func TestIssueSearch(t *testing.T) { gock.New("https://api.github.com"). Get("/search/issues"). - //MatchParam("page", "1"). - //MatchParam("per_page", "30"). - //MatchParam("state", "all"). Reply(200). Type("application/json"). SetHeaders(mockHeaders). @@ -138,7 +144,10 @@ func TestIssueSearch(t *testing.T) { want := []*scm.SearchIssue{} raw, _ := ioutil.ReadFile("testdata/issue_search.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } data, _ := json.Marshal(got) t.Log(string(data)) @@ -158,9 +167,6 @@ func TestIssuePPRSearch(t *testing.T) { gock.New("https://api.github.com"). Get("/search/issues"). - //MatchParam("page", "1"). - //MatchParam("per_page", "30"). - //MatchParam("state", "all"). Reply(200). Type("application/json"). SetHeaders(mockHeaders). @@ -180,7 +186,10 @@ func TestIssuePPRSearch(t *testing.T) { want := []*scm.SearchIssue{} raw, _ := ioutil.ReadFile("testdata/issue_search_prs.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } data, _ := json.Marshal(got) t.Log(string(data)) @@ -217,7 +226,10 @@ func TestIssueListComments(t *testing.T) { want := []*scm.Comment{} raw, _ := ioutil.ReadFile("testdata/issue_comments.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -253,7 +265,10 @@ func TestIssueCreate(t *testing.T) { want := new(scm.Issue) raw, _ := ioutil.ReadFile("testdata/issue.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -287,7 +302,10 @@ func TestIssueCreateComment(t *testing.T) { want := new(scm.Comment) raw, _ := ioutil.ReadFile("testdata/issue_comment.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -343,7 +361,10 @@ func TestIssueEditComment(t *testing.T) { want := new(scm.Comment) raw, _ := ioutil.ReadFile("testdata/issue_comment.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/github/org.go b/scm/driver/github/org.go index a0429fc0f..7216e28d2 100644 --- a/scm/driver/github/org.go +++ b/scm/driver/github/org.go @@ -71,7 +71,7 @@ func (s *organizationService) Delete(context.Context, string) (*scm.Response, er return nil, scm.ErrNotSupported } -func (s *organizationService) IsMember(ctx context.Context, org string, user string) (bool, *scm.Response, error) { +func (s *organizationService) IsMember(ctx context.Context, org, user string) (bool, *scm.Response, error) { path := fmt.Sprintf("orgs/%s/members/%s", org, user) res, err := s.client.do(ctx, "GET", path, nil, nil) if err != nil && res == nil { @@ -89,7 +89,7 @@ func (s *organizationService) IsMember(ctx context.Context, org string, user str return false, res, fmt.Errorf("unexpected status: %d", code) } -func (s *organizationService) IsAdmin(ctx context.Context, org string, user string) (bool, *scm.Response, error) { +func (s *organizationService) IsAdmin(ctx context.Context, org, user string) (bool, *scm.Response, error) { path := fmt.Sprintf("orgs/%s/memberships/%s", org, user) out := membership{} res, err := s.client.do(ctx, "GET", path, nil, &out) diff --git a/scm/driver/github/org_test.go b/scm/driver/github/org_test.go index e4ce33458..a4d290edb 100644 --- a/scm/driver/github/org_test.go +++ b/scm/driver/github/org_test.go @@ -38,7 +38,10 @@ func TestOrganizationFind(t *testing.T) { want := new(scm.Organization) raw, _ := ioutil.ReadFile("testdata/org.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -71,7 +74,10 @@ func TestOrganizationList(t *testing.T) { want := []*scm.Organization{} raw, _ := ioutil.ReadFile("testdata/orgs.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -107,7 +113,10 @@ func TestTeamList(t *testing.T) { want := []*scm.Team{} raw, _ := ioutil.ReadFile("testdata/teams.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -145,7 +154,10 @@ func TestTeamMembers(t *testing.T) { want := []*scm.TeamMember{} raw, _ := ioutil.ReadFile("testdata/team_members.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -179,7 +191,10 @@ func TestOrgMembers(t *testing.T) { want := []*scm.TeamMember{} raw, _ := ioutil.ReadFile("testdata/org_members.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/github/pr_test.go b/scm/driver/github/pr_test.go index 320fcd664..6525e4ae5 100644 --- a/scm/driver/github/pr_test.go +++ b/scm/driver/github/pr_test.go @@ -35,7 +35,10 @@ func TestPullFind(t *testing.T) { want := new(scm.PullRequest) raw, _ := ioutil.ReadFile("testdata/pr.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -69,7 +72,10 @@ func TestPullList(t *testing.T) { want := []*scm.PullRequest{} raw, _ := ioutil.ReadFile("testdata/pulls.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -101,7 +107,10 @@ func TestPullListChanges(t *testing.T) { want := []*scm.Change{} raw, _ := ioutil.ReadFile("testdata/pr_files.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -205,7 +214,13 @@ func TestPullCreate(t *testing.T) { want := new(scm.PullRequest) raw, err := ioutil.ReadFile("testdata/pr_create.json.golden") - json.Unmarshal(raw, want) + if err != nil { + t.Fatal(err) + } + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -241,7 +256,13 @@ func TestPullUpdate(t *testing.T) { want := new(scm.PullRequest) raw, err := ioutil.ReadFile("testdata/pr_create.json.golden") - json.Unmarshal(raw, want) + if err != nil { + t.Fatal(err) + } + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/github/release.go b/scm/driver/github/release.go index ed43eac0e..32c2e8bb4 100644 --- a/scm/driver/github/release.go +++ b/scm/driver/github/release.go @@ -41,7 +41,7 @@ func (s *releaseService) Find(ctx context.Context, repo string, id int) (*scm.Re return convertRelease(out), res, err } -func (s *releaseService) FindByTag(ctx context.Context, repo string, tag string) (*scm.Release, *scm.Response, error) { +func (s *releaseService) FindByTag(ctx context.Context, repo, tag string) (*scm.Release, *scm.Response, error) { path := fmt.Sprintf("repos/%s/releases/tags/%s", repo, tag) out := new(release) res, err := s.client.do(ctx, "GET", path, nil, out) @@ -75,7 +75,7 @@ func (s *releaseService) Delete(ctx context.Context, repo string, id int) (*scm. return s.client.do(ctx, "DELETE", path, nil, nil) } -func (s *releaseService) DeleteByTag(ctx context.Context, repo string, tag string) (*scm.Response, error) { +func (s *releaseService) DeleteByTag(ctx context.Context, repo, tag string) (*scm.Response, error) { rel, _, _ := s.FindByTag(ctx, repo, tag) return s.Delete(ctx, repo, rel.ID) } @@ -102,7 +102,7 @@ func (s *releaseService) Update(ctx context.Context, repo string, id int, input return convertRelease(out), res, err } -func (s *releaseService) UpdateByTag(ctx context.Context, repo string, tag string, input *scm.ReleaseInput) (*scm.Release, *scm.Response, error) { +func (s *releaseService) UpdateByTag(ctx context.Context, repo, tag string, input *scm.ReleaseInput) (*scm.Release, *scm.Response, error) { rel, _, _ := s.FindByTag(ctx, repo, tag) return s.Update(ctx, repo, rel.ID, input) } diff --git a/scm/driver/github/repo.go b/scm/driver/github/repo.go index e89391302..cbfef9e85 100644 --- a/scm/driver/github/repo.go +++ b/scm/driver/github/repo.go @@ -160,7 +160,7 @@ func (s *repositoryService) Find(ctx context.Context, repo string) (*scm.Reposit } // FindHook returns a repository hook. -func (s *repositoryService) FindHook(ctx context.Context, repo string, id string) (*scm.Hook, *scm.Response, error) { +func (s *repositoryService) FindHook(ctx context.Context, repo, id string) (*scm.Hook, *scm.Response, error) { path := fmt.Sprintf("repos/%s/hooks/%s", repo, id) out := new(hook) res, err := s.client.do(ctx, "GET", path, nil, out) @@ -178,7 +178,7 @@ func (s *repositoryService) FindPerms(ctx context.Context, repo string) (*scm.Pe // FindPerms returns the repository permissions. // // https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level -func (s *repositoryService) FindUserPermission(ctx context.Context, repo string, user string) (string, *scm.Response, error) { +func (s *repositoryService) FindUserPermission(ctx context.Context, repo, user string) (string, *scm.Response, error) { path := fmt.Sprintf("repos/%s/collaborators/%s/permission", repo, user) var out struct { Perm string `json:"permission"` @@ -328,7 +328,7 @@ func (s *repositoryService) CreateStatus(ctx context.Context, repo, ref string, } // DeleteHook deletes a repository webhook. -func (s *repositoryService) DeleteHook(ctx context.Context, repo string, id string) (*scm.Response, error) { +func (s *repositoryService) DeleteHook(ctx context.Context, repo, id string) (*scm.Response, error) { path := fmt.Sprintf("repos/%s/hooks/%s", repo, id) return s.client.do(ctx, "DELETE", path, nil, nil) } @@ -419,8 +419,7 @@ func convertHookEvents(from scm.HookEvents) []string { events = append(events, "issue_comment") } if from.Branch || from.Tag { - events = append(events, "create") - events = append(events, "delete") + events = append(events, "create", "delete") } if from.Deployment { events = append(events, "deployment") diff --git a/scm/driver/github/repo_test.go b/scm/driver/github/repo_test.go index 70703c620..bcf2d0f61 100644 --- a/scm/driver/github/repo_test.go +++ b/scm/driver/github/repo_test.go @@ -34,7 +34,10 @@ func TestRepositoryFind(t *testing.T) { want := new(scm.Repository) raw, _ := ioutil.ReadFile("testdata/repo.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -64,7 +67,10 @@ func TestRepositoryPerms(t *testing.T) { want := new(scm.Repository) raw, _ := ioutil.ReadFile("testdata/repo.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want.Perm); diff != "" { t.Errorf("Unexpected Results") @@ -118,7 +124,10 @@ func TestRepositoryList(t *testing.T) { want := []*scm.Repository{} raw, _ := ioutil.ReadFile("testdata/repos.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -152,7 +161,10 @@ func TestStatusList(t *testing.T) { want := []*scm.Status{} raw, _ := ioutil.ReadFile("testdata/statuses.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -228,7 +240,10 @@ func TestStatusCreate(t *testing.T) { want := new(scm.Status) raw, _ := ioutil.ReadFile("testdata/status.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -258,7 +273,10 @@ func TestRepositoryHookFind(t *testing.T) { want := new(scm.Hook) raw, _ := ioutil.ReadFile("testdata/hook.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -319,7 +337,10 @@ func TestRepositoryHookList(t *testing.T) { want := []*scm.Hook{} raw, _ := ioutil.ReadFile("testdata/hooks.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -381,7 +402,10 @@ func TestRepositoryHookCreate(t *testing.T) { want := new(scm.Hook) raw, _ := ioutil.ReadFile("testdata/hook.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -416,7 +440,10 @@ func TestRepositoryCreate(t *testing.T) { want := new(scm.Repository) raw, _ := ioutil.ReadFile("testdata/repo_create.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -455,7 +482,10 @@ func TestRepositoryFork(t *testing.T) { want := new(scm.Repository) raw, _ := ioutil.ReadFile("testdata/repo_create.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/github/review.go b/scm/driver/github/review.go index ff0aa88d9..3eee41f4a 100644 --- a/scm/driver/github/review.go +++ b/scm/driver/github/review.go @@ -54,14 +54,14 @@ func (s *reviewService) Delete(ctx context.Context, repo string, number, id int) return s.client.do(ctx, "DELETE", path, nil, nil) } -func (s *reviewService) ListComments(ctx context.Context, repo string, prID int, reviewID int, options scm.ListOptions) ([]*scm.ReviewComment, *scm.Response, error) { +func (s *reviewService) ListComments(ctx context.Context, repo string, prID, reviewID int, options scm.ListOptions) ([]*scm.ReviewComment, *scm.Response, error) { path := fmt.Sprintf("repos/%s/pulls/%d/reviews/%d/comments?%s", repo, prID, reviewID, encodeListOptions(options)) out := []*reviewComment{} res, err := s.client.do(ctx, "GET", path, nil, &out) return convertReviewCommentList(out), res, err } -func (s *reviewService) Update(ctx context.Context, repo string, prID int, reviewID int, body string) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Update(ctx context.Context, repo string, prID, reviewID int, body string) (*scm.Review, *scm.Response, error) { path := fmt.Sprintf("repos/%s/pulls/%d/reviews/%d", repo, prID, reviewID) in := &reviewUpdateInput{Body: body} @@ -70,7 +70,7 @@ func (s *reviewService) Update(ctx context.Context, repo string, prID int, revie return convertReview(out), res, err } -func (s *reviewService) Submit(ctx context.Context, repo string, prID int, reviewID int, input *scm.ReviewSubmitInput) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Submit(ctx context.Context, repo string, prID, reviewID int, input *scm.ReviewSubmitInput) (*scm.Review, *scm.Response, error) { path := fmt.Sprintf("repos/%s/pulls/%d/reviews/%d/events", repo, prID, reviewID) in := &reviewSubmitInput{ Body: input.Body, @@ -82,7 +82,7 @@ func (s *reviewService) Submit(ctx context.Context, repo string, prID int, revie return convertReview(out), res, err } -func (s *reviewService) Dismiss(ctx context.Context, repo string, prID int, reviewID int, msg string) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Dismiss(ctx context.Context, repo string, prID, reviewID int, msg string) (*scm.Review, *scm.Response, error) { path := fmt.Sprintf("repos/%s/pulls/%d/reviews/%d/dismissals", repo, prID, reviewID) in := &reviewDismissInput{ Message: msg, diff --git a/scm/driver/github/review_test.go b/scm/driver/github/review_test.go index b7cc6c9d3..9cc104d99 100644 --- a/scm/driver/github/review_test.go +++ b/scm/driver/github/review_test.go @@ -34,7 +34,10 @@ func TestReviewFind(t *testing.T) { want := new(scm.Review) raw, _ := ioutil.ReadFile("testdata/reviews_find.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -67,7 +70,10 @@ func TestReviewList(t *testing.T) { want := []*scm.Review{} raw, _ := ioutil.ReadFile("testdata/reviews_list.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -112,7 +118,10 @@ func TestReviewCreate(t *testing.T) { want := new(scm.Review) raw, _ := ioutil.ReadFile("testdata/reviews_find.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -165,7 +174,10 @@ func TestReviewListComments(t *testing.T) { want := []*scm.ReviewComment{} raw, _ := ioutil.ReadFile("testdata/reviews_list_comments.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -197,7 +209,10 @@ func TestReviewUpdate(t *testing.T) { want := new(scm.Review) raw, _ := ioutil.ReadFile("testdata/reviews_find.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -232,7 +247,10 @@ func TestReviewSubmit(t *testing.T) { want := new(scm.Review) raw, _ := ioutil.ReadFile("testdata/reviews_find.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -263,7 +281,10 @@ func TestReviewDismiss(t *testing.T) { want := new(scm.Review) raw, _ := ioutil.ReadFile("testdata/reviews_find.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/github/user_test.go b/scm/driver/github/user_test.go index 90a0d664e..1ed4a7b91 100644 --- a/scm/driver/github/user_test.go +++ b/scm/driver/github/user_test.go @@ -39,7 +39,10 @@ func TestUserFind(t *testing.T) { want := new(scm.User) raw, _ := ioutil.ReadFile("testdata/user.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -71,12 +74,18 @@ func TestUserLoginFind(t *testing.T) { want := new(scm.User) raw, _ := ioutil.ReadFile("testdata/user.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") t.Log(diff) - json.NewEncoder(os.Stdout).Encode(got) + err := json.NewEncoder(os.Stdout).Encode(got) + if err != nil { + t.Error(err) + } } t.Run("Request", testRequest(res)) @@ -130,12 +139,18 @@ func TestUserListInvitations(t *testing.T) { want := []*scm.Invitation{} raw, _ := ioutil.ReadFile("testdata/list_invitations.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") t.Log(diff) - json.NewEncoder(os.Stdout).Encode(got) + err := json.NewEncoder(os.Stdout).Encode(got) + if err != nil { + t.Error(err) + } } t.Run("Request", testRequest(res)) diff --git a/scm/driver/github/webhook.go b/scm/driver/github/webhook.go index 8e8f79be6..6f05634af 100644 --- a/scm/driver/github/webhook.go +++ b/scm/driver/github/webhook.go @@ -204,16 +204,6 @@ func (s *webhookService) parseCheckRunHook(data []byte) (scm.Webhook, error) { return to, err } -func (s *webhookService) parseStarHook(data []byte) (scm.Webhook, error) { - src := new(starHook) - err := json.Unmarshal(data, src) - if err != nil { - return nil, err - } - to := convertStarHook(src) - return to, err -} - func (s *webhookService) parseCheckSuiteHook(data []byte) (scm.Webhook, error) { src := new(checkSuiteHook) err := json.Unmarshal(data, src) @@ -429,14 +419,6 @@ type ( Installation *installationRef `json:"installation"` } - // github star repo payload - starHook struct { - Action string `json:"action"` - Repository repository `json:"repository"` - Sender user `json:"sender"` - StarredAt time.Time `json:"starred_at"` - } - // github check_suite payload checkSuiteHook struct { Action string `json:"action"` @@ -829,15 +811,6 @@ func convertCheckRunHook(dst *checkRunHook) *scm.CheckRunHook { } } -func convertStarHook(dst *starHook) *scm.StarHook { - return &scm.StarHook{ - Action: convertAction(dst.Action), - StarredAt: dst.StarredAt, - Repo: *convertRepository(&dst.Repository), - Sender: *convertUser(&dst.Sender), - } -} - func convertCheckSuiteHook(dst *checkSuiteHook) *scm.CheckSuiteHook { return &scm.CheckSuiteHook{ Action: convertAction(dst.Action), @@ -1144,7 +1117,7 @@ func convertPullRequestComment(comment *reviewCommentFromHook) *scm.Comment { // regexp help determine if the named git object is a tag. // this is not meant to be 100% accurate. -var tagRE = regexp.MustCompile("^v?(\\d+).(.+)") +var tagRE = regexp.MustCompile(`^v?(\\d+).(.+)`) func convertReviewAction(src string) (action scm.Action) { switch src { diff --git a/scm/driver/github/webhook_test.go b/scm/driver/github/webhook_test.go index 046462304..719314887 100644 --- a/scm/driver/github/webhook_test.go +++ b/scm/driver/github/webhook_test.go @@ -356,7 +356,10 @@ func TestWebhooks(t *testing.T) { t.Log(diff) // debug only. remove once implemented - json.NewEncoder(os.Stdout).Encode(o) + err := json.NewEncoder(os.Stdout).Encode(o) + if err != nil { + t.Fatal(err) + } } switch event := o.(type) { diff --git a/scm/driver/gitlab/content_test.go b/scm/driver/gitlab/content_test.go index ce18a62ce..485f63786 100644 --- a/scm/driver/gitlab/content_test.go +++ b/scm/driver/gitlab/content_test.go @@ -43,7 +43,10 @@ func TestContentFind(t *testing.T) { want := new(scm.Content) raw, _ := ioutil.ReadFile("testdata/content.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -104,7 +107,7 @@ func TestContentCreate(t *testing.T) { content := []byte("testing") branch := "my-test-branch" - encoded := base64.StdEncoding.EncodeToString([]byte(content)) + encoded := base64.StdEncoding.EncodeToString(content) gock.New("https://gitlab.com"). Post("api/v4/projects/octocat/hello-world/repository/commits"). diff --git a/scm/driver/gitlab/git_test.go b/scm/driver/gitlab/git_test.go index 7a3bd2f44..2abcc8c06 100644 --- a/scm/driver/gitlab/git_test.go +++ b/scm/driver/gitlab/git_test.go @@ -40,7 +40,10 @@ func TestGitFindCommit(t *testing.T) { want := new(scm.Commit) raw, _ := ioutil.ReadFile("testdata/commit.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -70,7 +73,10 @@ func TestGitFindBranch(t *testing.T) { want := new(scm.Reference) raw, _ := ioutil.ReadFile("testdata/branch.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -100,7 +106,10 @@ func TestGitFindTag(t *testing.T) { want := new(scm.Reference) raw, _ := ioutil.ReadFile("testdata/tag.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -134,7 +143,10 @@ func TestGitListCommits(t *testing.T) { want := []*scm.Commit{} raw, _ := ioutil.ReadFile("testdata/commits.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -168,7 +180,10 @@ func TestGitListBranches(t *testing.T) { want := []*scm.Reference{} raw, _ := ioutil.ReadFile("testdata/branches.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -202,7 +217,10 @@ func TestGitListTags(t *testing.T) { want := []*scm.Reference{} raw, _ := ioutil.ReadFile("testdata/tags.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -233,7 +251,10 @@ func TestGitListChanges(t *testing.T) { want := []*scm.Change{} raw, _ := ioutil.ReadFile("testdata/commit_diff.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -265,7 +286,10 @@ func TestGitCompareCommits(t *testing.T) { want := []*scm.Change{} raw, _ := ioutil.ReadFile("testdata/compare.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -296,7 +320,10 @@ func TestGitCreateRef(t *testing.T) { want := &scm.Reference{} raw, _ := ioutil.ReadFile("testdata/create_branch.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/gitlab/gitlab.go b/scm/driver/gitlab/gitlab.go index 526f3505b..5539e1ec3 100644 --- a/scm/driver/gitlab/gitlab.go +++ b/scm/driver/gitlab/gitlab.go @@ -33,7 +33,7 @@ func New(uri string) (*scm.Client, error) { return nil, err } if !strings.HasSuffix(base.Path, "/") { - base.Path = base.Path + "/" + base.Path += "/" } client := &wrapper{new(scm.Client)} client.BaseURL = base @@ -50,7 +50,7 @@ func New(uri string) (*scm.Client, error) { client.Reviews = &reviewService{client} client.Commits = &commitService{client} - //add the user service to the webhook service so it can be used for fetching users + // add the user service to the webhook service so it can be used for fetching users us := &userService{client} client.Users = us client.Webhooks = &webhookService{ @@ -138,7 +138,10 @@ func (c *wrapper) do(ctx context.Context, method, path string, in, out interface // write it to the body of the request. if in != nil { buf := new(bytes.Buffer) - json.NewEncoder(buf).Encode(in) + err := json.NewEncoder(buf).Encode(in) // #nosec + if err != nil { + return nil, err + } if req.Header == nil { req.Header = map[string][]string{} } diff --git a/scm/driver/gitlab/gitlab_test.go b/scm/driver/gitlab/gitlab_test.go index 47a057c58..8c753aaee 100644 --- a/scm/driver/gitlab/gitlab_test.go +++ b/scm/driver/gitlab/gitlab_test.go @@ -90,7 +90,10 @@ func TestClientDo(t *testing.T) { defer r.Body.Close() w.Header().Set("Content-Type", mimeJSON) body = bytes.TrimSpace(b) - w.Write([]byte(`{"new":"value"}`)) + _, err = w.Write([]byte(`{"new":"value"}`)) + if err != nil { + t.Fatal(err) + } })) defer ts.Close() @@ -124,7 +127,10 @@ func TestClientDoResponseValues(t *testing.T) { h.Set("RateLimit-Limit", "200") h.Set("RateLimit-Remaining", "180") h.Set("RateLimit-Reset", "3600") - w.Write([]byte(`{"new":"value"}`)) + _, err := w.Write([]byte(`{"new":"value"}`)) + if err != nil { + t.Fatal(err) + } })) defer ts.Close() diff --git a/scm/driver/gitlab/issue.go b/scm/driver/gitlab/issue.go index 3602c4317..55dd65bd7 100644 --- a/scm/driver/gitlab/issue.go +++ b/scm/driver/gitlab/issue.go @@ -196,7 +196,7 @@ func (s *issueService) DeleteComment(ctx context.Context, repo string, number, i return s.client.do(ctx, "DELETE", path, nil, nil) } -func (s *issueService) EditComment(ctx context.Context, repo string, number int, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { +func (s *issueService) EditComment(ctx context.Context, repo string, number, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { in := &updateNoteOptions{Body: input.Body} path := fmt.Sprintf("api/v4/projects/%s/issues/%d/notes/%d", encode(repo), number, id) out := new(issueComment) @@ -228,7 +228,7 @@ func (s *issueService) Unlock(ctx context.Context, repo string, number int) (*sc return res, err } -func (s *issueService) SetMilestone(ctx context.Context, repo string, issueID int, number int) (*scm.Response, error) { +func (s *issueService) SetMilestone(ctx context.Context, repo string, issueID, number int) (*scm.Response, error) { in := &updateIssueOptions{ MilestoneID: &number, } @@ -289,6 +289,7 @@ type issueAssignee struct { Username string `json:"username"` } +// nolint type issueComment struct { ID int `json:"id"` Number int `json:"noteable_iid"` diff --git a/scm/driver/gitlab/issue_test.go b/scm/driver/gitlab/issue_test.go index 78b78e34e..82bb69f40 100644 --- a/scm/driver/gitlab/issue_test.go +++ b/scm/driver/gitlab/issue_test.go @@ -35,7 +35,10 @@ func TestIssueFind(t *testing.T) { want := new(scm.Issue) raw, _ := ioutil.ReadFile("testdata/issue.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -65,7 +68,10 @@ func TestIssueCommentFind(t *testing.T) { want := new(scm.Comment) raw, _ := ioutil.ReadFile("testdata/issue_note.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -99,7 +105,10 @@ func TestIssueList(t *testing.T) { want := []*scm.Issue{} raw, _ := ioutil.ReadFile("testdata/issues.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -133,7 +142,10 @@ func TestIssueListComments(t *testing.T) { want := []*scm.Comment{} raw, _ := ioutil.ReadFile("testdata/issue_notes.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -206,7 +218,10 @@ func TestIssueCreate(t *testing.T) { want := new(scm.Issue) raw, _ := ioutil.ReadFile("testdata/issue.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -241,7 +256,10 @@ func TestIssueCreateComment(t *testing.T) { want := new(scm.Comment) raw, _ := ioutil.ReadFile("testdata/issue_note.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -296,7 +314,10 @@ func TestIssueEditComment(t *testing.T) { want := new(scm.Comment) raw, _ := ioutil.ReadFile("testdata/issue_note.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/gitlab/org.go b/scm/driver/gitlab/org.go index 8f0e5843e..fb8c38d24 100644 --- a/scm/driver/gitlab/org.go +++ b/scm/driver/gitlab/org.go @@ -24,7 +24,7 @@ func (s *organizationService) Delete(context.Context, string) (*scm.Response, er return nil, scm.ErrNotSupported } -func (s *organizationService) IsMember(ctx context.Context, org string, user string) (bool, *scm.Response, error) { +func (s *organizationService) IsMember(ctx context.Context, org, user string) (bool, *scm.Response, error) { var resp *scm.Response var users []scm.User var err error @@ -48,7 +48,7 @@ func (s *organizationService) IsMember(ctx context.Context, org string, user str return false, resp, err } -func (s *organizationService) IsAdmin(ctx context.Context, org string, user string) (bool, *scm.Response, error) { +func (s *organizationService) IsAdmin(ctx context.Context, org, user string) (bool, *scm.Response, error) { // TODO implement me return false, nil, nil } diff --git a/scm/driver/gitlab/org_test.go b/scm/driver/gitlab/org_test.go index 3bc63f74f..301b334e6 100644 --- a/scm/driver/gitlab/org_test.go +++ b/scm/driver/gitlab/org_test.go @@ -35,7 +35,10 @@ func TestOrganizationFind(t *testing.T) { want := new(scm.Organization) raw, _ := ioutil.ReadFile("testdata/group.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -68,7 +71,10 @@ func TestOrganizationList(t *testing.T) { want := []*scm.Organization{} raw, _ := ioutil.ReadFile("testdata/groups.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/gitlab/pr.go b/scm/driver/gitlab/pr.go index 5275b337d..6f58adf4e 100644 --- a/scm/driver/gitlab/pr.go +++ b/scm/driver/gitlab/pr.go @@ -93,7 +93,7 @@ func (s *pullService) DeleteLabel(ctx context.Context, repo string, number int, return s.setLabels(ctx, repo, number, label, "remove_labels") } -func (s *pullService) setLabels(ctx context.Context, repo string, number int, labelsStr string, operation string) (*scm.Response, error) { +func (s *pullService) setLabels(ctx context.Context, repo string, number int, labelsStr, operation string) (*scm.Response, error) { in := url.Values{} in.Set(operation, labelsStr) path := fmt.Sprintf("api/v4/projects/%s/merge_requests/%d?%s", encode(repo), number, in.Encode()) @@ -116,7 +116,7 @@ func (s *pullService) DeleteComment(ctx context.Context, repo string, index, id return res, err } -func (s *pullService) EditComment(ctx context.Context, repo string, number int, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { +func (s *pullService) EditComment(ctx context.Context, repo string, number, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { in := &updateNoteOptions{Body: input.Body} path := fmt.Sprintf("api/v4/projects/%s/merge_requests/%d/notes/%d", encode(repo), number, id) out := new(issueComment) @@ -244,7 +244,7 @@ func (s *pullService) Update(ctx context.Context, repo string, number int, input return s.updateMergeRequestField(ctx, repo, number, updateOpts) } -func (s *pullService) SetMilestone(ctx context.Context, repo string, prID int, number int) (*scm.Response, error) { +func (s *pullService) SetMilestone(ctx context.Context, repo string, prID, number int) (*scm.Response, error) { updateOpts := &updateMergeRequestOptions{ MilestoneID: &number, } diff --git a/scm/driver/gitlab/pr_test.go b/scm/driver/gitlab/pr_test.go index 46a75b8b6..44fa80dc4 100644 --- a/scm/driver/gitlab/pr_test.go +++ b/scm/driver/gitlab/pr_test.go @@ -147,7 +147,10 @@ func TestPullListChanges(t *testing.T) { want := []*scm.Change{} raw, _ := ioutil.ReadFile("testdata/merge_diff.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -239,7 +242,10 @@ func TestPullCommentFind(t *testing.T) { want := new(scm.Comment) raw, _ := ioutil.ReadFile("testdata/merge_note.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -272,7 +278,10 @@ func TestPullListComments(t *testing.T) { want := []*scm.Comment{} raw, _ := ioutil.ReadFile("testdata/merge_notes.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -308,7 +317,10 @@ func TestPullCreateComment(t *testing.T) { want := new(scm.Comment) raw, _ := ioutil.ReadFile("testdata/merge_note.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -363,7 +375,10 @@ func TestPullEditComment(t *testing.T) { want := new(scm.Comment) raw, _ := ioutil.ReadFile("testdata/merge_note.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -420,7 +435,10 @@ func TestPullCreate(t *testing.T) { want := new(scm.PullRequest) raw, _ := ioutil.ReadFile("testdata/pr_create.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(want, got); diff != "" { t.Errorf("Unexpected Results") @@ -476,7 +494,10 @@ func TestPullUpdate(t *testing.T) { want := new(scm.PullRequest) raw, _ := ioutil.ReadFile("testdata/pr_create.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/gitlab/release.go b/scm/driver/gitlab/release.go index 959963f62..c6a58458c 100644 --- a/scm/driver/gitlab/release.go +++ b/scm/driver/gitlab/release.go @@ -31,7 +31,7 @@ func (s *releaseService) Find(ctx context.Context, repo string, id int) (*scm.Re panic("gitlab only allows to find a release by tag") } -func (s *releaseService) FindByTag(ctx context.Context, repo string, tag string) (*scm.Release, *scm.Response, error) { +func (s *releaseService) FindByTag(ctx context.Context, repo, tag string) (*scm.Release, *scm.Response, error) { path := fmt.Sprintf("api/v4/projects/%s/releases/%s", encode(repo), tag) out := new(release) res, err := s.client.do(ctx, "GET", path, nil, out) @@ -62,7 +62,7 @@ func (s *releaseService) Delete(ctx context.Context, repo string, id int) (*scm. panic("gitlab only allows to delete a release by tag") } -func (s *releaseService) DeleteByTag(ctx context.Context, repo string, tag string) (*scm.Response, error) { +func (s *releaseService) DeleteByTag(ctx context.Context, repo, tag string) (*scm.Response, error) { path := fmt.Sprintf("api/v4/projects/%s/releases/%s", encode(repo), tag) return s.client.do(ctx, "DELETE", path, nil, nil) } @@ -72,7 +72,7 @@ func (s *releaseService) Update(ctx context.Context, repo string, id int, input panic("gitlab only allows to update a release by tag") } -func (s *releaseService) UpdateByTag(ctx context.Context, repo string, tag string, input *scm.ReleaseInput) (*scm.Release, *scm.Response, error) { +func (s *releaseService) UpdateByTag(ctx context.Context, repo, tag string, input *scm.ReleaseInput) (*scm.Release, *scm.Response, error) { path := fmt.Sprintf("api/v4/projects/%s/releases/%s", encode(repo), tag) in := &releaseInput{} if input.Title != "" { diff --git a/scm/driver/gitlab/repo.go b/scm/driver/gitlab/repo.go index 19d323c2e..24c28ee60 100644 --- a/scm/driver/gitlab/repo.go +++ b/scm/driver/gitlab/repo.go @@ -212,7 +212,7 @@ func (s *repositoryService) FindCombinedStatus(ctx context.Context, repo, ref st }, resp, err } -func (s *repositoryService) FindUserPermission(ctx context.Context, repo string, user string) (string, *scm.Response, error) { +func (s *repositoryService) FindUserPermission(ctx context.Context, repo, user string) (string, *scm.Response, error) { var resp *scm.Response var err error firstRun := false @@ -303,7 +303,7 @@ func (s *repositoryService) Find(ctx context.Context, repo string) (*scm.Reposit return convertRepository(out), res, err } -func (s *repositoryService) FindHook(ctx context.Context, repo string, id string) (*scm.Hook, *scm.Response, error) { +func (s *repositoryService) FindHook(ctx context.Context, repo, id string) (*scm.Hook, *scm.Response, error) { path := fmt.Sprintf("api/v4/projects/%s/hooks/%s", encode(repo), id) out := new(hook) res, err := s.client.do(ctx, "GET", path, nil, out) @@ -361,9 +361,6 @@ func (s *repositoryService) CreateHook(ctx context.Context, repo string, input * hasStarEvents = true } } - if input.Events.Branch { - // no-op - } if input.Events.Issue || hasStarEvents { params.Set("issues_events", "true") } @@ -406,9 +403,6 @@ func (s *repositoryService) UpdateHook(ctx context.Context, repo string, input * hasStarEvents = true } } - if input.Events.Branch { - // no-op - } if input.Events.Issue || hasStarEvents { params.Set("issues_events", "true") } else { @@ -461,7 +455,7 @@ func (s *repositoryService) CreateStatus(ctx context.Context, repo, ref string, return convertStatus(out), res, err } -func (s *repositoryService) DeleteHook(ctx context.Context, repo string, id string) (*scm.Response, error) { +func (s *repositoryService) DeleteHook(ctx context.Context, repo, id string) (*scm.Response, error) { path := fmt.Sprintf("api/v4/projects/%s/hooks/%s", encode(repo), id) return s.client.do(ctx, "DELETE", path, nil, nil) } diff --git a/scm/driver/gitlab/repo_test.go b/scm/driver/gitlab/repo_test.go index 64fccd33d..4de7476a2 100644 --- a/scm/driver/gitlab/repo_test.go +++ b/scm/driver/gitlab/repo_test.go @@ -53,7 +53,10 @@ func TestRepositoryCreate(t *testing.T) { want := new(scm.Repository) raw, _ := ioutil.ReadFile("testdata/repo.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -88,7 +91,10 @@ func TestRepositoryFork(t *testing.T) { want := new(scm.Repository) raw, _ := ioutil.ReadFile("testdata/repo.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -118,7 +124,10 @@ func TestRepositoryFind(t *testing.T) { want := new(scm.Repository) raw, _ := ioutil.ReadFile("testdata/repo.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -148,7 +157,10 @@ func TestRepositoryFindNested(t *testing.T) { want := new(scm.Repository) raw, _ := ioutil.ReadFile("testdata/nested_repo.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -234,7 +246,10 @@ func TestRepositoryList(t *testing.T) { want := []*scm.Repository{} raw, _ := ioutil.ReadFile("testdata/repos.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -296,7 +311,10 @@ func TestListContributor(t *testing.T) { want := []scm.User{} raw, _ := ioutil.ReadFile("testdata/contributors.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -330,7 +348,10 @@ func TestStatusList(t *testing.T) { want := []*scm.Status{} raw, _ := ioutil.ReadFile("testdata/statuses.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -363,7 +384,10 @@ func TestCombinedStatus(t *testing.T) { var want *scm.CombinedStatus raw, _ := ioutil.ReadFile("testdata/combined_status.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -410,7 +434,10 @@ func TestStatusCreate(t *testing.T) { want := new(scm.Status) raw, _ := ioutil.ReadFile("testdata/status.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -440,7 +467,10 @@ func TestRepositoryHookFind(t *testing.T) { want := new(scm.Hook) raw, _ := ioutil.ReadFile("testdata/hook.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -473,7 +503,10 @@ func TestRepositoryHookList(t *testing.T) { want := []*scm.Hook{} raw, _ := ioutil.ReadFile("testdata/hooks.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -538,7 +571,10 @@ func TestRepositoryHookCreate(t *testing.T) { want := new(scm.Hook) raw, _ := ioutil.ReadFile("testdata/hook.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -574,7 +610,10 @@ func TestRepositoryHookUpdate(t *testing.T) { want := new(scm.Hook) raw, _ := ioutil.ReadFile("testdata/hook.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/gitlab/review.go b/scm/driver/gitlab/review.go index a9677b4bb..540225105 100644 --- a/scm/driver/gitlab/review.go +++ b/scm/driver/gitlab/review.go @@ -30,18 +30,18 @@ func (s *reviewService) Delete(ctx context.Context, repo string, number, id int) return nil, scm.ErrNotSupported } -func (s *reviewService) ListComments(ctx context.Context, repo string, prID int, reviewID int, options scm.ListOptions) ([]*scm.ReviewComment, *scm.Response, error) { +func (s *reviewService) ListComments(ctx context.Context, repo string, prID, reviewID int, options scm.ListOptions) ([]*scm.ReviewComment, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } -func (s *reviewService) Update(ctx context.Context, repo string, prID int, reviewID int, body string) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Update(ctx context.Context, repo string, prID, reviewID int, body string) (*scm.Review, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } -func (s *reviewService) Submit(ctx context.Context, repo string, prID int, reviewID int, input *scm.ReviewSubmitInput) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Submit(ctx context.Context, repo string, prID, reviewID int, input *scm.ReviewSubmitInput) (*scm.Review, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } -func (s *reviewService) Dismiss(ctx context.Context, repo string, prID int, reviewID int, msg string) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Dismiss(ctx context.Context, repo string, prID, reviewID int, msg string) (*scm.Review, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } diff --git a/scm/driver/gitlab/user_test.go b/scm/driver/gitlab/user_test.go index 492923677..1e2f7309e 100644 --- a/scm/driver/gitlab/user_test.go +++ b/scm/driver/gitlab/user_test.go @@ -36,13 +36,19 @@ func TestUserFind(t *testing.T) { want := new(scm.User) raw, _ := ioutil.ReadFile("testdata/user.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") t.Log(diff) - json.NewEncoder(os.Stdout).Encode(got) + err := json.NewEncoder(os.Stdout).Encode(got) + if err != nil { + t.Error(err) + } } t.Run("Request", testRequest(res)) @@ -69,7 +75,10 @@ func TestUserLoginFind(t *testing.T) { want := new(scm.User) raw, _ := ioutil.ReadFile("testdata/user_search.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -136,7 +145,7 @@ func TestUserEmailFind(t *testing.T) { t.Error(err) return } - if got, want := got, "john@example.com"; got != want { + if want := "john@example.com"; got != want { t.Errorf("Want user Email %q, got %q", want, got) } diff --git a/scm/driver/gitlab/webhook.go b/scm/driver/gitlab/webhook.go index 33c74e208..7c6e89ae0 100644 --- a/scm/driver/gitlab/webhook.go +++ b/scm/driver/gitlab/webhook.go @@ -20,7 +20,7 @@ import ( type webhookService struct { client *wrapper - //need the user service as well + // need the user service as well userService webhookUserService } @@ -49,7 +49,7 @@ func (s *webhookService) Parse(req *http.Request, fn scm.SecretFunc) (scm.Webhoo case "Note Hook": hook, err = parseCommentHook(s, data) case "Release Hook": - hook, err = parseReleaseHook(s, data) + hook, err = parseReleaseHook(data) default: return nil, scm.UnknownWebhook{Event: event} } @@ -115,6 +115,7 @@ func parsePullRequestHook(data []byte) (scm.Webhook, error) { default: return nil, scm.UnknownWebhook{Event: event} } + // nolint switch { default: return convertPullRequestHook(src), nil @@ -141,7 +142,7 @@ func parseCommentHook(s *webhookService, data []byte) (scm.Webhook, error) { } } -func parseReleaseHook(s *webhookService, data []byte) (scm.Webhook, error) { +func parseReleaseHook(data []byte) (scm.Webhook, error) { src := new(releaseHook) err := json.Unmarshal(data, src) if err != nil { @@ -541,8 +542,9 @@ type ( ProjectID int `json:"project_id"` Project project `json:"project"` ObjectAttributes struct { - ID int `json:"id"` - Note string `json:"note"` + ID int `json:"id"` + Note string `json:"note"` + // nolint NoteableType string `json:"noteable_type"` AuthorID int `json:"author_id"` CreatedAt string `json:"created_at"` @@ -551,12 +553,13 @@ type ( Attachment interface{} `json:"attachment"` LineCode string `json:"line_code"` CommitID string `json:"commit_id"` - NoteableID int `json:"noteable_id"` - StDiff interface{} `json:"st_diff"` - System bool `json:"system"` - UpdatedByID interface{} `json:"updated_by_id"` - Type string `json:"type"` - Position struct { + // nolint + NoteableID int `json:"noteable_id"` + StDiff interface{} `json:"st_diff"` + System bool `json:"system"` + UpdatedByID interface{} `json:"updated_by_id"` + Type string `json:"type"` + Position struct { BaseSha string `json:"base_sha"` StartSha string `json:"start_sha"` HeadSha string `json:"head_sha"` diff --git a/scm/driver/gitlab/webhook_test.go b/scm/driver/gitlab/webhook_test.go index e1e58c673..5774593ad 100644 --- a/scm/driver/gitlab/webhook_test.go +++ b/scm/driver/gitlab/webhook_test.go @@ -205,7 +205,10 @@ func TestWebhooks(t *testing.T) { t.Log(diff) // debug only. remove once implemented - json.NewEncoder(os.Stdout).Encode(o) + err := json.NewEncoder(os.Stdout).Encode(o) + if err != nil { + t.Error(err) + } } switch event := o.(type) { diff --git a/scm/driver/gogs/git_test.go b/scm/driver/gogs/git_test.go index 45ad01f2f..ed2036a0a 100644 --- a/scm/driver/gogs/git_test.go +++ b/scm/driver/gogs/git_test.go @@ -36,7 +36,10 @@ func TestCommitFind(t *testing.T) { } want := new(scm.Commit) raw, _ := ioutil.ReadFile("testdata/commits.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -89,7 +92,10 @@ func TestBranchFind(t *testing.T) { want := new(scm.Reference) raw, _ := ioutil.ReadFile("testdata/branch.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -114,7 +120,10 @@ func TestBranchList(t *testing.T) { want := []*scm.Reference{} raw, _ := ioutil.ReadFile("testdata/branches.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/gogs/gogs.go b/scm/driver/gogs/gogs.go index eb62f8b05..a6724dcb5 100644 --- a/scm/driver/gogs/gogs.go +++ b/scm/driver/gogs/gogs.go @@ -30,7 +30,7 @@ func New(uri string) (*scm.Client, error) { return nil, err } if !strings.HasSuffix(base.Path, "/") { - base.Path = base.Path + "/" + base.Path += "/" } client := &wrapper{new(scm.Client)} client.BaseURL = base @@ -66,7 +66,10 @@ func (c *wrapper) do(ctx context.Context, method, path string, in, out interface // write it to the body of the request. if in != nil { buf := new(bytes.Buffer) - json.NewEncoder(buf).Encode(in) // #nosec + err := json.NewEncoder(buf).Encode(in) // #nosec + if err != nil { + return nil, err + } req.Header = map[string][]string{ "Content-Type": {"application/json"}, } diff --git a/scm/driver/gogs/issue.go b/scm/driver/gogs/issue.go index 531e8601a..ac27b716c 100644 --- a/scm/driver/gogs/issue.go +++ b/scm/driver/gogs/issue.go @@ -96,7 +96,7 @@ func (s *issueService) DeleteComment(ctx context.Context, repo string, index, id return s.client.do(ctx, "DELETE", path, nil, nil) } -func (s *issueService) EditComment(ctx context.Context, repo string, number int, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { +func (s *issueService) EditComment(ctx context.Context, repo string, number, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } @@ -116,7 +116,7 @@ func (s *issueService) Unlock(ctx context.Context, repo string, number int) (*sc return nil, scm.ErrNotSupported } -func (s *issueService) SetMilestone(ctx context.Context, repo string, issueID int, number int) (*scm.Response, error) { +func (s *issueService) SetMilestone(ctx context.Context, repo string, issueID, number int) (*scm.Response, error) { return nil, scm.ErrNotSupported } diff --git a/scm/driver/gogs/issue_test.go b/scm/driver/gogs/issue_test.go index d7ae01e41..e35585bbb 100644 --- a/scm/driver/gogs/issue_test.go +++ b/scm/driver/gogs/issue_test.go @@ -36,7 +36,10 @@ func TestIssueFind(t *testing.T) { want := new(scm.Issue) raw, _ := ioutil.ReadFile("testdata/issue.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -61,7 +64,10 @@ func TestIssueList(t *testing.T) { want := []*scm.Issue{} raw, _ := ioutil.ReadFile("testdata/issues.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -91,7 +97,10 @@ func TestIssueCreate(t *testing.T) { want := new(scm.Issue) raw, _ := ioutil.ReadFile("testdata/issue.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -152,7 +161,10 @@ func TestIssueCommentList(t *testing.T) { want := []*scm.Comment{} raw, _ := ioutil.ReadFile("testdata/comments.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -177,7 +189,10 @@ func TestIssueCommentCreate(t *testing.T) { want := new(scm.Comment) raw, _ := ioutil.ReadFile("testdata/comment.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/gogs/org.go b/scm/driver/gogs/org.go index bd2cd2420..833798136 100644 --- a/scm/driver/gogs/org.go +++ b/scm/driver/gogs/org.go @@ -23,11 +23,11 @@ func (s *organizationService) Delete(context.Context, string) (*scm.Response, er return nil, scm.ErrNotSupported } -func (s *organizationService) IsMember(ctx context.Context, org string, user string) (bool, *scm.Response, error) { +func (s *organizationService) IsMember(ctx context.Context, org, user string) (bool, *scm.Response, error) { return false, nil, scm.ErrNotSupported } -func (s *organizationService) IsAdmin(ctx context.Context, org string, user string) (bool, *scm.Response, error) { +func (s *organizationService) IsAdmin(ctx context.Context, org, user string) (bool, *scm.Response, error) { return false, nil, scm.ErrNotSupported } diff --git a/scm/driver/gogs/org_test.go b/scm/driver/gogs/org_test.go index 0a90ed408..f5f6f8659 100644 --- a/scm/driver/gogs/org_test.go +++ b/scm/driver/gogs/org_test.go @@ -33,7 +33,10 @@ func TestOrgFind(t *testing.T) { want := new(scm.Organization) raw, _ := ioutil.ReadFile("testdata/organization.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -58,7 +61,10 @@ func TestOrgList(t *testing.T) { want := []*scm.Organization{} raw, _ := ioutil.ReadFile("testdata/organizations.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/gogs/pr.go b/scm/driver/gogs/pr.go index 101534e21..f49eccf06 100644 --- a/scm/driver/gogs/pr.go +++ b/scm/driver/gogs/pr.go @@ -59,7 +59,7 @@ func (s *pullService) DeleteComment(context.Context, string, int, int) (*scm.Res return nil, scm.ErrNotSupported } -func (s *pullService) EditComment(ctx context.Context, repo string, number int, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { +func (s *pullService) EditComment(ctx context.Context, repo string, number, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } @@ -99,7 +99,7 @@ func (s *pullService) UnrequestReview(ctx context.Context, repo string, number i return nil, scm.ErrNotSupported } -func (s *pullService) SetMilestone(ctx context.Context, repo string, prID int, number int) (*scm.Response, error) { +func (s *pullService) SetMilestone(ctx context.Context, repo string, prID, number int) (*scm.Response, error) { return nil, scm.ErrNotSupported } diff --git a/scm/driver/gogs/repo.go b/scm/driver/gogs/repo.go index f4f17ff4b..354571105 100644 --- a/scm/driver/gogs/repo.go +++ b/scm/driver/gogs/repo.go @@ -29,7 +29,7 @@ func (s *repositoryService) FindCombinedStatus(ctx context.Context, repo, ref st return nil, nil, scm.ErrNotSupported } -func (s *repositoryService) FindUserPermission(ctx context.Context, repo string, user string) (string, *scm.Response, error) { +func (s *repositoryService) FindUserPermission(ctx context.Context, repo, user string) (string, *scm.Response, error) { return "", nil, scm.ErrNotSupported } @@ -56,7 +56,7 @@ func (s *repositoryService) Find(ctx context.Context, repo string) (*scm.Reposit return convertRepository(out), res, err } -func (s *repositoryService) FindHook(ctx context.Context, repo string, id string) (*scm.Hook, *scm.Response, error) { +func (s *repositoryService) FindHook(ctx context.Context, repo, id string) (*scm.Hook, *scm.Response, error) { path := fmt.Sprintf("api/v1/repos/%s/hooks/%s", repo, id) out := new(hook) res, err := s.client.do(ctx, "GET", path, nil, out) @@ -71,7 +71,7 @@ func (s *repositoryService) FindPerms(ctx context.Context, repo string) (*scm.Pe } func (s *repositoryService) List(ctx context.Context, _ scm.ListOptions) ([]*scm.Repository, *scm.Response, error) { - path := fmt.Sprintf("api/v1/user/repos") + path := "api/v1/user/repos" out := []*repository{} res, err := s.client.do(ctx, "GET", path, nil, &out) return convertRepositoryList(out), res, err @@ -109,6 +109,7 @@ func (s *repositoryService) CreateHook(ctx context.Context, repo string, input * in.Config.Secret = input.Secret in.Config.ContentType = "json" in.Config.URL = input.Target + // nolint in.Events = append( input.NativeEvents, convertHookEvent(input.Events)..., @@ -126,7 +127,7 @@ func (s *repositoryService) CreateStatus(context.Context, string, string, *scm.S return nil, nil, scm.ErrNotSupported } -func (s *repositoryService) DeleteHook(ctx context.Context, repo string, id string) (*scm.Response, error) { +func (s *repositoryService) DeleteHook(ctx context.Context, repo, id string) (*scm.Response, error) { path := fmt.Sprintf("api/v1/repos/%s/hooks/%s", repo, id) return s.client.do(ctx, "DELETE", path, nil, nil) } @@ -244,8 +245,7 @@ func convertHookEvent(from scm.HookEvents) []string { events = append(events, "issue_comment") } if from.Branch || from.Tag { - events = append(events, "create") - events = append(events, "delete") + events = append(events, "create", "delete") } if from.Push { events = append(events, "push") diff --git a/scm/driver/gogs/repo_test.go b/scm/driver/gogs/repo_test.go index 9d4f2a5a5..003411df3 100644 --- a/scm/driver/gogs/repo_test.go +++ b/scm/driver/gogs/repo_test.go @@ -36,7 +36,10 @@ func TestRepoFind(t *testing.T) { want := new(scm.Repository) raw, _ := ioutil.ReadFile("testdata/repo.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -61,7 +64,10 @@ func TestRepoFindPerm(t *testing.T) { want := new(scm.Repository) raw, _ := ioutil.ReadFile("testdata/repo.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want.Perm); diff != "" { t.Errorf("Unexpected Results") @@ -86,7 +92,10 @@ func TestRepoList(t *testing.T) { want := []*scm.Repository{} raw, _ := ioutil.ReadFile("testdata/repos.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -132,7 +141,10 @@ func TestHookFind(t *testing.T) { want := new(scm.Hook) raw, _ := ioutil.ReadFile("testdata/hook.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -157,7 +169,10 @@ func TestHookList(t *testing.T) { want := []*scm.Hook{} raw, _ := ioutil.ReadFile("testdata/hooks.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -182,7 +197,10 @@ func TestHookCreate(t *testing.T) { want := new(scm.Hook) raw, _ := ioutil.ReadFile("testdata/hook.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/gogs/review.go b/scm/driver/gogs/review.go index dd58c42a4..3032f795f 100644 --- a/scm/driver/gogs/review.go +++ b/scm/driver/gogs/review.go @@ -30,18 +30,18 @@ func (s *reviewService) Delete(ctx context.Context, repo string, number, id int) return nil, scm.ErrNotSupported } -func (s *reviewService) ListComments(ctx context.Context, repo string, prID int, reviewID int, options scm.ListOptions) ([]*scm.ReviewComment, *scm.Response, error) { +func (s *reviewService) ListComments(ctx context.Context, repo string, prID, reviewID int, options scm.ListOptions) ([]*scm.ReviewComment, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } -func (s *reviewService) Update(ctx context.Context, repo string, prID int, reviewID int, body string) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Update(ctx context.Context, repo string, prID, reviewID int, body string) (*scm.Review, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } -func (s *reviewService) Submit(ctx context.Context, repo string, prID int, reviewID int, input *scm.ReviewSubmitInput) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Submit(ctx context.Context, repo string, prID, reviewID int, input *scm.ReviewSubmitInput) (*scm.Review, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } -func (s *reviewService) Dismiss(ctx context.Context, repo string, prID int, reviewID int, msg string) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Dismiss(ctx context.Context, repo string, prID, reviewID int, msg string) (*scm.Review, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } diff --git a/scm/driver/gogs/user_test.go b/scm/driver/gogs/user_test.go index df9a83540..082434195 100644 --- a/scm/driver/gogs/user_test.go +++ b/scm/driver/gogs/user_test.go @@ -33,7 +33,10 @@ func TestUserFind(t *testing.T) { want := new(scm.User) raw, _ := ioutil.ReadFile("testdata/user.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -58,7 +61,10 @@ func TestUserLoginFind(t *testing.T) { want := new(scm.User) raw, _ := ioutil.ReadFile("testdata/user.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/internal/null/bool.go b/scm/driver/internal/null/bool.go index 0dd8a74e1..f020eb71b 100644 --- a/scm/driver/internal/null/bool.go +++ b/scm/driver/internal/null/bool.go @@ -20,9 +20,9 @@ type Bool struct { // number and null input. 0 will not be considered a null // Bool. It also supports unmarshalling a sql.NullBool. func (b *Bool) UnmarshalJSON(data []byte) error { - var err error var v interface{} - if err = json.Unmarshal(data, &v); err != nil { + err := json.Unmarshal(data, &v) + if err != nil { return err } switch x := v.(type) { diff --git a/scm/driver/internal/null/int.go b/scm/driver/internal/null/int.go index 3774474d6..90057f5a7 100644 --- a/scm/driver/internal/null/int.go +++ b/scm/driver/internal/null/int.go @@ -22,9 +22,9 @@ type Int struct { // number and null input. 0 will not be considered a null // Int. It also supports unmarshalling a sql.NullInt64. func (i *Int) UnmarshalJSON(data []byte) error { - var err error var v interface{} - if err = json.Unmarshal(data, &v); err != nil { + err := json.Unmarshal(data, &v) + if err != nil { return err } switch x := v.(type) { @@ -32,8 +32,8 @@ func (i *Int) UnmarshalJSON(data []byte) error { // Unmarshal again, directly to int64, to avoid intermediate float64 err = json.Unmarshal(data, &i.Int64) case string: - str := string(x) - if len(str) == 0 { + str := x + if str == "" { i.Valid = false return nil } diff --git a/scm/driver/internal/null/string.go b/scm/driver/internal/null/string.go index 9a854cbb3..b106f986a 100644 --- a/scm/driver/internal/null/string.go +++ b/scm/driver/internal/null/string.go @@ -22,9 +22,9 @@ type String struct { // does not produce a null String. It also supports // unmarshalling a sql.NullString. func (s *String) UnmarshalJSON(data []byte) error { - var err error var v interface{} - if err = json.Unmarshal(data, &v); err != nil { + err := json.Unmarshal(data, &v) + if err != nil { return err } switch x := v.(type) { diff --git a/scm/driver/stash/content_test.go b/scm/driver/stash/content_test.go index 7c2cc15a9..2013e5d51 100644 --- a/scm/driver/stash/content_test.go +++ b/scm/driver/stash/content_test.go @@ -34,7 +34,10 @@ func TestContentFind(t *testing.T) { want := new(scm.Content) raw, _ := ioutil.ReadFile("testdata/content.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/stash/git_test.go b/scm/driver/stash/git_test.go index e371f6c77..78bc2289d 100644 --- a/scm/driver/stash/git_test.go +++ b/scm/driver/stash/git_test.go @@ -33,7 +33,10 @@ func TestGitFindCommit(t *testing.T) { want := new(scm.Commit) raw, _ := ioutil.ReadFile("testdata/commit.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -59,7 +62,10 @@ func TestGitFindBranch(t *testing.T) { want := new(scm.Reference) raw, _ := ioutil.ReadFile("testdata/branch.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -85,7 +91,10 @@ func TestGitFindTag(t *testing.T) { want := new(scm.Reference) raw, _ := ioutil.ReadFile("testdata/tag.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -119,14 +128,15 @@ func TestGitListBranches(t *testing.T) { want := []*scm.Reference{} raw, _ := ioutil.ReadFile("testdata/branches.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") t.Log(diff) } - // - // t.Run("Page", testPage(res)) } func TestGitListTags(t *testing.T) { @@ -147,14 +157,15 @@ func TestGitListTags(t *testing.T) { want := []*scm.Reference{} raw, _ := ioutil.ReadFile("testdata/tags.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") t.Log(diff) } - - // t.Run("Page", testPage(res)) } func TestGitListChanges(t *testing.T) { @@ -175,7 +186,10 @@ func TestGitListChanges(t *testing.T) { want := []*scm.Change{} raw, _ := ioutil.ReadFile("testdata/changes.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -203,7 +217,10 @@ func TestGitCompareCommits(t *testing.T) { want := []*scm.Change{} raw, _ := ioutil.ReadFile("testdata/changes.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/stash/issue.go b/scm/driver/stash/issue.go index d2a87c43b..e19988fa2 100644 --- a/scm/driver/stash/issue.go +++ b/scm/driver/stash/issue.go @@ -88,7 +88,7 @@ func (s *issueService) DeleteComment(ctx context.Context, repo string, number, i return nil, scm.ErrNotSupported } -func (s *issueService) EditComment(ctx context.Context, repo string, number int, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { +func (s *issueService) EditComment(ctx context.Context, repo string, number, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } @@ -108,7 +108,7 @@ func (s *issueService) Unlock(ctx context.Context, repo string, number int) (*sc return nil, scm.ErrNotSupported } -func (s *issueService) SetMilestone(ctx context.Context, repo string, issueID int, number int) (*scm.Response, error) { +func (s *issueService) SetMilestone(ctx context.Context, repo string, issueID, number int) (*scm.Response, error) { return nil, scm.ErrNotSupported } diff --git a/scm/driver/stash/org.go b/scm/driver/stash/org.go index 6a86054a3..753ab656a 100644 --- a/scm/driver/stash/org.go +++ b/scm/driver/stash/org.go @@ -58,7 +58,7 @@ func (s *organizationService) ListOrgMembers(ctx context.Context, org string, op } // IsMember checks if the user opening a pull request is part of the org -func (s *organizationService) IsMember(ctx context.Context, org string, user string) (bool, *scm.Response, error) { +func (s *organizationService) IsMember(ctx context.Context, org, user string) (bool, *scm.Response, error) { opts := scm.ListOptions{ Size: 1000, } @@ -79,13 +79,13 @@ func (s *organizationService) IsMember(ctx context.Context, org string, user str } } // Retrieve the list of groups attached to the project - groups, err := getProjectGroups(org, s, opts, ctx) + groups, err := getProjectGroups(ctx, org, s, opts) if err != nil { return false, res, err } for _, pgroup := range groups { // Get list of users in a group - users, err := usersInGroups(pgroup.Group.Name, s, opts, ctx) + users, err := usersInGroups(ctx, pgroup.Group.Name, s, opts) if err != nil { return false, res, err } @@ -99,7 +99,7 @@ func (s *organizationService) IsMember(ctx context.Context, org string, user str } // getProjectGroups returns the groups which have some permissions in the project -func getProjectGroups(org string, os *organizationService, opts scm.ListOptions, ctx context.Context) ([]*projGroup, error) { +func getProjectGroups(ctx context.Context, org string, os *organizationService, opts scm.ListOptions) ([]*projGroup, error) { path := fmt.Sprintf("rest/api/1.0/projects/%s/permissions/groups?%s", org, encodeListOptions(opts)) out := new(projGroups) res, err := os.client.do(ctx, "GET", path, nil, out) @@ -114,7 +114,7 @@ func getProjectGroups(org string, os *organizationService, opts scm.ListOptions, } // usersInGroups returns the members/users in a group -func usersInGroups(group string, os *organizationService, opts scm.ListOptions, ctx context.Context) ([]*member, error) { +func usersInGroups(ctx context.Context, group string, os *organizationService, opts scm.ListOptions) ([]*member, error) { path := fmt.Sprintf("rest/api/1.0/admin/groups/more-members?context=%s&%s", group, encodeListOptions(opts)) out := new(members) res, err := os.client.do(ctx, "GET", path, nil, out) @@ -128,7 +128,7 @@ func usersInGroups(group string, os *organizationService, opts scm.ListOptions, return out.Values, nil } -func (s *organizationService) IsAdmin(ctx context.Context, org string, user string) (bool, *scm.Response, error) { +func (s *organizationService) IsAdmin(ctx context.Context, org, user string) (bool, *scm.Response, error) { opts := scm.ListOptions{ Size: 1000, } diff --git a/scm/driver/stash/org_test.go b/scm/driver/stash/org_test.go index 90bc46420..3dc362812 100644 --- a/scm/driver/stash/org_test.go +++ b/scm/driver/stash/org_test.go @@ -44,7 +44,10 @@ func TestOrganizationList(t *testing.T) { var want []*scm.Organization raw, _ := ioutil.ReadFile("testdata/orgs.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -73,7 +76,10 @@ func TestOrganizationListOrgMembers(t *testing.T) { var want []*scm.TeamMember raw, _ := ioutil.ReadFile("testdata/org_members.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -129,7 +135,7 @@ func TestOrganizationIsMember(t *testing.T) { } for k, v := range testCases { - t.Logf("Runing test %q: %s", k, v.description) + t.Logf("Running test %q: %s", k, v.description) client, _ := New("http://example.com:7990") got, _, err := client.Organizations.IsMember(context.Background(), "some-project", v.user) diff --git a/scm/driver/stash/pr.go b/scm/driver/stash/pr.go index 5071e7db9..d3d4526c2 100644 --- a/scm/driver/stash/pr.go +++ b/scm/driver/stash/pr.go @@ -29,7 +29,7 @@ func (s *pullService) Find(ctx context.Context, repo string, number int) (*scm.P return convertPullRequest(out), res, err } -func (s *pullService) FindComment(ctx context.Context, repo string, number int, id int) (*scm.Comment, *scm.Response, error) { +func (s *pullService) FindComment(ctx context.Context, repo string, number, id int) (*scm.Comment, *scm.Response, error) { namespace, name := scm.Split(repo) path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests/%d/comments/%d", namespace, name, number, id) out := new(pullRequestComment) @@ -169,7 +169,7 @@ func (s *pullService) CreateComment(ctx context.Context, repo string, number int return convertPullRequestComment(out), res, err } -func (s *pullService) DeleteComment(ctx context.Context, repo string, number int, id int) (*scm.Response, error) { +func (s *pullService) DeleteComment(ctx context.Context, repo string, number, id int) (*scm.Response, error) { namespace, name := scm.Split(repo) existingComment, res, err := s.FindComment(ctx, repo, number, id) if err != nil { @@ -185,7 +185,7 @@ func (s *pullService) DeleteComment(ctx context.Context, repo string, number int return s.client.do(ctx, "DELETE", path, nil, nil) } -func (s *pullService) EditComment(ctx context.Context, repo string, number int, id int, in *scm.CommentInput) (*scm.Comment, *scm.Response, error) { +func (s *pullService) EditComment(ctx context.Context, repo string, number, id int, in *scm.CommentInput) (*scm.Comment, *scm.Response, error) { input := pullRequestCommentInput{Text: in.Body} namespace, name := scm.Split(repo) existingComment, res, err := s.FindComment(ctx, repo, number, id) @@ -293,7 +293,7 @@ func (s *pullService) UnrequestReview(ctx context.Context, repo string, number i return res, err } -func (s *pullService) SetMilestone(ctx context.Context, repo string, prID int, number int) (*scm.Response, error) { +func (s *pullService) SetMilestone(ctx context.Context, repo string, prID, number int) (*scm.Response, error) { return nil, scm.ErrNotSupported } diff --git a/scm/driver/stash/pr_test.go b/scm/driver/stash/pr_test.go index 4066bf221..a268e0da0 100644 --- a/scm/driver/stash/pr_test.go +++ b/scm/driver/stash/pr_test.go @@ -35,7 +35,10 @@ func TestPullFind(t *testing.T) { want := new(scm.PullRequest) raw, _ := ioutil.ReadFile("testdata/pr.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -71,7 +74,10 @@ func TestPullUpdate(t *testing.T) { want := new(scm.PullRequest) raw, _ := ioutil.ReadFile("testdata/pr.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -96,7 +102,10 @@ func TestPullFindComment(t *testing.T) { want := new(scm.Comment) raw, _ := ioutil.ReadFile("testdata/pr_comment.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -121,7 +130,10 @@ func TestPullListComments(t *testing.T) { want := []*scm.Comment{} raw, _ := ioutil.ReadFile("testdata/pr_comments.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -167,7 +179,10 @@ func TestPullList(t *testing.T) { want := []*scm.PullRequest{} raw, _ := ioutil.ReadFile("testdata/prs.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -194,7 +209,10 @@ func TestPullListChanges(t *testing.T) { want := []*scm.Change{} raw, _ := ioutil.ReadFile("testdata/pr_change.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -276,7 +294,10 @@ func TestPullCreateComment(t *testing.T) { want := new(scm.Comment) raw, _ := ioutil.ReadFile("testdata/pr_comment.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -309,7 +330,10 @@ func TestPullEditComment(t *testing.T) { want := new(scm.Comment) raw, _ := ioutil.ReadFile("testdata/pr_comment.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -342,7 +366,10 @@ func TestPullCreate(t *testing.T) { want := new(scm.PullRequest) raw, _ := ioutil.ReadFile("testdata/pr.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -406,6 +433,7 @@ func TestPullListLabels(t *testing.T) { var want []*scm.Label want = append(want, &scm.Label{Name: "test"}) raw, _ := ioutil.ReadFile("testdata/pr_labels.json.golden") + //nolint json.Unmarshal(raw, &want) if diff := cmp.Diff(got, want); diff != "" { diff --git a/scm/driver/stash/repo.go b/scm/driver/stash/repo.go index cd3b5b41a..4e7c7241f 100644 --- a/scm/driver/stash/repo.go +++ b/scm/driver/stash/repo.go @@ -217,7 +217,7 @@ func (s *repositoryService) FindCombinedStatus(ctx context.Context, repo, ref st }, res, err } -func (s *repositoryService) FindUserPermission(ctx context.Context, repo string, user string) (string, *scm.Response, error) { +func (s *repositoryService) FindUserPermission(ctx context.Context, repo, user string) (string, *scm.Response, error) { namespace, name := scm.Split(repo) path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/permissions/users?filter=%s", namespace, name, url.QueryEscape(user)) out := new(participants) @@ -277,7 +277,6 @@ func (s *repositoryService) ListCollaborators(ctx context.Context, repo string, opts := scm.ListOptions{ Size: 1000, } - // path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/participants?role=PARTICIPANT&%s", namespace, name, encodeListOptions(opts)) path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/permissions/users?%s", namespace, name, encodeListOptions(opts)) out := new(participants) res, err := s.client.do(ctx, "GET", path, nil, out) @@ -303,7 +302,7 @@ func (s *repositoryService) Find(ctx context.Context, repo string) (*scm.Reposit } // FindHook returns a repository hook. -func (s *repositoryService) FindHook(ctx context.Context, repo string, id string) (*scm.Hook, *scm.Response, error) { +func (s *repositoryService) FindHook(ctx context.Context, repo, id string) (*scm.Hook, *scm.Response, error) { namespace, name := scm.Split(repo) path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/webhooks/%s", namespace, name, id) out := new(hook) @@ -415,6 +414,7 @@ func (s *repositoryService) CreateHook(ctx context.Context, repo string, input * in.Active = true in.Name = input.Name in.Config.Secret = input.Secret + // nolint in.Events = append( input.NativeEvents, convertHookEvents(input.Events)..., @@ -449,7 +449,7 @@ func (s *repositoryService) CreateStatus(ctx context.Context, repo, ref string, } // DeleteHook deletes a repository webhook. -func (s *repositoryService) DeleteHook(ctx context.Context, repo string, id string) (*scm.Response, error) { +func (s *repositoryService) DeleteHook(ctx context.Context, repo, id string) (*scm.Response, error) { namespace, name := scm.Split(repo) path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/webhooks/%s", namespace, name, id) return s.client.do(ctx, "DELETE", path, nil, nil) @@ -534,17 +534,10 @@ func convertHookEvents(from scm.HookEvents) []string { events = append(events, "repo:refs_changed") } if from.PullRequest { - events = append(events, "pr:declined") - events = append(events, "pr:modified") - events = append(events, "pr:deleted") - events = append(events, "pr:opened") - events = append(events, "pr:merged") - events = append(events, "pr:from_ref_updated") + events = append(events, "pr:declined", "pr:modified", "pr:deleted", "pr:opened", "pr:merged", "pr:from_ref_updated") } if from.PullRequestComment { - events = append(events, "pr:comment:added") - events = append(events, "pr:comment:deleted") - events = append(events, "pr:comment:edited") + events = append(events, "pr:comment:added", "pr:comment:deleted", "pr:comment:edited") } return events } diff --git a/scm/driver/stash/repo_test.go b/scm/driver/stash/repo_test.go index 7b0fdd0ab..da851eef5 100644 --- a/scm/driver/stash/repo_test.go +++ b/scm/driver/stash/repo_test.go @@ -39,7 +39,10 @@ func TestRepositoryCreate(t *testing.T) { want := new(scm.Repository) raw, _ := ioutil.ReadFile("testdata/repo.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -68,7 +71,10 @@ func TestRepositoryFork(t *testing.T) { want := new(scm.Repository) raw, _ := ioutil.ReadFile("testdata/repo.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -93,7 +99,10 @@ func TestRepositoryFind(t *testing.T) { want := new(scm.Repository) raw, _ := ioutil.ReadFile("testdata/repo.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -297,7 +306,10 @@ func TestRepositoryList(t *testing.T) { want := []*scm.Repository{} raw, _ := ioutil.ReadFile("testdata/repos.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -324,7 +336,10 @@ func TestStatusList(t *testing.T) { want := []*scm.Status{} raw, _ := ioutil.ReadFile("testdata/commit_build_status.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -351,7 +366,10 @@ func TestRepositoryService_FindCombinedStatus(t *testing.T) { want := &scm.CombinedStatus{} raw, _ := ioutil.ReadFile("testdata/combined_status.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -399,7 +417,10 @@ func TestRepositoryHookFind(t *testing.T) { want := new(scm.Hook) raw, _ := ioutil.ReadFile("testdata/webhook.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -426,7 +447,10 @@ func TestRepositoryHookList(t *testing.T) { want := []*scm.Hook{} raw, _ := ioutil.ReadFile("testdata/webhooks.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -478,7 +502,10 @@ func TestRepositoryHookCreate(t *testing.T) { want := new(scm.Hook) raw, _ := ioutil.ReadFile("testdata/webhook.json.golden") - json.Unmarshal(raw, want) + err = json.Unmarshal(raw, want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/stash/review.go b/scm/driver/stash/review.go index 2971439db..c0302f893 100644 --- a/scm/driver/stash/review.go +++ b/scm/driver/stash/review.go @@ -30,18 +30,18 @@ func (s *reviewService) Delete(ctx context.Context, repo string, number, id int) return nil, scm.ErrNotSupported } -func (s *reviewService) ListComments(ctx context.Context, repo string, prID int, reviewID int, options scm.ListOptions) ([]*scm.ReviewComment, *scm.Response, error) { +func (s *reviewService) ListComments(ctx context.Context, repo string, prID, reviewID int, options scm.ListOptions) ([]*scm.ReviewComment, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } -func (s *reviewService) Update(ctx context.Context, repo string, prID int, reviewID int, body string) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Update(ctx context.Context, repo string, prID, reviewID int, body string) (*scm.Review, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } -func (s *reviewService) Submit(ctx context.Context, repo string, prID int, reviewID int, input *scm.ReviewSubmitInput) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Submit(ctx context.Context, repo string, prID, reviewID int, input *scm.ReviewSubmitInput) (*scm.Review, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } -func (s *reviewService) Dismiss(ctx context.Context, repo string, prID int, reviewID int, msg string) (*scm.Review, *scm.Response, error) { +func (s *reviewService) Dismiss(ctx context.Context, repo string, prID, reviewID int, msg string) (*scm.Review, *scm.Response, error) { return nil, nil, scm.ErrNotSupported } diff --git a/scm/driver/stash/stash.go b/scm/driver/stash/stash.go index 88ab1bf1d..83b6ff7aa 100644 --- a/scm/driver/stash/stash.go +++ b/scm/driver/stash/stash.go @@ -32,7 +32,7 @@ func New(uri string) (*scm.Client, error) { return nil, err } if !strings.HasSuffix(base.Path, "/") { - base.Path = base.Path + "/" + base.Path += "/" } client := &wrapper{new(scm.Client)} client.BaseURL = base @@ -75,7 +75,10 @@ func (c *wrapper) do(ctx context.Context, method, path string, in, out interface // write it to the body of the request. if in != nil { buf := new(bytes.Buffer) - json.NewEncoder(buf).Encode(in) // #nosec + err := json.NewEncoder(buf).Encode(in) // #nosec + if err != nil { + return nil, err + } req.Header.Add("Content-Type", "application/json") req.Body = buf } @@ -94,6 +97,7 @@ func (c *wrapper) do(ctx context.Context, method, path string, in, out interface return res, scm.ErrNotAuthorized } else if res.Status > 300 { err := new(Error) + // nolint json.NewDecoder(res.Body).Decode(err) // #nosec return res, err } diff --git a/scm/driver/stash/user.go b/scm/driver/stash/user.go index 8f8d11e99..0eef3151a 100644 --- a/scm/driver/stash/user.go +++ b/scm/driver/stash/user.go @@ -29,7 +29,7 @@ func (s *userService) DeleteToken(context.Context, int64) (*scm.Response, error) } func (s *userService) Find(ctx context.Context) (*scm.User, *scm.Response, error) { - path := fmt.Sprintf("plugins/servlet/applinks/whoami") + path := "plugins/servlet/applinks/whoami" out := new(bytes.Buffer) res, err := s.client.do(ctx, "GET", path, nil, out) if err != nil { diff --git a/scm/driver/stash/user_test.go b/scm/driver/stash/user_test.go index 09af3e3ea..7939aca8a 100644 --- a/scm/driver/stash/user_test.go +++ b/scm/driver/stash/user_test.go @@ -41,7 +41,10 @@ func TestUserFind(t *testing.T) { want := new(scm.User) raw, _ := ioutil.ReadFile("testdata/user.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") @@ -66,7 +69,10 @@ func TestUserLoginFind(t *testing.T) { want := new(scm.User) raw, _ := ioutil.ReadFile("testdata/user.json.golden") - json.Unmarshal(raw, &want) + err = json.Unmarshal(raw, &want) + if err != nil { + t.Error(err) + } if diff := cmp.Diff(got, want); diff != "" { t.Errorf("Unexpected Results") diff --git a/scm/driver/stash/webhook.go b/scm/driver/stash/webhook.go index c13f049d9..3a05a57dc 100644 --- a/scm/driver/stash/webhook.go +++ b/scm/driver/stash/webhook.go @@ -87,7 +87,7 @@ func (s *webhookService) parsePushHook(data []byte, guid string) (scm.Webhook, e return nil, err } if len(dst.Changes) == 0 { - return nil, errors.New("Push hook has empty changeset") + return nil, errors.New("push hook has empty changeset") } change := dst.Changes[0] switch { diff --git a/scm/driver/stash/webhook_test.go b/scm/driver/stash/webhook_test.go index ea3f9e858..5a7f4ec44 100644 --- a/scm/driver/stash/webhook_test.go +++ b/scm/driver/stash/webhook_test.go @@ -190,7 +190,10 @@ func TestWebhooks(t *testing.T) { t.Log(diff) // debug only. remove once implemented - json.NewEncoder(os.Stdout).Encode(o) + err := json.NewEncoder(os.Stdout).Encode(o) + if err != nil { + t.Fatal(err) + } } switch event := o.(type) { diff --git a/scm/example_test.go b/scm/example_test.go index 87a5ac611..b75a1fb2c 100644 --- a/scm/example_test.go +++ b/scm/example_test.go @@ -704,7 +704,10 @@ func ExampleWebhook() { } http.HandleFunc("/hook", handler) - http.ListenAndServe(":8000", nil) + err := http.ListenAndServe(":8000", nil) + if err != nil { + log.Fatal(err) + } } func ExampleWebhook_lookupSecret() { @@ -733,9 +736,14 @@ func ExampleWebhook_lookupSecret() { event.Repo.Name, event.Sender.Login, ) + default: + log.Fatal(err) } } http.HandleFunc("/hook", handler) - http.ListenAndServe(":8000", nil) + err := http.ListenAndServe(":8000", nil) + if err != nil { + log.Fatal(err) + } } diff --git a/scm/transport/oauth1/encode.go b/scm/transport/oauth1/encode.go index 7397e7598..1d1327264 100644 --- a/scm/transport/oauth1/encode.go +++ b/scm/transport/oauth1/encode.go @@ -48,7 +48,7 @@ func percentEncode(input string) string { // according to RFC 3986 2.1. func shouldEscape(c byte) bool { // RFC3986 2.3 unreserved characters - if 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' { + if c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c >= '0' && c <= '9' { return false } switch c { diff --git a/scm/transport/oauth1/oauth1.go b/scm/transport/oauth1/oauth1.go index 41f8ae7e0..2472099ec 100644 --- a/scm/transport/oauth1/oauth1.go +++ b/scm/transport/oauth1/oauth1.go @@ -114,7 +114,10 @@ func (t *Transport) nonce() string { return t.noncer.Nonce() } b := make([]byte, 32) - rand.Read(b) // #nosec + _, err := rand.Read(b) + if err != nil { + return err.Error() + } return base64.StdEncoding.EncodeToString(b) } diff --git a/scm/transport/oauth2/refresh.go b/scm/transport/oauth2/refresh.go index d03b3cdb3..a85b181ef 100644 --- a/scm/transport/oauth2/refresh.go +++ b/scm/transport/oauth2/refresh.go @@ -108,10 +108,10 @@ func (t *Refresher) client() *http.Client { // expired reports whether the token is expired. func expired(token *scm.Token) bool { - if len(token.Refresh) == 0 { + if token.Refresh == "" { return false } - if token.Expires.IsZero() && len(token.Token) != 0 { + if token.Expires.IsZero() && token.Token != "" { return false } return token.Expires.Add(-expiryDelta). diff --git a/scm/util.go b/scm/util.go index ca634493b..184b64c01 100644 --- a/scm/util.go +++ b/scm/util.go @@ -68,7 +68,7 @@ func IsTag(ref string) bool { return strings.HasPrefix(ref, "refs/tags/") } -//ConvertStatusInputsToStatuses converts the inputs to status objects +// ConvertStatusInputsToStatuses converts the inputs to status objects func ConvertStatusInputsToStatuses(inputs []*StatusInput) []*Status { answer := []*Status{} for _, input := range inputs { diff --git a/scm/webhook.go b/scm/webhook.go index 1ac8d7fc0..6e2325099 100644 --- a/scm/webhook.go +++ b/scm/webhook.go @@ -29,7 +29,7 @@ const ( WebhookKindFork WebhookKind = "fork" // WebhookKindInstallation is for app installation events WebhookKindInstallation WebhookKind = "installation" - // WebhookKindInstallationRepository is for app isntallation in a repository events + // WebhookKindInstallationRepository is for app installation in a repository events WebhookKindInstallationRepository WebhookKind = "installation_repository" // WebhookKindIssue is for issue events WebhookKindIssue WebhookKind = "issue" @@ -66,7 +66,7 @@ const ( var ( // ErrSignatureInvalid is returned when the webhook // signature is invalid or cannot be calculated. - ErrSignatureInvalid = errors.New("Invalid webhook signature") + ErrSignatureInvalid = errors.New("invalid webhook signature") ) type ( diff --git a/scm/webhook_test.go b/scm/webhook_test.go index 956bcfc62..fc9cfddd2 100644 --- a/scm/webhook_test.go +++ b/scm/webhook_test.go @@ -2,11 +2,12 @@ package scm_test import ( "encoding/json" - "github.com/jenkins-x/go-scm/scm" - "github.com/stretchr/testify/require" "io/ioutil" "path/filepath" "testing" + + "github.com/jenkins-x/go-scm/scm" + "github.com/stretchr/testify/require" ) func TestWebhookWrapper(t *testing.T) {