Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion .github/scripts/fork-head-gate-workflow.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,14 +436,31 @@ def caller_group(event, run_id, sha="", workflow_ref="o/p/.github/workflows/gate
check("README setup: the warning it names is head_gate's own text",
"the review trigger's run does not name a head" in setup.replace("\n ", " ")
and "the review trigger's run does not name a head" in gate_src)
# #651: step 8 tells the two "no open pull request" failures apart by the count head_gate logs, and says which pull
# request to pick (a branch the gate looks up, a fork that still exists), so a skipped name is not read as a failure.
step8 = setup.replace("\n ", " ")
check("README setup: step 8 tells the filter from the head apart by the count the gate logs, in the gate's words",
"no open pull request on that branch" in step8 and "filter differs" in step8 and "a count means" in step8
and "GitHub listed " in gate_src and "'no open pull request on that branch'" in gate_src,
step8[-600:])
check("README setup: step 8 picks a plain branch name and a fork that still exists",
"branch name is plain" in step8 and "fork still exists" in step8 and "does not look them up" in step8, step8[-600:])
check("README setup: step 8 says an older pin logs the warning as a plain line",
"logs it as a plain line" in step8, step8[-600:])
notes = (root / "fork-ci/RELEASE-NOTES.md").read_text()
entry = next((e for e in notes.split("\n### ")[1:] if "review-trigger.example.yml" in e and "#375" in e.split("\n")[0]),
"")
upgrade = entry[entry.find("**Upgrade:**"):] if "**Upgrade:**" in entry else ""
check("release notes: the review trigger has an entry whose Upgrade copies the trigger and the caller's workflow_run, "
"if and concurrency, pin first, then the live check (setup step 8)",
all(w in upgrade for w in ("review-trigger.example.yml", "gate-caller.example.yml", "`workflow_run`", "`if`",
"`concurrency`", "pinned to an older release", "setup step 8")), upgrade[:300])
"`concurrency`", "pinned to an older release", "is refused", "setup step 8")),
upgrade[:300])
# #651: ci/v15 has the trigger, so a pin at ci/v15..ci/v19 needs no move first ("a release that has this entry" said it
# did); the refusal is for ci/v14 and before.
check("release notes: the Upgrade's pin is ci/v15 or later, and the refused older pin is ci/v14 or before",
"at `ci/v15` or later" in upgrade and "(`ci/v14` or before) is refused" in upgrade
and "a release that has this entry" not in upgrade, upgrade[:400])

