Merge #307 (post-merge-review path check) into train 2

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-20 15:01:45 -08:00
commit 274a3fe921
5 files changed
+417

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
# What a PR labelled "post-merge-review" may touch.
#
# A labelled PR passes the "post-merge-review paths" check only if EVERY
# changed file (both names of a rename) matches an allow line and no "!"
# line. Anything not allowed is denied: new files start denied. A PR that
# fails keeps the full review before merge. Without the label the check
# passes and nothing changes.
#
# One bash glob per line, matched against the repo-relative path; "*" also
# matches "/". A line starting with "!" denies, and wins over any allow line.
# Blank lines and text after "#" are ignored. The check reads this file from
# the base branch, so a PR cannot loosen its own check; a missing or empty
# file fails the check.
# ---- Allowed ----
# Docs.
docs/*
*.md
# Tests: added or modified, never deleted or moved (the check enforces
# that); a test file in a denied directory stays denied.
*_test.go
# Status, metrics and observation files, by name.
core/host/master/observation_service.go
core/host/master/observation_snapshot.go
core/host/volume/memory_status.go
core/ops/observation.go
core/ops/observation_bundle.go
core/ops/observation_dashboard.go
core/ops/observation_report.go
core/ops/volume_status_artifacts.go
core/ops/volume_status_report.go
core/ops/volume_status_report_collector.go
core/ops/volume_status_summary.go
# ---- Denied even if allowed above ----
# The check itself and CI.
!.github/*
# The test gates: the testops engine, its CLI and the gate definitions
# (their docs stay allowed).
!internal/testops/*.go
!internal/testops/*.json
!cmd/sw-testops/*
# Data path and replication.
!core/storage/*
!core/frontend/*
!core/transport/*
!core/replication/*
!core/recovery/*
!core/engine/*
!core/adapter/*
# Authority: lease, epoch, fencing, move.
!core/authority/*
!core/host/master/workspace_*
!core/host/volume/workspace_*
!cmd/blockvolume/workspace_lease*
# Wire and persisted formats.
!*.proto
!*.pb.go
!core/host/master/journal_isolation*
!core/lifecycle/store*
!cmd/sparrow/persist*
# GC and delete paths.
!cmd/sw-block/chunk_gc*
!cmd/sw-block/chunk_volume_delete*
!core/lifecycle/deletion_reservations*
!core/ops/delete_safety_contract*
!*/gc[_.]*
!*_gc[_.]*
# Lease, epoch, claim, admission, GC, delete or format code (tests included)
# wherever it lives, by file name.
!*/lease*.go
!*_lease*.go
!*epoch*.go
!*fenc*.go
!*claim*.go
!*admission*.go
!*/gc_*.go
!*_gc.go
!*_gc_*.go
!*delet*.go
!*purge*.go
!*persist*.go
!*journal*.go
!*format*.go
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
# Usage: post-merge-review-changed.sh BASE HEAD
#
# Writes what a PR changes (BASE...HEAD) as NUL-separated STATUS, PATH pairs,
# unquoted, so any byte in a name reaches the check as itself. --no-renames
# reports a move as a delete of the old name and an add of the new one.
set -euo pipefail
base=${1:?usage: post-merge-review-changed.sh BASE HEAD}
head=${2:?usage: post-merge-review-changed.sh BASE HEAD}
exec git diff -z --name-status --no-renames "$base...$head"
@@ -0,0 +1,186 @@
#!/usr/bin/env bash
# Self-test of the post-merge-review check: the matcher against the checked-in
# allow list, the real Git diff it consumes, and the workflow's triggers.
set -euo pipefail
shopt -s inherit_errexit
here=$(cd "$(dirname "$0")" && pwd)
check=$here/post-merge-review-check.sh
changed=$here/post-merge-review-changed.sh
allow=$here/../post-merge-review-allow.txt
workflow=$here/../workflows/post-merge-review.yml
fail=0
expect_changes() { # expect_changes WANT_EXIT NAME [STATUS PATH]...
local want=$1 name=$2
shift 2
local got=0
if (($#)); then printf '%s\0' "$@"; fi | bash "$check" "$allow" >/dev/null || got=$?
if [[ $got != "$want" ]]; then
echo "FAIL $name: exit $got, want $want"
fail=1
fi
}
expect() { # expect WANT_EXIT NAME PATH... : each path modified
local want=$1 name=$2 p
shift 2
local args=()
for p in "$@"; do args+=(M "$p"); done
expect_changes "$want" "$name" ${args[@]+"${args[@]}"}
}
# Allowed.
expect 0 "docs" docs/README.md README.md cmd/sw-block/NOTES.md
expect 0 "testops docs" internal/testops/registry-overview.md
expect 0 "test outside denied dirs" core/ops/cleanup_projection_test.go cmd/blockmaster/topology_test.go
expect 0 "named status files" core/host/master/observation_service.go core/host/volume/memory_status.go
expect 0 "release is not lease" cmd/sw-block/release_test.go
expect 0 "names with a space, tab or newline" "docs/a b.md" $'docs/a\tb.md' $'docs/a\nb.md'
expect 0 "no paths"
expect_changes 0 "test added" A cmd/foo/new_test.go
expect_changes 0 "doc deleted" D docs/old.md
# Denied by default: not on the allow list.
expect 1 "new file anywhere" core/newpkg/decide.go
expect 1 "decision code by name" core/host/master/product_loop.go core/lifecycle/planner.go
expect 1 "go.mod" go.mod
expect 1 "go.sum" go.sum
expect 1 "status-like but not named" core/ops/operator_status_controller.go
expect 1 "status server: registers a mutating endpoint" core/host/volume/status_server.go
# Denied explicitly, even where an allow line matches.
expect 1 "storage" core/storage/walstore.go
expect 1 "unicode name in storage" $'core/storage/\xc3\xa9_test.go'
expect 1 "durable test file" core/frontend/durable/provider_chunk_detach_abort_test.go
expect 1 "doc inside a denied dir" core/storage/NOTES.md
expect 1 "authority test" core/authority/lease_stats_test.go
expect 1 "master workspace test" core/host/master/workspace_binding_review_test.go
expect 1 "volume workspace runtime" core/host/volume/workspace_runtime.go
expect 1 "volume workspace runtime test" core/host/volume/workspace_runtime_test.go
expect 1 "proto" core/rpc/proto/control.proto
expect 1 "lease test by name" cmd/foo/lease_table_test.go
expect 1 "gc test by name" cmd/sw-block/chunk_gc_test.go
expect 1 "the check itself" .github/post-merge-review-allow.txt
expect 1 "a workflow" .github/workflows/test.yml
expect 1 "PR template under .github" .github/pull_request_template.md
expect 1 "mixed: allowed plus denied" docs/README.md core/storage/walstore.go
expect 1 "mixed: allowed plus unlisted" docs/README.md go.mod
# Persisted-format, GC and delete paths stay denied, tests included.
expect 1 "lifecycle store test" core/lifecycle/store_test.go
expect 1 "journal isolation test" core/host/master/journal_isolation_test.go
expect 1 "sparrow persist test" cmd/sparrow/persist_sidecar_test.go
expect 1 "chunk volume delete test" cmd/sw-block/chunk_volume_delete_test.go
expect 1 "deletion reservations test" core/lifecycle/deletion_reservations_test.go
expect 1 "delete safety contract test" core/ops/delete_safety_contract_test.go
expect 1 "gc doc" docs/spec/cfg/RED_gc_ageonly.cfg
# The test gates: engine, CLI and gate definitions (and their tests).
expect 1 "testops engine" internal/testops/control.go
expect 1 "testops engine test" internal/testops/control_test.go
expect 1 "testops gate definition" internal/testops/registry/iscsi-p2-os-smoke.json
expect 1 "testops CLI" cmd/sw-testops/main.go
# A test may be added or modified, never deleted or replaced.
expect_changes 1 "test deleted" D cmd/foo/guard_test.go
expect_changes 1 "test type changed" T cmd/foo/guard_test.go
expect_changes 1 "test moved (delete plus add)" D cmd/foo/guard_test.go A cmd/foo/renamed_test.go
# A malformed change list or a missing or empty allow list fails closed.
malformed() { # malformed NAME PRINTF-FORMAT
local got=0
printf "$2" | bash "$check" "$allow" >/dev/null 2>&1 || got=$?
[[ $got == 2 ]] || { echo "FAIL malformed list, $1: exit $got, want 2"; fail=1; }
}
malformed "status without a path" 'M\0'
malformed "bare unterminated status" 'M'
malformed "trailing partial status after a valid pair" 'M\0docs/x.md\0D'
malformed "unterminated path" 'M\0docs/x.md'
malformed "unknown status" 'BOGUS\0docs/x.md\0'
malformed "rename status (renames are off)" 'R100\0docs/x.md\0'
malformed "empty status" '\0docs/x.md\0'
malformed "empty path" 'M\0\0'
empty=$(mktemp)
for bad in "$empty" "/nonexistent/post-merge-review-allow.txt"; do
got=0
printf 'M\0docs/README.md\0' | bash "$check" "$bad" >/dev/null 2>&1 || got=$?
[[ $got == 2 ]] || { echo "FAIL allow list $bad: exit $got, want 2"; fail=1; }
done
rm -f "$empty"
# Real Git: names that git quotes in its default output, a rename out of a
# denied directory, a retarget onto a base that differs, and test deletes.
repo=$(mktemp -d)
trap 'rm -rf "$repo" "${changes:-}"' EXIT
g() { git -C "$repo" -c user.name=t -c user.email=t@t -c core.quotePath=true -c core.protectNTFS=false "$@"; }
blob=
commit() { # commit PARENT PATH... : PARENT's tree plus PATHs (-PATH removes)
local parent=$1
shift
g read-tree "$parent"
local p
for p in "$@"; do
if [[ $p == -* ]]; then
g update-index --force-remove -- "${p#-}"
else
g update-index --add --cacheinfo "100644,$blob,$p"
fi
done
g commit-tree "$(g write-tree)" -p "$parent" -m c
}
changes=$(mktemp)
expect_diff() { # expect_diff WANT_EXIT NAME BASE HEAD
local got=0
if ! (cd "$repo" && bash "$changed" "$3" "$4") >"$changes"; then
echo "FAIL $2: listing the changes failed"
fail=1
return
fi
bash "$check" "$allow" <"$changes" >/dev/null || got=$?
if [[ $got != "$1" ]]; then
echo "FAIL $2: exit $got, want $1"
fail=1
fi
}
g init -q
blob=$(printf x | g hash-object -w --stdin)
g read-tree --empty
root=$(g commit-tree "$(g write-tree)" -m root)
main=$(commit "$root" core/storage/old.go docs/x.md cmd/foo/guard_test.go)
unicode=$(commit "$main" $'core/storage/\xc3\xa9_test.go')
oddnames=$(commit "$main" $'docs/tab\there.md' $'docs/new\nline.md' $'docs/\xc3\xa9.md')
moved=$(commit "$main" -core/storage/old.go docs/moved.md)
parent=$(commit "$main" core/storage/parent.go)
stacked=$(commit "$parent" docs/child.md)
later=$(commit "$main" core/storage/later.go)
testadd=$(commit "$main" cmd/foo/extra_test.go)
testdel=$(commit "$main" -cmd/foo/guard_test.go)
testmove=$(commit "$main" -cmd/foo/guard_test.go cmd/foo/renamed_test.go)
n=$( (cd "$repo" && bash "$changed" "$main" "$oddnames") | tr -cd '\000' | wc -c | tr -d ' ')
[[ $n == 6 ]] || { echo "FAIL fixture: $n NULs for 3 odd names, want 6"; fail=1; }
if ! (cd "$repo" && git diff --name-only "$main...$unicode") | grep -q '^"'; then
echo "FAIL fixture: expected git to quote the unicode name in its default output"
fail=1
fi
expect_diff 1 "git: unicode test file in storage" "$main" "$unicode"
expect_diff 0 "git: tab, newline and unicode names in docs" "$main" "$oddnames"
expect_diff 1 "git: moved out of storage into docs" "$main" "$moved"
expect_diff 0 "git: stacked PR against its parent" "$parent" "$stacked"
expect_diff 1 "git: the same PR retargeted to main" "$main" "$stacked"
expect_diff 0 "git: the base moved on with denied changes; the PR did not" "$later" "$oddnames"
expect_diff 0 "git: a test added" "$main" "$testadd"
expect_diff 1 "git: a test deleted" "$main" "$testdel"
expect_diff 1 "git: a test moved" "$main" "$testmove"
if (cd "$repo" && bash "$changed" 0000000000000000000000000000000000000001 "$main") >/dev/null 2>&1; then
echo "FAIL git: listing against an unknown base succeeded"
fail=1
fi
# The workflow reruns on every change that can change the verdict, including
# a retarget (edited).
types=$(sed -n 's/^ *types: *\[\(.*\)\].*/\1/p' "$workflow" | tr -d ' ')
for want in opened synchronize reopened labeled unlabeled edited; do
[[ ",$types," == *",$want,"* ]] || { echo "FAIL workflow does not run on $want"; fail=1; }
done
if ((fail)); then exit 1; fi
echo "post-merge-review check: self-test ok"
@@ -0,0 +1,76 @@
#!/usr/bin/env bash
# Usage: post-merge-review-check.sh ALLOW_FILE < changes
#
# changes are NUL-separated STATUS, PATH pairs (post-merge-review-changed.sh).
# A path passes only if it matches an allow pattern and no "!" pattern, and
# a test file may be added or modified but not deleted (a move deletes its
# old name). Prints every change that does not pass and exits 1 if there is
# one; exits 0 if all pass, 2 on a usage error, a malformed change or a
# missing or empty allow list.
set -euo pipefail
allow_file=${1:?usage: post-merge-review-check.sh ALLOW_FILE < changes}
if [[ ! -f $allow_file ]]; then
echo "post-merge-review: allow list $allow_file missing" >&2
exit 2
fi
allow=()
deny=()
while IFS= read -r line; do
line=${line%%#*}
line=${line%"${line##*[![:space:]]}"}
line=${line#"${line%%[![:space:]]*}"}
[[ -n $line ]] || continue
if [[ $line == '!'* ]]; then
deny+=("${line#!}")
else
allow+=("$line")
fi
done <"$allow_file"
if ((${#allow[@]} == 0)); then
echo "post-merge-review: empty allow list $allow_file" >&2
exit 2
fi
denied=0
checked=0
malformed() {
echo "post-merge-review: malformed change list: $1" >&2
exit 2
}
while :; do
# Every field ends in NUL: a field cut off at the end is malformed, not
# skipped, and only the statuses git diff --no-renames writes are known.
if ! IFS= read -r -d '' status; then
[[ -z $status ]] || malformed "unterminated status ${status@Q}"
break
fi
[[ $status == [ADMT] ]] || malformed "unknown status ${status@Q}"
IFS= read -r -d '' path || malformed "status $status without a terminated path"
[[ -n $path ]] || malformed "status $status with an empty path"
checked=$((checked + 1))
why="not on the allow list"
for p in "${allow[@]}"; do
# Unquoted on the right: a glob, where * also matches /.
if [[ $path == $p ]]; then
why=""
break
fi
done
for p in ${deny[@]+"${deny[@]}"}; do
if [[ $path == $p ]]; then
why="matches !$p"
break
fi
done
# A labelled PR may not delete a test (or turn it into something else).
if [[ -z $why && $path == *_test.go && $status != [AM] ]]; then
why="test file status $status: only adding or modifying a test is allowed"
fi
if [[ -n $why ]]; then
printf 'DENY %s %q (%s)\n' "$status" "$path" "$why"
denied=1
fi
done
echo "post-merge-review: $checked changed path(s) checked against ${#allow[@]} allow and ${#deny[@]} deny pattern(s)"
exit "$denied"
+51
View File
@@ -0,0 +1,51 @@
name: post-merge-review paths
# A PR labelled "post-merge-review" may merge before its full review only if
# every changed file is allowed by .github/post-merge-review-allow.txt.
# This check fails such a PR otherwise; without the label it passes.
#
# pull_request_target: the workflow, the allow list and the script all come
# from the base branch, so a PR cannot change its own check. The PR's code is
# never checked out or run: only its commit is fetched, for the file list.
on:
pull_request_target:
# edited: a retarget changes the base, and with it the diff.
types: [opened, synchronize, reopened, labeled, unlabeled, edited]
permissions:
contents: read
concurrency:
group: post-merge-review-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
paths:
name: post-merge-review paths
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Not labelled
if: ${{ !contains(github.event.pull_request.labels.*.name, 'post-merge-review') }}
run: echo "no post-merge-review label; nothing to check"
- name: Checkout base
if: ${{ contains(github.event.pull_request.labels.*.name, 'post-merge-review') }}
uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
with:
ref: ${{ github.event.pull_request.base.sha }}
fetch-depth: 0
persist-credentials: false
- name: Check changed paths
if: ${{ contains(github.event.pull_request.labels.*.name, 'post-merge-review') }}
env:
BASE: ${{ github.event.pull_request.base.sha }}
HEAD: ${{ github.event.pull_request.head.sha }}
PR: ${{ github.event.pull_request.number }}
run: |
set -euo pipefail
bash .github/scripts/post-merge-review-check-test.sh
git fetch --no-tags origin "+refs/pull/${PR}/head:refs/remotes/pr/head"
bash .github/scripts/post-merge-review-changed.sh "$BASE" "$HEAD" >changed.bin
bash .github/scripts/post-merge-review-check.sh .github/post-merge-review-allow.txt <changed.bin