From 2c8d8d3a142b754c902e41905f3f927aba686376 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 5 Dec 2026 05:53:05 -0800 Subject: [PATCH] fix(block-ci): refuse ambiguous merge bases in readiness checks --- .github/block/scripts/block-ci-port-test.py | 27 +++++++++++++++++++++ .github/block/scripts/safe-merge-check.py | 5 +++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/.github/block/scripts/block-ci-port-test.py b/.github/block/scripts/block-ci-port-test.py index be6973f14e..ad5f95f209 100644 --- a/.github/block/scripts/block-ci-port-test.py +++ b/.github/block/scripts/block-ci-port-test.py @@ -286,6 +286,33 @@ class PortTests(unittest.TestCase): check.local_checks({"number": 1, "body": "body", "title": "title", "head": {"sha": "head"}}, "base") self.assertEqual(run.call_count, 1) # fetch only; no checker certification + def test_control_path_diff_refuses_multiple_merge_bases(self): + spec = importlib.util.spec_from_file_location("safe_merge", HERE / "safe-merge-check.py") + check = importlib.util.module_from_spec(spec); spec.loader.exec_module(check) + real_output = subprocess.check_output + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + def git(*args): return real_output(["git", *args], cwd=root, timeout=15).decode().strip() + git("init", "-b", "master") + git("config", "user.name", "Example"); git("config", "user.email", "example@example.invalid") + git("config", "commit.gpgsign", "false") + git("commit", "--allow-empty", "-m", "base"); base = git("rev-parse", "HEAD") + git("checkout", "-q", "-b", "left") + (root / "left.txt").write_text("left") + git("add", "."); git("commit", "-m", "left"); left = git("rev-parse", "HEAD") + git("checkout", "-q", "-b", "right", base) + (root / "right.txt").write_text("right") + git("add", "."); git("commit", "-m", "right"); right = git("rev-parse", "HEAD") + git("checkout", "-q", "left"); git("merge", "--no-ff", right, "-m", "left merge") + left_merge = git("rev-parse", "HEAD") + git("checkout", "-q", "right"); git("merge", "--no-ff", left, "-m", "right merge") + right_merge = git("rev-parse", "HEAD") + self.assertEqual(set(git("merge-base", "--all", left_merge, right_merge).splitlines()), {left, right}) + with patch.object(check.subprocess, "check_output", side_effect=lambda args, **kw: real_output(args, cwd=root, **kw)): + with self.assertRaisesRegex(ValueError, "ambiguous_merge_base"): + check.check_control_paths(left_merge, right_merge) + check.check_control_paths(left, left_merge) + def test_fork_runner_guard_is_required_not_only_the_fallback(self): expected = "${{ (github.event_name == 'push' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' || github.event.pull_request.head.repo.full_name == github.repository) && vars.BLOCK_RUNNER_LABEL || 'ubuntu-latest' }}" for name in ("test", "spec-trace", "known-bugs"): diff --git a/.github/block/scripts/safe-merge-check.py b/.github/block/scripts/safe-merge-check.py index 28040608e7..b7c2567675 100644 --- a/.github/block/scripts/safe-merge-check.py +++ b/.github/block/scripts/safe-merge-check.py @@ -48,7 +48,10 @@ def check_control_paths(base, head): # need independent workflow review; their own Actions results cannot waive it. # Diff from the merge base: master's later control changes are not the PR's, # and the tested merge commit already ran them. - paths = subprocess.check_output(["git", "diff", "--no-renames", "--name-only", "-z", base + "..." + head]).split(b"\0") + bases = subprocess.check_output(["git", "merge-base", "--all", base, head]).decode("ascii").splitlines() + if len(bases) != 1: + raise ValueError("ambiguous_merge_base: expected exactly one merge base") + paths = subprocess.check_output(["git", "diff", "--no-renames", "--name-only", "-z", bases[0], head]).split(b"\0") controls = [p.decode("utf-8", errors="replace") for p in paths if p.startswith((b".github/workflows/", b".github/block/", b".github/dockerfiles/block-", b"seaweed-block/.github/"))] if controls: raise ValueError("workflow_review_required: CI controls changed: " + ", ".join(controls))