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
+35 -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, merged_pull, open_pull, other_base, Pending, POLL_SECONDS, ready_pulls, ReadyPull, required_checks, unmergeable, WAIT_SECONDS
from failure import Failure
from forge.commands import forge_of, gitea, runner, url_beside
@@ -50,6 +50,40 @@ 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]}")
def gitea_pulls(newer: int, sought: dict) -> typing.Callable[[list[str]], str]:
"""A fake tea listing `newer` pull requests, newest first, then `sought` last: it serves the `limit` and `page` of the endpoint like Gitea."""
def pull(number: int, label: str, sha: str, merged: bool) -> dict:
return {"number": number, "merged": merged, "head": {"label": label, "sha": sha, "repo": {"full_name": "o/r"}}, "base": {"repo": {"full_name": "o/r"}}}
everything = [pull(1000 - n, f"other-{n}", "f" * 40, True) for n in range(newer)] + [pull(sought["number"], sought["label"], sought["sha"], sought["merged"])]
def run(command: list[str]) -> str:
query = dict(part.split("=") for part in command[-1].split("?")[1].split("&"))
limit, page = int(query["limit"]), int(query.get("page", "1"))
return json.dumps(everything[(page - 1) * limit:page * limit])
return run
class PullsBeyondTheFirstPage(unittest.TestCase):
SOUGHT = {"number": 652, "label": "item-3", "sha": "abc", "merged": True}
def test_finds_a_merged_pull_request_that_more_than_50_newer_ones_follow(self):
self.assertEqual(merged_pull("tea", "item-3", "abc", gitea_pulls(120, self.SOUGHT)), "652")
def test_finds_one_that_is_the_first_after_a_full_page(self):
self.assertEqual(merged_pull("tea", "item-3", "abc", gitea_pulls(50, self.SOUGHT)), "652")
def test_finds_none_after_the_last_page_where_none_matches(self):
self.assertIsNone(merged_pull("tea", "item-9", "abc", gitea_pulls(120, self.SOUGHT)))
def test_asks_for_no_page_past_the_one_that_comes_back_short(self):
asked = []
run = gitea_pulls(70, self.SOUGHT)
merged_pull("tea", "item-9", "abc", lambda command: asked.append(command[-1]) or run(command))
self.assertEqual(asked, ["repos/{owner}/{repo}/pulls?state=closed&limit=50", "repos/{owner}/{repo}/pulls?state=closed&limit=50&page=2"])
def test_finds_an_open_pull_request_the_same_way(self):
self.assertEqual(open_pull("tea", "item-3", "abc", gitea_pulls(120, {**self.SOUGHT, "merged": False})), "652")
class Unmergeable(unittest.TestCase):
def gh(self, mergeable: str, state: str) -> str | None | Failure:
commands: list[list[str]] = []