From 157434b7be48e64f3dd6151447729e073bde06a6 Mon Sep 17 00:00:00 2001 From: KsaweryZietara <91937141+KsaweryZietara@users.noreply.github.com> Date: Fri, 1 Mar 2024 16:41:33 +0100 Subject: [PATCH] Fix issues reported by golangci (#623) * Create go linter workflow, update Makefile * Fix golangci errors --- .github/workflows/run-go-linter.yaml | 58 +++++++++++++++++++ .golangci.yml | 13 +++++ Makefile | 24 +++++--- .../btpoperator_controller_updating_test.go | 9 +-- main.go | 1 + 5 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/run-go-linter.yaml create mode 100644 .golangci.yml diff --git a/.github/workflows/run-go-linter.yaml b/.github/workflows/run-go-linter.yaml new file mode 100644 index 000000000..021d1c74c --- /dev/null +++ b/.github/workflows/run-go-linter.yaml @@ -0,0 +1,58 @@ +name: Run go linter + +on: + pull_request: + branches: [ main ] + types: + - opened + - reopened + - synchronize + - ready_for_review + +jobs: + run-go-linter: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Get changed files + id: changed-files-specific + uses: tj-actions/changed-files@90a06d6ba9543371ab4df8eeca0be07ca6054959 + with: + files: | + .github/** + api/** + cmd/** + config/** + controllers/** + deployments/** + examples/** + hack/** + internal/** + module-chart/** + module-resources/** + scripts/** + config.yaml + Dockerfile + go.mod + go.sum + main.go + Makefile + **/*.go + **/*.sh + + - name: Set up go environment + if: steps.changed-files-specific.outputs.any_modified == 'true' + uses: actions/setup-go@v4 + with: + go-version-file: 'go.mod' + + - name: Go linter + if: steps.changed-files-specific.outputs.any_modified == 'true' + uses: golangci/golangci-lint-action@v3 + with: + version: v1.55.2 + skip-cache: true + only-new-issues: true + args: --timeout 2m0s diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 000000000..9e930587a --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,13 @@ +run: + allow-parallel-runners: true +output: + format: "colored-tab:stderr,github-actions:stdout" + print-issued-lines: true + print-linter-name: true + uniq-by-line: false +linters: + disable-all: true + enable: + - errcheck + - goimports + - govet \ No newline at end of file diff --git a/Makefile b/Makefile index c76b4e11c..7e672e35f 100644 --- a/Makefile +++ b/Makefile @@ -33,6 +33,8 @@ endif SHELL = /usr/bin/env bash -o pipefail .SHELLFLAGS = -ec +GOLINT_VER = v1.55.2 + .PHONY: all all: build @@ -179,10 +181,18 @@ fmt: ## Run go fmt against code. vet: ## Run go vet against code. go vet ./... -GOLANG_CI_LINT = $(LOCALBIN)/golangci-lint -GOLANG_CI_LINT_VERSION ?= v1.50.1 -.PHONY: lint -lint: ## Download & Build & Run golangci-lint against code. - GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANG_CI_LINT_VERSION) - $(LOCALBIN)/golangci-lint run - +go-lint-install: ## linter config in file at root of project -> '.golangci.yaml' + @if [ "$(shell command golangci-lint version --format short)" != "$(GOLINT_VER)" ]; then \ + echo golangci in version $(GOLINT_VER) not found. will be downloaded; \ + go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLINT_VER); \ + echo golangci installed with version: $(shell command golangci-lint version --format short); \ + fi; + +.PHONY: go-lint +go-lint: go-lint-install ## linter config in file at root of project -> '.golangci.yaml' + golangci-lint run + +.PHONY: fix +fix: go-lint-install ## try to fix automatically issues + go mod tidy + golangci-lint run --fix diff --git a/controllers/btpoperator_controller_updating_test.go b/controllers/btpoperator_controller_updating_test.go index 3ff699dc4..be81d1ec7 100644 --- a/controllers/btpoperator_controller_updating_test.go +++ b/controllers/btpoperator_controller_updating_test.go @@ -2,21 +2,22 @@ package controllers import ( "fmt" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "os" "time" - "github.com/kyma-project/btp-manager/api/v1alpha1" - "github.com/kyma-project/btp-manager/internal/manifest" - "github.com/kyma-project/btp-manager/internal/ymlutils" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" apimachienerytypes "k8s.io/apimachinery/pkg/types" controllerruntime "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + + "github.com/kyma-project/btp-manager/api/v1alpha1" + "github.com/kyma-project/btp-manager/internal/manifest" + "github.com/kyma-project/btp-manager/internal/ymlutils" ) const ( diff --git a/main.go b/main.go index 24c403e3f..df4551477 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ package main import ( "flag" "os" + //test // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)