Let a project's config name a working branch per epic that chunks branch off and merge into
CI / tests (pull_request) Successful in 5m4s

This commit is contained in:
Lilleman
2026-11-20 19:32:59 +01:00
parent 9531e5c337
commit 72a4d346d7
33 changed files with 991 additions and 56 deletions
+129 -1
View File
@@ -55,6 +55,8 @@ class Checkout:
self.open = open
self.allowed = allowed
self.statuses: list[str] = []
# The epic's working branch the checkout records for the branch checked out; empty where it records none. Reading it is the one command the fake doesn't record.
self.chunk_base = ""
# 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.
@@ -82,9 +84,11 @@ class Checkout:
return failed
texts = {name: str(answer).strip() for name, answer in named.items()}
paths = {name: pathlib.Path(texts[name]) for name in ("plan", "changelog", "comprehension-history") if texts.get(name, "").startswith("/")}
return ProjectConfig("proj", pathlib.Path("/config"), pathlib.Path("/checkout"), paths, wait_for_approval=texts.get("wait-for-approval") == "true", merge=texts.get("merge", "chunk"), commit_title=texts.get("commit-title", "{summary}"), pull_request_title=texts.get("pull-request-title", "{summary}"), branch=texts.get("branch", "item-{id}"))
return ProjectConfig("proj", pathlib.Path("/config"), pathlib.Path("/checkout"), paths, wait_for_approval=texts.get("wait-for-approval") == "true", merge=texts.get("merge", "chunk"), commit_title=texts.get("commit-title", "{summary}"), pull_request_title=texts.get("pull-request-title", "{summary}"), branch=texts.get("branch", "item-{id}"), epic_branch=texts.get("epic-branch"), review_request_comment=texts.get("review-request-comment"))
def __call__(self, command: list[str]) -> str | Failure:
if command[:3] == ["git", "config", "--get"] and command[3].endswith(".chunk-base"):
return self.chunk_base
self.ran.append(command)
if command[0] == ("tea" if self.github else "gh"):
return Failure(f"{command[0]} on the wrong forge")
@@ -1025,6 +1029,130 @@ class Clean(unittest.TestCase):
self.assert_kept()
class EpicBranch(unittest.TestCase):
"""A chunk whose branch the checkout records as merging into the epic's working branch ABC-1, in a project whose config sets `epic-branch`."""
COMMENT_GH = ["gh", "pr", "comment", "12", "--body", "@reviewer review"]
COMMENT_TEA = ["tea", "comment", "12", "@reviewer review"]
def checkout(self, origin: str = GITHUB, open: bool = True, comment: bool = True) -> Checkout:
checkout = Checkout(origin, open=open)
checkout.chunk_base = "ABC-1\n"
checkout.answers[(PROJECT_CONFIG, "epic-branch")] = "{epic}\n"
if comment:
checkout.answers[(PROJECT_CONFIG, "review-request-comment")] = "@reviewer review\n"
checkout.answers[("git", "rev-list", "--count", "HEAD..origin/ABC-1")] = "0\n"
return checkout
def test_open_opens_the_draft_into_the_epic_branch(self):
for origin, create in ((GITHUB, ["gh", "pr", "create", "--draft", "--head", "item-7", "--base", "ABC-1", "--title", "Do it", "--body", ""]), (GITEA, ["tea", "pulls", "create", "--draft", "--head", "item-7", "--base", "ABC-1", "--title", "Do it", "--description", ""])):
with self.subTest(origin):
checkout = self.checkout(origin, open=False)
self.assertEqual(step(["open", "Do it"], checkout, checkout.config), "Opened draft pull request #12.")
self.assertIn(create, checkout.ran)
def test_sync_merges_the_epic_branch_not_the_default_branch(self):
checkout = self.checkout()
checkout.answers[("git", "rev-list", "--count", "HEAD..origin/ABC-1")] = "2\n"
self.assertIn("Merged origin/ABC-1 into item-7.", str(step(["sync"], checkout, checkout.config)))
self.assertIn(["git", "merge", "--quiet", "--no-edit", "origin/ABC-1"], checkout.ran)
self.assertNotIn(MERGE, checkout.ran)
def test_merge_merges_without_a_person_whatever_the_config_says_of_the_default_branch(self):
checkout = self.checkout()
checkout.answers[(PROJECT_CONFIG, "merge")] = "human\n"
checkout.answers[tuple(WAITS)] = "true\n"
checkout.decisions = ["REVIEW_REQUIRED"]
self.assertEqual(step(["merge"], checkout, checkout.config, parks=True), "Merged pull request #12 (merge).")
self.assertNotIn(GH_DECISION, checkout.ran)
def test_ready_asks_for_the_review_the_config_names(self):
for origin, comment in ((GITHUB, self.COMMENT_GH), (GITEA, self.COMMENT_TEA)):
with self.subTest(origin):
checkout = self.checkout(origin)
self.assertEqual(step(["ready"], checkout, checkout.config), "Marked pull request #12 ready, and asked for a review with “@reviewer review”.")
self.assertEqual(checkout.acted()[-1], comment)
def test_ready_asks_for_no_review_where_the_config_names_none_or_the_pull_request_goes_into_the_default_branch(self):
unnamed = self.checkout(comment=False)
self.assertEqual(step(["ready"], unnamed, unnamed.config), "Marked pull request #12 ready.")
into_default = self.checkout()
into_default.chunk_base = ""
self.assertEqual(step(["ready"], into_default, into_default.config), "Marked pull request #12 ready.")
for checkout in (unnamed, into_default):
self.assertNotIn(self.COMMENT_GH, checkout.ran)
def test_a_review_request_the_forge_refuses_fails_after_the_pull_request_is_ready(self):
checkout = self.checkout()
checkout.answers[tuple(self.COMMENT_GH)] = Failure("gh pr comment: denied")
self.assertEqual(step(["ready"], checkout, checkout.config), Failure("marked pull request #12 ready, but could not ask for a review: gh pr comment: denied"))
def test_push_pushes_the_branch_and_asks_for_a_review_of_the_new_head(self):
checkout = self.checkout()
self.assertEqual(step(["push"], checkout, checkout.config), "Pushed item-7, and asked for a review of pull request #12 with “@reviewer review”.")
self.assertEqual(checkout.acted(), [PUSH, self.COMMENT_GH])
def test_push_only_pushes_where_no_review_is_to_ask_for(self):
unopened, unnamed, into_default = self.checkout(open=False), self.checkout(comment=False), self.checkout()
into_default.chunk_base = ""
for checkout in (unopened, unnamed, into_default):
self.assertEqual(step(["push"], checkout, checkout.config), "Pushed item-7.")
self.assertEqual(checkout.acted(), [PUSH])
def test_a_failed_push_asks_for_no_review(self):
checkout = self.checkout()
checkout.answers[tuple(PUSH)] = Failure("git push: rejected")
self.assertEqual(step(["push"], checkout, checkout.config), Failure("git push: rejected"))
self.assertNotIn(self.COMMENT_GH, checkout.ran)
def epic(self, origin: str = GITHUB) -> Checkout:
"""A Checkout whose origin holds the epic branch ABC-1, level with main."""
checkout = self.checkout(origin)
checkout.answers[("git", "rev-list", "--count", "origin/ABC-1..origin/main")] = "0\n"
checkout.answers[("git", "rev-parse", "origin/ABC-1")] = "abc123\n"
return checkout
def test_open_epic_levels_the_epic_branch_and_opens_its_pull_request_into_the_default_branch_ready_for_a_person(self):
for origin, create in ((GITHUB, ["gh", "pr", "create", "--head", "ABC-1", "--base", "main", "--title", "Ship the epic", "--body", "What it holds"]), (GITEA, ["tea", "pulls", "create", "--head", "ABC-1", "--base", "main", "--title", "Ship the epic", "--description", "What it holds"])):
with self.subTest(origin):
checkout = self.epic(origin)
self.assertEqual(step(["open-epic", "ABC-1", "Ship the epic", "What it holds"], checkout, checkout.config), "origin/ABC-1 already holds origin/main. Opened the pull request from ABC-1 into main, which a person merges.")
self.assertEqual(checkout.acted()[-1], create)
self.assertIn(FETCH, checkout.ran)
def test_open_epic_merges_a_newer_default_branch_into_the_epic_branch_first(self):
checkout = self.epic()
checkout.answers[("git", "rev-list", "--count", "origin/ABC-1..origin/main")] = "3\n"
checkout.answers[("git", "merge-tree", "--write-tree", "origin/ABC-1", "origin/main")] = "7ree\n"
checkout.answers[("git", "commit-tree", "7ree", "-p", "origin/ABC-1", "-p", "origin/main", "-m", "Merge remote-tracking branch 'origin/main' into ABC-1")] = "c0mm17\n"
self.assertIn("Merged origin/main into origin/ABC-1. Opened", str(step(["open-epic", "ABC-1", "Ship the epic"], checkout, checkout.config)))
self.assertIn(["git", "push", "--quiet", "origin", "c0mm17:refs/heads/ABC-1"], checkout.ran)
def test_open_epic_leaves_a_conflict_with_the_default_branch_to_a_person_and_opens_nothing(self):
checkout = self.epic()
checkout.answers[("git", "rev-list", "--count", "origin/ABC-1..origin/main")] = "3\n"
checkout.answers[("git", "merge-tree", "--write-tree", "origin/ABC-1", "origin/main")] = Failure("git merge-tree: CONFLICT")
done = step(["open-epic", "ABC-1", "Ship the epic"], checkout, checkout.config)
self.assertIsInstance(done, Failure)
self.assertIn("a person merges it", done.reason)
self.assertFalse(any(command[:3] == ["gh", "pr", "create"] for command in checkout.ran))
def test_open_epic_opens_no_second_pull_request(self):
checkout = self.epic()
checkout.answers[("gh", "pr", "list", "--head", "ABC-1", *GH_LIST[5:])] = "31\tabc123\tfalse\n"
self.assertEqual(step(["open-epic", "ABC-1", "Ship the epic"], checkout, checkout.config), "origin/ABC-1 already holds origin/main. Pull request #31 from ABC-1 is open already.")
self.assertFalse(any(command[:3] == ["gh", "pr", "create"] for command in checkout.ran))
def test_open_epic_refuses_where_the_config_names_no_epic_branch_or_the_title_is_not_one_line(self):
unset = Checkout(GITHUB, open=False)
self.assertEqual(step(["open-epic", "ABC-1", "Ship the epic"], unset, unset.config), Failure("the project's config sets no `epic-branch`, so the epic has no branch to open a pull request from (`project-config --help`)"))
self.assertEqual(unset.ran, [])
checkout = self.epic()
self.assertEqual(step(["open-epic", "ABC-1", "Ship\nit"], checkout, checkout.config), Failure("give a title of one line"))
self.assertEqual(step(["open-epic", "ABC-1"], checkout, checkout.config), Failure(USAGE))
self.assertEqual(checkout.ran, [])
class Launcher(unittest.TestCase):
def test_help_exits_0_and_a_wrong_step_exits_1(self):
self.assertEqual(subprocess.run([str(TOOL), "--help"], capture_output=True, env=isolated_env()).returncode, 0)