diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index 0f039bfa..830e4166 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -33,4 +33,7 @@ jobs: # Check that go fmt ./... produces a zero diff; clean up any changes afterwards. go fmt ./... && git diff --exit-code; code=$?; git checkout -- .; (exit $code) # Check that go fix ./... produces a zero diff; clean up any changes afterwards. - go fix ./... && git diff --exit-code; code=$?; git checkout -- .; (exit $code) + # + # Renable this after https://github.com/golang/go/commit/7fd62ba821b1044e8e4077df052b0a1232672d57 + # has been released. + # go fix ./... && git diff --exit-code; code=$?; git checkout -- .; (exit $code) diff --git a/.golangci.yml b/.golangci.yml index ef42aeda..36e56668 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -32,6 +32,18 @@ linters-settings: lines: 160 statements: 70 + # https://github.com/daixiang0/gci + # ensure import order is consistent + # gci write --custom-order -s standard -s default -s blank -s dot -s "prefix(github.com/go-vela)" . + gci: + custom-order: true + sections: + - standard + - default + - blank + - dot + - prefix(github.com/go-vela) + # https://github.com/denis-tingaikin/go-header goheader: template: |- @@ -60,45 +72,46 @@ linters: # enable a specific set of linters to run enable: - - bidichk # checks for dangerous unicode character sequences - - bodyclose # checks whether HTTP response body is closed successfully - - contextcheck # check the function whether use a non-inherited context - - deadcode # finds unused code - - dupl # code clone detection - - errcheck # checks for unchecked errors - - errorlint # find misuses of errors - - exportloopref # check for exported loop vars - - funlen # detects long functions - - goconst # finds repeated strings that could be replaced by a constant - - gocyclo # computes and checks the cyclomatic complexity of functions - - godot # checks if comments end in a period - - gofmt # checks whether code was gofmt-ed - - goheader # checks is file header matches to pattern - - goimports # fixes imports and formats code in same style as gofmt - - gomoddirectives # manage the use of 'replace', 'retract', and 'excludes' directives in go.mod - - goprintffuncname # checks that printf-like functions are named with f at the end - - gosec # inspects code for security problems - - gosimple # linter that specializes in simplifying a code - - govet # reports suspicious constructs, ex. Printf calls whose arguments don't align with the format string - - ineffassign # detects when assignments to existing variables aren't used - - makezero # finds slice declarations with non-zero initial length - - misspell # finds commonly misspelled English words in comments - - nakedret # finds naked returns in functions greater than a specified function length - - nilerr # finds the code that returns nil even if it checks that the error is not nil - - noctx # noctx finds sending http request without context.Context - - nolintlint # reports ill-formed or insufficient nolint directives - - revive # linter for go - - staticcheck # applies static analysis checks, go vet on steroids - - structcheck # finds unused struct fields - - stylecheck # replacement for golint - - tenv # analyzer that detects using os.Setenv instead of t.Setenv since Go1.17 - - typecheck # parses and type-checks go code, like the front-end of a go compiler - - unconvert # remove unnecessary type conversions - - unparam # reports unused function parameters - - unused # checks for unused constants, variables, functions and types - - varcheck # finds unused global variables and constants - - whitespace # detects leading and trailing whitespace - - wsl # forces code to use empty lines + - bidichk # checks for dangerous unicode character sequences + - bodyclose # checks whether HTTP response body is closed successfully + - contextcheck # check the function whether use a non-inherited context + - deadcode # finds unused code + - dupl # code clone detection + - errcheck # checks for unchecked errors + - errorlint # find misuses of errors + - exportloopref # check for exported loop vars + - funlen # detects long functions + - gci # consistent import ordering + - goconst # finds repeated strings that could be replaced by a constant + - gocyclo # computes and checks the cyclomatic complexity of functions + - godot # checks if comments end in a period + - gofmt # checks whether code was gofmt-ed + - goheader # checks is file header matches to pattern + - goimports # fixes imports and formats code in same style as gofmt + - gomoddirectives # manage the use of 'replace', 'retract', and 'excludes' directives in go.mod + - goprintffuncname # checks that printf-like functions are named with f at the end + - gosec # inspects code for security problems + - gosimple # linter that specializes in simplifying a code + - govet # reports suspicious constructs, ex. Printf calls whose arguments don't align with the format string + - ineffassign # detects when assignments to existing variables aren't used + - makezero # finds slice declarations with non-zero initial length + - misspell # finds commonly misspelled English words in comments + - nakedret # finds naked returns in functions greater than a specified function length + - nilerr # finds the code that returns nil even if it checks that the error is not nil + - noctx # noctx finds sending http request without context.Context + - nolintlint # reports ill-formed or insufficient nolint directives + - revive # linter for go + - staticcheck # applies static analysis checks, go vet on steroids + - structcheck # finds unused struct fields + - stylecheck # replacement for golint + - tenv # analyzer that detects using os.Setenv instead of t.Setenv since Go1.17 + - typecheck # parses and type-checks go code, like the front-end of a go compiler + - unconvert # remove unnecessary type conversions + - unparam # reports unused function parameters + - unused # checks for unused constants, variables, functions and types + - varcheck # finds unused global variables and constants + - whitespace # detects leading and trailing whitespace + - wsl # forces code to use empty lines # static list of linters we know golangci can run but we've # chosen to leave disabled for now @@ -112,13 +125,13 @@ linters: # - exhaustivestruct - style preference # - forbidigo - unused # - forcetypeassert - unused - # - gci - use goimports # - gochecknoinits - unused # - gochecknoglobals - global variables allowed # - gocognit - unused complexity metric # - gocritic - style preference # - godox - to be used in the future # - goerr113 - to be used in the future + # - goimports - use gci # - golint - archived, replaced with revive # - gofumpt - use gofmt # - gomnd - get too many false-positives diff --git a/Makefile b/Makefile index 373dbb88..bd4d3e59 100644 --- a/Makefile +++ b/Makefile @@ -142,3 +142,24 @@ schema: @go get github.com/alecthomas/jsonschema @go get github.com/iancoleman/orderedmap @go run cmd/schema/main.go > schema.json + +# The `lint` target is intended to lint the +# Go source code with golangci-lint. +# +# Usage: `make lint` +.PHONY: lint +lint: + @echo + @echo "### Linting Go Code" + @golangci-lint run ./... + +# The `lintfix` target is intended to lint the +# Go source code with golangci-lint and apply +# any fixes that can be automatically applied. +# +# Usage: `make lintfix` +.PHONY: lintfix +lintfix: + @echo + @echo "### Fixing Go code with linter" + @golangci-lint run ./... --fix \ No newline at end of file diff --git a/database/deployment.go b/database/deployment.go index ba97ecdd..61a56136 100644 --- a/database/deployment.go +++ b/database/deployment.go @@ -7,10 +7,11 @@ import ( "errors" "fmt" + "github.com/lib/pq" + "github.com/go-vela/types/constants" "github.com/go-vela/types/library" "github.com/go-vela/types/raw" - "github.com/lib/pq" ) var ( diff --git a/database/deployment_test.go b/database/deployment_test.go index 3a3c6f2f..e22b4481 100644 --- a/database/deployment_test.go +++ b/database/deployment_test.go @@ -6,10 +6,11 @@ import ( "database/sql" "testing" - "github.com/go-vela/types/library" - "github.com/go-vela/types/raw" "github.com/google/go-cmp/cmp" "github.com/lib/pq" + + "github.com/go-vela/types/library" + "github.com/go-vela/types/raw" ) func TestDatabase_Deployment_Nullify(t *testing.T) { diff --git a/database/repo.go b/database/repo.go index 03a3d22c..5c4f8c2c 100644 --- a/database/repo.go +++ b/database/repo.go @@ -7,9 +7,10 @@ import ( "encoding/base64" "errors" + "github.com/lib/pq" + "github.com/go-vela/types/constants" "github.com/go-vela/types/library" - "github.com/lib/pq" ) var ( diff --git a/database/schedule.go b/database/schedule.go index 4f26b278..bd678b79 100644 --- a/database/schedule.go +++ b/database/schedule.go @@ -7,6 +7,7 @@ import ( "errors" "github.com/adhocore/gronx" + "github.com/go-vela/types/library" ) diff --git a/database/secret.go b/database/secret.go index 1c93cf85..33dcd800 100644 --- a/database/secret.go +++ b/database/secret.go @@ -8,10 +8,10 @@ import ( "errors" "strings" + "github.com/lib/pq" + "github.com/go-vela/types/constants" "github.com/go-vela/types/library" - - "github.com/lib/pq" ) var ( diff --git a/database/user.go b/database/user.go index 1e312755..a5030bcc 100644 --- a/database/user.go +++ b/database/user.go @@ -8,9 +8,10 @@ import ( "errors" "regexp" + "github.com/lib/pq" + "github.com/go-vela/types/constants" "github.com/go-vela/types/library" - "github.com/lib/pq" ) var ( diff --git a/database/worker.go b/database/worker.go index 59c2f343..45d76388 100644 --- a/database/worker.go +++ b/database/worker.go @@ -6,9 +6,10 @@ import ( "database/sql" "errors" + "github.com/lib/pq" + "github.com/go-vela/types/constants" "github.com/go-vela/types/library" - "github.com/lib/pq" ) var ( diff --git a/go.mod b/go.mod index 14587f80..07d1e9ed 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/go-vela/types -go 1.21 +go 1.21.9 require ( github.com/adhocore/gronx v1.8.1 diff --git a/library/actions/pull.go b/library/actions/pull.go index 8e0063b1..b7d8f93c 100644 --- a/library/actions/pull.go +++ b/library/actions/pull.go @@ -1,6 +1,5 @@ // SPDX-License-Identifier: Apache-2.0 -// -//nolint:dupl // similar code to push.go + package actions import "github.com/go-vela/types/constants" diff --git a/library/actions/push.go b/library/actions/push.go index ed19c48f..612b2cff 100644 --- a/library/actions/push.go +++ b/library/actions/push.go @@ -1,6 +1,5 @@ // SPDX-License-Identifier: Apache-2.0 -// -//nolint:dupl // similar code to comment.go + package actions import "github.com/go-vela/types/constants" diff --git a/library/build_test.go b/library/build_test.go index 5c82c7a3..d9b810d7 100644 --- a/library/build_test.go +++ b/library/build_test.go @@ -8,8 +8,9 @@ import ( "testing" "time" - "github.com/go-vela/types/raw" "github.com/google/go-cmp/cmp" + + "github.com/go-vela/types/raw" ) func TestLibrary_Build_Duration(t *testing.T) { diff --git a/library/events_test.go b/library/events_test.go index 86771ea1..c224f989 100644 --- a/library/events_test.go +++ b/library/events_test.go @@ -6,9 +6,10 @@ import ( "reflect" "testing" + "github.com/google/go-cmp/cmp" + "github.com/go-vela/types/constants" "github.com/go-vela/types/library/actions" - "github.com/google/go-cmp/cmp" ) func TestLibrary_Events_Getters(t *testing.T) { diff --git a/library/repo.go b/library/repo.go index 0f82207b..8755d524 100644 --- a/library/repo.go +++ b/library/repo.go @@ -618,8 +618,6 @@ func (r *Repo) SetApproveBuild(v string) { } // String implements the Stringer interface for the Repo type. -// -//nolint:dupl // ignore duplicate with test func func (r *Repo) String() string { return fmt.Sprintf(`{ Active: %t, diff --git a/library/repo_test.go b/library/repo_test.go index f9eaba2b..e102e103 100644 --- a/library/repo_test.go +++ b/library/repo_test.go @@ -7,8 +7,9 @@ import ( "reflect" "testing" - "github.com/go-vela/types/constants" "github.com/google/go-cmp/cmp" + + "github.com/go-vela/types/constants" ) func TestLibrary_Repo_Environment(t *testing.T) { diff --git a/library/secret_test.go b/library/secret_test.go index 5074831f..48655a7c 100644 --- a/library/secret_test.go +++ b/library/secret_test.go @@ -8,10 +8,11 @@ import ( "testing" "time" + "github.com/google/go-cmp/cmp" + "github.com/go-vela/types/constants" "github.com/go-vela/types/library/actions" "github.com/go-vela/types/pipeline" - "github.com/google/go-cmp/cmp" ) func TestLibrary_Secret_Sanitize(t *testing.T) { diff --git a/yaml/build_test.go b/yaml/build_test.go index 8da97c90..a1ca6cc3 100644 --- a/yaml/build_test.go +++ b/yaml/build_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/buildkite/yaml" + "github.com/go-vela/types/library" "github.com/go-vela/types/raw" ) diff --git a/yaml/ruleset_test.go b/yaml/ruleset_test.go index 05a67ce1..9b95f304 100644 --- a/yaml/ruleset_test.go +++ b/yaml/ruleset_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/buildkite/yaml" + "github.com/go-vela/types/pipeline" ) diff --git a/yaml/secret_test.go b/yaml/secret_test.go index f70637e6..9918b8cf 100644 --- a/yaml/secret_test.go +++ b/yaml/secret_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/buildkite/yaml" + "github.com/go-vela/types/pipeline" ) diff --git a/yaml/service_test.go b/yaml/service_test.go index d0404eef..1289095c 100644 --- a/yaml/service_test.go +++ b/yaml/service_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/buildkite/yaml" + "github.com/go-vela/types/pipeline" "github.com/go-vela/types/raw" ) diff --git a/yaml/stage.go b/yaml/stage.go index bfc0dffb..65943bd9 100644 --- a/yaml/stage.go +++ b/yaml/stage.go @@ -5,10 +5,10 @@ package yaml import ( "fmt" + "github.com/buildkite/yaml" + "github.com/go-vela/types/pipeline" "github.com/go-vela/types/raw" - - "github.com/buildkite/yaml" ) type ( diff --git a/yaml/stage_test.go b/yaml/stage_test.go index cf90ba48..5cad3cb7 100644 --- a/yaml/stage_test.go +++ b/yaml/stage_test.go @@ -7,9 +7,9 @@ import ( "reflect" "testing" - "github.com/go-vela/types/pipeline" - "github.com/buildkite/yaml" + + "github.com/go-vela/types/pipeline" ) func TestYaml_StageSlice_ToPipeline(t *testing.T) { diff --git a/yaml/step_test.go b/yaml/step_test.go index 2d0b6034..8136fcaf 100644 --- a/yaml/step_test.go +++ b/yaml/step_test.go @@ -7,10 +7,10 @@ import ( "reflect" "testing" + "github.com/buildkite/yaml" + "github.com/go-vela/types/pipeline" "github.com/go-vela/types/raw" - - "github.com/buildkite/yaml" ) func TestYaml_StepSlice_ToPipeline(t *testing.T) { diff --git a/yaml/template_test.go b/yaml/template_test.go index 39827760..e7f760cb 100644 --- a/yaml/template_test.go +++ b/yaml/template_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/buildkite/yaml" + "github.com/go-vela/types/library" ) diff --git a/yaml/ulimit_test.go b/yaml/ulimit_test.go index 46d28bde..dc6fdac6 100644 --- a/yaml/ulimit_test.go +++ b/yaml/ulimit_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/buildkite/yaml" + "github.com/go-vela/types/pipeline" ) diff --git a/yaml/volume_test.go b/yaml/volume_test.go index dc7d9513..837703ad 100644 --- a/yaml/volume_test.go +++ b/yaml/volume_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/buildkite/yaml" + "github.com/go-vela/types/pipeline" )