Find a Gitea pull request by its branch and head however many pull requests are newer
CI / tests (pull_request) Successful in 4m48s

This commit is contained in:
2026-11-27 15:54:05 +01:00
parent 07006793d4
commit afc9ee1653
3 changed files with 71 additions and 10 deletions
+16 -9
View File
@@ -13,6 +13,8 @@ from forge.reviews import GITEA_DRAFT
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
# How many pull requests one page of Gitea's list holds: its default cap.
PAGE_SIZE = 50
# What gh says of a pull request whose head has no check.
NO_CHECKS = "no checks reported"
@@ -35,15 +37,20 @@ def pull_at(forge: Forge, branch: str, head: str, state: typing.Literal["open",
return listed
rows = [line.split("\t") for line in listed.splitlines()]
return next((row[0] for row in rows if row[1:] == [head, "false"]), None)
# Gitea lists at most 50 a page; the pull request sought is among the newest. Gitea has no merged state: a merged pull request is a closed one marked merged.
pulls = gitea(run, f"repos/{{owner}}/{{repo}}/pulls?state={'open' if state == 'open' else 'closed'}&limit=50")
if isinstance(pulls, Failure):
return pulls
try:
# Match the label, not the ref: once origin deletes a merged branch, Gitea reports its ref as refs/pull/<N>/head.
return next((str(pull["number"]) for pull in pulls if pull["head"]["label"] == branch and pull["head"]["sha"] == head and pull["head"]["repo"]["full_name"] == pull["base"]["repo"]["full_name"] and (state == "open" or pull["merged"] is True)), None)
except (KeyError, TypeError):
return Failure(f"tea api listed pull requests in a shape that can't be read: {str(pulls)[:200]}")
# Gitea lists at most PAGE_SIZE a page, newest first, and the pull request sought may be older than any number of them, so read each page until one matches or one comes back short. Gitea has no merged state: a merged pull request is a closed one marked merged.
page = 1
while True:
pulls = gitea(run, f"repos/{{owner}}/{{repo}}/pulls?state={'open' if state == 'open' else 'closed'}&limit={PAGE_SIZE}" + (f"&page={page}" if page > 1 else ""))
if isinstance(pulls, Failure):
return pulls
try:
# Match the label, not the ref: once origin deletes a merged branch, Gitea reports its ref as refs/pull/<N>/head.
found = next((str(pull["number"]) for pull in pulls if pull["head"]["label"] == branch and pull["head"]["sha"] == head and pull["head"]["repo"]["full_name"] == pull["base"]["repo"]["full_name"] and (state == "open" or pull["merged"] is True)), None)
except (KeyError, TypeError):
return Failure(f"tea api listed pull requests in a shape that can't be read: {str(pulls)[:200]}")
if found or len(pulls) < PAGE_SIZE:
return found
page += 1
@dataclasses.dataclass(frozen=True)