From 3bbc3700b0f20c6d3da9751225b6f51a30f03d09 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 20 Jul 2023 15:39:23 +0200 Subject: [PATCH] ci: speed up lint job (#17071) --- .github/PULL_REQUEST_TEMPLATE.md | 1 + .github/workflows/lint.yml | 20 ++++++++++++++++++-- .golangci.yml | 31 ++++++++++++++++--------------- Makefile | 9 ++++++--- scripts/go-lint-all.bash | 28 +++++++++++++++++++++++++--- 5 files changed, 66 insertions(+), 23 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index ef76a1033ad3..56227fdea67d 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -31,6 +31,7 @@ I have... * [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) * [ ] updated the relevant documentation or specification * [ ] reviewed "Files changed" and left comments if necessary +* [ ] run `make lint` and `make test` * [ ] confirmed all CI checks have passed ### Reviewers Checklist diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index c035bdb2937e..a0cae0ae3692 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -20,6 +20,19 @@ jobs: check-latest: true - uses: technote-space/get-diff-action@v6.1.2 id: git_diff + with: + PATTERNS: | + Makefile + **/Makefile + .golangci.yml + - name: run linting (long) + if: env.GIT_DIFF + id: lint_long + run: | + make lint + - uses: technote-space/get-diff-action@v6.1.2 + if: steps.lint_long.outcome == 'skipped' + id: git_diff_all with: PATTERNS: | **/*.go @@ -27,7 +40,10 @@ jobs: go.sum **/go.mod **/go.sum - - name: run linting - if: env.GIT_DIFF + - name: run linting (short) + if: steps.lint_long.outcome == 'skipped' && env.GIT_DIFF run: | make lint + env: + GIT_DIFF: ${{ env.GIT_DIFF }} + LINT_DIFF: 1 diff --git a/.golangci.yml b/.golangci.yml index 899b55f3cc7c..132d4a2ea950 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,11 +1,14 @@ run: tests: true - timeout: 10m + timeout: 15m sort-results: true allow-parallel-runners: true exclude-dir: testutil/testdata skip-files: - server/grpc/gogoreflection/fix_registration.go + - ".*\\.pb\\.go$" + - ".*\\.pb\\.gw\\.\\.go$" + - ".*\\.pulsar\\.go$" linters: disable-all: true @@ -34,40 +37,39 @@ linters: issues: exclude-rules: - - text: 'Use of weak random number generator' + - text: "Use of weak random number generator" linters: - gosec - - text: 'ST1003:' + - text: "ST1003:" linters: - stylecheck # FIXME: Disabled until golangci-lint updates stylecheck with this fix: # https://github.com/dominikh/go-tools/issues/389 - - text: 'ST1016:' + - text: "ST1016:" linters: - stylecheck - - path: 'migrations' - text: 'SA1019:' + - path: "migrations" + text: "SA1019:" linters: - staticcheck - - text: 'SA1019: codec.NewAminoCodec is deprecated' # TODO remove once migration path is set out + - text: "SA1019: codec.NewAminoCodec is deprecated" # TODO remove once migration path is set out linters: - staticcheck - - text: 'SA1019: legacybech32.MustMarshalPubKey' # TODO remove once ready to remove from the sdk + - text: "SA1019: legacybech32.MustMarshalPubKey" # TODO remove once ready to remove from the sdk linters: - staticcheck - - text: 'SA1019: legacybech32.MarshalPubKey' # TODO remove once ready to remove from the sdk + - text: "SA1019: legacybech32.MarshalPubKey" # TODO remove once ready to remove from the sdk linters: - staticcheck - - text: 'SA1019: legacybech32.UnmarshalPubKey' # TODO remove once ready to remove from the sdk + - text: "SA1019: legacybech32.UnmarshalPubKey" # TODO remove once ready to remove from the sdk linters: - staticcheck - - text: 'SA1019: params.SendEnabled is deprecated' # TODO remove once ready to remove from the sdk + - text: "SA1019: params.SendEnabled is deprecated" # TODO remove once ready to remove from the sdk linters: - staticcheck - - text: 'leading space' + - text: "leading space" linters: - nolintlint - max-issues-per-linter: 10000 max-same-issues: 10000 @@ -137,8 +139,7 @@ linters-settings: require-explanation: true require-specific: false gosimple: - checks: ['all'] - + checks: ["all"] gocritic: disabled-checks: - regexpMust diff --git a/Makefile b/Makefile index 1507de861d85..f1f12ef2ed05 100644 --- a/Makefile +++ b/Makefile @@ -377,17 +377,20 @@ benchmark: ### Linting ### ############################################################################### -golangci_lint_cmd=golangci-lint golangci_version=v1.53.3 +lint-install: + @echo "--> Installing golangci-lint $(golangci_version)" + @go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(golangci_version) + lint: @echo "--> Running linter" - @go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(golangci_version) + $(MAKE) lint-install @./scripts/go-lint-all.bash --timeout=15m lint-fix: @echo "--> Running linter" - @go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(golangci_version) + $(MAKE) lint-install @./scripts/go-lint-all.bash --fix .PHONY: lint lint-fix diff --git a/scripts/go-lint-all.bash b/scripts/go-lint-all.bash index b99606844d79..e20822bec344 100755 --- a/scripts/go-lint-all.bash +++ b/scripts/go-lint-all.bash @@ -1,6 +1,6 @@ #!/usr/bin/env bash -set -eu -o pipefail +set -e -o pipefail REPO_ROOT="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd )" export REPO_ROOT @@ -14,5 +14,27 @@ lint_module() { } export -f lint_module -find "${REPO_ROOT}" -type f -name go.mod -print0 | - xargs -0 -I{} bash -c 'lint_module "$@"' _ {} "$@" # Prepend go.mod file before command-line args. +lint_files() { + local go_files="$(git diff --name-only --diff-filter=d | grep \.go$ | grep -v \.pb\.go$)" + if [[ -z "$go_files" && $GIT_DIFF ]]; then + go_files="$(echo $GIT_DIFF | grep \.go$ | grep -v \.pb\.go$)" + elif [[ -z "$go_files" ]]; then + echo "no files to lint" + exit 0 + fi + + for f in $go_files; do + local dir_name="$(dirname $f)" + echo "linting ${dir_name} [$(date -Iseconds -u)]" + golangci-lint run "${dir_name}" -c "${REPO_ROOT}/.golangci.yml" "$@" + done +} +export -f lint_files + +# if LINT_DIFF env is set, only lint the files in the current commit otherwise lint all files +if [[ -z "${LINT_DIFF:-}" ]]; then + find "${REPO_ROOT}" -type f -name go.mod -print0 | + xargs -0 -I{} bash -c 'lint_module "$@"' _ {} "$@" +else + lint_files "$@" +fi \ No newline at end of file