beads/scripts/ci/pr-policy.sh
ecuthiell 46f182aa26
fix(ci): require direct workflow build tags (#5679)
* fix(ci): require direct workflow build tags

Parse workflow run scalars structurally and require recognizable direct Go commands to declare the literal gms_pure_go tag. Keep shell and Make checks lightweight and preserve their existing conventions.

Fixes #5527

Agent-Signature: codex-gpt-5-unknown-reasoning on behalf of Ewen Cuthiell
Signed-off-by: Ewen Cuthiell <ewencuthiell@gmail.com>

* fix(ci): recognize double-dash Go build tag flags

Address PR #5679 review 5191180453. Preserve last-tag precedence and test argument boundaries, including --args.

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell

* fix(ci): bazel-sync the new checker, restore its opt-out and the Bazel census

Maintainer review fixes for PR #5679, adoption iterations 1 and 2. Iteration
1's synthesis is `request_changes` on one major plus three minors; all four
are addressed. Iteration 2 approves with one minor, also addressed.

Major (Release Safety): the PR adds two production Go packages with no
BUILD.bazel, so `bazel build //...` could not see them and the repo's own
"BUILD files in sync" step fails on the resulting gazelle drift. Added the two
generated files via `make bazel-sync`; yaml.v3 is already a direct go.mod
requirement, so no module change is owed.

Minor (Release Safety): the new checker runs in the required pr-policy lane with
no way out, while the shell checker it partly replaces honors
`# build-tags: allow-bare` in a file's first five lines (check-build-tags.sh, and
engdocs/ICU-POLICY.md documents that window). CheckDir now honors the same
marker in the same window -- one convention, not two -- pinned by a table that
includes the fifth line and the sixth, so the two checkers cannot drift apart on
where the window ends.

Minor (Change Impact): dropping the workflow glob from `candidates` moved
workflows to the Go checker but also, silently, out of the `bazel_users` census --
and the Go checker does no Bazel handling at all, so .bazelrc could have lost its
tag with this gate still green. Workflows now feed the census through their own
`git ls-files` list and a separate loop, deliberately not rejoining the `go`-verb
scan. This is not hypothetical: `.github/workflows/bazel.yml` matches the Bazel
invocation regex, so it is exactly the file the census was losing.

Minor (Test Evidence): `Line` is the operator-facing half of a violation -- it is
what the error message points at -- and nothing asserted it, so reporting every
violation at line 1 would have been invisible. Added a `wantLine` column, asserted
on the multi-line "source is not workflow authority" case (line 2), and asserted
`Path`/`Job` where CheckDir fills them in.

One deviation, recorded: the finding also asked for `Line` on the existing
"continuation" case. That case is tagged, so it reports no violation and has no
Line to assert. Added a continuing-but-violating case instead, which makes the
same attribution observable -- a logical line spanning three physical ones is
reported at the first.

Not included: `make bazel-sync` also wants `//internal/debug` in
internal/utils/BUILD.bazel. That is base drift, not PR content -- absent at this
branch's rebase base but already present on origin/main, 24 commits ahead -- so it
heals on the next base refresh, and carrying it would duplicate main's own fix on
a file this PR never touches.

Minor (Test Evidence, iteration 2): the census half of the Change Impact fix
was right in code but pinned by nothing -- turning the workflow `bazel_users`
append into a no-op, or feeding the census-only list into the `go`-verb loop,
left every check_build_tags_test.go case green. Two cases on the existing
fixture harness now pin both directions: a workflow that runs `bazel build`
with an untagged .bazelrc fails and is named as the Bazel user, and a workflow
`run: go test ./...` passes here because scripts/checkworkflowtags owns it.
Each fails under its mutant, as does one more apiece (re-adding the workflow
glob to `candidates`; dropping `*.yml` from the census list), while the three
existing cases stay green under all four.

Validation: ./scripts/workflowtags passes (all four CheckDir tests and the full
CheckRun table), `go vet ./scripts/...` clean, `bash -n` clean, and both checkers
run green against the repository itself -- check-build-tags.sh reports 90 files
scanned all clear, and checkworkflowtags reports all direct commands clear.
For iteration 2, `go test ./scripts/` passes, and on a merge with current main
`bazel test //scripts:scripts_test` passes too; on this branch's own base that
target cannot build, because of the internal/utils drift noted above.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(ci): pin main.yml's bd-cmd-test build to the literal gms_pure_go tag

