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]}")
+9 -1
View File
@@ -9,7 +9,7 @@ import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from forge.pulls import BEHIND, CheckFailed, checks_now, checks_passed, CONFLICTS, open_pull, other_base, Pending, POLL_SECONDS, ready_pulls, ReadyPull, required_checks, unmergeable, WAIT_SECONDS
from forge.pulls import BEHIND, CheckFailed, checks_now, checks_passed, CONFLICTS, open_pull, other_base, Pending, POLL_SECONDS, pull_from, ready_pulls, ReadyPull, required_checks, unmergeable, WAIT_SECONDS
from failure import Failure
from forge.commands import forge_of, gitea, runner, url_beside
@@ -46,6 +46,14 @@ class Gitea(unittest.TestCase):
class OpenPull(unittest.TestCase):
def test_finds_the_open_pull_request_from_a_branch_whatever_its_head_is(self):
gh = "12\told\tfalse\n13\tfork\ttrue\n"
tea = json.dumps([{"number": 14, "head": {"label": "item-3", "sha": "old", "repo": {"full_name": "o/r"}}, "base": {"repo": {"full_name": "o/r"}}}, {"number": 15, "head": {"label": "item-3", "sha": "fork", "repo": {"full_name": "f/r"}}, "base": {"repo": {"full_name": "o/r"}}}])
self.assertEqual(pull_from("gh", "item-3", lambda command: gh), "12")
self.assertEqual(pull_from("tea", "item-3", lambda command: tea), "14")
self.assertEqual(open_pull("gh", "item-3", "new", lambda command: gh), None)
self.assertIsNone(pull_from("gh", "item-3", lambda command: "13\tfork\ttrue\n"))
def test_fails_quoting_the_first_200_characters_where_tea_lists_pull_requests_in_another_shape(self):
self.assertEqual(open_pull("tea", "item-3", "abc", lambda command: LONG).reason, f"tea api listed pull requests in a shape that can't be read: {LONG[:200]}")