if failures:
print(f"\n{len(failures)} failed")
Expand Down
16 changes: 9 additions & 7 deletions fork-ci/RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,19 @@ comments and does nothing but finish. The gate caller runs on `workflow_run` of
request whose head branch, head repository and head sha are the run's (the number comes from the API, never from the
run). Two open pull requests on one head (one branch into two bases) are judged once, in the job for the lowest number.
Before this, a review left the check showing the older answer until the next push or issue comment. First in `ci/v15`.
After `ci/v19` (#379), a review run whose head fields are missing or odd judges nothing with a warning (before: a plain
log line).
After `ci/v20` (#379, #651), a review run whose head fields are missing or not GitHub's form judges nothing with a
warning (before: a plain log line); a head branch git allows but the gate does not look up (an invisible or joining
character, or longer than 255) keeps the plain line, and "no open pull request has the head" says how many GitHub listed
on that branch.

**Upgrade:** nothing breaks if you only move the pin: a caller without the new trigger never runs on `workflow_run`.
To use it, in one pull request on the default branch, after the pin is at a release that has this entry: copy
To use it, in one pull request on the default branch, after the pin is at `ci/v15` or later: copy
`review-trigger.example.yml` to `.github/workflows/` unchanged, and add the caller's `workflow_run` trigger, its `if`
(with the event clause) and its `concurrency` from `gate-caller.example.yml`. The pin moves first or in that pull
request: a caller with `workflow_run` pinned to an older release is refused on that event (its guard job fails, nothing
is posted). Then do the
live check once ([head-check README](actions/head-check/README.md), setup step 8): post a review on one open pull
request and check that the gate ran for it. The GitHub fields the lookup uses are tested only against fakes.
request: a caller with `workflow_run` pinned to an older release (`ci/v14` or before) is refused on that event (its
guard job fails, nothing is posted). Then do the live check once ([head-check README](actions/head-check/README.md),
setup step 8): post a review on one open pull request and check that the gate ran for it. The GitHub fields the lookup
uses are tested only against fakes.

### pr-queue and head-check: stricter rule order in `review` (#378)

Expand Down
30 changes: 20 additions & 10 deletions fork-ci/actions/head-check/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,18 @@ Setup (the repository owner's steps; nothing here creates them):
`workflow_run` logs `review run: #N has the head …`, and the check run on
that head is new. The GitHub fields the lookup uses (the review run's head
branch, head repository and head sha, and the list filter
`head=owner:branch`) are tested only against fakes. If the run warns
"the review trigger's run does not name a head", or logs "no open pull
request has the head" for a review on an open pull request whose head did
not move, they differ: reviews then never run the gate again. Report it
on the fork and use the fallback (an issue comment or a dispatch, see
"Run again after a review") until it is fixed.
`head=owner:branch`) are tested only against fakes. If the run warns "the
review trigger's run does not name a head" (a pin at `ci/v20` or before logs
it as a plain line), or logs "no open pull request has the head" for a
review on an open pull request whose head did not move, they differ: reviews
then never run the gate again. After `ci/v20` that line also says how many
open pull requests GitHub listed on the branch: "no open pull request on
that branch" means the `head=owner:branch` filter differs; a count means the
head sha or head repository differs. Pick a pull request whose branch name
is plain (letters, digits, `-`, `_`, `/`) and whose fork still exists: other
names log that the gate does not look them up, and a deleted fork leaves no
head repository. Report it on the fork and use the fallback (an issue
comment or a dispatch, see "Run again after a review") until it is fixed.

