From 4e53c3177303ed802fe60d6d65f804a4870e0007 Mon Sep 17 00:00:00 2001 From: scuttletoby <123843277+scuttletoby@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:43:39 +0100 Subject: [PATCH] head gate: #392 lens minors (docs wording, number-order tests) - README: a merge conflict stops only pull_request, pull_request_review and pull_request_review_comment runs (GitHub's docs: pull_request_target still runs there); "removes or breaks its own copy" reworded so it does not read against the next paragraph on cancelling another PR's runs. - review_run_pr log: with several open PRs on one head it lists them all and says the head is judged once in the job for the lowest number; it no longer says "#12 judges it for #14" (the lowest may be a draft or into another base; target's "judging" line names the ones judged). Verdicts unchanged. - Tests: 9 vs 10 vs 100 (number order, not text), a PR number given as a string (or 12.0) is refused, a number listed twice is named once, the log claims no PR as judge. - Workflow test reads REVIEW_EVENTS with ast.literal_eval, so a double-quoted extra event goes red. Fixes #400 Co-Authored-By: Claude Opus 5.5 --- .../scripts/fork-head-gate-workflow.test.py | 10 ++++- fork-ci/actions/head-check/README.md | 16 +++++--- fork-ci/actions/head-check/head_gate.py | 20 +++++---- fork-ci/actions/head-check/head_gate.test.py | 41 +++++++++++++++++-- 4 files changed, 69 insertions(+), 18 deletions(-) diff --git a/.github/scripts/fork-head-gate-workflow.test.py b/.github/scripts/fork-head-gate-workflow.test.py index 61d3cb3012659..57c993c288a28 100644 --- a/.github/scripts/fork-head-gate-workflow.test.py +++ b/.github/scripts/fork-head-gate-workflow.test.py @@ -9,6 +9,7 @@ against every fork workflow, in fork-contracts.test.py (head-gate). """ +import ast import json import os import pathlib @@ -332,8 +333,13 @@ def args(*a): and "Needed:" in EXAMPLE.read_text()) check("example: no pass-drafts anywhere (a draft's check run would count for a ready pull request at its head)", "pass-drafts" not in EXAMPLE.read_text() and "pass-drafts" not in text) -REVIEW_EVENTS = re.search(r"^REVIEW_EVENTS = \(([^)]*)\)", GATE_PY.read_text(), re.M) -REVIEW_EVENTS = re.findall(r"'([a-z_]+)'", REVIEW_EVENTS.group(1)) if REVIEW_EVENTS else [] +# The tuple as Python reads it (any quoting), from head_gate.py's top-level assignment; [] if it is not a literal. +REVIEW_EVENTS = next((n.value for n in ast.parse(GATE_PY.read_text()).body if isinstance(n, ast.Assign) + and [getattr(t, "id", None) for t in n.targets] == ["REVIEW_EVENTS"]), None) +try: + REVIEW_EVENTS = list(ast.literal_eval(REVIEW_EVENTS)) if REVIEW_EVENTS is not None else [] +except (ValueError, TypeError, SyntaxError): + REVIEW_EVENTS = [] check("example: an issue comment that is not on a pull request starts nothing, nor a review trigger run that did " "not succeed (a newer one replaced it) or that another event started (#379: skipped before a guard job " "starts; the events are head_gate's REVIEW_EVENTS)", diff --git a/fork-ci/actions/head-check/README.md b/fork-ci/actions/head-check/README.md index 1d45c47369161..f4d210ebd630b 100644 --- a/fork-ci/actions/head-check/README.md +++ b/fork-ci/actions/head-check/README.md @@ -324,8 +324,9 @@ gate again, through two workflows are GitHub's fields of the run, never its outputs, artifacts or names). A match is judged like an issue comment on it. More than one (one branch into two bases, so every match has the same head): the gate judges a head, not a - pull request, so the lowest number judges it once for all of them, and the - log names the others. None (closed or merged since, or the head moved on: + pull request, so it judges the head once, in the job for the lowest number. + The log lists them all; the target step names the ones it judges (ready, + into the default branch). None (closed or merged since, or the head moved on: the push ran the gate already): nothing is judged and the log says why. - Review trigger runs on one head wait in one group per caller: one judges, the newest waits, the ones in between are dropped while they wait. A run is @@ -342,8 +343,9 @@ name, but the caller only learns "look at this head again", which any issue comment on the pull request can already say. A run from a fork carries the fork as its head repository, so a fork that copies another pull request's head sha finds only its own pull request, and the head is judged as a whole -anyway. A pull request that removes or breaks its copy only stops runs on -itself. +anyway. A pull request that removes or breaks its own copy of the trigger +stops only its own review runs; whether another pull request can cancel them +is the next paragraph. The trigger is a convenience, not a guard. Withdrawing a lens read (deleting an inline lens comment, editing a review) runs the gate again only through @@ -358,7 +360,8 @@ run. Fallback: when the trigger does not run (a fork's run waiting for approval, a review posted with a workflow's `GITHUB_TOKEN`, which starts no workflow, a pull request with a merge conflict, on which GitHub runs no -`pull_request*` workflow, or a project that has not copied it yet), post an +`pull_request`, `pull_request_review` or `pull_request_review_comment` +workflow, or a project that has not copied it yet), post an issue comment on the pull request or dispatch the gate with its number. A new push, ready or draft, a base change and any issue comment start the gate anyway. Copy the new caller @@ -401,7 +404,8 @@ head "not passed" unless it judged the head and the head passed. - A review trigger's run whose pull request cannot be found: the list of open pull requests with its head fails every try (the guard job fails), or no open pull request has its head (nothing to judge, the log says why). - More than one is no such case: the lowest number judges the head once. + More than one is no such case: the head is judged once, in the job for + the lowest number. - The backfill cannot list the open pull requests after its tries, or finds more than 256 heads. Dispatch again (or once per pull request). - A step before start fails for a reason of GitHub or the runner, never diff --git a/fork-ci/actions/head-check/head_gate.py b/fork-ci/actions/head-check/head_gate.py index 7f4f284d22c81..e380231117316 100644 --- a/fork-ci/actions/head-check/head_gate.py +++ b/fork-ci/actions/head-check/head_gate.py @@ -38,8 +38,9 @@ not its rate limit). On a workflow_run (the review trigger's run finished, README.md "Run again after a review"): no number is taken; the open pull requests whose head is the run's head sha from the run's head repository and branch (--run-*, GitHub's fields of the run), read from the API. More than one (one - branch into two bases): the lowest number judges the head once, the log names the others. None, or a run - of another event: nothing to judge (a log line says why). + branch into two bases): the head is judged once, in the job for the lowest number; target names the ones + it judges (ready, into the default branch). None, or a run of another event: nothing to judge (a log + line says why). target before any token: post=false only for bad arguments. Else post=true: the head gets a failure (judge writes it) when the guard did not pass, on a re-run (run attempt 2 or later), for a caller that is not the default branch's current one or does not pin this gate release, for a lens list with a name that names no lens, for @@ -349,11 +350,16 @@ def review_run_pr(repo, run, gh, out): 'or the head moved on: its push ran the gate)') return [], None # One branch into two bases: every match has this head, and the gate judges a head, not a pull request (preflight - # lists every open pull request with it again; the number only names the job). So it is judged once. - first, others = min(matches), sorted(set(matches) - {min(matches)}) - also = f'; #{first} judges it for {", ".join(f"#{n}" for n in others)} too' if others else '' - out(f'review run: #{first} has the head {head_check.escape(where)}{also}') - return [{'pr': first, 'sha': sha}], None + # lists every open pull request with it again; the number only names the job). So it is judged once. The listing + # has no draft or base, so this line cannot say which of them are judged: target's "judging" line does. + numbers = sorted(set(matches)) # numbers, so 9 < 10; a number GitHub listed twice is named once + if len(numbers) == 1: + out(f'review run: #{numbers[0]} has the head {head_check.escape(where)}') + else: + out(f'review run: open pull requests {", ".join(f"#{n}" for n in numbers)} have the head ' + f'{head_check.escape(where)}; judged once, in the job for #{numbers[0]} (the lowest number); the target ' + 'step names the ones it judges (ready, into the default branch)') + return [{'pr': numbers[0], 'sha': sha}], None def prs(repo, event, pr_text, gh, out, env, event_sha='', git_head=None, run=None): diff --git a/fork-ci/actions/head-check/head_gate.test.py b/fork-ci/actions/head-check/head_gate.test.py index c558e97cfb853..461422724dfb2 100644 --- a/fork-ci/actions/head-check/head_gate.test.py +++ b/fork-ci/actions/head-check/head_gate.test.py @@ -476,7 +476,7 @@ def test_no_open_pull_request_with_the_head_judges_nothing(self): def test_two_open_pull_requests_with_the_head_judge_it_once(self): # #379: same branch into two bases # The gate judges a head, not a pull request (preflight lists every open pull request with it again), so the - # lowest number judges it once for all of them; the others are named in the log. Nothing is skipped: a lens + # head is judged once, in the job for the lowest number; the log lists them all. Nothing is skipped: a lens # read withdrawn on one of them leaves no stale pass. Order of the listing, a third one: the same answer. for rows in ([(12, self.H, REPO), (14, self.H, REPO)], [(14, self.H, REPO), (12, self.H.upper(), REPO)], [(15, self.H, REPO), (14, self.H, REPO), (12, self.H, REPO), (13, self.OTHER, REPO)]): @@ -485,8 +485,41 @@ def test_two_open_pull_requests_with_the_head_judge_it_once(self): # #379: sam self.assertEqual((code, self.found()), (0, [{'pr': 12, 'sha': self.H}]), out) self.assertNotIn('nothing to judge', out) self.assertNotIn('each number', out) - others = ', '.join(f'#{n}' for n, h, _ in sorted(rows) if h.lower() == self.H and n != 12) - self.assertIn(f'#12 judges it for {others} too', out) + listed = ', '.join(f'#{n}' for n, h, _ in sorted(rows) if h.lower() == self.H) + self.assertIn(f'review run: open pull requests {listed} have the head ', out) + self.assertIn('judged once, in the job for #12 (the lowest number)', out) + + def test_the_lowest_is_the_lowest_number_not_the_first_text(self): # #400: 9 < 10 < 100, though '10' < '9' + for rows in ([(10, self.H, REPO), (9, self.H, REPO)], + [(100, self.H, REPO), (9, self.H, REPO), (10, self.H, REPO)]): + with self.subTest(rows=rows): + code, out, _ = self.review(rows) + self.assertEqual((code, self.found()), (0, [{'pr': 9, 'sha': self.H}]), out) + self.assertIn('open pull requests #9, #10' + (', #100' if len(rows) == 3 else '') + ' have', out) + + def test_the_log_names_no_pull_request_as_judged(self): # #400: the lowest may be a draft or into another base + # The listing has no draft or base (HEAD_JQ), so this line cannot know which of them are judged: it names + # them as the open ones with the head, and target's "judging" line names the ones judged. + code, out, _ = self.review([(12, self.H, REPO), (14, self.H, REPO)]) + self.assertEqual((code, self.found()), (0, [{'pr': 12, 'sha': self.H}]), out) + self.assertNotRegex(out, r'#\d+ judges') + self.assertIn('the target step names the ones it judges (ready, into the default branch)', out) + code, out, _ = self.review([(12, self.H, REPO)]) # one: unchanged + self.assertEqual((code, self.found()), (0, [{'pr': 12, 'sha': self.H}]), out) + self.assertIn('review run: #12 has the head ', out) + self.assertNotIn('judged once', out) + + def test_a_number_listed_twice_is_named_once(self): # #400: pages that overlap while the list changes + for rows in ([(12, self.H, REPO), (14, self.H, REPO), (14, self.H, REPO)], + [(14, self.H, REPO), (12, self.H, REPO), (12, self.H, REPO), (14, self.H, REPO)], + [(12, self.H, REPO), (12, self.H, REPO)]): + with self.subTest(rows=rows): + code, out, _ = self.review(rows) + self.assertEqual((code, self.found()), (0, [{'pr': 12, 'sha': self.H}]), out) + line = next(l for l in out.splitlines() if l.startswith('review run:')) + want = 'open pull requests #12, #14 have' if 14 in (r[0] for r in rows) else '#12 has' + self.assertTrue(line.startswith(f'review run: {want} the head '), line) + self.assertEqual(line.count('#14'), int(14 in (r[0] for r in rows)), line) def test_one_of_two_closed_half_way_judges_the_one_left(self): # stops half-way: the listing has the one left for rows in ([(14, self.H, REPO)], [(12, self.H, REPO)]): @@ -533,6 +566,8 @@ def test_a_number_on_a_workflow_run_is_refused(self): # the number comes only def test_an_unreadable_or_odd_listing_is_refused_and_posts_nothing(self): # stops half-way: an older check stays for rows in (answer(1, '', 'gh: Server Error (HTTP 502)'), answer(0, 'x\n'), answer(0, '[12, "x", null]\n'), answer(0, '[true, "' + self.H + '", null]\n'), answer(0, '[12, "' + self.H + '", 5]\n'), + answer(0, '["12", "' + self.H + '", "' + REPO + '"]\n'), # #400: a number as a string + answer(0, '[12.0, "' + self.H + '", "' + REPO + '"]\n'), answer(0, '[12, false, "' + self.H + '", null]\n'), # the old four-field row answer(0, '[12, "' + self.H + '", "' + REPO + '", false]\n')): with self.subTest(rows=rows.stdout[:20]):