#6940 pinned main.yml build-artifacts' non-race bd-cmd-test build as
`go test -tags "$BEADS_BUILD_TAGS" -c -o artifacts/bd-cmd-test ./cmd/bd`.
This PR requires a literal gms_pure_go tag on direct workflow Go commands
and rewrites that line to `go test -tags gms_pure_go -c ...`, so on
current main TestBazelDoltServerTiersMirrorPRRisk fails in both
`go test ./scripts` and //scripts:scripts_test. Restoring the pinned
spelling is not an option: checkworkflowtags rejects it ("direct `go test`
must declare literal gms_pure_go in -tags").

Update the pin to the literal spelling. It still pins what #6940 cares
about, a non-race bd-cmd-test built from ./cmd/bd: adding -race to that
line still fails the test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Signed-off-by: Ewen Cuthiell <ewencuthiell@gmail.com>
Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 01:24:03 -07:00

119 lines
4 KiB
Bash
Executable file

#!/usr/bin/env bash
# Required PR repository policy checks.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
# shellcheck source=../.buildflags
source "$REPO_ROOT/.buildflags"
# shellcheck source=lib/timing.sh
source "$REPO_ROOT/scripts/ci/lib/timing.sh"
cd "$REPO_ROOT"
tmpdir=""
cleanup() {
if [[ -n "$tmpdir" ]]; then
rm -rf "$tmpdir"
fi
}
trap cleanup EXIT
check_no_beads_jsonl_changes() {
local base_ref=""
if [[ "${CI_SKIP_BEADS_CHANGE_CHECK:-0}" == "1" ]]; then
printf 'Skipping .beads/issues.jsonl guard because CI_SKIP_BEADS_CHANGE_CHECK=1\n'
return 0
fi
if [[ -n "${CI_BEADS_DIFF_BASE:-}" ]]; then
base_ref="$CI_BEADS_DIFF_BASE"
elif [[ -n "${GITHUB_BASE_REF:-}" ]]; then
base_ref="origin/${GITHUB_BASE_REF}"
elif git rev-parse --verify --quiet origin/main >/dev/null; then
base_ref="origin/main"
printf 'No PR base ref found; checking .beads/issues.jsonl against origin/main.\n'
else
printf 'No diff base available; skipping .beads/issues.jsonl guard.\n'
return 0
fi
if ! git rev-parse --verify --quiet "$base_ref" >/dev/null; then
printf 'Diff base %s is unavailable; skipping .beads/issues.jsonl guard.\n' "$base_ref"
return 0
fi
if git diff --name-only "$base_ref"...HEAD | grep -q '^\.beads/issues\.jsonl$'; then
cat >&2 <<'EOF'
This change includes .beads/issues.jsonl.
That file is the project's issue database and should not be modified in PRs.
To fix:
git checkout origin/main -- .beads/issues.jsonl
git commit --amend
EOF
return 1
fi
printf 'No .beads/issues.jsonl changes detected.\n'
}
check_workapi_frontend_boundary() {
# The import half of this boundary is the workapi-frontend-boundary
# depguard rule in .golangci.yml. This is the symbol half depguard cannot
# express: internal/workapi legitimately imports internal/config (for
# GetCustomTypesFromYAML, a workspace-scoped read), but process-local
# reads derived from a client's cwd or environment are meaningless in a
# long-lived server and must not leak into the shared contract.
local banned='config\.GetDirectoryLabels|os\.Getenv'
if [[ ! -d internal/workapi ]]; then
printf 'internal/workapi does not exist; skipping frontend-boundary check.\n'
return 0
fi
if grep -rnE "$banned" internal/workapi --include='*.go'; then
cat >&2 <<'EOF'
internal/workapi must not read cwd- or environment-derived state.
Those reads describe the client's process, which a server process does not
share. Resolve them in the frontend and pass the result in as a parameter.
See internal/workapi/doc.go.
EOF
return 1
fi
printf 'No banned cwd/env accessors in internal/workapi.\n'
}
build_docs_binary() {
tmpdir="$(mktemp -d)"
local build
build="$(git rev-parse --short HEAD)"
env CGO_ENABLED=0 go build \
-ldflags="-X main.Build=${build}" \
-o "$tmpdir/bd" \
./cmd/bd
}
ci_time "check build-tag policy" -- ./scripts/check-build-tags.sh
ci_time "check workflow build-tag declarations" -- \
go run -tags=gms_pure_go ./scripts/checkworkflowtags
ci_time "check go install guidance" -- ./scripts/check-go-install-guidance.sh
ci_time "check version consistency" -- ./scripts/check-versions.sh
ci_time "build bd for docs checks" -- build_docs_binary
ci_time "check doc flags" -- ./scripts/check-doc-flags.sh "$tmpdir/bd"
ci_time "check doc freshness" -- ./scripts/check-doc-freshness.sh
ci_time "check testing.Short boundaries" -- ./scripts/check-testing-short.sh
ci_time "check workapi frontend boundary" -- check_workapi_frontend_boundary
ci_time "check no .beads/issues.jsonl changes" -- check_no_beads_jsonl_changes
# The OpenAPI drift gate belongs to a REQUIRED PR job. The Main workflow is
# perpetually concurrency-cancelled, so a gate that only ran there would be
# unenforced in practice.
ci_time "check openapi spec gate" -- make api-check