diff --git a/bin/chunk_pr/test_chunk_pr.py b/bin/chunk_pr/test_chunk_pr.py index 46727ddc..409baae9 100644 --- a/bin/chunk_pr/test_chunk_pr.py +++ b/bin/chunk_pr/test_chunk_pr.py @@ -938,6 +938,8 @@ class Clean(unittest.TestCase): return f"12\t{self.merged_at}\tfalse\n" if self.merged_at else "" if command == ["tea", "api", "repos/{owner}/{repo}/pulls?state=closed&limit=50"] and self.url == GITEA: return json.dumps([{"number": 12, "merged": self.merged_at is not None, "head": {"ref": "refs/pull/12/head" if self.merged_at else "item-7", "label": "item-7", "sha": self.merged_at or self.head, "repo": {"full_name": "me/project"}}, "base": {"repo": {"full_name": "me/project"}}}]) + if command == ["tea", "api", "repos/{owner}/{repo}/pulls?state=closed&limit=50&page=2"] and self.url == GITEA: + return "[]" if command[0] != "git": return Failure(f"the fake forge doesn't answer {command}") done = subprocess.run(command, cwd=folder, capture_output=True, encoding="utf-8", env=isolated_env()) diff --git a/bin/deploy_release/test_deploy_release.py b/bin/deploy_release/test_deploy_release.py index 5cf69ada..b1737902 100644 --- a/bin/deploy_release/test_deploy_release.py +++ b/bin/deploy_release/test_deploy_release.py @@ -193,7 +193,9 @@ def merge(number, guard): git("update-ref", "refs/heads/main", head) pull(number)["state"] = "merged" opened = [found for found in state["pulls"] if found["state"] == "open"] -if forge == "tea" and args == ["api", "repos/{owner}/{repo}/pulls?state=open&limit=50"]: +if forge == "tea" and args == ["api", "repos/{owner}/{repo}/pulls?state=open&limit=50&page=2"]: + print("[]") +elif forge == "tea" and args == ["api", "repos/{owner}/{repo}/pulls?state=open&limit=50"]: repo = lambda found: {"full_name": "fork/r" if found["fork"] else "o/r"} print(json.dumps([{"number": found["number"], "head": {"ref": found["head"], "label": found["head"], "sha": sha(found), "repo": repo(found)}, "base": {"repo": {"full_name": "o/r"}}} for found in opened])) elif forge == "gh" and args[:3] == ["pr", "list", "--head"] and args[4:6] == ["--json", "number,headRefOid,isCrossRepository"]: diff --git a/bin/forge/pulls.py b/bin/forge/pulls.py index 9815ebd2..411e818f 100644 --- a/bin/forge/pulls.py +++ b/bin/forge/pulls.py @@ -13,7 +13,7 @@ from forge.reviews import GITEA_DRAFT POLL_SECONDS = 10 # How long checks_passed waits on Gitea for the checks before it gives up; CI took about 3 minutes in 2026-11. gh's watch waits as long as the checks run. WAIT_SECONDS = 30 * 60 -# How many pull requests one page of Gitea's list holds: its default cap. +# How many pull requests pull_at asks Gitea for in a page; Gitea's default cap is 50. PAGE_SIZE = 50 # What gh says of a pull request whose head has no check. NO_CHECKS = "no checks reported" @@ -37,7 +37,8 @@ def pull_at(forge: Forge, branch: str, head: str, state: typing.Literal["open", 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) - # Gitea lists at most PAGE_SIZE a page, newest first, and the pull request sought may be older than any number of them, so read each page until one matches or one comes back short. Gitea has no merged state: a merged pull request is a closed one marked merged. + # 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 while True: pulls = gitea(run, f"repos/{{owner}}/{{repo}}/pulls?state={'open' if state == 'open' else 'closed'}&limit={PAGE_SIZE}" + (f"&page={page}" if page > 1 else "")) @@ -48,8 +49,9 @@ def pull_at(forge: Forge, branch: str, head: str, state: typing.Literal["open", 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) 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 len(pulls) < PAGE_SIZE: + if found or not pulls or pulls == last: return found + last = pulls page += 1 diff --git a/bin/forge/test_forge.py b/bin/forge/test_forge.py index ff1a0cd7..080e819f 100644 --- a/bin/forge/test_forge.py +++ b/bin/forge/test_forge.py @@ -50,14 +50,14 @@ 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 gitea_pulls(newer: int, sought: dict, cap: int = 50) -> typing.Callable[[list[str]], str]: + """A fake tea that answers the pull request list like Gitea, newest first: `newer` others, then `sought`. A page holds at most `cap`.""" 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")) + limit, page = min(int(query["limit"]), cap), int(query.get("page", "1")) return json.dumps(everything[(page - 1) * limit:page * limit]) return run @@ -65,20 +65,27 @@ def gitea_pulls(newer: int, sought: dict) -> typing.Callable[[list[str]], str]: 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): + def test_finds_a_merged_pull_request_that_has_more_than_50_newer_ones(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_one_where_gitea_caps_a_page_below_the_size_asked(self): + self.assertEqual(merged_pull("tea", "item-3", "abc", gitea_pulls(70, self.SOUGHT, cap=30)), "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): + def test_asks_for_no_page_after_the_first_that_comes_back_empty(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"]) + self.assertEqual([endpoint.partition("&limit=50")[2] for endpoint in asked], ["", "&page=2", "&page=3"]) + + def test_stops_where_gitea_answers_the_same_page_to_every_request(self): + page = json.dumps([{"number": 1, "merged": True, "head": {"label": "x", "sha": "y", "repo": {"full_name": "o/r"}}, "base": {"repo": {"full_name": "o/r"}}}]) + self.assertIsNone(merged_pull("tea", "item-3", "abc", lambda command: page)) 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")