Resume a chunk that awaits a person's merge once its pull request no longer merges into its base
CI / tests (pull_request) Successful in 3m29s

This commit is contained in:
Lilleman
2026-11-19 21:37:01 +01:00
parent 005a23f431
commit 11ee0f7bbf
7 changed files with 101 additions and 12 deletions
+29 -1
View File
@@ -1,4 +1,4 @@
"""Finding a pull request from origin's own branch, and waiting for the checks on its head commit."""
"""Finding a pull request from origin's own branch, reading whether it merges into its base, and waiting for the checks on its head commit."""
import dataclasses
import fnmatch
import json
@@ -44,6 +44,34 @@ def pull_at(forge: Forge, branch: str, head: str, state: typing.Literal["open",
return Failure(f"tea api listed pull requests in a shape that can't be read: {str(pulls)[:200]}")
# Why a pull request doesn't merge into its base as it stands, as the words after "pull request #<N>".
CONFLICTS = "conflicts with its base branch"
BEHIND = "is behind its base branch, whose protection requires it up to date"
def unmergeable(forge: Forge, number: str, run: Run) -> str | None | Failure:
"""Why pull request number doesn't merge into its base as it stands: CONFLICTS or BEHIND. None where it merges, and while GitHub still computes whether it does.
GitHub reports a pull request as behind only where the base's protection requires an up-to-date branch. Gitea's `mergeable` never tells that case. It is also false for the seconds Gitea checks for conflicts after a push to either branch, which reads as CONFLICTS here.
"""
if forge == "gh":
viewed = run(["gh", "pr", "view", number, "--json", "mergeable,mergeStateStatus"])
if isinstance(viewed, Failure):
return viewed
try:
state = json.loads(viewed)
return CONFLICTS if state["mergeable"] == "CONFLICTING" else BEHIND if state["mergeStateStatus"] == "BEHIND" else None
except (ValueError, KeyError, TypeError):
return Failure(f"gh pr view printed no mergeability: {viewed.strip()[:200]}")
pull = gitea(run, f"repos/{{owner}}/{{repo}}/pulls/{number}")
if isinstance(pull, Failure):
return pull
try:
return CONFLICTS if pull["mergeable"] is False else None
except (KeyError, TypeError):
return Failure(f"tea api answered pull request {number} in a shape that can't be read: {str(pull)[:200]}")
def required_checks(forge: Forge, base: str, run: Run) -> set[str] | Failure:
"""The checks, or glob patterns of checks, that the forge's protection of the branch base requires before a merge. None on GitHub, where gh's watch covers them.
+37 -1
View File
@@ -6,7 +6,7 @@ import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from forge.pulls import CheckFailed, checks_now, checks_passed, open_pull, Pending, POLL_SECONDS, required_checks, WAIT_SECONDS
from forge.pulls import BEHIND, CheckFailed, checks_now, checks_passed, CONFLICTS, open_pull, Pending, POLL_SECONDS, required_checks, unmergeable, WAIT_SECONDS
from failure import Failure
from forge.commands import forge_of, gitea, url_beside
@@ -47,6 +47,42 @@ class OpenPull(unittest.TestCase):
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]}")
class Unmergeable(unittest.TestCase):
def gh(self, mergeable: str, state: str) -> str | None | Failure:
commands: list[list[str]] = []
answer = unmergeable("gh", "7", lambda command: commands.append(command) or f'{{"mergeable": "{mergeable}", "mergeStateStatus": "{state}"}}')
self.assertEqual(commands, [["gh", "pr", "view", "7", "--json", "mergeable,mergeStateStatus"]])
return answer
def test_a_github_pull_request_that_conflicts_with_its_base_says_so(self):
self.assertEqual(self.gh("CONFLICTING", "DIRTY"), CONFLICTS)
def test_a_github_pull_request_behind_a_base_that_requires_it_up_to_date_says_so(self):
self.assertEqual(self.gh("MERGEABLE", "BEHIND"), BEHIND)
def test_a_github_pull_request_merges_whatever_else_holds_it_and_while_github_computes(self):
for mergeable, state in (("MERGEABLE", "CLEAN"), ("MERGEABLE", "BLOCKED"), ("MERGEABLE", "UNSTABLE"), ("UNKNOWN", "UNKNOWN")):
with self.subTest(mergeable=mergeable, state=state):
self.assertIsNone(self.gh(mergeable, state))
def test_a_gitea_pull_request_conflicts_only_where_gitea_says_it_is_not_mergeable(self):
commands: list[list[str]] = []
self.assertEqual(unmergeable("tea", "7", lambda command: commands.append(command) or '{"number": 7, "mergeable": false}'), CONFLICTS)
self.assertEqual(commands, [["tea", "api", "repos/{owner}/{repo}/pulls/7"]])
self.assertIsNone(unmergeable("tea", "7", lambda command: '{"number": 7, "mergeable": true}'))
def test_fails_where_the_forge_cannot_be_read_or_answers_in_another_shape(self):
unread = Failure("no network")
for forge in ("gh", "tea"):
with self.subTest(forge=forge):
self.assertIs(unmergeable(forge, "7", lambda command: unread), unread)
self.assertEqual(unmergeable("gh", "7", lambda command: LONG).reason, f"gh pr view printed no mergeability: {LONG[:200]}")
self.assertEqual(unmergeable("gh", "7", lambda command: "not json").reason, "gh pr view printed no mergeability: not json")
self.assertEqual(unmergeable("gh", "7", lambda command: '{"mergeable": "MERGEABLE"}').reason, 'gh pr view printed no mergeability: {"mergeable": "MERGEABLE"}')
self.assertEqual(unmergeable("tea", "7", lambda command: LONG).reason, f"tea api answered pull request 7 in a shape that can't be read: {LONG[:200]}")
self.assertEqual(unmergeable("tea", "7", lambda command: '{"number": 7}').reason, "tea api answered pull request 7 in a shape that can't be read: {'number': 7}")
def tea(statuses: typing.Iterable[str], branch: str = '{"name": "main"}') -> typing.Callable[[list[str]], str]:
"""A tea answering each read of the head's status with the next of statuses, and of branch main with branch."""
answers = iter(statuses)