Skip to content

Commit

Permalink
ci: speed up lint job (backport #17071) (#17076)
Browse files Browse the repository at this point in the history
Co-authored-by: Julien Robert <julien@rbrt.fr>
  • Loading branch information
mergify[bot] and julienrbrt authored Jul 20, 2023
1 parent 75501a1 commit ab140e5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,30 @@ 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
go.mod
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
7 changes: 4 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -54,7 +57,6 @@ issues:
- text: "leading space"
linters:
- nolintlint

max-issues-per-linter: 10000
max-same-issues: 10000

Expand Down Expand Up @@ -121,7 +123,6 @@ linters-settings:
require-specific: false
gosimple:
checks: ["all"]

gocritic:
disabled-checks:
- regexpMust
Expand Down
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -379,17 +379,20 @@ benchmark:
### Linting ###
###############################################################################

golangci_lint_cmd=golangci-lint
golangci_version=v1.51.2

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
Expand Down
28 changes: 25 additions & 3 deletions scripts/go-lint-all.bash
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

0 comments on commit ab140e5

Please sign in to comment.