After a change to the caller, a new pin, or a change to the queue config or
the lens rules on the default branch, dispatch the backfill again: it judges
Expand Down Expand Up @@ -442,10 +448,14 @@ head "not passed" unless it judged the head and the head passed.
pull request read and git's `refs/pull/N/head` both fail every try.
- 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). A
run that names no head (its head fields missing or odd) judges nothing
with a warning: GitHub's fields are not what the lookup expects (setup
step 8).
no open pull request has its head (nothing to judge, the log says why and
how many GitHub listed on the branch). A run that names no head (its head
fields missing or not GitHub's form) judges nothing with a warning: most
likely GitHub's fields are not what the lookup expects (setup step 8); a
fork deleted while its pull request is open also leaves no head
repository. A head branch that git allows but the gate does not look up
(an invisible or joining character such as a zero-width space, or longer
than 255) judges nothing with a plain log line.
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
Expand Down
32 changes: 24 additions & 8 deletions fork-ci/actions/head-check/head_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
LIST_JQ = '.[] | [.number, .draft, .head.sha, .base.ref] | @json'
HEAD_JQ = '.[] | [.number, .head.sha, .head.repo.full_name] | @json' # head.repo is null for a deleted fork
BRANCH_MAX = 255
GIT_REF_CONTROL = re.compile(r'[\x00-\x1f\x7f]') # git refuses these in a ref name (check-ref-format)
SERVER = 'https://github.com'


Expand Down Expand Up @@ -324,21 +325,31 @@ def review_run_pr(repo, run, gh, out):
out(f'nothing to judge: the run that finished was started by {event or "no event"!r}, not by a review or an '
f'inline review comment ({", ".join(REVIEW_EVENTS)})')
return [], None
if not (pr_queue.SHA.fullmatch(sha) and pr_queue.REPO_RE.fullmatch(head_repo) and 0 < len(branch) <= BRANCH_MAX
and branch.isprintable()):
# A warning, not a log line: a review run always has these fields (GitHub's, of a pull request event), so a
# run without them means GitHub's fields are not what this lookup expects, and no review would ever re-run the
# gate while every run stays green. The annotation makes that seen (the live check after setup looks for it).
if not (pr_queue.SHA.fullmatch(sha) and pr_queue.REPO_RE.fullmatch(head_repo) and branch
and not GIT_REF_CONTROL.search(branch)):
# A warning, not a log line: a review run of a pull request event has these fields, so a run without them (or
# with a branch git cannot have) most likely means GitHub's fields are not what this lookup expects, and no
# review would ever re-run the gate while every run stays green. The annotation makes that seen (the live
# check after setup looks for it). One known harmless cause: a fork deleted while its pull request is open
# leaves the run without a head repository, and nothing could be judged then anyway.
out("::warning::head gate: nothing to judge: the review trigger's run does not name a head (head sha, head "
'repository, head branch); dispatch the gate with the pull request number')
'repository, head branch), or its head repository is gone (a deleted fork); dispatch the gate with the '
'pull request number')
return [], None
if len(branch) > BRANCH_MAX or not branch.isprintable():
# A name git allows (a no-break or zero-width space, an emoji joined by a zero-width joiner) that the gate does
# not look up: GitHub's fields are fine, so a plain line, never the warning above. The name is not logged.
out('nothing to judge: the head branch of the review run has characters the gate does not look up (an '
f'invisible or joining one), or is longer than {BRANCH_MAX}; post an issue comment or dispatch the gate '
'with the pull request number')
return [], None
owner = urllib.parse.quote(head_repo.split('/')[0], safe='')
query = (f'repos/{repo}/pulls?state=open&per_page=100&head={owner}:'
+ urllib.parse.quote(branch, safe='/'))
r = read(gh, '--paginate', query, '--jq', HEAD_JQ)
if r.returncode != 0:
return None, f'could not list the open pull requests with the head of the review run: {said(r)}'
matches = []
matches, listed = [], set()
for line in r.stdout.splitlines():
try:
n, head, from_repo = json.loads(line)
Expand All @@ -347,12 +358,17 @@ def review_run_pr(repo, run, gh, out):
if not (type(n) is int and n > 0 and isinstance(head, str)
and pr_queue.SHA.fullmatch(head.lower()) and (from_repo is None or isinstance(from_repo, str))):
return None, 'GitHub listed something that is not a pull request'
listed.add(n)
if head.lower() == sha and isinstance(from_repo, str) and from_repo.lower() == head_repo.lower():
matches.append(n)
where = f'{sha} of {head_repo} branch {branch!r}'
if not matches:
# How many GitHub listed tells the live check's two failures apart (setup step 8): none for an open pull
# request means the head=owner:branch filter is not GitHub's; some means the head sha or repository differs.
out(f'nothing to judge: no open pull request has the head {head_check.escape(where)} (closed or merged since, '
'or the head moved on: its push ran the gate)')
'or the head moved on: its push ran the gate); GitHub listed '
+ (f'{len(listed)} open pull request{"s" if len(listed) > 1 else ""} on that branch, none with this head'
if listed else 'no open pull request on that branch'))
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. The listing
Expand Down
54 changes: 49 additions & 5 deletions fork-ci/actions/head-check/head_gate.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
_spec.loader.exec_module(T) # first: its guard puts a fake gh on PATH, drops tokens and isolates git

import argparse, base64, io, json, shutil, subprocess, tempfile, types, unittest, unittest.mock # noqa: E402
import urllib.parse # noqa: E402

sys.path.insert(0, HERE)
import head_check as H # noqa: E402
Expand Down Expand Up @@ -474,6 +475,24 @@ def test_no_open_pull_request_with_the_head_judges_nothing(self):
self.assertEqual((code, self.found()), (0, []), out)
self.assertIn('nothing to judge', out)
self.assertIn('no open pull request', out)
self.assertNotIn('::', out) # a plain line: a closed pull request is normal

def test_no_match_says_how_many_github_listed_on_the_branch(self): # #651: the live check's two failures
# None listed for an open pull request: the head=owner:branch filter is not GitHub's. Some listed: the head
# sha or repository differs (or the head moved on). A number listed twice (pages overlap) counts once.
for rows, said in (([], 'GitHub listed no open pull request on that branch'),
([(12, self.OTHER, REPO)], 'GitHub listed 1 open pull request on that branch, none with '
'this head'),
([(12, self.H, None)], 'GitHub listed 1 open pull request on that branch'),
([(12, self.OTHER, REPO), (12, self.OTHER, REPO)], 'GitHub listed 1 open pull request '),
([(12, self.OTHER, REPO), (14, self.H, 'someone/else')],
'GitHub listed 2 open pull requests on that branch, none with this head')):
with self.subTest(rows=rows):
code, out, _ = self.review(rows)
self.assertEqual((code, self.found()), (0, []), out)
self.assertEqual(len(out.splitlines()), 1, out)
self.assertIn(said, out)
self.assertIn('no open pull request has the head', out)

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
Expand Down Expand Up @@ -551,18 +570,43 @@ def test_the_branch_is_one_query_value(self): # a branch name is the pusher's

