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
+68 -3
View File
@@ -12,7 +12,7 @@ import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from chunk_pr.approval import awaited
from chunk_pr.main import cleaned, merged, QUEUE, step, turn, USAGE
from chunk_pr.main import Base, base_of, cleaned, merged, QUEUE, step, turn, USAGE
from failure import Failure
from test_env import isolated_env
from project_config.settings import ProjectConfig
@@ -22,8 +22,9 @@ from forge.reviews import THREADS
TOOL = pathlib.Path(__file__).resolve().parents[1] / "chunk-pr"
GITHUB = "git@github.com:me/project.git"
GITEA = "ssh://git@gitea.example.org:22/me/project.git"
HEAD = "c0ffee"
HEAD = "c0ffee" * 7
PUSH = ["git", "push", "--quiet", "--set-upstream", "origin", "item-7"]
OLD = "badc0de" * 6
GH_LIST = ["gh", "pr", "list", "--head", "item-7", "--json", "number,headRefOid,isCrossRepository", "--template", "{{range .}}{{.number}}\t{{.headRefOid}}\t{{.isCrossRepository}}\n{{end}}"]
TEA_LIST = ["tea", "api", "repos/{owner}/{repo}/pulls?state=open&limit=50"]
GH_SETTINGS = ["gh", "repo", "view", "--json", "mergeCommitAllowed,rebaseMergeAllowed,squashMergeAllowed"]
@@ -61,6 +62,8 @@ class Checkout:
self.chunk_base = ""
# The branch the forge merges the pull request into on each read; the last one repeats. Empty where the forge has the checkout's: chunk_base, else main.
self.bases: list[str] = []
# The heads GitHub lists pull request #12 at on each read; the last one repeats. Empty where it lists HEAD.
self.listed_heads: list[str] = []
# What GitHub reports as the pull request's review decision on each read; the last one repeats.
self.decisions = ["APPROVED"]
# Set, what GitHub prints instead of a decision's JSON.
@@ -101,7 +104,7 @@ class Checkout:
if command[2:3] == ["create"]:
self.open = True
if command == GH_LIST:
return f"12\t{HEAD}\tfalse\n" if self.open else ""
return f"12\t{self.listed_heads.pop(0) if len(self.listed_heads) > 1 else self.listed_heads[0] if self.listed_heads else HEAD}\tfalse\n" if self.open else ""
if command == TEA_LIST:
return json.dumps([{"number": 12, "head": {"ref": "item-7", "label": "item-7", "sha": HEAD, "repo": {"full_name": "me/project"}}, "base": {"repo": {"full_name": "me/project"}}}] if self.open else [])
if command == TEA_STATUS:
@@ -365,6 +368,13 @@ class Merge(unittest.TestCase):
self.assertEqual(merged(checkout, checkout.config, slept.append), "Merged pull request #12 (merge).")
self.assertEqual(sum(slept), 60)
def test_the_merge_step_waits_with_the_sleep_it_is_given(self):
checkout = Checkout(GITEA, open=True)
checkout.statuses = [json.dumps({"total_count": 0, "state": "pending", "statuses": []})] * 7
slept: list[float] = []
self.assertEqual(step(["merge"], checkout, checkout.config, sleep=slept.append), "Merged pull request #12 (merge).")
self.assertEqual(sum(slept), 60)
def requiring_approval(self, origin: str = GITHUB, *decisions: str) -> Checkout:
"""A Checkout whose project config sets wait-for-approval, and which reports each of decisions in turn as the review decision."""
checkout = Checkout(origin, open=True)
@@ -1080,6 +1090,27 @@ class EpicBranch(unittest.TestCase):
self.assertEqual(step(["merge"], checkout, checkout.config, parks=True), "Merged pull request #12 (merge).")
self.assertNotIn(GH_DECISION, checkout.ran)
def test_a_recorded_base_that_is_origins_default_branch_leaves_the_pull_request_to_a_person(self):
checkout = self.checkout()
checkout.chunk_base = "main\n"
checkout.answers[(PROJECT_CONFIG, "merge")] = "human\n"
done = step(["merge"], checkout, checkout.config)
self.assertIn("awaits a person's merge", str(done))
self.assertFalse(any("merge" in command[1:4] for command in checkout.acted() if command[0] != "git"), checkout.acted())
def test_a_branch_whose_base_git_does_not_record_merges_into_the_default_branch(self):
self.assertEqual(base_of("item-7", self.unrecorded), Base("main", False))
def test_a_recorded_base_is_an_epics_working_branch_unless_it_is_the_default_branch(self):
self.assertEqual(base_of("item-7", self.recorded("main")), Base("main", False))
self.assertEqual(base_of("item-7", self.recorded("ABC-1")), Base("ABC-1", True))
def unrecorded(self, command: list[str]) -> str | Failure:
return Failure("git config: exit 1") if command[:3] == ["git", "config", "--get"] else "origin/main\n"
def recorded(self, base: str) -> Run:
return lambda command: f"{base}\n" if command[:3] == ["git", "config", "--get"] else "origin/main\n"
def test_merge_fails_naming_both_branches_where_the_forge_merges_the_pull_request_into_another_branch_than_the_checkout_records(self):
for origin in (GITHUB, GITEA):
with self.subTest(origin):
@@ -1187,6 +1218,40 @@ class EpicBranch(unittest.TestCase):
self.assertEqual(step(["push"], checkout, checkout.config), "Pushed item-7.")
self.assertEqual(checkout.acted(), [PUSH])
def test_push_waits_for_the_forge_to_show_the_new_head_before_it_asks_for_a_review(self):
checkout, slept = self.checkout(), []
checkout.listed_heads = [OLD, OLD, OLD, OLD, HEAD]
self.assertEqual(step(["push"], checkout, checkout.config, sleep=slept.append), "Pushed item-7, and asked for a review of pull request #12 with “@reviewer review”.")
self.assertEqual(slept, [5, 5])
def test_push_fails_naming_the_pull_request_where_the_forge_still_shows_the_old_head_after_a_minute(self):
checkout, slept = self.checkout(), []
checkout.listed_heads = [OLD]
done = step(["push"], checkout, checkout.config, sleep=slept.append)
self.assertIsInstance(done, Failure)
for fact in ("pushed item-7", "pull request #12", f"{HEAD[:12]} as", "60 seconds"):
self.assertIn(fact, done.reason)
self.assertEqual(sum(slept), 60)
self.assertNotIn(self.COMMENT_GH, checkout.ran)
def test_push_asks_for_a_review_where_the_forge_shows_the_new_head_at_the_last_poll(self):
checkout, slept = self.checkout(), []
checkout.listed_heads = [OLD] * 24 + [HEAD]
self.assertIn("asked for a review of pull request #12", str(step(["push"], checkout, checkout.config, sleep=slept.append)))
self.assertEqual(sum(slept), 60)
def test_push_does_not_wait_where_the_pull_request_goes_into_the_default_branch(self):
checkout, slept = self.checkout(), []
checkout.chunk_base = ""
checkout.listed_heads = [OLD]
self.assertEqual(step(["push"], checkout, checkout.config, sleep=slept.append), "Pushed item-7.")
self.assertEqual(slept, [])
def test_push_does_not_wait_where_no_pull_request_is_open(self):
checkout, slept = self.checkout(open=False), []
self.assertEqual(step(["push"], checkout, checkout.config, sleep=slept.append), "Pushed item-7.")
self.assertEqual(slept, [])
def test_a_failed_push_asks_for_no_review(self):
checkout = self.checkout()
checkout.answers[tuple(PUSH)] = Failure("git push: rejected")