Skip to content

Commit

Permalink
fix: linting issues & add .golangci.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
rajatgupta24 committed Mar 7, 2022
1 parent 45c76db commit 9eb4e2c
Show file tree
Hide file tree
Showing 127 changed files with 1,288 additions and 566 deletions.
121 changes: 121 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -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"
30 changes: 30 additions & 0 deletions .lighthouse/jenkins-x/lint.yaml
Original file line number Diff line number Diff line change
@@ -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: {}
11 changes: 9 additions & 2 deletions .lighthouse/jenkins-x/triggers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions pkg/hmac/hmac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
package hmac

import (
// nolint
"crypto/md5"
// nolint
"crypto/sha1"
"crypto/sha256"
"hash"
Expand Down
2 changes: 1 addition & 1 deletion scm/const_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions scm/driver/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"},
}
Expand Down
5 changes: 4 additions & 1 deletion scm/driver/bitbucket/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
19 changes: 1 addition & 18 deletions scm/driver/bitbucket/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
40 changes: 32 additions & 8 deletions scm/driver/bitbucket/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down
5 changes: 3 additions & 2 deletions scm/driver/bitbucket/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
5 changes: 2 additions & 3 deletions scm/driver/bitbucket/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

Expand Down
Loading

0 comments on commit 9eb4e2c

Please sign in to comment.