From 8be30161581c185866410ec1e6c5f37ecfb5d56c Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Fri, 14 Feb 2025 15:23:49 -0800 Subject: [PATCH] ci: lint only new changes (#23710) --- .github/workflows/lint.yml | 4 ++-- Makefile | 5 +++++ scripts/go-lint-all.bash | 4 ++-- scripts/go-lint-changes.bash | 37 ++++++++++++++++++++++++++++++++++++ tests/systemtests/go.mod | 4 ++-- tests/systemtests/go.sum | 4 ++-- 6 files changed, 50 insertions(+), 8 deletions(-) create mode 100755 scripts/go-lint-changes.bash diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index d1ec8eb054..61d209ced3 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -31,7 +31,7 @@ jobs: if: env.GIT_DIFF id: lint_long run: | - make lint + make lint-changes - uses: technote-space/get-diff-action@v6.1.2 if: steps.lint_long.outcome == 'skipped' with: @@ -41,7 +41,7 @@ jobs: - name: run linting (short) if: steps.lint_long.outcome == 'skipped' && env.GIT_DIFF run: | - make lint + make lint-changes env: GIT_DIFF: ${{ env.GIT_DIFF }} LINT_DIFF: 1 diff --git a/Makefile b/Makefile index ec6af44d1b..f91c3cd09b 100644 --- a/Makefile +++ b/Makefile @@ -383,6 +383,11 @@ lint: $(MAKE) lint-install @./scripts/go-lint-all.bash --timeout=15m +lint-changes: + @echo "--> Running linter" + $(MAKE) lint-install + @./scripts/go-lint-changes.bash --timeout=15m + lint-fix: @echo "--> Running linter" $(MAKE) lint-install diff --git a/scripts/go-lint-all.bash b/scripts/go-lint-all.bash index 8850c65cd1..c8bb1ea50d 100755 --- a/scripts/go-lint-all.bash +++ b/scripts/go-lint-all.bash @@ -10,7 +10,7 @@ lint_module() { shift cd "$(dirname "$root")" && echo "linting $(grep "^module" go.mod) [$(date -Iseconds -u)]" && - golangci-lint run ./... -c "${REPO_ROOT}/.golangci.yml" "$@" + golangci-lint run ./... -c "${REPO_ROOT}/.golangci.yml" "$@" --new-from-rev=HEAD~ } export -f lint_module @@ -31,7 +31,7 @@ else for f in $(dirname $(echo "$GIT_DIFF" | tr -d "'") | uniq); do echo "linting $f [$(date -Iseconds -u)]" && cd $f && - golangci-lint run ./... -c "${REPO_ROOT}/.golangci.yml" "$@" && + golangci-lint run ./... -c "${REPO_ROOT}/.golangci.yml" "$@" --new-from-rev=HEAD~ && cd $REPO_ROOT done fi \ No newline at end of file diff --git a/scripts/go-lint-changes.bash b/scripts/go-lint-changes.bash new file mode 100755 index 0000000000..c8bb1ea50d --- /dev/null +++ b/scripts/go-lint-changes.bash @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +set -e -o pipefail + +REPO_ROOT="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd )" +export REPO_ROOT + +lint_module() { + local root="$1" + shift + cd "$(dirname "$root")" && + echo "linting $(grep "^module" go.mod) [$(date -Iseconds -u)]" && + golangci-lint run ./... -c "${REPO_ROOT}/.golangci.yml" "$@" --new-from-rev=HEAD~ +} +export -f lint_module + +# 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 + if [[ -z $GIT_DIFF ]]; then + GIT_DIFF=$(git diff --name-only --diff-filter=d | grep \.go$ | grep -v \.pb\.go$) || true + fi + + if [[ -z "$GIT_DIFF" ]]; then + echo "no files to lint" + exit 0 + fi + + for f in $(dirname $(echo "$GIT_DIFF" | tr -d "'") | uniq); do + echo "linting $f [$(date -Iseconds -u)]" && + cd $f && + golangci-lint run ./... -c "${REPO_ROOT}/.golangci.yml" "$@" --new-from-rev=HEAD~ && + cd $REPO_ROOT + done +fi \ No newline at end of file diff --git a/tests/systemtests/go.mod b/tests/systemtests/go.mod index 97b05a31bb..f944dbdb3f 100644 --- a/tests/systemtests/go.mod +++ b/tests/systemtests/go.mod @@ -6,7 +6,7 @@ toolchain go1.24.0 require ( github.com/cosmos/cosmos-proto v1.0.0-beta.5 // indirect - github.com/cosmos/cosmos-sdk v0.50.11 + github.com/cosmos/cosmos-sdk v0.50.11 // indirect github.com/cosmos/gogogateway v1.2.0 // indirect github.com/cosmos/gogoproto v1.7.0 // indirect github.com/cosmos/iavl v1.2.2 // indirect @@ -61,7 +61,7 @@ require ( github.com/cockroachdb/pebble v1.1.2 // indirect github.com/cockroachdb/redact v1.1.5 // indirect github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect - github.com/cometbft/cometbft v0.38.15 // indirect + github.com/cometbft/cometbft v0.38.17 // indirect github.com/cometbft/cometbft-db v0.14.1 // indirect github.com/cometbft/cometbft/api v1.0.0-rc.1 // indirect github.com/cosmos/btcutil v1.0.5 // indirect diff --git a/tests/systemtests/go.sum b/tests/systemtests/go.sum index 4558b19018..30bc2dc18e 100644 --- a/tests/systemtests/go.sum +++ b/tests/systemtests/go.sum @@ -135,8 +135,8 @@ github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZ github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 h1:zuQyyAKVxetITBuuhv3BI9cMrmStnpT18zmgmTxunpo= github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06/go.mod h1:7nc4anLGjupUW/PeY5qiNYsdNXj7zopG+eqsS7To5IQ= github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= -github.com/cometbft/cometbft v0.38.15 h1:5veFd8k1uXM27PBg9sMO3hAfRJ3vbh4OmmLf6cVrqXg= -github.com/cometbft/cometbft v0.38.15/go.mod h1:+wh6ap6xctVG+JOHwbl8pPKZ0GeqdPYqISu7F4b43cQ= +github.com/cometbft/cometbft v0.38.17 h1:FkrQNbAjiFqXydeAO81FUzriL4Bz0abYxN/eOHrQGOk= +github.com/cometbft/cometbft v0.38.17/go.mod h1:5l0SkgeLRXi6bBfQuevXjKqML1jjfJJlvI1Ulp02/o4= github.com/cometbft/cometbft-db v0.14.1 h1:SxoamPghqICBAIcGpleHbmoPqy+crij/++eZz3DlerQ= github.com/cometbft/cometbft-db v0.14.1/go.mod h1:KHP1YghilyGV/xjD5DP3+2hyigWx0WTp9X+0Gnx0RxQ= github.com/cometbft/cometbft/api v1.0.0-rc.1 h1:GtdXwDGlqwHYs16A4egjwylfYOMYyEacLBrs3Zvpt7g=