Answer stability round 1: 1 🔴, 1 🟡 (0 misread); wait for a pull request at another head, and only where a review is owed

This commit is contained in:
2026-11-27 15:39:03 +01:00
parent 3b125bbcbd
commit c13f4cc154
4 changed files with 69 additions and 46 deletions
+8 -3
View File
@@ -22,26 +22,31 @@ def open_pull(forge: Forge, branch: str, head: str, run: Run) -> str | None | Fa
return pull_at(forge, branch, head, "open", run)
def pull_from(forge: Forge, branch: str, run: Run) -> str | None | Failure:
"""The number of the open pull request from origin's own branch, whatever its head is; None where there is none."""
return pull_at(forge, branch, None, "open", run)
def merged_pull(forge: Forge, branch: str, head: str, run: Run) -> str | None | Failure:
"""The number of the merged pull request from origin's own branch whose head was head; None where there is none. A rebase or a squash merge counts: the pull request keeps the head it had."""
return pull_at(forge, branch, head, "merged", run)
def pull_at(forge: Forge, branch: str, head: str, state: typing.Literal["open", "merged"], run: Run) -> str | None | Failure:
def pull_at(forge: Forge, branch: str, head: str | None, state: typing.Literal["open", "merged"], run: Run) -> str | None | Failure:
if forge == "gh":
# gh lists the open pull requests unless told another state.
listed = run(["gh", "pr", "list", "--head", branch, *(["--state", "merged"] if state == "merged" else []), "--json", "number,headRefOid,isCrossRepository", "--template", "{{range .}}{{.number}}\t{{.headRefOid}}\t{{.isCrossRepository}}\n{{end}}"])
if isinstance(listed, Failure):
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)
return next((row[0] for row in rows if row[1:] == ([head, "false"] if head is not None else row[1:2] + ["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)
return next((str(pull["number"]) for pull in pulls if pull["head"]["label"] == branch and (head is None or 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]}")