def test_a_run_of_another_event_or_with_odd_fields_judges_nothing_and_reads_nothing(self):
for run in ({'event': 'pull_request'}, {'event': 'push'}, {'event': ''}, {'sha': 'abc'}, {'sha': ''},
{'repo': ''}, {'repo': 'no-slash'}, {'branch': ''}, {'branch': 'a\nb'}, {'branch': 'x' * 256}):
{'sha': None}, {'repo': ''}, {'repo': None}, {'repo': 'no-slash'}, {'branch': ''},
{'branch': None}, {'branch': 'a\nb'}, {'branch': 'a\x7fb'}, {'branch': 'a\tb'}, {}):
with self.subTest(run=run):
code, out, gh = self.review(run=run)
if not run: # nothing at all: a run with none of the fields
code, out, gh = self.review(run={'sha': None, 'repo': None, 'branch': None}, query='-')
else:
code, out, gh = self.review(run=run, query='-')
self.assertEqual((code, self.found(), gh.calls), (0, [], []), out)
self.assertIn('nothing to judge', out)
# #379: a review run always has its head fields, so odd ones mean GitHub's fields are not what the
# lookup expects (no review would ever re-run the gate): a warning the live check can see, one line.
# Another event is normal (an older caller without the event clause): a plain log line.
# #379: a review run of a pull request event has its head fields, so missing or malformed ones (a
# branch git cannot have) most likely mean GitHub's fields are not what the lookup expects (no
# review would ever re-run the gate): a warning the live check can see, one line. Another event is
# normal (an older caller without the event clause): a plain log line.
odd = 'event' not in run
self.assertEqual(out.startswith('::warning::head gate: nothing to judge: '), odd, out)
self.assertEqual(out.count('::'), 2 if odd else 0, out)
self.assertNotIn('::error::', out)
if odd: # #651: a fork deleted while its pull request is open leaves no head repository
self.assertIn('or its head repository is gone (a deleted fork)', out)

def test_a_branch_git_allows_but_the_gate_does_not_look_up_is_a_plain_line(self): # #651
# Git allows these (check-ref-format --branch): a no-break space, a zero-width space, an emoji joined by a
# zero-width joiner, a name longer than BRANCH_MAX. GitHub's fields are fine, so never the warning that says
# they are not; the head is skipped as before (the fallback covers it), and the name is not logged.
for branch in ('feat/\u00a0x', 'a\u200bb', 'fix-\U0001F468\u200d\U0001F469', '\u202eevil', 'x' * 256):
with self.subTest(branch=branch):
code, out, gh = self.review(run={'branch': branch}, query='-')
self.assertEqual((code, self.found(), gh.calls), (0, [], []), out)
self.assertEqual(out, 'nothing to judge: the head branch of the review run has characters the gate '
'does not look up (an invisible or joining one), or is longer than 255; post an '
'issue comment or dispatch the gate with the pull request number')
for branch in ('x' * 255, 'feat/é-ü', 'fix-\U0001F468'): # printable, at most BRANCH_MAX: looked up
with self.subTest(branch=branch):
rows = [(12, self.H, REPO)]
code, out, gh = self.review(rows, {'branch': branch},
query=self.query(REPO.split('/')[0], urllib.parse.quote(branch, safe='/')))
self.assertEqual((code, self.found()), (0, [{'pr': 12, 'sha': self.H}]), out)

def test_the_listing_reads_only_the_fields_it_uses(self): # #379: no draft (preflight fails a draft's head)
self.assertEqual(G.HEAD_JQ, '.[] | [.number, .head.sha, .head.repo.full_name] | @json')
Expand Down
Loading