From 256e22bec0dac553d35fe0ccd52ed4230ddae1d2 Mon Sep 17 00:00:00 2001 From: JJ Eaton <58495321+oldbettie@users.noreply.github.com> Date: Sat, 31 Oct 2026 21:54:40 +0700 Subject: [PATCH] feat(ci): make check-pr runs what a change needs; the affected selection is guarded make check-pr (scripts/check-pr.sh) is the default answer to docs/CI.md "What to run": from the branch's changes it runs make lint and make test on the affected packages (or make ci when a shared input changed), check-generated, deploy-check, actionlint, release-smoke and secret-scan as they apply, stops at the first failure, and prints the make ci-local RAN line naming exactly what ran. scripts/affected.sh now runs everything when sqlc's queries or generated code change (they reach the database tests through the schema, not only through imports), and fails (exit 3) when its selection does not match the diff: a changed Go file outside deploy/ in no package, a changed package not selected, or Go changes with nothing selected. A deleted package with no importers left needs nothing. check-fast and check-pr fail with it. Co-Authored-By: Claude Opus 5.5 --- .agents/skills/github-prs/SKILL.md | 5 +- .claude/skills/github-prs/SKILL.md | 5 +- AGENTS.md | 2 +- Makefile | 4 ++ docs/CI.md | 2 +- scripts/affected.sh | 64 +++++++++++++---- scripts/check-pr.sh | 111 +++++++++++++++++++++++++++++ 7 files changed, 174 insertions(+), 19 deletions(-) create mode 100755 scripts/check-pr.sh diff --git a/.agents/skills/github-prs/SKILL.md b/.agents/skills/github-prs/SKILL.md index 111cca4..c2c8752 100644 --- a/.agents/skills/github-prs/SKILL.md +++ b/.agents/skills/github-prs/SKILL.md @@ -12,7 +12,7 @@ The rules on what may merge are in `AGENTS.md` ("Verifying", "Pull requests", "E ## 1. Before every commit - **Never skip the hooks.** `make hooks` installs the pre-push hook (`make check-fast`); never push with `--no-verify` except in an emergency, and say so. -- Run what your change needs (the table is `docs/CI.md`, "What to run"): `make lint LINT_DIRS=` and `make test TEST_PKGS=` for the Go packages it can affect (`scripts/affected.sh` prints them; `make check-fast` is the seconds-long pass), `make check-generated` for sqlc, templ or mock inputs, `make check-migrations` for a migration, `make deploy-check` for `deploy/`, `make actionlint` for workflows, `make release-smoke` for a Dockerfile or what it builds, `make ci` for a change to the tooling. Tests only at the test boundaries (`AGENTS.md`, "Test boundaries"); never a test in `deploy/` or the UI. +- **Default: `make check-pr`.** It runs what the branch's changes need (the table is `docs/CI.md`, "What to run"), fails when the affected-package selection does not match the diff, and prints the `make ci-local RAN="…"` line to attest with. By hand: `make lint LINT_DIRS=` and `make test TEST_PKGS=` for the Go packages it can affect (`scripts/affected.sh` prints them; `make check-fast` is the seconds-long pass), `make check-generated` for sqlc, templ or mock inputs, `make check-migrations` for a migration, `make deploy-check` for `deploy/`, `make actionlint` for workflows, `make release-smoke` for a Dockerfile or what it builds, `make ci` for a change to the tooling. Tests only at the test boundaries (`AGENTS.md`, "Test boundaries"); never a test in `deploy/` or the UI. - A change adds no lint or format findings, warnings included. - Add the paths you changed; never `git add -A` in an old worktree. @@ -22,7 +22,8 @@ After **every** push or rebase, once you have run what the change needs: ```bash git push # pushed head, clean worktree -make ci-local RAN="make lint LINT_DIRS=./apps/web/... ; make test TEST_PKGS=./apps/web/internal/services" +make check-pr # runs what the change needs, prints the RAN line +make ci-local RAN="make lint LINT_DIRS='./apps/web/internal/services ...'; make test TEST_PKGS='...'; make secret-scan" ``` - It runs nothing and takes seconds. It refuses a dirty worktree, a detached HEAD (except `COMMIT=` of a `main` commit), a head that is not the pushed branch head, or a head that already has a failed `local-ci`. diff --git a/.claude/skills/github-prs/SKILL.md b/.claude/skills/github-prs/SKILL.md index 111cca4..c2c8752 100644 --- a/.claude/skills/github-prs/SKILL.md +++ b/.claude/skills/github-prs/SKILL.md @@ -12,7 +12,7 @@ The rules on what may merge are in `AGENTS.md` ("Verifying", "Pull requests", "E ## 1. Before every commit - **Never skip the hooks.** `make hooks` installs the pre-push hook (`make check-fast`); never push with `--no-verify` except in an emergency, and say so. -- Run what your change needs (the table is `docs/CI.md`, "What to run"): `make lint LINT_DIRS=` and `make test TEST_PKGS=` for the Go packages it can affect (`scripts/affected.sh` prints them; `make check-fast` is the seconds-long pass), `make check-generated` for sqlc, templ or mock inputs, `make check-migrations` for a migration, `make deploy-check` for `deploy/`, `make actionlint` for workflows, `make release-smoke` for a Dockerfile or what it builds, `make ci` for a change to the tooling. Tests only at the test boundaries (`AGENTS.md`, "Test boundaries"); never a test in `deploy/` or the UI. +- **Default: `make check-pr`.** It runs what the branch's changes need (the table is `docs/CI.md`, "What to run"), fails when the affected-package selection does not match the diff, and prints the `make ci-local RAN="…"` line to attest with. By hand: `make lint LINT_DIRS=` and `make test TEST_PKGS=` for the Go packages it can affect (`scripts/affected.sh` prints them; `make check-fast` is the seconds-long pass), `make check-generated` for sqlc, templ or mock inputs, `make check-migrations` for a migration, `make deploy-check` for `deploy/`, `make actionlint` for workflows, `make release-smoke` for a Dockerfile or what it builds, `make ci` for a change to the tooling. Tests only at the test boundaries (`AGENTS.md`, "Test boundaries"); never a test in `deploy/` or the UI. - A change adds no lint or format findings, warnings included. - Add the paths you changed; never `git add -A` in an old worktree. @@ -22,7 +22,8 @@ After **every** push or rebase, once you have run what the change needs: ```bash git push # pushed head, clean worktree -make ci-local RAN="make lint LINT_DIRS=./apps/web/... ; make test TEST_PKGS=./apps/web/internal/services" +make check-pr # runs what the change needs, prints the RAN line +make ci-local RAN="make lint LINT_DIRS='./apps/web/internal/services ...'; make test TEST_PKGS='...'; make secret-scan" ``` - It runs nothing and takes seconds. It refuses a dirty worktree, a detached HEAD (except `COMMIT=` of a `main` commit), a head that is not the pushed branch head, or a head that already has a failed `local-ci`. diff --git a/AGENTS.md b/AGENTS.md index f2d8525..46ab639 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -88,7 +88,7 @@ The most common defect is a change that works on the path you tested and is miss - **`make check-fast` before every push (mandatory).** It runs gofmt, golangci-lint (whose `govet` runs `go vet`'s analyzers), the test-location check and plain `go test` (no `-race`) on only the packages your changes can affect, plus the generators whose inputs changed, the `deploy/` module (lint, names and `make deploy-vars`) when it, `.env.example` or the Makefile changed, the migration order when a migration changed (see "Migrations"), and `make vulncheck` when a `go.mod`, `go.sum`, `package.json` or `pnpm-lock.yaml` changed. Changes mean everything that differs from the merge base with origin/main: commits, staged, unstaged and untracked files, mapped to packages by `scripts/affected.sh` (`make affected` prints them), so a shared input (go.mod, tooling, lint config, migrations) checks everything. It takes seconds (a docs-only change: under a second; a full run: under 10 s warm), so it fits machines where a full `make ci` is not allowed. Database tests skip unless `TEST_DATABASE_URL` is set; `make test` runs them with `-race` against Postgres. `make hooks` installs a pre-push hook that runs it from every worktree of the clone (`make hooks-remove` undoes it; `git push --no-verify` skips it once, for emergencies only). Anything that runs git or go from a hook drops git's repository env first (`git rev-parse --local-env-vars`), as the hook and `scripts/check-fast.sh` do. - **Release smoke** (`make release-smoke`; run it whenever a build input changes: a Dockerfile, or code a Dockerfile's binary is built from): builds every binary that has a Dockerfile with that Dockerfile's own build-stage `ENV` and `RUN` line (`scripts/dockerfile-builds.sh` reads them, so never copy build flags elsewhere) and fails on a dynamically linked one, runs `server migrate` and `admin migrate` on fresh databases, and boots web and admin with `APP_ENV=production` and only the config production requires until `/healthz` answers 200. It catches what tests cannot: an image that will not start on distroless (a dependency that links dynamically even with `CGO_ENABLED=0`), and a production config that refuses to boot without an optional integration. A new required production setting goes in its env list. - `make lint test` before every commit when you can run the full suite. `make ci` runs every check on the checkout as it is: build, generated-code diff (sqlc, mocks, templ), gofmt + golangci-lint, the test-location check, `go test -race` against a throwaway Postgres with per-boundary coverage floors, and the probes that prove the layering and test rules still bite (`make lint-layering lint-tests`). `make vulncheck` (also in `make ci`) fails on any known vulnerability in a dependency of any Go module or the mobile lockfile ([docs/CI.md](docs/CI.md#known-vulnerabilities)). -- **A pull request is verified locally, on its exact head, then attested (mandatory; [docs/CI.md](docs/CI.md)).** Run what the change needs ("What to run" there): for Go, `make lint test` scoped with `LINT_DIRS`/`TEST_PKGS` from `scripts/affected.sh`, plus `make check-generated`, `make check-migrations`, `make deploy-check`, `make actionlint` or `make release-smoke` for what you touched, `make ci` for a change to the tooling. Commit, push, then `make ci-local RAN=""` in the clean worktree. It runs nothing and takes seconds: it refuses a dirty, unpushed or already-failed head, posts the `local-ci` commit status and fills the PR body's "Local verification" section with your statement, which is never edited by hand. Every push or rebase needs a new attestation. Stack PRs and merge right away. +- **A pull request is verified locally, on its exact head, then attested (mandatory; [docs/CI.md](docs/CI.md)).** Run what the change needs ("What to run" there): `make check-pr` runs it and prints the statement to attest with; by hand, for Go, `make lint test` scoped with `LINT_DIRS`/`TEST_PKGS` from `scripts/affected.sh`, plus `make check-generated`, `make check-migrations`, `make deploy-check`, `make actionlint` or `make release-smoke` for what you touched, `make ci` for a change to the tooling. Commit, push, then `make ci-local RAN=""` in the clean worktree. It runs nothing and takes seconds: it refuses a dirty, unpushed or already-failed head, posts the `local-ci` commit status and fills the PR body's "Local verification" section with your statement, which is never edited by hand. Every push or rebase needs a new attestation. Stack PRs and merge right away. - **A failed head is final.** A failure is fixed forward with a new commit, never attested until green: a head attested as failed (`make ci-local FAILED=""`) is refused for good by `make ci-local`, the `Local verification` check and `make pr-merge`. A failure in what the branch changed is fixed in the PR; any other is fixed forward at once in its own PR off `main` with a regression test that fails before the fix, and the branch rebases on it. - GitHub runs no verification: on a PR only `Secret scan` and `Local verification` (which shows "Expected" until `make ci-local` claims the head), started by `make ci-local`'s body edit, and on `main` only `Deploy staging`, which ships a commit only when `make ci-local` attested it. Avoid adding workflow jobs: a new check is a make target the agent runs, listed in docs/CI.md "What to run". The PR jobs run only `main`'s code (`pull_request_target`) and read the head as data; never check out or run the head there ([docs/CI.md](docs/CI.md#trusted-code-only)). - Run the narrowest thing first: `go test ./apps/web/internal/services/...`. diff --git a/Makefile b/Makefile index bda59b2..345b398 100644 --- a/Makefile +++ b/Makefile @@ -143,6 +143,10 @@ secret-scan: ## gitleaks (pinned) on the files changed since the merge base, or vulncheck: ## Known vulnerabilities: govulncheck (pinned) on every Go module + pnpm audit on the mobile lockfile; exceptions in scripts/vulncheck/allowlist.txt (ARGS=-go-only) $(GO) run ./scripts/vulncheck $(ARGS) +.PHONY: check-pr +check-pr: ## Before make ci-local: run what this branch's changes need (docs/CI.md "What to run"), then print the RAN line (ARGS=--dry-run: only say what) + ./scripts/check-pr.sh $(ARGS) + .PHONY: check-fast check-fast: ## Before every push: gofmt, golangci-lint (incl. govet), plain tests for only what your changes vs origin/main affect (seconds) ./scripts/check-fast.sh diff --git a/docs/CI.md b/docs/CI.md index 6e3598d..0bc17bb 100644 --- a/docs/CI.md +++ b/docs/CI.md @@ -20,7 +20,7 @@ A push to `main` runs only `Deploy staging` (`deploy-staging.yml`), which deploy ### What to run -Run what your change needs on the head you attest, and nothing more. `scripts/affected.sh` (`make affected`) prints the Go dirs and packages a change can affect; `make check-fast` is the seconds-long pass over them. +Run what your change needs on the head you attest, and nothing more. **`make check-pr` is the default:** it runs the rows below that match the branch's changes (lint and `-race` tests on the affected packages, or `make ci` when a shared input changed), stops at the first failure, and prints the `make ci-local RAN="…"` line naming exactly what ran (`ARGS=--dry-run` only lists it). `scripts/affected.sh` (`make affected`) picks the Go dirs and packages: the changed packages and everything that imports them, or everything when go.mod, tooling, lint config, a migration or sqlc's config, queries or generated code changed. It fails, and so does everything built on it, when its selection does not match the diff: a changed Go file outside `deploy/` in no package, a changed package not selected, or Go changes and nothing selected. `make check-fast` is the seconds-long pre-push pass over the same packages. | Changed | Run | |---|---| diff --git a/scripts/affected.sh b/scripts/affected.sh index a497481..d230633 100755 --- a/scripts/affected.sh +++ b/scripts/affected.sh @@ -3,7 +3,15 @@ # and the LINT_DIRS/TEST_PKGS to run before make ci-local): the Go packages whose own files changed plus every # package whose code or tests import them, transitively (Go's package graph # via `go list -test -deps`). Changes to shared inputs (go.mod/go.sum, -# tooling, scripts, lint config, migrations, generator config) mean a full run. +# tooling, scripts, lint config, migrations, sqlc's config, queries and +# generated code) mean a full run: sqlc changes reach the database tests +# through the schema, not only through imports. +# +# A guard fails (exit 3) when the selection does not match the diff: a +# changed Go file outside deploy/ (its own module) that maps to no package, a +# changed package missing from the selection, or Go changes and nothing +# selected. A wrong selection would let make check-fast and make check-pr +# pass on code they never checked. # # Usage: scripts/affected.sh [] (default: merge-base with origin/main, # scripts/branches.sh) @@ -38,6 +46,12 @@ out() { if [ -n "${GITHUB_OUTPUT:-}" ]; then printf '%s=%s\n' "$1" "$2" >>"$GITHUB_OUTPUT"; fi } +# sqlc inputs and outputs: every queries:/out: path in sqlc.yaml (schemas are +# migrations). +sqlcdir=packages/database +mapfile -t sqlcpaths < <(sed -nE 's/^[[:space:]]*(queries|out):[[:space:]]*([^[:space:]#]+).*/\2/p' "$sqlcdir/sqlc.yaml" | + while read -r p; do echo "$sqlcdir/${p%/}/"; done) + # Shared inputs: anything here can change every package's result. # AFFECTED_FULL=1 forces a full run. full=false @@ -45,12 +59,17 @@ full=false [ "$full" = true ] || for f in "${changed[@]}"; do case $f in go.mod | go.sum | mise.toml | Makefile | .golangci.yml | tools/* | scripts/* | \ - */migrations/* | packages/database/sqlc.yaml) + */migrations/* | "$sqlcdir/sqlc.yaml") full=true - echo "full run: $f" >&2 - break ;; esac + for p in "${sqlcpaths[@]}"; do + if [[ $f == "$p"* ]]; then full=true; fi + done + if [ "$full" = true ]; then + echo "full run: $f" >&2 + break + fi done if [ "$full" = true ]; then @@ -64,16 +83,9 @@ if [ "$full" = true ]; then exit 0 fi -# sqlc inputs and outputs: every queries:/out: path in sqlc.yaml (schemas are -# migrations, which force a full run above). -sqlcdir=packages/database -mapfile -t sqlcpaths < <(sed -nE 's/^[[:space:]]*(queries|out):[[:space:]]*([^[:space:]#]+).*/\2/p' "$sqlcdir/sqlc.yaml" | - while read -r p; do echo "$sqlcdir/${p%/}/"; done) +# sqlc changes ran everything above; templ inputs and outputs. sqlc=false templ=false for f in "${changed[@]}"; do - for p in "${sqlcpaths[@]}"; do - if [[ $f == "$p"* ]]; then sqlc=true; fi - done case $f in *.templ | *_templ.go) templ=true ;; esac @@ -95,6 +107,7 @@ filemap=$(go list -e -f '{{$d := .Dir}}{{$p := .ImportPath}}{{range .GoFiles}}{{ dirmap=$(go list -e -f '{{.Dir}} {{.ImportPath}}' ./... | sed "s|^$PWD/||") declare -A seeds=() +unmapped=() for f in "${changed[@]}"; do p=$(awk -v f="$f" '$1 == f { print $2; exit }' <<<"$filemap") if [ -z "$p" ]; then @@ -105,7 +118,13 @@ for f in "${changed[@]}"; do d=$(dirname "$d") done fi - if [ -n "$p" ]; then seeds[$p]=1; fi + if [ -n "$p" ]; then + seeds[$p]=1 + elif [[ $f == *.go && $f != deploy/* && -e $f ]]; then + # A deleted file of a deleted package maps to nothing, and needs nothing: + # its importers changed too, or they no longer build. + unmapped+=("$f") + fi done declare -A affected=() @@ -124,6 +143,25 @@ if [ "${#seeds[@]}" -gt 0 ]; then fi pkgs=$(printf '%s\n' "${!affected[@]}" | sed '/^$/d' | sort) + +# Guard: the selection must cover the diff. +problems=() +for f in "${unmapped[@]}"; do problems+=("$f is a changed Go file in no package of $mod"); done +for p in "${!seeds[@]}"; do + [ -n "${affected[$p]:-}" ] || problems+=("$p changed but is not selected") +done +gochanged=false +for f in "${changed[@]}"; do + # Existing files: a deleted one in a live package maps to it above, and a + # deleted package with no importers left needs nothing checked. + if [[ $f == *.go && $f != deploy/* && -e $f ]]; then gochanged=true; fi +done +if [ "$gochanged" = true ] && [ -z "$pkgs" ]; then problems+=("Go files changed but no package is selected"); fi +if [ "${#problems[@]}" -gt 0 ]; then + echo "affected: the selection does not match the diff (run everything: AFFECTED_FULL=1, or make ci):" >&2 + printf ' %s\n' "${problems[@]}" >&2 + exit 3 +fi dirs=$(sed "s|^$mod|.|" <<<"$pkgs") # go:generate: packages whose directive source or -destination output changed. diff --git a/scripts/check-pr.sh b/scripts/check-pr.sh new file mode 100755 index 0000000..ff6e65c --- /dev/null +++ b/scripts/check-pr.sh @@ -0,0 +1,111 @@ +#!/usr/bin/env bash +# What a pull request needs before make ci-local (`make check-pr`): runs the +# checks docs/CI.md "What to run" lists for this checkout's changes against +# the merge base with origin/main, in order, stopping at the first failure, +# then prints the `make ci-local RAN="…"` line that says exactly what ran. +# +# Go packages make lint LINT_DIRS=, make test TEST_PKGS= +# generator inputs make check-generated +# deploy/, .env.example make deploy-check +# workflows, actions make actionlint +# package.json, pnpm-lock.yaml make vulncheck (go.mod and scripts/ run make ci, which has it) +# a Dockerfile or what it builds make release-smoke +# a shared input (scripts/affected.sh: go.mod, tooling, migrations, sqlc) +# make ci instead of lint, test and check-generated +# every change make secret-scan +# +# scripts/affected.sh picks the packages and fails when its selection does +# not match the diff. apps/mobile-app has its own pnpm checks, which this +# names but does not run. +# +# Usage: scripts/check-pr.sh [--dry-run] (CHECK_BASE= compares with another base) +set -euo pipefail +# shellcheck disable=SC2046 # one name per word +unset $(git rev-parse --local-env-vars) +cd "$(dirname "$0")/.." +. scripts/branches.sh + +dry=false +case ${1:-} in +--dry-run) dry=true ;; +"") ;; +*) echo "usage: scripts/check-pr.sh [--dry-run]" >&2 && exit 2 ;; +esac + +base_ref=${CHECK_BASE:-$(landing_ref)} +if [ -z "$base_ref" ] || ! base=$(git merge-base "$base_ref" HEAD 2>/dev/null); then + echo "check-pr: no merge base with ${base_ref:-$(deployed_names)} (git fetch origin, or set CHECK_BASE)" >&2 + exit 2 +fi + +# Committed, staged and unstaged changes, then untracked files: the checks +# run on the working tree, so they cover what it holds. +mapfile -t changed < <({ + git diff --name-only --no-renames "$base" -- + git ls-files --others --exclude-standard +} | sort -u) +if [ "${#changed[@]}" -eq 0 ]; then + echo "check-pr: nothing differs from $base_ref" + exit 0 +fi + +if ! aff=$(AFFECTED_FILES="${changed[*]}" ./scripts/affected.sh); then + echo "check-pr: scripts/affected.sh failed; nothing ran" >&2 + exit 2 +fi +full=false dirs="" packages="" sqlc=false templ=false generate="" smoke=false +while IFS='=' read -r k v; do + case $k in + full) full=$v ;; dirs) dirs=$v ;; packages) packages=$v ;; sqlc) sqlc=$v ;; + templ) templ=$v ;; generate) generate=$v ;; smoke) smoke=$v ;; + esac +done <<<"$aff" + +deploy=false workflows=false mobile=false deps=false +for f in "${changed[@]}"; do + case $f in + deploy/* | .env.example) deploy=true ;; + .github/workflows/* | .github/actions/*) workflows=true ;; + package.json | */package.json | pnpm-lock.yaml | */pnpm-lock.yaml) deps=true ;; + apps/mobile-app/* | apps/web/internal/config/routes.go) mobile=true ;; + esac +done + +plan=() +if [ "$full" = true ]; then + plan+=("make ci") # deploy-check included +else + if [ -n "$packages" ]; then + plan+=("make lint LINT_DIRS='$dirs'" "make test TEST_PKGS='$packages'") + fi + if [ "$sqlc" = true ] || [ "$templ" = true ] || [ -n "$generate" ]; then plan+=("make check-generated"); fi + if [ "$deploy" = true ]; then plan+=("make deploy-check"); fi + if [ "$deps" = true ]; then plan+=("make vulncheck"); fi +fi +if [ "$workflows" = true ]; then plan+=("make actionlint"); fi +if [ "$smoke" = true ]; then plan+=("make release-smoke"); fi +plan+=("make secret-scan") + +echo "check-pr: ${#changed[@]} changed files vs $base_ref$([ "$full" = true ] && echo ', a shared input: everything')" +printf ' %s\n' "${plan[@]}" +if [ "$mobile" = true ]; then + echo " (not run here: apps/mobile-app's pnpm checks, docs/CI.md \"What to run\")" +fi +[ "$dry" = false ] || exit 0 + +ran=() +for step in "${plan[@]}"; do + echo "== $step" + if ! bash -c "$step"; then + echo "check-pr: FAIL: $step" + echo "fix it and run make check-pr again; never attest a failing head" + exit 1 + fi + ran+=("$step") +done + +statement=$(printf '%s; ' "${ran[@]}") +statement=${statement%; } +statement=${statement//\'/} +echo "check-pr: ok. Commit and push, then attest:" +echo " make ci-local RAN=\"$statement\""