diff --git a/.github/block/scripts/block-ci-port-test.py b/.github/block/scripts/block-ci-port-test.py index b2637ed5c0..caf07ee46f 100644 --- a/.github/block/scripts/block-ci-port-test.py +++ b/.github/block/scripts/block-ci-port-test.py @@ -50,7 +50,36 @@ class PortTests(unittest.TestCase): self.assertNotRegex(test, r"if: matrix\.suite == 'process'\s*\n") post = (ROOT / ".github/workflows/block-post-merge-review.yml").read_text() self.assertIn("working-directory: ${{ github.workspace }}", post) - self.assertIn("""git -C .. diff -z --name-status --no-renames "$BASE...$HEAD" -- . ':(exclude)seaweed-block/'""", post) + + def test_labelled_post_merge_review_refuses_host_files(self): + post = (ROOT / ".github/workflows/block-post-merge-review.yml").read_text() + step = post.split(" - name: Check changed paths\n", 1)[1].split(" run: |\n", 1)[1].split("\n\n - name:", 1)[0] + script = "\n".join(line[10:] for line in step.splitlines()) + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp); module = root / "seaweed-block" + def git(*args): return subprocess.check_output(["git", *args], cwd=root, timeout=15) + git("init", "-b", "master"); git("config", "user.name", "Example"); git("config", "user.email", "example@example.invalid"); git("config", "commit.gpgsign", "false") + for name in ("post-merge-review-check.sh", "post-merge-review-allow.txt"): + source = ROOT / "seaweed-block/.github" / ("scripts/" + name if name.endswith(".sh") else name) + target = module / ".github" / ("scripts/" + name if name.endswith(".sh") else name) + target.parent.mkdir(parents=True, exist_ok=True); target.write_bytes(source.read_bytes()) + (module / ".github/scripts/post-merge-review-check-test.sh").write_text("exit 0\n") + (root / ".github/block/scripts").mkdir(parents=True) + (root / ".github/block/scripts/post-merge-review-changed.sh").write_bytes((HERE / "post-merge-review-changed.sh").read_bytes()) + git("add", "."); git("commit", "-m", "base"); git("remote", "add", "origin", str(root)) + base = git("rev-parse", "HEAD").decode().strip() + def check(*paths): + git("reset", "-q", "--hard", base) + for path in paths: (root / path).parent.mkdir(parents=True, exist_ok=True); (root / path).write_text("changed\n") + git("add", "--", *paths); git("commit", "-m", "change") + git("update-ref", "refs/pull/1/head", "HEAD") + env = dict(os.environ, BASE=base, HEAD=git("rev-parse", "HEAD").decode().strip(), PR="1") + return subprocess.run(["bash", "-eo", "pipefail", "-c", script], cwd=module, env=env, capture_output=True, timeout=30) + self.assertEqual(check("seaweed-block/docs/a.md").returncode, 0) + for host in ("weed/example_test.go", "README.md", "docs/a.md"): + out = check("seaweed-block/docs/a.md", host) + self.assertEqual(out.returncode, 1, out) + self.assertEqual([l for l in out.stdout.splitlines() if l.startswith(b"DENY")], [b"DENY host " + host.encode() + b" (outside seaweed-block)"]) def test_known_bugs_use_the_module_verdict_helper(self): text = (ROOT / ".github/workflows/block-known-bugs.yml").read_text() diff --git a/.github/workflows/block-post-merge-review.yml b/.github/workflows/block-post-merge-review.yml index a1123d21fb..a004a418bc 100644 --- a/.github/workflows/block-post-merge-review.yml +++ b/.github/workflows/block-post-merge-review.yml @@ -82,13 +82,17 @@ jobs: set -euo pipefail bash .github/scripts/post-merge-review-check-test.sh git -c fetch.fsckObjects=true fetch --no-tags origin "+refs/pull/${PR}/head:refs/remotes/pr/head" - { - bash ../.github/block/scripts/post-merge-review-changed.sh "$BASE" "$HEAD" - # Host files keep their root path, so the allow list refuses them. - git -C .. diff -z --name-status --no-renames "$BASE...$HEAD" -- . ':(exclude)seaweed-block/' \ - ':(exclude).github/workflows/block-*.yml' ':(exclude).github/dockerfiles/block-*.Dockerfile' ':(exclude).github/block/**' - } >changed.bin + bash ../.github/block/scripts/post-merge-review-changed.sh "$BASE" "$HEAD" >changed.bin + # The allow list speaks for the module only: any host file keeps the full review. + git -C .. diff -z --name-only --no-renames "$BASE...$HEAD" -- . ':(exclude)seaweed-block/' \ + ':(exclude).github/workflows/block-*.yml' ':(exclude).github/dockerfiles/block-*.Dockerfile' ':(exclude).github/block/**' >host.bin + host=0 + while IFS= read -r -d '' path; do + printf 'DENY host %q (outside seaweed-block)\n' "$path" + host=1 + done