From 297b4581d4fa939746e04cbf9a1a4fe4ba70dce1 Mon Sep 17 00:00:00 2001 From: Antoine Gelloz Date: Tue, 20 Aug 2024 16:36:12 +0200 Subject: [PATCH] fix: linter --- .github/workflows/qa.yml | 5 +++++ .gitignore | 2 ++ .golangci.yml | 38 ------------------------------------ Makefile | 13 ++++-------- pkg/dag/dag.go | 1 + pkg/dag/image.go | 1 + pkg/dib/build.go | 2 ++ pkg/dib/generate_dag.go | 3 +-- pkg/dib/generate_dag_test.go | 4 +++- pkg/kubernetes/pod.go | 1 + pkg/mock/builder.go | 1 + 11 files changed, 21 insertions(+), 50 deletions(-) delete mode 100644 .golangci.yml diff --git a/.github/workflows/qa.yml b/.github/workflows/qa.yml index d7eafec4..46ab7f79 100644 --- a/.github/workflows/qa.yml +++ b/.github/workflows/qa.yml @@ -19,11 +19,16 @@ jobs: lint: name: Run linters runs-on: ubuntu-latest + env: + # renovate: datasource=github-releases depName=radiofrance/lint-config + LINT_CONFIG_VERSION: v1.0.0 steps: - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 - uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2 with: go-version-file: "go.mod" + - name: Download golangci-lint config file + run: curl -o .golangci.yml https://raw.githubusercontent.com/radiofrance/lint-config/${{ env.LINT_CONFIG_VERSION }}/.golangci.yml - uses: golangci/golangci-lint-action@aaa42aa0628b4ae2578232a66b541047968fac86 # v6.1.0 with: # renovate: datasource=github-releases depName=golangci/golangci-lint diff --git a/.gitignore b/.gitignore index a8463802..07268668 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ pkg/dib/tests pkg/trivy/reports venv coverage.output +.golangci.yml +golangci-lint-report.xml diff --git a/.golangci.yml b/.golangci.yml deleted file mode 100644 index 43ee5de5..00000000 --- a/.golangci.yml +++ /dev/null @@ -1,38 +0,0 @@ ---- -run: - timeout: 5m - concurrency: 4 -linters: - enable-all: true - disable: - - maligned # Disable because they are deprecated and throw warning in logs - - interfacer # Disable because they are deprecated and throw warning in logs - - scopelint # Disable because they are deprecated and throw warning in logs - - golint # Disable because they are deprecated and throw warning in logs - - exhaustivestruct # Disable because they are deprecated and throw warning in logs - - ifshort # Disable because they are deprecated and throw warning in logs - - deadcode # Disable because they are deprecated and throw warning in logs - - varcheck # Disable because they are deprecated and throw warning in logs - - structcheck # Disable because they are deprecated and throw warning in logs - - nosnakecase # Disable because they are deprecated and throw warning in logs - - cyclop # calculates cyclomatic complexities of functions or packages => legit, but we prefer to use sonarqube and be non-blocking - - depguard # We do not have particular restrictions on which packages should be allowed or not - - exhaustruct # Find structs that have uninitialized fields. => We want to rely on default field initialization - - funlen # checks for long functions => legit, but we prefer to use sonarqube and be non-blocking - - gci # Control golang package import order and make it always deterministic. => We use goimport which is native to most IDEs - - gochecknoglobals # Check that no globals are present in Go code. => We want to use global variables in some situations - - gochecknoinits # Check that no init functions are present in Go code. => We want to use init functions - - gocognit # Calculates cognitive complexities of functions => legit, but we prefer to use sonarqube and be non-blocking - - godox # Search for TODO/BUG/FIXME => Prevent us from using any of these words - - goerr113 # check the errors handling expressions => Is a little extreme - - gomnd # Magic number detector for Go. => We don't care - - nestif # Detect deeply nested if statements in Go source code => legit, but we prefer to use sonarqube and be non-blocking - - nlreturn # Checks for a new line before return and branch statements. => We don't care - - prealloc # find slice declarations that could potentially be preallocated. => Much complexity for small interest - - tagliatelle # A linter that handles struct tags. => Too many false positive errors - - wsl # Whitespace Linter - Forces you to use empty lines! => We don't care - - wrapcheck # A Go linter to check that errors from external packages are wrapped => A bit too annoying - - exhaustive # We don't want to force struct exhaustive - - musttag # Enforce field tags in (un)marshaled structs => Too many false positive errors, and lint code out of project scope - - perfsprint # Performance is not an issue - - dupl # Duplicate code necessary in tests diff --git a/Makefile b/Makefile index 651fca33..cbf82802 100644 --- a/Makefile +++ b/Makefile @@ -27,13 +27,11 @@ docs: build ## ---------------------- ## -qa: lint test ## Run all QA process +qa: lint test -lint: ## Lint source code - golangci-lint run -v - -lint.fix: ## Lint and fix source code - golangci-lint run --fix -v +lint: + curl -o .golangci.yml https://raw.githubusercontent.com/radiofrance/lint-config/main/.golangci.yml + golangci-lint run --verbose PKG := "./..." RUN := ".*" @@ -50,6 +48,3 @@ test: ## Run tests sed 's/PAUSE/$(BLUE)PAUSE$(RESET)/g' | \ sed 's/PASS/$(GREEN)PASS$(RESET)/g' | \ sed 's/FAIL/$(RED)FAIL$(RESET)/g' - -fmt: ## Run `go fmt` on all files - find -name '*.go' -exec gofmt -w -s '{}' ';' diff --git a/pkg/dag/dag.go b/pkg/dag/dag.go index 99ea40c7..15cb8725 100644 --- a/pkg/dag/dag.go +++ b/pkg/dag/dag.go @@ -156,6 +156,7 @@ func (d *DAG) Filter(filterFunc func(*Node) bool) *DAG { return &filteredGraph } +//nolint:musttag func (d *DAG) ListImage() string { imagesList := make(map[string]Image) d.Walk(func(node *Node) { diff --git a/pkg/dag/image.go b/pkg/dag/image.go index 2c44a126..cdd53eba 100644 --- a/pkg/dag/image.go +++ b/pkg/dag/image.go @@ -42,6 +42,7 @@ func (img Image) DockerRef(version string) string { return fmt.Sprintf("%s:%s", img.Name, version) } +//nolint:musttag func (img Image) Print() string { strImg, err := yaml.Marshal(img) if err != nil { diff --git a/pkg/dib/build.go b/pkg/dib/build.go index 161426c7..3c197ac1 100644 --- a/pkg/dib/build.go +++ b/pkg/dib/build.go @@ -45,6 +45,8 @@ type BuildOpts struct { } // RebuildGraph iterates over the graph to rebuild all the images that are marked to be rebuilt. +// +//nolint:musttag func (p *Builder) RebuildGraph( builder types.ImageBuilder, rateLimiter ratelimit.RateLimiter, diff --git a/pkg/dib/generate_dag.go b/pkg/dib/generate_dag.go index 669a9225..49bf7585 100644 --- a/pkg/dib/generate_dag.go +++ b/pkg/dib/generate_dag.go @@ -232,8 +232,7 @@ func generateHashes(graph *dag.DAG, allFiles []string, customHashListPath string // isFileIgnored checks whether a file matches the images ignore patterns. // It returns true if the file matches at least one pattern (meaning it should be ignored). func isFileIgnored(node *dag.Node, file string) bool { - if node.Image.IgnorePatterns == nil || - len(node.Image.IgnorePatterns) == 0 { + if len(node.Image.IgnorePatterns) == 0 { return false } diff --git a/pkg/dib/generate_dag_test.go b/pkg/dib/generate_dag_test.go index c59da5e1..00462a71 100644 --- a/pkg/dib/generate_dag_test.go +++ b/pkg/dib/generate_dag_test.go @@ -19,7 +19,7 @@ const ( registryPrefix = "eu.gcr.io/my-test-repository" ) -//nolint:paralleltest +//nolint:paralleltest,dupl func TestGenerateDAG(t *testing.T) { t.Run("basic tests", func(t *testing.T) { graph, err := dib.GenerateDAG(buildPath1, registryPrefix, "", nil) @@ -52,6 +52,7 @@ func TestGenerateDAG(t *testing.T) { multistageNode0 := nodes0["multistage"] // When I add a new file in bullseye/ (root node) + //nolint:gosec require.NoError(t, os.WriteFile( path.Join(buildPath, "bullseye/newfile"), []byte("any content"), @@ -83,6 +84,7 @@ func TestGenerateDAG(t *testing.T) { multistageNode0 := nodes0["multistage"] // When I add a new file in bullseye/multistage/ (child node) + //nolint:gosec require.NoError(t, os.WriteFile( path.Join(buildPath, "bullseye/multistage/newfile"), []byte("file contents"), diff --git a/pkg/kubernetes/pod.go b/pkg/kubernetes/pod.go index 31b52a58..9bff4b96 100644 --- a/pkg/kubernetes/pod.go +++ b/pkg/kubernetes/pod.go @@ -70,6 +70,7 @@ func WaitPodReady(ctx context.Context, watcher watch.Interface) (chan struct{}, logger.Infof("Pod %s/%s failed", pod.ObjectMeta.Namespace, pod.ObjectMeta.Name) errChan <- fmt.Errorf("pod %s terminated (failed)", pod.Name) return + case corev1.PodPending, corev1.PodUnknown: } case <-time.After(1 * time.Hour): errChan <- fmt.Errorf("timeout waiting for pod to run to completion") diff --git a/pkg/mock/builder.go b/pkg/mock/builder.go index bcc1015c..0f944b59 100644 --- a/pkg/mock/builder.go +++ b/pkg/mock/builder.go @@ -22,6 +22,7 @@ func NewBuilder() *Builder { const ReportsDir = "tests/mock-reports" +//nolint:musttag func (e *Builder) Build(opts types.ImageBuilderOpts) error { if err := os.MkdirAll(path.Join(ReportsDir, e.ID), 0o755); err != nil && !os.IsExist(err) { return fmt.Errorf("failed to create mock-reports directory: %w", err)