From 5499ebfac866928c91348c3ef811abfd7951a49c Mon Sep 17 00:00:00 2001 From: Kay Yan Date: Fri, 27 Dec 2024 04:00:32 +0000 Subject: [PATCH] feat: introduce linter to run on PR builds Signed-off-by: Kay Yan --- .github/workflows/golangci_lint.yaml | 20 ++++ .golangci.yml | 141 +++++++++++++++++++++++++++ Makefile | 5 + controllers/analysis_step.go | 2 +- 4 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/golangci_lint.yaml create mode 100644 .golangci.yml diff --git a/.github/workflows/golangci_lint.yaml b/.github/workflows/golangci_lint.yaml new file mode 100644 index 00000000..4c6f75ff --- /dev/null +++ b/.github/workflows/golangci_lint.yaml @@ -0,0 +1,20 @@ +name: Run golangci-lint + +on: + pull_request: + branches: [main] + +jobs: + golangci-lint: + runs-on: ubuntu-latest + steps: + - name: Check out code into the Go module directory + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 + + - name: golangci-lint + uses: reviewdog/action-golangci-lint@7708105983c614f7a2725e2172908b7709d1c3e4 # v2 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + reporter: github-pr-check + golangci_lint_flags: "--timeout=240s" + level: warning diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 00000000..21e02d8c --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,141 @@ +--- +run: + concurrency: 6 +linters: + disable-all: true + enable: + # - depguard + - gofmt + - goimports + - govet + - ineffassign + - misspell + - nakedret + - prealloc + - typecheck + # - asciicheck + # - bodyclose + # - dogsled + # - dupl + # - errcheck + # - errorlint + # - exhaustive + # - exhaustivestruct + # - exportloopref + # - funlen + # - gci + # - gochecknoglobals + # - gochecknoinits + # - gocognit + # - goconst + # - gocritic + # - gocyclo + # - godot + # - godox + # - goerr113 + # - gofumpt + # - goheader + # - golint + # - gomnd + # - gomodguard + # - goprintffuncname + # - gosec (gas) + - gosimple # (megacheck) + # - interfacer + # - lll + # - maligned + # - nestif + # - nlreturn + # - noctx + # - nolintlint + - revive + # - rowserrcheck + # - scopelint + # - sqlclosecheck + - staticcheck + - stylecheck + # - testpackage + # - tparallel + - unconvert + # - unparam + - unused + # - whitespace + # - wrapcheck + # - wsl +linters-settings: + gocritic: + enabled-checks: + # Diagnostic + - appendAssign + - argOrder + - badCond + - caseOrder + - codegenComment + - commentedOutCode + - deprecatedComment + - dupArg + - dupBranchBody + - dupCase + - dupSubExpr + - exitAfterDefer + - flagDeref + - flagName + - nilValReturn + - offBy1 + - sloppyReassign + - weakCond + - octalLiteral + + # Performance + - appendCombine + - equalFold + - hugeParam + - indexAlloc + - rangeExprCopy + - rangeValCopy + + # Style + - assignOp + - boolExprSimplify + - captLocal + - commentFormatting + - commentedOutImport + - defaultCaseOrder + - docStub + - elseif + - emptyFallthrough + - emptyStringTest + - hexLiteral + - ifElseChain + - methodExprCall + - regexpMust + - singleCaseSwitch + - sloppyLen + - stringXbytes + - switchTrue + - typeAssertChain + - typeSwitchVar + - underef + - unlabelStmt + - unlambda + - unslice + - valSwap + - wrapperFunc + - yodaStyleExpr + + # Opinionated + - builtinShadow + - importShadow + - initClause + - nestingReduce + - paramTypeCombine + - ptrToRefParam + - typeUnparen + - unnamedResult + - unnecessaryBlock + +issues: + exclude-rules: + - linters: + - revive + text: "unused-parameter" diff --git a/Makefile b/Makefile index e69298ae..fdd42da9 100644 --- a/Makefile +++ b/Makefile @@ -54,6 +54,11 @@ fmt: ## Run go fmt against code. vet: ## Run go vet against code. go vet ./... +## lint: Run go lint against code. +.PHONY: lint +lint: + @golangci-lint run -v --timeout=5m ./... + .PHONY: test test: manifests generate fmt vet envtest ## Run tests. KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out diff --git a/controllers/analysis_step.go b/controllers/analysis_step.go index a6ba5110..1a6a4329 100644 --- a/controllers/analysis_step.go +++ b/controllers/analysis_step.go @@ -72,7 +72,7 @@ func (step *AnalysisStep) execute(instance *K8sGPTInstance) (ctrl.Result, error) } // Prior to creating or updating any results we will delete any stale results that - // no longer are relevent, we can do this by using the resultSpec composed name against + // no longer are relevant, we can do this by using the resultSpec composed name against // the custom resource name err = step.cleanUpStaleResults(rawResults, instance) if err != nil {