Repository navigation
Reconcile eligibility dispatch after finding resolution #1562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3eca5fd
d26ca8b
9d9d15f
ffd6ed9
44e7683
9b2fdeb
71a4432
71a8ff7
67df7d4
da45c03
2fb45fa
9d0988c
0234af9
2cd175e
c3de7fd
1aae039
0c68daf
b945542
db8a695
6c65b6a
251719a
e780406
98d04b1
1e7cd89
c1d8bb7
6ac05fd
edccfe5
97aafaa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1129,13 +1129,44 @@ def eligibility_dispatch_args(repo: str, number: int, base: str) -> tuple[str, . | |
| "-f", f"ref={base}", "-f", f"inputs[pr]={number}") | ||
|
|
||
|
|
||
| def refresh_eligibility(repo: str, number: int, base: str) -> None: | ||
| args = eligibility_dispatch_args(repo, number, base) | ||
| def _eligibility_dispatched(repo: str, number: int, base: str, since: str) -> bool: | ||
| query = f"event=workflow_dispatch&branch={quote(base, safe='')}&created={quote('>=' + since, safe='')}&per_page=100" | ||
| data = api_get(f"repos/{repo}/actions/workflows/eligibility.yml/runs?{query}") | ||
| runs = data.get("workflow_runs") if isinstance(data, dict) else None | ||
| complete = isinstance(runs, list) and data.get("total_count") == len(runs) | ||
| identified = [run.get("display_title") if isinstance(run, dict) else None for run in runs] if complete else [] | ||
| if f"Refresh eligibility for PR #{number}" in identified: | ||
| return True | ||
| if not complete or any(not isinstance(name, str) or not re.fullmatch(r"Refresh eligibility for PR #[0-9]+", name) for name in identified): | ||
| raise GhError(("refresh Loop-zero Eligibility",), "could not identify all recent refresh runs") | ||
| return False | ||
|
|
||
|
|
||
| def refresh_eligibility(repo: str, pr: PR, branch: str, base: str, publishers: tuple[str, ...]) -> None: | ||
| args = eligibility_dispatch_args(repo, pr.number, base) | ||
| since = datetime.fromtimestamp(time.time(), UTC).isoformat(timespec="seconds") | ||
| try: | ||
| _gh(*args) | ||
| try: | ||
| _gh(*args) | ||
| except (GhUnavailable, ProcTimeout) as exc: | ||
| if isinstance(exc, GhUnavailable) and not _transient_error(exc.tail, False): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Second transient check repeats
|
||
| raise | ||
| _retry_sleep(RETRY_DELAY) | ||
| current = pr_for_branch(repo, branch, open_only=True) | ||
| if current is None or (current.number, current.head_sha, current.base_ref) != ( | ||
| pr.number, pr.head_sha, pr.base_ref, | ||
| ): | ||
| raise GhError(("refresh Loop-zero Eligibility",), "the PR or head changed after resolution") from exc | ||
| if _eligibility_dispatched(repo, pr.number, base, since): | ||
| return | ||
| if open_blocking_findings(repo, pr.number, None, publishers): | ||
| raise GhError(("refresh Loop-zero Eligibility",), "blocking findings appeared after resolution") from exc | ||
| _gh(*args) | ||
| except (GhError, ProcTimeout, OSError) as exc: | ||
| retry = shlex.join(("gh", *args)) | ||
| raise GhError(("refresh Loop-zero Eligibility",), f"{exc}\nRun manually: {retry}") from exc | ||
| raise GhError(("refresh Loop-zero Eligibility",), | ||
| f"Finding on PR #{pr.number} is resolved. {exc}\n" | ||
| f"Verify the PR head, remaining findings and recent runs before retrying: {retry}") from exc | ||
|
|
||
|
|
||
| def open_blocking_findings( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4016,35 +4016,35 @@ def finding_pr(wt, gh, head): | |
| THREAD_RESOLVED = {"data": {"resolveReviewThread": {"thread": {"isResolved": True}}}} | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("outcome", ["refreshed", "remaining", "dispatch failed"]) | ||
| def test_resolve_lists_then_replies_and_resolves_one_finding(finding_pr, gh: FakeGh, capsys, outcome) -> None: | ||
| listing = threads_json(finding_pr) | ||
| remaining = listing if outcome == "remaining" else threads_json() | ||
| gh.respond("api graphql", listing, listing, REPLY_ACCEPTED, THREAD_RESOLVED, remaining) | ||
| gh.respond("api -X", "", exit=int(outcome == "dispatch failed"), stderr="HTTP 403") | ||
|
|
||
| @pytest.mark.parametrize("outcome", ["refreshed", "remaining", "denied", "applied", "retry", "unknown"]) | ||
| def test_resolve_lists_then_replies_and_resolves_one_finding(finding_pr, gh: FakeGh, capsys, monkeypatch, outcome): | ||
| listing, backoffs = threads_json(finding_pr), [] | ||
| gh.respond("api graphql", listing, listing, REPLY_ACCEPTED, THREAD_RESOLVED, listing if outcome == "remaining" else threads_json()) | ||
| endpoint = f"repos/{REPO}/actions/workflows/eligibility.yml" | ||
| transient = outcome in {"applied", "retry", "unknown"} | ||
| gh.respond("api -X", {"stdout": "", "exit": 1, "stderr": "HTTP 503" if transient else "HTTP 403"} if transient or outcome == "denied" else "", "") | ||
| monkeypatch.setattr(github, "_retry_sleep", backoffs.append) | ||
| runs = [] if outcome == "retry" else [{"display_title": "Refresh eligibility for PR #7" if outcome == "applied" else "unknown"}] | ||
| monkeypatch.setattr(github.time, "time", lambda: 1795640554) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Test patches the global
|
||
| gh.respond(f"api {endpoint}/runs?event=workflow_dispatch&branch=main&created=%3E%3D2026-11-25T21%3A02%3A34%2B00%3A00&per_page=100", {"workflow_runs": runs, "total_count": len(runs)}) | ||
| code, out, err = run(capsys, "resolve") | ||
| assert (code, err) == (0, "") and out == "0b9b59e7 important src/a.py:3 Off by one\n" | ||
| assert (code, err, out) == (0, "", "0b9b59e7 important src/a.py:3 Off by one\n") | ||
| assert not any("eligibility.yml/dispatches" in " ".join(c["argv"]) for c in gh.calls) | ||
|
|
||
| code, out, err = run(capsys, "resolve", "0b9b59e7", "Fixed in abc1234: loop bound corrected.") | ||
| assert out.splitlines()[0] == "resolved 0b9b59e7: Off by one" | ||
| calls = [c["argv"] for c in gh.calls | ||
| if c["argv"][:2] == ["api", "graphql"] and "viewer {" not in c["argv"][3]][2:] | ||
| assert "addPullRequestReviewThreadReply" in calls[0][3] and "resolveReviewThread" in calls[1][3] | ||
| assert "body=Fixed in abc1234: loop bound corrected." in calls[0] and "thread=THREAD_1" in calls[1] | ||
| endpoint = f"repos/{REPO}/actions/workflows/eligibility.yml/dispatches" | ||
| dispatches = [c["argv"] for c in gh.calls if endpoint in c["argv"]] | ||
| if outcome == "remaining": | ||
| assert (code, err, dispatches) == (0, "", []) | ||
| assert len(out.splitlines()) == 1 | ||
| dispatches = [c["argv"] for c in gh.calls if f"{endpoint}/dispatches" in c["argv"]] | ||
| assert dispatches == [["api", "-X", "POST", f"{endpoint}/dispatches", "-f", "ref=main", "-f", "inputs[pr]=7", "--include"]] * {"remaining": 0, "retry": 2}.get(outcome, 1) | ||
| assert backoffs == ([5] if transient else []) | ||
| if outcome in {"denied", "unknown"}: | ||
| assert code == 1 and "is resolved." in err and ("HTTP 403" if outcome == "denied" else "identify all recent refresh runs") in err | ||
| assert f"gh api -X POST {endpoint}/dispatches -f ref=main -f 'inputs[pr]=7'" in err | ||
| else: | ||
| assert dispatches == [["api", "-X", "POST", endpoint, "-f", "ref=main", "-f", "inputs[pr]=7", "--include"]] | ||
| if outcome == "refreshed": | ||
| assert (code, err) == (0, "") and out.splitlines()[-1] == "refreshed Loop-zero Eligibility for PR #7" | ||
| else: | ||
| assert code == 1 and "HTTP 403" in err | ||
| assert f"gh api -X POST {endpoint} -f ref=main -f 'inputs[pr]=7'" in err | ||
| assert (code, err) == (0, "") | ||
| assert out.splitlines()[1:] == ([] if outcome == "remaining" else ["refreshed Loop-zero Eligibility for PR #7"]) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("cut", ["reply", "resolution"]) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: An empty run list after one 5 s wait still allows a duplicate dispatch
sincecomes from the local clock, and_eligibility_dispatchedreturns False whenever the filtered list is empty. That can be wrong in two cases: (a) the local clock runs ahead of GitHub's, so a run that was accepted hascreated_at < sinceand is filtered out; (b) GitHub creates the run asynchronously after the dispatch is accepted, and it can take longer thanRETRY_DELAYduring the same incident that caused the 5xx or timeout. Either way the code dispatches a second time. The run uses a non-cancelling concurrency group, so the duplicate is mostly harmless. Still, the acceptance asks that observations which cannot rule out an accepted refresh do not authorize another one. Smallest fix: movesinceback by a small skew margin (e.g. 30–60 s). Any extra runs this picks up are refreshes of the same PR, which count as success anyway. Also state in a comment that an empty list is treated as best-effort evidence.