Read a pull request's checks and waiting review threads once, and fail chunk-pr merge at once where do-epic parks
This commit is contained in:
+55
-21
@@ -1,5 +1,7 @@
|
||||
"""Finding a pull request from origin's own branch, and waiting for the checks on its head commit."""
|
||||
import dataclasses
|
||||
import fnmatch
|
||||
import json
|
||||
import time
|
||||
import typing
|
||||
|
||||
@@ -10,6 +12,8 @@ from forge.commands import Forge, gitea, Run
|
||||
POLL_SECONDS = 10
|
||||
# How long checks_passed waits on Gitea for the checks before it gives up; CI took about 3 minutes in 2026-11. gh's watch waits as long as the checks run.
|
||||
WAIT_SECONDS = 30 * 60
|
||||
# What gh says of a pull request whose head has no check.
|
||||
NO_CHECKS = "no checks reported"
|
||||
|
||||
|
||||
def open_pull(forge: Forge, branch: str, head: str, run: Run) -> str | None | Failure:
|
||||
@@ -57,37 +61,67 @@ def required_checks(forge: Forge, base: str, run: Run) -> set[str] | Failure:
|
||||
return Failure(f"tea api answered branch {base} in a shape that can't be read: {str(branch)[:200]}")
|
||||
|
||||
|
||||
def checks_passed(forge: Forge, number: str, head: str, required: set[str], run: Run, sleep: typing.Callable[[float], None] = time.sleep, settle: int = 0) -> Failure | None:
|
||||
"""None once every check on pull request number, whose head commit is head, has passed, or where it has none for settle seconds; a Failure where one fails, or where they have not all passed within WAIT_SECONDS.
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class CheckFailed(Failure):
|
||||
"""A check on a pull request's head failed, which a new push answers. Any other Failure of a read of the checks says the forge could not be read."""
|
||||
|
||||
On Gitea it also waits for a passed check to match each of required: a check CI has yet to queue is no status at all.
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Pending:
|
||||
"""The checks on a pull request's head have not all passed yet, and none failed."""
|
||||
# The checks, or glob patterns, that the protection requires and no passed check matches yet. Empty where a check that reported is still running.
|
||||
missing: list[str]
|
||||
|
||||
|
||||
def checks_now(forge: Forge, number: str, head: str, required: set[str], run: Run, settled: bool = True) -> Pending | Failure | None:
|
||||
"""The checks on pull request number, whose head commit is head, read once: None where every one passed, Pending while one still runs, a CheckFailed where one failed, and any other Failure where the forge cannot be read.
|
||||
|
||||
On Gitea a passed check must also match each of required: a check CI has yet to queue is no status at all. A head without any check counts as passed once settled, and as pending before, since CI may not have queued its checks yet. gh reports no check as pending before it starts, so there such a head always counts as passed.
|
||||
"""
|
||||
if forge == "gh":
|
||||
listed = run(["gh", "pr", "checks", number, "--json", "bucket"])
|
||||
if isinstance(listed, Failure):
|
||||
return None if NO_CHECKS in listed.reason else listed
|
||||
try:
|
||||
buckets = {check["bucket"] for check in json.loads(listed)}
|
||||
except (ValueError, KeyError, TypeError):
|
||||
return Failure(f"gh pr checks printed no checks: {listed.strip()[:200]}")
|
||||
if buckets & {"fail", "cancel"}:
|
||||
return CheckFailed(f"a check on pull request {number} failed")
|
||||
return Pending([]) if "pending" in buckets else None
|
||||
status = gitea(run, f"repos/{{owner}}/{{repo}}/commits/{head}/status")
|
||||
if isinstance(status, Failure):
|
||||
return status
|
||||
try:
|
||||
count, state = status["total_count"], status["state"]
|
||||
passed = [check["context"] for check in status.get("statuses") or [] if check["status"] == "success"]
|
||||
except (KeyError, TypeError, AttributeError):
|
||||
return Failure(f"tea api printed no commit status for {head}: {str(status)[:200]}")
|
||||
if state not in ("pending", "success"):
|
||||
return CheckFailed(f"a check on pull request {number} ended in {state}")
|
||||
# Gitea matches a required context as a glob, and reads a commit with no status as pending, with total_count 0.
|
||||
missing = sorted(pattern for pattern in required if not fnmatch.filter(passed, pattern))
|
||||
return None if (count == 0 and settled or state == "success") and not missing else Pending(missing)
|
||||
|
||||
|
||||
def checks_passed(forge: Forge, number: str, head: str, required: set[str], run: Run, sleep: typing.Callable[[float], None] = time.sleep, settle: int = 0) -> Failure | None:
|
||||
"""None once every check on pull request number, whose head commit is head, has passed, or where it has none for settle seconds; a Failure where one fails, or where they have not all passed within WAIT_SECONDS. checks_now says what counts as passed on Gitea."""
|
||||
if forge == "gh":
|
||||
for waited in range(0, settle + 1, POLL_SECONDS):
|
||||
watched = run(["gh", "pr", "checks", number, "--watch", "--fail-fast"])
|
||||
if not isinstance(watched, Failure):
|
||||
return None
|
||||
if "no checks reported" not in watched.reason:
|
||||
if NO_CHECKS not in watched.reason:
|
||||
return watched
|
||||
if waited < settle:
|
||||
sleep(POLL_SECONDS)
|
||||
return None
|
||||
for _ in range(WAIT_SECONDS // POLL_SECONDS + 1):
|
||||
status = gitea(run, f"repos/{{owner}}/{{repo}}/commits/{head}/status")
|
||||
if isinstance(status, Failure):
|
||||
return status
|
||||
try:
|
||||
count, state = status["total_count"], status["state"]
|
||||
passed = [check["context"] for check in status.get("statuses") or [] if check["status"] == "success"]
|
||||
except (KeyError, TypeError, AttributeError):
|
||||
return Failure(f"tea api printed no commit status for {head}: {str(status)[:200]}")
|
||||
if state not in ("pending", "success"):
|
||||
return Failure(f"a check on pull request {number} ended in {state}")
|
||||
# Gitea matches a required context as a glob, and reads a commit with no status as pending, with total_count 0.
|
||||
missing = sorted(pattern for pattern in required if not fnmatch.filter(passed, pattern))
|
||||
if (count == 0 and _ * POLL_SECONDS >= settle or state == "success") and not missing:
|
||||
return None
|
||||
if _ < WAIT_SECONDS // POLL_SECONDS:
|
||||
polls = WAIT_SECONDS // POLL_SECONDS
|
||||
for poll in range(polls + 1):
|
||||
now = checks_now(forge, number, head, required, run, settled=poll * POLL_SECONDS >= settle)
|
||||
if not isinstance(now, Pending):
|
||||
return now
|
||||
if poll < polls:
|
||||
sleep(POLL_SECONDS)
|
||||
waited_on = f"{', '.join(missing)}, which the protection requires, never passed" if missing else "a check is still pending"
|
||||
waited_on = f"{', '.join(now.missing)}, which the protection requires, never passed" if now.missing else "a check is still pending"
|
||||
return Failure(f"gave up on pull request {number} after {WAIT_SECONDS // 60} minutes: {waited_on}")
|
||||
|
||||
Reference in New Issue
Block a user