Merge remote-tracking branch 'origin/main' into item-541
CI / tests (pull_request) Has been cancelled

# Conflicts:
#	bin/forge/pulls.py
#	bin/forge/test_forge.py
#	docs/reading-cost.md
This commit is contained in:
2026-11-27 19:49:41 +01:00
22 changed files with 641 additions and 155 deletions
+8 -3
View File
@@ -24,19 +24,24 @@ 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 len(row) == 3 and row[2] == "false" and head in (None, row[1])), None)
# The pull request sought may be older than any number of others, so read pages, newest first, until one matches or the list ends: an empty page, or one that repeats the last. A short page doesn't end it, since Gitea's admin may cap a page below PAGE_SIZE. Gitea has no merged state: a merged pull request is a closed one marked merged.
last: typing.Any = None
page = 1
@@ -46,7 +51,7 @@ def pull_at(forge: Forge, branch: str, head: str, state: typing.Literal["open",
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)
found = 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]}")
if found or not pulls or pulls == last: