From a8872b413b1ff0004bd67ee5584e3a507a7ab988 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Deluan=20Quint=C3=A3o?= Date: Sun, 15 Nov 2026 19:23:25 -0500 Subject: [PATCH] ci: fail PRs that add new //nolint directives (#6481) --- .github/workflows/check-new-nolint.sh | 48 +++++++++++++++++++++++++++ .github/workflows/nolint-gate.yml | 31 +++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100755 .github/workflows/check-new-nolint.sh create mode 100644 .github/workflows/nolint-gate.yml diff --git a/.github/workflows/check-new-nolint.sh b/.github/workflows/check-new-nolint.sh new file mode 100755 index 000000000..198ccaaa1 --- /dev/null +++ b/.github/workflows/check-new-nolint.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +# Fails when a PR adds a //nolint directive to a Go file; a directive kept on an edited line is not new. +# Compares HEAD against $BASE_REF (default origin/master) and needs full history (fetch-depth: 0 in CI). +set -euo pipefail + +BASE_REF="${BASE_REF:-origin/master}" + +git diff --unified=0 --no-color "$BASE_REF"...HEAD -- '*.go' | awk ' +function directives(s, out) { + out = "" + while (match(s, /\/\/nolint(:[A-Za-z0-9_,-]+)?/)) { + out = out " " substr(s, RSTART, RLENGTH) + s = substr(s, RSTART + RLENGTH) + } + return out +} +/^diff --git / { header = 1; next } +/^@@/ { + split($3, pos, ",") + line = substr(pos[1], 2) + header = 0 + delete removed + next +} +header && /^\+\+\+ / { file = substr($0, 7); next } +header { next } +/^-/ { + n = split(directives($0), ds, " ") + for (i = 1; i <= n; i++) removed[ds[i]]++ + next +} +/^\+/ { + n = split(directives($0), ds, " ") + for (i = 1; i <= n; i++) { + if (removed[ds[i]] > 0) { removed[ds[i]]--; continue } + found++ + printf "%s:%d: new %s directive\n", file, line, ds[i] + if (ENVIRON["GITHUB_ACTIONS"] == "true") + printf "::error file=%s,line=%d::New %s directive. Fix the lint issue instead. If this is a real exception, a maintainer can add the allow-nolint label.\n", file, line, ds[i] + } + line++ +} +END { + if (found) { + printf "\n%d new //nolint directive(s). Fix the lint issues instead; a maintainer can approve real exceptions with the allow-nolint label.\n", found + exit 1 + } +}' diff --git a/.github/workflows/nolint-gate.yml b/.github/workflows/nolint-gate.yml new file mode 100644 index 000000000..462a26b9d --- /dev/null +++ b/.github/workflows/nolint-gate.yml @@ -0,0 +1,31 @@ +name: New nolint gate +on: + pull_request: + types: [opened, synchronize, reopened, labeled, unlabeled] + +permissions: + contents: read + +jobs: + check-new-nolint: + name: Check for new //nolint directives + runs-on: ubuntu-latest + steps: + - name: Allowed by label + if: contains(github.event.pull_request.labels.*.name, 'allow-nolint') + run: echo "::notice::The allow-nolint label is set, so new //nolint directives are allowed." + + - uses: actions/checkout@v7 + if: "!contains(github.event.pull_request.labels.*.name, 'allow-nolint')" + with: + fetch-depth: 0 + + - name: Fetch latest base branch + if: "!contains(github.event.pull_request.labels.*.name, 'allow-nolint')" + run: git fetch --no-tags origin "+refs/heads/${{ github.event.pull_request.base.ref }}:refs/remotes/origin/${{ github.event.pull_request.base.ref }}" + + - name: Fail on new //nolint directives + if: "!contains(github.event.pull_request.labels.*.name, 'allow-nolint')" + env: + BASE_REF: origin/${{ github.event.pull_request.base.ref }} + run: ./.github/workflows/check-new-nolint.sh