From aaad96ef866319e3d86dcef79e05c7c7b419766b Mon Sep 17 00:00:00 2001 From: lyingbug Date: Thu, 13 Aug 2026 19:26:30 +0800 Subject: [PATCH] ci: add golangci-lint workflow gating new PR code (#2696) * ci: add golangci-lint workflow gating new PR code The repo already ships a .golangci.yml and a `make lint` target, but linting never ran in CI. A full-tree run currently reports ~5.4k pre-existing findings, so blocking on it is not viable; instead the workflow enables `only-new-issues` so a PR is judged solely on the code it introduces. Runs on pull_request only: `only-new-issues` relies on the PR diff and would degrade into a full-tree run (and fail) on push. The matrix covers all three Go modules, since a single run cannot span separate go.mod trees; each resolves the shared root .golangci.yml via upward config discovery. * fix: migrate .golangci.yml to the v2 schema The file declared `version: 2` while still using v1 keys. `golangci-lint run` tolerates this, but `config verify` (which golangci-lint-action runs before linting) rejects it: `version` must be a string, and `linters-settings` is not a valid top-level key in v2. Quoting the version and moving the block under `linters.settings` also restores the lll settings, which were silently ignored until now -- applying tab-width 4 raises the lll count in client/ from 32 to 42. * ci: warm the lint cache on main so pull requests stay fast Caches written by a pull request run are scoped to that pull request, and the gating job only runs on `pull_request`, so no shared cache was ever produced. Every pull request therefore recompiled the whole package graph: the first run on this branch spent 5m39s of its 6m09s with both the Go build cache and the golangci-lint analysis cache reporting a miss. Add a job on main that lints purely for the cache side effect, with `--issues-exit-code=0` so the existing backlog cannot turn the default branch red. Manual runs route here as well, giving a full backlog report without the misleading red status a gating run would produce. * ci: split the lint cache warmer into its own workflow A job excluded by a job-level `if` is still reported as a check on every pull request, and because the matrix is never expanded for a skipped job, GitHub renders the name literally as "warm lint cache (${{ matrix.module }})". Separating the triggers into two files removes the stray check instead of papering over the name. Cache keys are derived from the working directory and go.sum rather than the workflow, so the caches written on main are still the ones pull requests restore. --- .github/workflows/go-lint-cache.yml | 59 +++++++++++++++++++++++++++++ .github/workflows/go-lint.yml | 53 ++++++++++++++++++++++++++ .golangci.yml | 10 ++--- 3 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/go-lint-cache.yml create mode 100644 .github/workflows/go-lint.yml diff --git a/.github/workflows/go-lint-cache.yml b/.github/workflows/go-lint-cache.yml new file mode 100644 index 00000000..780f62d4 --- /dev/null +++ b/.github/workflows/go-lint-cache.yml @@ -0,0 +1,59 @@ +name: Go Lint Cache + +# Action caches written by a pull request run are scoped to that pull request, +# so only runs on the default branch produce caches every pull request can +# restore. Without this workflow each pull request would recompile the whole +# package graph from scratch (~6 minutes for the root module, versus ~40s warm). +# +# This lives apart from go-lint.yml rather than as a conditional job inside it: +# a job skipped by `if` still surfaces as a check on every pull request, and +# GitHub renders its name with the matrix expression unexpanded. + +on: + push: + branches: [main] + paths: + - "**/*.go" + - "**/go.mod" + - "**/go.sum" + - ".golangci.yml" + - ".github/workflows/go-lint-cache.yml" + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: read + +env: + GO_VERSION: "1.26" + GOLANGCI_LINT_VERSION: "v2.12.2" + GOPROXY: https://proxy.golang.org,direct + +jobs: + warm-cache: + name: warm lint cache (${{ matrix.module }}) + runs-on: ubuntu-latest + timeout-minutes: 30 + strategy: + fail-fast: false + matrix: + module: [".", "cli", "client"] + steps: + - uses: actions/checkout@v6 + + - uses: actions/setup-go@v6 + with: + go-version: ${{ env.GO_VERSION }} + cache-dependency-path: ${{ matrix.module }}/go.sum + + - uses: golangci/golangci-lint-action@v9 + with: + version: ${{ env.GOLANGCI_LINT_VERSION }} + working-directory: ${{ matrix.module }} + # This workflow exists for its cache side effect only; the backlog of + # existing findings must not turn main red. The run still prints the + # full report, which makes a manual dispatch a useful backlog view. + args: --issues-exit-code=0 diff --git a/.github/workflows/go-lint.yml b/.github/workflows/go-lint.yml new file mode 100644 index 00000000..68ae1527 --- /dev/null +++ b/.github/workflows/go-lint.yml @@ -0,0 +1,53 @@ +name: Go Lint + +# The repository carries a large backlog of pre-existing golangci-lint findings, +# so pull requests are only judged on the code they introduce (`only-new-issues`). +# That filter needs a pull request diff, which is why this workflow has no other +# trigger; the companion go-lint-cache.yml keeps the caches this job restores +# from warm on main. + +on: + pull_request: + paths: + - "**/*.go" + - "**/go.mod" + - "**/go.sum" + - ".golangci.yml" + - ".github/workflows/go-lint.yml" + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: read + +env: + GO_VERSION: "1.26" + GOLANGCI_LINT_VERSION: "v2.12.2" + GOPROXY: https://proxy.golang.org,direct + +jobs: + golangci: + name: golangci-lint (${{ matrix.module }}) + runs-on: ubuntu-latest + timeout-minutes: 20 + strategy: + fail-fast: false + matrix: + module: [".", "cli", "client"] + steps: + - uses: actions/checkout@v6 + with: + fetch-depth: 0 + + - uses: actions/setup-go@v6 + with: + go-version: ${{ env.GO_VERSION }} + cache-dependency-path: ${{ matrix.module }}/go.sum + + - uses: golangci/golangci-lint-action@v9 + with: + version: ${{ env.GOLANGCI_LINT_VERSION }} + working-directory: ${{ matrix.module }} + only-new-issues: true diff --git a/.golangci.yml b/.golangci.yml index cf49aee7..63b35f01 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,13 +1,13 @@ -version: 2 -linters-settings: - lll: - line-length: 120 - tab-width: 4 +version: "2" linters: enable: - lll # 控制行宽 - govet - revive + settings: + lll: + line-length: 120 + tab-width: 4 formatters: enable: - gofmt