Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

merge-approved.yml: runs only from main's copy, and a PR merged by hand mid-run passes (Refs #3026, Refs #3027) (#3035)

Eyal Golana61ffd27636d
diff --git a/.github/workflows/merge-approved.yml b/.github/workflows/merge-approved.yml
index c7ad2d4cf..46094336d 100644
--- a/.github/workflows/merge-approved.yml
+++ b/.github/workflows/merge-approved.yml
@@ -2,11 +2,12 @@ name: Merge approved
# Squash-merges an open pull request into main once the maintainer has approved its latest commit and every check
# on that commit has passed (owner ruling, 2026-10-11). Outside contributors get no write access; this job merges for
-# them. It runs from main's copy of this file and never checks out, builds or runs pull-request code, so the write
-# token never meets outside code. It starts when CI finishes (workflow_run runs in this repository's context with a
-# write token, also for forks), when a review is submitted (for a fork's pull request GitHub gives that run a read-only
-# token, so its merge is refused and the next CI or timer run merges), on a timer as a fallback (GitHub runs
-# schedules late or not at all under load), and by hand. It reads only numbers, SHAs and states from the API, never titles;
+# them. Every trigger runs main's copy of this file, and the job never checks out, builds or runs pull-request code,
+# so the write token never meets outside code. It starts when CI finishes (workflow_run runs in this repository's
+# context with a write token, also for forks), on a timer as a fallback (GitHub runs schedules late or not at all
+# under load), and by hand. A submitted review does not start it (#3026): GitHub would run that from the pull
+# request's merge ref, so a branch that edits this file would choose what its run does. An approval merges on the next
+# CI or timer run. It reads only numbers, SHAs and states from the API, never titles;
# the body is read only inside jq, for the licence tick, and never reaches the shell.
#
# A pull request merges only when all of these hold on its current head commit:
@@ -21,16 +22,15 @@ name: Merge approved
# so gh would refuse it. The run says it waits, and it merges once it is updated (#2698).
# GitHub does not start other workflows from a merge made with this token, so CI on main is not re-run by it.
#
-# A refused merge is tolerated only when it is expected (#2698): the head moved after this run read it (GitHub then
-# reports a head other than the approved SHA), or a review run met "Resource not accessible by integration" (the fork's
-# read-only token above). Any other failure is printed, the remaining pull requests are still tried, and the run fails.
+# A refused merge is tolerated only when it is expected. After a refusal the run re-reads the pull request, and passes
+# when GitHub reports it MERGED (someone merged it by hand after this run read it, #3027) or a head other than the
+# approved SHA (the head moved, #2698). Any other failure is printed, the remaining pull requests are still tried, and
+# the run fails.
on:
workflow_run:
workflows: [CI]
types: [completed]
- pull_request_review:
- types: [submitted]
schedule:
- cron: "*/10 * * * *"
workflow_dispatch:
@@ -86,11 +86,12 @@ jobs:
continue
fi
printf '%s\n' "$out"
- now=$(gh pr view "$n" --json headRefOid --jq '.headRefOid')
- if [ "$now" != "$ready" ]; then
+ after=$(gh pr view "$n" --json state,headRefOid --jq '"\(.state) \(.headRefOid)"')
+ read -r state now <<<"$after"
+ if [ "$state" = MERGED ]; then
+ echo "#$n: merge refused, GitHub reports it already merged (by hand during this run)"
+ elif [ "$now" != "$ready" ]; then
echo "#$n: merge refused, the head moved to $now; a later run checks the new head"
- elif [ "$GITHUB_EVENT_NAME" = pull_request_review ] && grep -qF 'Resource not accessible by integration' <<<"$out"; then
- echo "#$n: merge refused, a review run for a fork's pull request has a read-only token; the next CI or timer run merges"
else
echo "::error::#$n: merge failed for a reason this workflow does not expect; see the gh output above"
failed=1
diff --git a/scripts/oss/tests/test_github_config.py b/scripts/oss/tests/test_github_config.py
index 53e9422d6..773672943 100644
--- a/scripts/oss/tests/test_github_config.py
+++ b/scripts/oss/tests/test_github_config.py
@@ -188,7 +188,9 @@ def test_merge_workflow_never_touches_pull_request_code():
# Owner ruling 2026-10-11: approved pull requests merge without giving contributors write access. The job holds a
# write token, so it must never check out, build or run pull-request code, and never read PR text into the shell.
wf = yaml.safe_load(MERGE.read_text(encoding="utf-8"))
- assert set(triggers(wf)) == {"workflow_run", "pull_request_review", "schedule", "workflow_dispatch"}
+ # Owner decision on #3026 (10 Nov): no pull_request_review trigger. A review run would use the file from the pull
+ # request's merge ref; these three triggers all run main's copy.
+ assert set(triggers(wf)) == {"workflow_run", "schedule", "workflow_dispatch"}
assert triggers(wf)["workflow_run"]["workflows"] == ["CI"]
assert "pull_request_target" not in triggers(wf)
assert wf["permissions"] == {"contents": "write", "pull-requests": "write"}
@@ -301,16 +303,18 @@ def test_a_merge_that_succeeds_passes(tmp_path):
assert "pr merge 7 --squash --match-head-commit abc" in calls
-def test_a_read_only_review_token_is_an_expected_refusal(tmp_path):
- # GitHub gives a fork's pull_request_review run a read-only token; the next CI or timer run merges.
- done, _ = run_merge_step(tmp_path, "pull_request_review", {7: (ready_pr(), 1, READ_ONLY, None)})
- assert done.returncode == 0, done.stdout + done.stderr
- assert "read-only token" in done.stdout
+def test_the_merge_step_does_not_depend_on_which_trigger_started_it():
+ # #3026: every trigger left runs main's copy with the write token, so no refusal is tolerated per event.
+ wf = yaml.safe_load(MERGE.read_text(encoding="utf-8"))
+ script = "\n".join(s.get("run", "") for s in steps(wf))
+ assert "GITHUB_EVENT_NAME" not in script and "Resource not accessible" not in script
+ assert "pull_request_review" not in MERGE.read_text(encoding="utf-8")
[email protected]("event", ["workflow_run", "schedule", "workflow_dispatch"])
-def test_a_read_only_refusal_fails_the_run_where_the_token_should_write(tmp_path, event):
- # These runs hold the write token, so "not accessible" means the permissions are wrong, not a fork review.
+# pull_request_review is no longer a trigger (#3026); it stays in the list to show its old read-only exception is gone.
[email protected]("event", ["workflow_run", "schedule", "workflow_dispatch", "pull_request_review"])
+def test_a_read_only_refusal_fails_the_run(tmp_path, event):
+ # Every run holds the write token, so "not accessible" means the permissions are wrong.
done, _ = run_merge_step(tmp_path, event, {7: (ready_pr(), 1, READ_ONLY, None)})
assert done.returncode != 0, done.stdout + done.stderr
@@ -329,6 +333,32 @@ def test_a_head_moved_message_without_a_new_head_fails_the_run(tmp_path):
assert done.returncode != 0, done.stdout + done.stderr
+def test_a_pull_request_merged_by_hand_mid_run_passes(tmp_path):
+ # Owner decision on #3027 (10 Nov): "Log and skip if MERGED (Recommended)". Someone merged it between gh's read
+ # and the mutation, so gh refused; GitHub now reports it MERGED on the same head.
+ out = "GraphQL: Pull request is not mergeable (mergePullRequest)\n"
+ done, _ = run_merge_step(tmp_path, "workflow_run", {7: (ready_pr("abc"), 1, out, ready_pr("abc", state="MERGED"))})
+ assert done.returncode == 0, done.stdout + done.stderr
+ assert "#7: merge refused, GitHub reports it already merged" in done.stdout
+
+
+def test_a_failed_re_read_after_a_refusal_fails_the_run(tmp_path):
+ # If gh cannot re-read the pull request, the run must not read the empty answer as a moved head.
+ out = "HTTP 502: Bad Gateway\n"
+ done, _ = run_merge_step(tmp_path, "workflow_run", {7: (ready_pr("abc"), 1, out, "not json")})
+ assert done.returncode != 0, done.stdout + done.stderr
+ assert "head moved" not in done.stdout
+
+
[email protected]("state", ["OPEN", "CLOSED"])
+def test_a_refusal_on_a_pull_request_that_did_not_merge_fails_the_run(tmp_path, state):
+ # Only MERGED is tolerated; a pull request closed mid-run, or still open on the same head, fails as before.
+ out = "GraphQL: Pull request is not mergeable (mergePullRequest)\n"
+ done, _ = run_merge_step(tmp_path, "workflow_run", {7: (ready_pr("abc"), 1, out, ready_pr("abc", state=state))})
+ assert done.returncode != 0, done.stdout + done.stderr
+ assert "::error::#7" in done.stdout
+
+
@pytest.mark.parametrize("out", [
"HTTP 401: Bad credentials (https://api.github.com/graphql)\n",
"GraphQL: Repository rule violations found\n\nChanges must be made through a pull request. (mergePullRequest)\n",