From 09d97b27d42593cb48a173fbb75c79a0a001ffdd Mon Sep 17 00:00:00 2001 From: Charles Treatman Date: Tue, 16 Jul 2024 13:37:48 -0500 Subject: [PATCH] chore(ci): lint with golangci-lint (#726) This introduces linting in CI to help maintain code quality and identify issues that may not be caught by tests. To avoid a big PR that resolves all existing linting issues, the CI workflow is configured to only look at files that are changed in a particular PR or commit; we'll need to keep an eye on this to make sure it's working as expected. This will allow us to chip away at existing issues over time. Eventually (ideally within a few months), the CI workflow should be updated to always lint the entire codebase. This PR also introduces a `make lint` task for local execution, but this task lints the entire codebase and, for the time being, will catch some issues that are not flagged for a particular PR or commit in CI. --- .github/workflows/golangci-lint.yml | 30 +++++++++++++++++++++++++++++ .golangci.yaml | 13 +++++++++++++ GNUmakefile | 7 ++++++- 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/golangci-lint.yml create mode 100644 .golangci.yaml diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml new file mode 100644 index 000000000..63a39d96b --- /dev/null +++ b/.github/workflows/golangci-lint.yml @@ -0,0 +1,30 @@ +name: golangci-lint +on: + push: + branches: + - main + pull_request: +permissions: + contents: read + pull-requests: read # Remove along with only-new-issues by 2025 +jobs: + golangci: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version-file: go.mod + - name: Determine golangci-lint version + id: golangcilint + run: | + line=$(grep GOLANGCI_LINT_VERSION= GNUmakefile) + version=$(echo ${line} | cut -d = -f2) + echo "version=$version" >> "$GITHUB_OUTPUT" + - name: golangci-lint + uses: golangci/golangci-lint-action@v6 + with: + args: --whole-files # Remove along with only-new-issues by 2025 + version: ${{ steps.golangcilint.outputs.version }} + only-new-issues: true # Remove along with pull-requests: read permission by 2025 diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 000000000..76de1758d --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,13 @@ +run: + modules-download-mode: readonly + skip-dirs: + - cmd/migration-tool +linters: + enable: + - errcheck + - goimports + - gosimple + - govet + - ineffassign + - staticcheck + - unused diff --git a/GNUmakefile b/GNUmakefile index 94a07e8db..c5a5a5141 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -11,6 +11,9 @@ ACCTEST_COUNT ?= 1 GOFMT_FILES ?=$$(find . -name '*.go' |grep -v vendor) PKG_NAME =equinix +GOLANGCI_LINT_VERSION=v1.56 +GOLANGCI_LINT=go run github.com/golangci/golangci-lint/cmd/golangci-lint@${GOLANGCI_LINT_VERSION} + ifneq ($(origin TESTS_REGEXP), undefined) TESTARGS = -run='$(TESTS_REGEXP)' endif @@ -45,6 +48,8 @@ clean: ${GOCMD} clean rm -f ${BINARY} +lint: + ${GOLANGCI_LINT} run -v vet: @echo "go vet ." @@ -117,4 +122,4 @@ tfproviderdocs-check: echo "Unexpected issues found in code with bflad/tfproviderdocs."; \ exit 1) -.PHONY: test testacc build install clean fmt fmtcheck errcheck test-compile docs-lint docs-lint-fix tfproviderlint tfproviderlint-fix tfproviderdocs-check +.PHONY: test testacc build install clean lint fmt fmtcheck errcheck test-compile docs-lint docs-lint-fix tfproviderlint tfproviderlint-fix tfproviderdocs-check