diff --git a/README.md b/README.md index d2d6cce6..d72bab15 100644 --- a/README.md +++ b/README.md @@ -121,7 +121,7 @@ A project whose pull requests need a human's approval sets `wait-for-approval = A project whose team merges its pull requests itself sets `merge = "human"`. `chunk-pr merge` then brings the branch level with origin's default branch, finishes the chunk's plan items, waits for the checks and marks the pull request ready, but leaves the merge to a person. A plan or changelog kept in the checkout is finished on the branch, so it lands with that merge; one kept in Jira, or in the config's folder, is finished at once; where you close the pull request unmerged, file the item again. A do-epic chunk parks until the pull request is merged, and its window's name says `awaits your merge`. The chunk resumes and brings the branch level again once the pull request conflicts with the default branch. It does so too once the branch is behind a default branch whose protection, on GitHub, requires an up-to-date branch. loop-claude doesn't start a chunk for an item that a ready pull request finishes. Once no item left in the release can start and one awaits a merge, loop-claude stops and names the pull requests to merge; run it again after you merge one. The first chunk after a merge runs `chunk-pr clean` in the worktree the merged pull request left, where nobody pushed to the pull request after the chunk did. Nobody works on a pull request that awaits your merge. To have a reviewer's comment answered, turn the pull request back into a draft: a chunk then takes its item up again. Left out, the setting is `"chunk"`: the chunk merges. -A project whose team wants an epic's work in one pull request sets `epic-branch`, a template with `{epic}` for the epic's key, such as `epic-branch = "{epic}"`. Under loop-claude the epic is the Jira epic the config's `plan` names, so a plan kept in a file gets no working branch. Under do-epic it is the epic do-epic drives. Each chunk then branches off the epic's working branch, here `ABC-123` for the epic ABC-123, and `chunk-pr` opens its pull request into that branch and merges it there without a person, also under `merge = "human"` and `wait-for-approval`: those two keep guarding the default branch. `chunk-preflight`, and do-epic as a chunk starts, make the branch on origin off the default branch, or merge the default branch into it with a merge commit, which needs git 2.38 or later. Where that merge conflicts, they stop, and a person merges it. Once the epic's last issue has landed, do-epic's finishing chunk runs `chunk-pr open-epic `, which brings the branch level once more and opens the pull request from it into the default branch. Under loop-claude, run it yourself. A person merges that pull request, and its checks and reviews are theirs to answer. An auto-reviewer that reviews only pull requests into the default branch is asked by a comment: set `review-request-comment` to the comment's text, such as `"@reviewbot review"`. `chunk-pr ready` then posts it on a pull request into the working branch, and `chunk-pr push` pushes later commits and posts it again. +A project whose team wants an epic's work in one pull request sets `epic-branch`, a template with `{epic}` for the epic's key, such as `epic-branch = "{epic}"`. Under loop-claude the epic is the Jira epic the config's `plan` names, so a plan kept in a file gets no working branch. Under do-epic it is the epic do-epic drives. Each chunk then branches off the epic's working branch, here `ABC-123` for the epic ABC-123, and `chunk-pr` opens its pull request into that branch and merges it there without a person, also under `merge = "human"` and `wait-for-approval`: those two keep guarding the default branch. A pull request that was open into the default branch when you set `epic-branch` stays yours to settle: `chunk-pr ready` and `merge` fail on it, naming both branches, until you change its base on the forge to the working branch, or merge it yourself. `chunk-preflight`, and do-epic as a chunk starts, make the branch on origin off the default branch, or merge the default branch into it with a merge commit, which needs git 2.38 or later. Where that merge conflicts, they stop, and a person merges it. Once the epic's last issue has landed, do-epic's finishing chunk runs `chunk-pr open-epic <epic's key> <title>`, which brings the branch level once more and opens the pull request from it into the default branch. Under loop-claude, run it yourself. A person merges that pull request, and its checks and reviews are theirs to answer. An auto-reviewer that reviews only pull requests into the default branch is asked by a comment: set `review-request-comment` to the comment's text, such as `"@reviewbot review"`. `chunk-pr ready` then posts it on a pull request into the working branch, and `chunk-pr push` pushes later commits and posts it again. For a client's project, use one sandbox VM per client, so that one client's code, transcripts and credentials never share a machine with another's. What the client's repo should not hold lives in the config's folder, at paths written as `folder:<path>`, as `project-config --help` shows: the plan, the working rules in an `AGENTS.md` I write for the agents, the records, and the goals and audience, as `goals-and-audience = "folder:goals.md"`. Name that file `goals.md`, so the rules for goals and audience load when an agent writes it. A file outside the checkout has no commits in the checkout's history, so four things are not supported for it yet. `score-outcomes` and loop-claude's routing figures count a project's chunks only where `todo.md`, `docs/comprehension-history.md` and `docs/reading-cost.md` are at those paths in the checkout; otherwise each says so, and the loop goes on. For a plan or a comprehension history outside the checkout, `larv-review` says it cannot tell whether a scoring run is owed, and for such a history it cannot find a paired ruling's base. For a plan outside the checkout, chunk-preflight and loop-claude don't see a release whose last item has shipped, and say that its release review and its cut are yours to call. And rule-hook checks a plan outside the checkout on every call, not only when it changed. diff --git a/bin/chunk_pr/main.py b/bin/chunk_pr/main.py index 13452e4d..2ececd93 100644 --- a/bin/chunk_pr/main.py +++ b/bin/chunk_pr/main.py @@ -13,13 +13,17 @@ pull request only while its head is the commit checked out, so push before you r Where the project's config sets `epic-branch` (`project-config --help`), a chunk's pull request goes into its epic's working branch, which chunk-preflight or do-epic recorded for the chunk's -branch when the chunk began. There, each step below reads "origin's default branch" as that -branch, with these differences: +branch as it started or continued the chunk. There, each step below reads "origin's default +branch" as that branch, with these differences: - merge merges the pull request itself, whatever `merge` and `wait-for-approval` say: those govern the pull request into the default branch, which open-epic opens; - ready and push ask for a review, where the config sets `review-request-comment`, by making that comment on the pull request; - clean still fast-forwards the main checkout to origin's default branch. +ready and merge fail, naming both branches, where the forge merges the pull request into another +branch than the one recorded, or than the default branch where the checkout records none. A person +then settles which holds: they change the pull request's base on the forge, or the record, which +is `branch.<branch>.chunk-base` in git's config. open pushes the branch, setting its upstream, and opens a draft pull request into origin's default branch. <title> is one line, as ~/.claude/CLAUDE.md → "Git, commits, and PRs" @@ -99,7 +103,8 @@ open-epic runs in any checkout of the repository, once the epic's last chunk ha that branch into the default branch, ready and titled <title>, and leaves it to a person to merge. Where the two don't merge cleanly it doesn't open anything and says how a person merges them. Where the pull request is open already, it doesn't open a second - one. It fails where the config sets no `epic-branch`, or origin has no such branch. + one. It fails where the config sets no `epic-branch`, or origin has no such branch, as + once its pull request has merged and the forge deleted it. It prints what it did, exit 0, or why it failed, exit 1. """ @@ -115,8 +120,8 @@ import time import typing from chunk_pr.approval import approval, approved, awaited, left_to_a_person, left_to_park, PARKS, unanswered -from forge.pulls import checks_passed, merged_pull, open_pull, required_checks -from git_cli.git_repo import CHUNK_BASE +from forge.pulls import checks_passed, merged_pull, open_pull, other_base, required_checks +from git_cli.git_repo import CHUNK_BASE, Git from git_cli import epic_branch from failure import Failure from forge.commands import Forge, forge_of, gitea, Run, runner @@ -239,7 +244,7 @@ def base_of(branch: str, run: Run) -> Base | Failure: def pull(run: Run) -> tuple[Forge, str, str] | Failure: - """The forge, the number of the branch's open pull request at its head, and that head.""" + """The forge, the number of the branch's open pull request at its head, and that head. A Failure where the forge merges that pull request into another branch than `base_of` names: that base says whether a person merges, so no step acts while the two differ.""" found = checkout(run) if isinstance(found, Failure): return found @@ -247,7 +252,12 @@ def pull(run: Run) -> tuple[Forge, str, str] | Failure: number = open_pull(forge, branch, head, run) if number is None: return Failure(f"no pull request from origin's {branch} is open at {head[:12]}; push the branch, or open one with chunk-pr open") - return number if isinstance(number, Failure) else (forge, number, head) + if isinstance(number, Failure): + return number + base = base_of(branch, run) + if isinstance(base, Failure): + return base + return other_base(forge, number, base.branch, run) or (forge, number, head) def opened(title: str, body: str, run: Run) -> str | Failure: @@ -402,10 +412,12 @@ def epic_opened(epic: str, title: str, body: str, run: Run, config: Config) -> s if isinstance(default, Failure): return default forge = forge_of(found[0]) + git: Git = lambda *args, cwd=None, timeout=None, guarded=False, errors="strict": run(["git", *args]) # `levelled` makes a branch origin lacks, which here would be a stray one: a mistyped key, or a branch its merge deleted. - if isinstance(held := answers(run, ["fetch", "--quiet", "origin"]), Failure) or isinstance(run(["git", "rev-parse", "--quiet", "--verify", f"refs/remotes/origin/{branch}"]), Failure): - return held if isinstance(held, Failure) else Failure(f"origin has no branch {branch}, so no chunk of {epic} has merged into one: check the epic's key, and whether the branch's pull request merged already") - level = epic_branch.levelled(branch, f"origin/{default}", run=lambda *args, cwd=None, timeout=None, guarded=False, errors="strict": run(["git", *args])) + held = epic_branch.on_origin(branch, None, None, git) + if not held: + return Failure(f"origin has no branch {branch}, so no chunk of {epic} has merged into one: check the epic's key, and whether the branch's pull request merged already") + level = held if isinstance(held, Failure) else epic_branch.levelled(branch, f"origin/{default}", run=git) head = level if isinstance(level, Failure) else answers(run, ["rev-parse", f"origin/{branch}"]) number = head if isinstance(head, Failure) else open_pull(forge, branch, head[0], run) if isinstance(number, Failure): @@ -518,6 +530,9 @@ def merge_turn(pull_request: Merging, finishes: typing.Sequence[list[str]], conf asked = unanswered(pull_request.number, run) if pull_request.waits else None if asked is not None: return asked + # A person may have changed the pull request's base since the turn began, as while its checks ran. + if moved := other_base(pull_request.forge, pull_request.number, pull_request.base, run): + return moved if pull_request.forge == "gh": done = run(["gh", "pr", "merge", pull_request.number, pull_request.method, "--match-head-commit", str(head)]) else: diff --git a/bin/chunk_pr/test_chunk_pr.py b/bin/chunk_pr/test_chunk_pr.py index 562ee6df..d7d9f6ea 100644 --- a/bin/chunk_pr/test_chunk_pr.py +++ b/bin/chunk_pr/test_chunk_pr.py @@ -38,6 +38,8 @@ WAITS = [str(pathlib.Path(__file__).resolve().parents[1] / "project-config"), "w # The settings a fake checkout's answers hold, each under `project-config <setting>` as the tool once read it. PROJECT_CONFIG = str(pathlib.Path(__file__).resolve().parents[1] / "project-config") GH_DECISION = ["gh", "pr", "view", "12", "--json", "reviewDecision"] +GH_BASE = ["gh", "pr", "view", "12", "--json", "baseRefName"] +TEA_PULL = ["tea", "api", "repos/{owner}/{repo}/pulls/12"] GH_THREADS = ["gh", "api", "graphql", "-f", f"query={THREADS}", "-F", "owner={owner}", "-F", "repo={repo}", "-F", "number=12"] BEHIND = ["git", "rev-list", "--count", "HEAD..origin/main"] COMMON = ["git", "rev-parse", "--path-format=absolute", "--git-common-dir"] @@ -57,6 +59,8 @@ class Checkout: 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 = "" + # 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] = [] # 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. @@ -102,6 +106,9 @@ class Checkout: 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: return self.statuses.pop(0) if self.statuses else json.dumps({"total_count": 1, "state": "success", "statuses": [{"context": "CI / tests (pull_request)", "status": "success"}]}) + if command in (GH_BASE, TEA_PULL): + base = (self.bases.pop(0) if len(self.bases) > 1 else self.bases[0]) if self.bases else self.chunk_base.strip() or "main" + return json.dumps({"baseRefName": base, "base": {"ref": base}}) if command == GH_DECISION and self.unreadable is not None: return self.unreadable if command == GH_DECISION: @@ -116,7 +123,7 @@ class Checkout: def acted(self) -> list[list[str]]: """What it ran beyond reading the checkout, the pull requests and the repository's settings.""" - reads = [GH_LIST, GH_DECISION, TEA_LIST, GH_SETTINGS, TEA_SETTINGS, OWED, GH_CHECKS, TEA_BRANCH, TEA_STATUS, GH_THREADS] + reads = [GH_LIST, GH_DECISION, GH_BASE, TEA_LIST, TEA_PULL, GH_SETTINGS, TEA_SETTINGS, OWED, GH_CHECKS, TEA_BRANCH, TEA_STATUS, GH_THREADS] return [command for command in self.ran if tuple(command) not in self.answers and command not in reads] @@ -335,7 +342,7 @@ class Merge(unittest.TestCase): def test_github_waits_for_the_checks_before_merging(self): checkout = Checkout(GITHUB, open=True) self.assertEqual(step(["merge"], checkout, checkout.config), "Merged pull request #12 (merge).") - self.assertEqual(checkout.ran[-2:], [GH_CHECKS, ["gh", "pr", "merge", "12", "--merge", "--match-head-commit", HEAD]]) + self.assertEqual(checkout.ran[-3:], [GH_CHECKS, GH_BASE, ["gh", "pr", "merge", "12", "--merge", "--match-head-commit", HEAD]]) def test_a_failed_check_on_github_leaves_it_unmerged(self): checkout = Checkout(GITHUB, open=True) @@ -428,7 +435,7 @@ class Merge(unittest.TestCase): def test_an_approved_pull_request_merges_after_the_checks_and_the_approval(self): checkout = self.requiring_approval() self.assertEqual(step(["merge"], checkout, checkout.config), "Merged pull request #12 (merge).") - self.assertEqual(checkout.ran[-4:], [GH_CHECKS, GH_DECISION, GH_THREADS, ["gh", "pr", "merge", "12", "--merge", "--match-head-commit", HEAD]]) + self.assertEqual(checkout.ran[-5:], [GH_CHECKS, GH_DECISION, GH_THREADS, GH_BASE, ["gh", "pr", "merge", "12", "--merge", "--match-head-commit", HEAD]]) def test_it_waits_a_minute_at_a_time_while_the_branch_protection_still_wants_a_review(self): checkout = self.requiring_approval(GITHUB, "REVIEW_REQUIRED", "REVIEW_REQUIRED", "APPROVED") @@ -1029,6 +1036,11 @@ class Clean(unittest.TestCase): self.assert_kept() +def elsewhere(forge: str, checkout: str) -> str: + """Why a step fails on pull request #12 where the forge merges it into the branch forge and the checkout takes the branch checkout as its base.""" + return f"pull request #12 merges into {forge} on the forge, but this checkout takes {checkout} as the base of its branch; a person settles which holds, and changes the pull request's base on the forge or the branch's `chunk-base` in git's config" + + 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`.""" @@ -1066,6 +1078,44 @@ 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_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): + checkout = self.checkout(origin) + checkout.answers[(PROJECT_CONFIG, "merge")] = "human\n" + checkout.bases = ["main"] + self.assertEqual(step(["merge"], checkout, checkout.config), Failure(elsewhere("main", "ABC-1"))) + self.assertEqual(changes(checkout), [], "neither levelled, merged nor marked ready") + + def test_merge_fails_naming_both_branches_where_the_forge_merges_into_an_epic_branch_a_pull_request_the_checkout_records_no_base_for(self): + checkout = self.checkout() + checkout.chunk_base, checkout.bases = "", ["ABC-1"] + self.assertEqual(step(["merge"], checkout, checkout.config), Failure(elsewhere("ABC-1", "main"))) + self.assertEqual(changes(checkout), []) + + def test_merge_merges_no_pull_request_whose_base_a_person_changed_while_its_checks_ran(self): + for origin in (GITHUB, GITEA): + with self.subTest(origin): + checkout = self.checkout(origin) + checkout.answers[(PROJECT_CONFIG, "merge")] = "human\n" + checkout.bases = ["ABC-1", "main"] + checkout.answers[("tea", "api", "repos/{owner}/{repo}/branches/ABC-1")] = json.dumps({"enable_status_check": False}) + self.assertEqual(step(["merge"], checkout, checkout.config), Failure(elsewhere("main", "ABC-1"))) + self.assertEqual(changes(checkout), []) + self.assertEqual([command for command in checkout.ran if command in (GH_BASE, TEA_PULL, GH_CHECKS, TEA_STATUS)][-2:], [GH_CHECKS, GH_BASE] if origin == GITHUB else [TEA_STATUS, TEA_PULL], "read again once the checks passed") + + def test_ready_marks_no_pull_request_ready_that_the_forge_merges_into_another_branch_than_the_checkout_records(self): + checkout = self.checkout() + checkout.bases = ["main"] + self.assertEqual(step(["ready"], checkout, checkout.config), Failure(elsewhere("main", "ABC-1"))) + self.assertEqual(checkout.acted(), []) + + def test_a_pull_requests_base_the_forge_cannot_tell_fails_the_merge(self): + checkout = self.checkout() + checkout.answers[tuple(GH_BASE)] = Failure("gh pr view: no network") + self.assertEqual(step(["merge"], checkout, checkout.config), Failure("gh pr view: no network")) + self.assertEqual(changes(checkout), []) + 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): @@ -1141,9 +1191,12 @@ class EpicBranch(unittest.TestCase): self.assertEqual(step(["push"], checkout, checkout.config), Failure("git push: rejected")) self.assertNotIn(self.COMMENT_GH, checkout.ran) + ON_ORIGIN = ("git", "ls-remote", "--heads", "origin", "refs/heads/ABC-1") + 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[self.ON_ORIGIN] = "abc123\trefs/heads/ABC-1\n" 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 @@ -1182,12 +1235,18 @@ class EpicBranch(unittest.TestCase): 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_makes_no_branch_where_origin_has_none_for_the_epic(self): + def test_open_epic_makes_no_branch_where_origin_has_none_for_the_epic_though_the_checkout_still_tracks_one_its_merge_deleted(self): checkout = self.epic() - checkout.answers[("git", "rev-parse", "--quiet", "--verify", "refs/remotes/origin/ABC-1")] = Failure("git rev-parse: no such ref") + checkout.answers[self.ON_ORIGIN] = "" self.assertEqual(step(["open-epic", "ABC-1", "Ship the epic"], checkout, checkout.config), Failure("origin has no branch ABC-1, so no chunk of ABC-1 has merged into one: check the epic's key, and whether the branch's pull request merged already")) self.assertFalse(any(command[:2] in (["git", "push"], ["gh", "pr"]) for command in checkout.ran)) + def test_open_epic_fails_with_gits_reason_where_origin_cannot_say_whether_it_has_the_branch(self): + checkout = self.epic() + checkout.answers[self.ON_ORIGIN] = Failure("git ls-remote: offline") + self.assertEqual(step(["open-epic", "ABC-1", "Ship the epic"], checkout, checkout.config), Failure("git ls-remote: offline")) + self.assertFalse(any(command[:2] in (["git", "push"], ["gh", "pr"]) 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" diff --git a/bin/chunk_preflight/test_chunk_preflight.py b/bin/chunk_preflight/test_chunk_preflight.py index 0be222a0..2ec1ee14 100644 --- a/bin/chunk_preflight/test_chunk_preflight.py +++ b/bin/chunk_preflight/test_chunk_preflight.py @@ -13,7 +13,7 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from project_config.templates import BRANCH from release_plan.next_chunk import CarriedOver, NoItem from project_config.settings import Epic, ProjectConfig -from chunk_preflight.main import epic_branch_of, item_worktree, line_count, ran, seconds, TIMEOUT +from chunk_preflight.main import epic_branch_of, item_facts as facts_made, item_worktree, line_count, ran, seconds, TIMEOUT from failure import Failure from chunk_preflight.gathered import Gathered, ItemFacts, LeftWorktree, NamedPath, Ran, Worktree from git_cli import git_repo @@ -637,6 +637,25 @@ class EpicBranchWorktree(unittest.TestCase): self.assertIsNone(epic_branch_of(dataclasses.replace(config, epic_branch=None))) self.assertEqual(epic_branch_of(dataclasses.replace(config, epic=None)), Failure("none made: the config sets `epic-branch`, but its `plan` names no Jira epic to name the branch by; chunk-preflight makes an epic's chunks only from a plan kept in Jira")) + def test_the_facts_of_an_item_put_its_worktree_on_the_epic_branch_they_are_given(self): + facts = facts_made(self.repo.root, "origin/main", planned(TODO, CUT, "origin/main", BRANCH), 30, True, "ABC-1") + self.assertEqual(facts.worktree.path, self.repo.temp / "proj-item-3") + self.assertTrue(facts.worktree.how.endswith("The chunk's pull request goes into ABC-1."), facts.worktree.how) + self.assertEqual(self.repo.git(self.repo.root, "config", "--get", "branch.item-3.chunk-base"), "ABC-1\n") + + def test_the_facts_of_an_item_make_no_worktree_where_the_epic_branch_cannot_be_named(self): + unnamed = Failure("none made: the plan names no epic") + self.assertEqual(facts_made(self.repo.root, "origin/main", planned(TODO, CUT, "origin/main", BRANCH), 30, True, unnamed).worktree, unnamed) + self.assertFalse((self.repo.temp / "proj-item-3").exists()) + + def test_the_tool_makes_no_worktree_where_the_config_sets_an_epic_branch_and_keeps_its_plan_in_a_file(self): + config = self.repo.temp / "home" / ".claude-projects" / "proj" / "config.toml" + config.write_text(config.read_text() + 'epic-branch = "{epic}"\n') + done = self.repo.preflight("--worktree") + self.assertEqual(done.returncode, 0, done.stderr) + self.assertIn("none made: the config sets `epic-branch`, but its `plan` names no Jira epic to name the branch by", done.stdout) + self.assertFalse((self.repo.temp / "proj-item-3").exists()) + class OpenPulls(unittest.TestCase): def test_lists_gh_rows_as_they_come_and_only_teas_rows_from_the_branch(self): diff --git a/bin/do_epic/gates.py b/bin/do_epic/gates.py index e21e52a8..41b94784 100644 --- a/bin/do_epic/gates.py +++ b/bin/do_epic/gates.py @@ -5,7 +5,7 @@ import dataclasses import typing from forge.reviews import ANSWERED, APPROVED, CHANGES_REQUESTED, review_decision, REVIEW_REQUIRED, waiting_threads -from forge.pulls import CheckFailed, checks_now, open_pull, Pending, required_checks, unmergeable +from forge.pulls import CheckFailed, checks_now, open_pull, other_base, Pending, required_checks, unmergeable from git_cli.git_repo import CHUNK_BASE from failure import Failure from forge.commands import forge_of, Run @@ -46,7 +46,7 @@ class Unmergeable(Open): def gate(branch: str, checkout: str, at: typing.Callable[[str], Run], by_human: bool) -> Gate | Open | Failure: - """What the chunk on branch waits on, in a worktree of checkout; at gives the runner of commands in a folder, and by_human says the project's config leaves the merge to a person (`merge = "human"`). A Failure where git or the forge cannot be read, which doesn't say anything about the gate. + """What the chunk on branch waits on, in a worktree of checkout; at gives the runner of commands in a folder, and by_human says the project's config leaves the merge to a person (`merge = "human"`). A Failure where git or the forge cannot be read, which doesn't say anything about the gate. A Failure too where the forge merges the pull request into another branch than the checkout takes as its base (`forge.pulls.other_base`): the caller chose by_human from that base. A draft never waits on a gate: its worker still works on it. On Gitea only the checks are a gate, since chunk-pr reads approvals on GitHub only (`chunk_pr.approval.awaited`). Changes a reviewer requested are news until the worker answered them (`forge.reviews.answered`); from then on the pull request awaits a new review. Where a person merges, a pull request with nothing else to wait on awaits that merge. The chunk has news again once the pull request is no longer open. It has news too once the pull request no longer merges into its base as it stands (`forge.pulls.unmergeable`): the worker then levels the branch for that person. """ @@ -58,10 +58,10 @@ def gate(branch: str, checkout: str, at: typing.Callable[[str], Run], by_human: failed = next((answer for answer in read if isinstance(answer, Failure)), None) if failed is not None: return failed - origin, head, base = (str(answer).strip() for answer in read) + origin, head, default = (str(answer).strip() for answer in read) # A chunk of an epic that keeps a working branch merges into that branch, so its protection names the checks. epic_base = run(["git", "config", "--get", f"branch.{branch}.{CHUNK_BASE}"]) - base = base if isinstance(epic_base, Failure) or not epic_base.strip() else epic_base.strip() + base = default.removeprefix("origin/") if isinstance(epic_base, Failure) or not epic_base.strip() else epic_base.strip() forge = forge_of(origin) pull = open_pull(forge, branch, head, run) if not isinstance(pull, str): @@ -69,7 +69,8 @@ def gate(branch: str, checkout: str, at: typing.Callable[[str], Run], by_human: decision = review_decision(forge, pull, head, run) if not isinstance(decision, str): return decision or Open(f"pull request #{pull} is a draft") - required = required_checks(forge, base.removeprefix("origin/"), run) + # do-epic takes the base as saying who merges, so the forge must merge the pull request into it too. + required = other_base(forge, pull, base, run) or required_checks(forge, base, run) checks = required if isinstance(required, Failure) else checks_now(forge, pull, head, required, run) if isinstance(checks, CheckFailed): return Open(f"{checks.reason}; fix it and push") diff --git a/bin/do_epic/main.py b/bin/do_epic/main.py index b7b864cf..4896e58c 100644 --- a/bin/do_epic/main.py +++ b/bin/do_epic/main.py @@ -4,6 +4,7 @@ After each cycle of a chunk in a repo, the window reads what the chunk's pull request waits on (`gates`). While that is a gate only time or a person passes, the chunk is parked (`parked`): the window frees its running slot and polls the forge, and the chunk's next cycle starts once the worker has something to do and a slot is free. `do-epic <EPIC-KEY> --status` prints what each open issue does.""" import dataclasses +import enum import pathlib import sys import typing @@ -79,6 +80,8 @@ STATUS_FLAG = "--status" DRY_RUN_FLAG = "--dry-run" # How often a parked chunk reads its pull request's gate, and a human task's window reads the epic: a person answers in hours, and each read is a few calls. POLL_SECONDS = 60 +# A call to origin that levels an epic's working branch is stopped after this long, chunk-preflight's limit for the same calls: one that hangs would hold the chunk's running slot before any cycle, with nothing said. +LEVEL_SECONDS = 60 # The store of an epic's issues and its record, for the epic with this key. Backend = typing.Callable[[DoEpicConfig, str], tuple[EpicStore, EpicRecord]] @@ -304,7 +307,7 @@ def chunk_run(key: str, epic: EpicRun) -> Stop: return issue if issue and isinstance(recorded(issue, epic.config.label_repos), HumanTask): human_tasks.awaited(issue, epic.key, epic.store, epic.config.label_repos, epic.config.unblocking, tmux.Window(epic.session, key, pane), POLL_SECONDS, epic.paths.home, epic.effects) - return handed_on(key, epic, humans=True) + return handed_on(key, epic, Landed.WITH_A_HUMAN) place = chunk_root(key, issue, epic) if isinstance(place, Stop): return place @@ -321,7 +324,7 @@ def chunk_run(key: str, epic: EpicRun) -> Stop: loop = Loop(root=root, session=session, window=tmux.Window(epic.session, key, pane), paths=epic.paths, env=epic.env, launcher=epic.launcher, effects=epic.effects) run = ChunkRun() # What a chunk that was parked is told as it resumes; empty for a chunk that wasn't. - news = parked_through(repo, loop, epic, told=None, finished=False) if repo is not None else "" + news = parked_through(repo, loop, epic, told=None, finished=False, based=based) if repo is not None else "" while True: if isinstance(news, Stop): return news @@ -339,10 +342,10 @@ def chunk_run(key: str, epic: EpicRun) -> Stop: if isinstance(closed, Stop): return closed run = closed - news = parked_through(repo, loop, epic, told=news, finished=isinstance(concluded.verdict, Finished)) if repo is not None else "" + news = parked_through(repo, loop, epic, told=news, finished=isinstance(concluded.verdict, Finished), based=based) if repo is not None else "" # A chunk that was parked has work again, whatever the judge ruled before its pull request passed the gate. if isinstance(concluded.verdict, Finished) and news == "": - return handed_on(key, epic, repo is not None, to_epic_branch=based is not None) + return handed_on(key, epic, Landed.ON_EPIC_BRANCH if based is not None else Landed.ON_DEFAULT_BRANCH if repo is not None else Landed.NOWHERE) def epic_based(key: str, root: str, epic: EpicRun) -> str | None | Stop: @@ -354,7 +357,7 @@ def epic_based(key: str, root: str, epic: EpicRun) -> str | None | Stop: return None branch = templates.epic_branch(config.epic_branch, epic.key) default = epic.effects.git("symbolic-ref", "--short", "refs/remotes/origin/HEAD", cwd=root) - level = default if isinstance(default, Failure) else epic_branch.levelled(branch, default.strip(), cwd=root, run=epic.effects.git) + level = default if isinstance(default, Failure) else epic_branch.levelled(branch, default.strip(), cwd=root, timeout=LEVEL_SECONDS, run=epic.effects.git) readied = level if isinstance(level, Failure) else record_chunk_base(key, branch, cwd=root, run=epic.effects.git) if isinstance(readied, Failure): return Stop(1, f"cannot ready the epic's working branch {branch} in {root} for {key}: {readied.reason}; once that is settled, run `do-epic {epic.key}` to start the chunk again") @@ -371,8 +374,8 @@ def issue_started(key: str, epic: EpicRun) -> None: say(f"{key} stays assigned to {other}", epic.effects, stdout=True) -def parked_through(repo: str, loop: Loop, epic: EpicRun, told: str | None, finished: bool) -> str | Stop: - """Keeps the chunk in loop's window parked. Its work is in the repo called repo. It stays parked for as long as its pull request waits on a gate, and returns once its worker has something to do: what to tell that worker, or empty where the chunk was never parked. told is the news its last cycle's worker got, None where the window just started, and finished says the judge ruled the chunk finished. A window that just started parks the chunk only if its file says so: the forge isn't read for a chunk that starts afresh. +def parked_through(repo: str, loop: Loop, epic: EpicRun, told: str | None, finished: bool, based: str | None) -> str | Stop: + """Keeps the chunk in loop's window parked. Its work is in the repo called repo. It stays parked for as long as its pull request waits on a gate, and returns once its worker has something to do: what to tell that worker, or empty where the chunk was never parked. told is the news its last cycle's worker got, None where the window just started, and finished says the judge ruled the chunk finished. based is the epic's working branch that `epic_based` recorded as the chunk's base when the window started, None where it recorded none. A window that just started parks the chunk only if its file says so: the forge isn't read for a chunk that starts afresh. Where a person merges, a worker ends its run with the pull request open, so the judge may rule the chunk finished before it is. For such a finished chunk, a gate that cannot be read is read again until it can, and a pull request no longer open, in a worktree that still stands, is news the worker hasn't been told: the worker removes the worktree. A pull request that doesn't merge into its base is news after every cycle that leaves it so: the worker levels the branch. @@ -385,8 +388,8 @@ def parked_through(repo: str, loop: Loop, epic: EpicRun, told: str | None, finis config = config_located(loop.root, epic.paths.home) if isinstance(config, Failure): return Stop(1, config.reason) - # A pull request into an epic's working branch merges without a person, whatever `merge` says. - poll = Poll(key, repo, config.merge == "human" and config.epic_branch is None, folder, loop, epic) + # A pull request into an epic's working branch merges without a person, whatever `merge` says. The base recorded says so, not the config: the gate fails where the forge merges the pull request elsewhere. + poll = Poll(key, repo, config.merge == "human" and based is None, folder, loop, epic) if key in recorded_as: return resumed_news(poll, recorded_as[key].on, shown=None) entered = None if told is None else poll.first_gate(again=finished) @@ -401,7 +404,7 @@ def parked_through(repo: str, loop: Loop, epic: EpicRun, told: str | None, finis return "" if uncleaned in (told or "") else uncleaned if failed := poll.record(entered): return Stop(1, failed.reason) - if config.epic_branch is not None and (status := epic.config.pull_open_status): + if based is not None and (status := epic.config.pull_open_status): # The team's automation moves an issue only from a pull request into the default branch. if isinstance(moved := epic.store.move_issue(key, status), Failure): say(f"{key} did not move to {status!r} though its pull request is open: {moved.reason}", epic.effects, stdout=True) @@ -520,13 +523,23 @@ def folder_made(root: str) -> Failure | None: return None -def handed_on(key: str, epic: EpicRun, in_repo: bool = False, humans: bool = False, to_epic_branch: bool = False) -> Stop: - """The stop of a do-epic whose chunk finished. It first moves the chunk's issue to done (the epic's finishing has none), and its split item too where that was the last of the item's sub-tasks, so that no finished chunk starts again and what they block may start, then starts each chunk that may start now. Where the team's automation moves issues, others move two kinds of issue. The automation moves the issue of a chunk in a repo, which in_repo marks, from its pull request: the stop waits for that move (`moved_by_automation`). The human moves their own task, which humans marks: the stop doesn't move or await anything. A chunk without a repo has no pull request, so its issue moves to done as ever, and so does a split item, once Jira counts each of its sub-tasks done. The automation doesn't see a pull request that merged into an epic's working branch, which to_epic_branch marks: the stop moves that chunk's issue to the status `jira-pull-request-merged-status` names. Where none is named, it moves the issue to done, or stops where the automation moves issues, since then nobody would move it. do-epic's window closes as it exits.""" +class Landed(enum.Enum): + """Where a finished chunk's work went, which decides who moves its issue.""" + # No repo took it, as for the epic's finishing and a chunk that changes none. + NOWHERE = enum.auto() + # A human did the task. + WITH_A_HUMAN = enum.auto() + ON_DEFAULT_BRANCH = enum.auto() + ON_EPIC_BRANCH = enum.auto() + + +def handed_on(key: str, epic: EpicRun, landed: Landed = Landed.NOWHERE) -> Stop: + """The stop of a do-epic whose chunk finished. It first moves the chunk's issue to done (the epic's finishing has none), and its split item too where that was the last of the item's sub-tasks, so that no finished chunk starts again and what they block may start, then starts each chunk that may start now. Where the team's automation moves issues, others move two kinds of issue. The automation moves the issue of a chunk whose work landed on the default branch, from its pull request: the stop waits for that move (`moved_by_automation`). The human moves their own task: the stop doesn't move or await anything. A chunk without a repo has no pull request, so its issue moves to done as ever, and so does a split item, once Jira counts each of its sub-tasks done. The automation doesn't see a pull request that merged into an epic's working branch: the stop moves that chunk's issue to the status `jira-pull-request-merged-status` names. Where none is named, it moves the issue to done, or stops where the automation moves issues, since then nobody would move it. do-epic's window closes as it exits.""" automated = epic.config.automation_moves # The epic's finishing has no issue to move: the epic is closed by `--close-epic`. Nor has a human's task where the human moves it: its window waited until it holds up no issue. - if key == epic.key or (humans and automated): + if key == epic.key or (landed is Landed.WITH_A_HUMAN and automated): issues: list[EpicIssue] | Failure = epic.store.read() - elif to_epic_branch and (status := epic.config.pull_merged_status): + elif landed is Landed.ON_EPIC_BRANCH and (status := epic.config.pull_merged_status): moved = epic.store.move_issue(key, status) # A chunk that never parked left its issue before the open status, from which a workflow may offer no way to the merged one. if isinstance(moved, Failure) and (opened := epic.config.pull_open_status) and not isinstance(epic.store.move_issue(key, opened), Failure): @@ -536,9 +549,9 @@ def handed_on(key: str, epic: EpicRun, in_repo: bool = False, humans: bool = Fal issues = epic.store.read() # A search may not show the move yet, so the issue counts as moved here whatever the read says. issues = issues if isinstance(issues, Failure) else [dataclasses.replace(each, status=status) if each.key == key else each for each in issues] - elif to_epic_branch and automated: + elif landed is Landed.ON_EPIC_BRANCH and automated: return Stop(1, f"the chunk finished, but its issue stays as it is: its pull request merged into the epic's working branch, where the team's automation moves no issue, and do-epic's config names no `{PULL_MERGED_STATUS}` to move it to. Move {key} in Jira, then run `do-epic {epic.key}` to start the chunks it blocks") - elif in_repo and automated: + elif landed is Landed.ON_DEFAULT_BRANCH and automated: issues = moved_by_automation(key, epic) else: moved = epic.store.finish_issue(key) diff --git a/bin/do_epic/test_gates.py b/bin/do_epic/test_gates.py index a9a3d6e7..681268da 100644 --- a/bin/do_epic/test_gates.py +++ b/bin/do_epic/test_gates.py @@ -28,7 +28,7 @@ def comment(by: str, at: str) -> dict: class Forge: """Stands in for git and the forge's CLI in the checkout and the chunk's worktree; `ran` holds each folder a command ran in. Only the CLI of origin's forge answers: `gh` fails on a Gitea origin, and `tea` on a GitHub one.""" - def __init__(self, origin: str = "git@github.com:o/billing.git", pulls: str = f"7\t{HEAD}\tfalse\n", draft: bool = False, decision: str = "REVIEW_REQUIRED", buckets: tuple[str, ...] = ("pass",), threads: tuple[dict, ...] = (), status: str = '{"state": "success", "total_count": 1}', mergeable: str = "MERGEABLE", merge_state: str = "CLEAN", worktrees: str = WORKTREES, fails: str = "", reviews: tuple[dict, ...] = (), comments: tuple[dict, ...] = (), title: str = "Add x") -> None: + def __init__(self, origin: str = "git@github.com:o/billing.git", pulls: str = f"7\t{HEAD}\tfalse\n", draft: bool = False, decision: str = "REVIEW_REQUIRED", buckets: tuple[str, ...] = ("pass",), threads: tuple[dict, ...] = (), status: str = '{"state": "success", "total_count": 1}', mergeable: str = "MERGEABLE", merge_state: str = "CLEAN", worktrees: str = WORKTREES, fails: str = "", reviews: tuple[dict, ...] = (), comments: tuple[dict, ...] = (), title: str = "Add x", base: str = "main") -> None: self.cli = "gh" if "github.com" in origin else "tea" self.ran: list[str] = [] self.commands: list[list[str]] = [] @@ -40,11 +40,11 @@ class Forge: "git symbolic-ref": "origin/main\n", "git config": Failure("git config: unset"), "gh pr list": pulls, - "gh pr view": json.dumps({"isDraft": draft, "reviewDecision": decision, "author": {"login": "worker"}, "reviews": list(reviews), "comments": list(comments), "mergeable": mergeable, "mergeStateStatus": merge_state}), + "gh pr view": json.dumps({"isDraft": draft, "reviewDecision": decision, "author": {"login": "worker"}, "reviews": list(reviews), "comments": list(comments), "mergeable": mergeable, "mergeStateStatus": merge_state, "baseRefName": base}), "gh pr checks": json.dumps([{"bucket": bucket} for bucket in buckets]), "gh api graphql": json.dumps({"data": {"repository": {"pullRequest": {"author": {"login": "worker"}, "reviewThreads": {"nodes": list(threads)}}}}}), "tea api repos/{owner}/{repo}/pulls?state=open&limit=50": json.dumps([{"number": 7, "merged": False, "head": {"label": "ABC-21", "sha": HEAD, "repo": {"full_name": "o/billing"}}, "base": {"repo": {"full_name": "o/billing"}}}]), - "tea api repos/{owner}/{repo}/pulls/7": json.dumps({"number": 7, "title": title, "mergeable": mergeable == "MERGEABLE"}), + "tea api repos/{owner}/{repo}/pulls/7": json.dumps({"number": 7, "title": title, "mergeable": mergeable == "MERGEABLE", "base": {"ref": base}}), "tea api repos/{owner}/{repo}/branches/main": '{"name": "main"}', "tea api repos/{owner}/{repo}/commits/" + HEAD + "/status": status, } @@ -122,13 +122,23 @@ class GateOfAChunk(unittest.TestCase): self.assertEqual(Forge(gitea).gate(), Open("pull request #7 passed its checks")) def test_a_chunk_that_merges_into_an_epics_working_branch_reads_the_checks_that_branch_requires(self): - forge = Forge("https://gitea.larvit.se/o/billing.git") + forge = Forge("https://gitea.larvit.se/o/billing.git", base="ABC-12") forge.answers["git config"] = "ABC-12\n" forge.answers["tea api repos/{owner}/{repo}/branches/ABC-12"] = '{"enable_status_check": true, "status_check_contexts": ["epic-gate"]}' self.assertEqual(forge.gate(), Gate("/code/billing-ABC-21", "7", CHECKS)) self.assertIn(["git", "config", "--get", "branch.ABC-21.chunk-base"], forge.commands) self.assertNotIn(["tea", "api", "repos/{owner}/{repo}/branches/main"], forge.commands) + def test_a_pull_request_the_forge_merges_into_another_branch_than_the_checkout_records_is_a_failure_naming_both(self): + differs = "pull request #7 merges into {} on the forge, but this checkout takes {} as the base of its branch; a person settles which holds, and changes the pull request's base on the forge or the branch's `chunk-base` in git's config" + for origin in ("git@github.com:o/billing.git", "https://gitea.larvit.se/o/billing.git"): + with self.subTest(origin): + recorded = Forge(origin, decision="APPROVED") + recorded.answers["git config"] = "ABC-12\n" + self.assertEqual(recorded.gate(by_human=False), Failure(differs.format("main", "ABC-12")), "no pull request into the default branch reads as one that merges without a person") + self.assertNotIn(["tea", "api", "repos/{owner}/{repo}/branches/ABC-12"], recorded.commands) + self.assertEqual(Forge(origin, decision="APPROVED", base="ABC-12").gate(by_human=True), Failure(differs.format("ABC-12", "main"))) + def test_a_gitea_draft_awaits_nothing_though_its_checks_run(self): gitea = "https://gitea.larvit.se/o/billing.git" for title in ("WIP: Add x", "[wip] Add x"): diff --git a/bin/do_epic/test_main.py b/bin/do_epic/test_main.py index e04f24aa..50008da0 100644 --- a/bin/do_epic/test_main.py +++ b/bin/do_epic/test_main.py @@ -1024,28 +1024,40 @@ class AnEpicsWorkingBranch(DoEpicTest): self.store = MemoryEpic("ABC-12", [self.issues[0], issue("ABC-22", "do-epic-claude", blockers=("ABC-20",))]) self.backend = lambda config, key: (self.store, EpicRecord(Description(self.description))) # type: ignore[method-assign,arg-type] self.git_calls: list[tuple[tuple[str, ...], str | None]] = [] + self.git_timeouts: list[tuple[str, object]] = [] def settings(self, more: str) -> None: (self.home / ".claude-projects" / "do-epic" / "config.toml").write_text(SETTINGS + more, encoding="utf-8") def with_git(self, fake: FakeEffects, **answers: str | Failure) -> FakeEffects: """fake, whose git answers each command by its first word: as answers has it, else as a checkout whose origin has the branch ABC-12, which holds origin/main.""" - answered = {"symbolic-ref": "origin/main\n", "fetch": "", "rev-parse": "aaa\n", "rev-list": "0\n", "config": ""} | {name.replace("_", "-"): answer for name, answer in answers.items()} + answered = {"symbolic-ref": "origin/main\n", "fetch": "", "ls-remote": "aaa\trefs/heads/ABC-12\n", "rev-parse": "aaa\n", "rev-list": "0\n", "config": ""} | {name.replace("_", "-"): answer for name, answer in answers.items()} def git(*args: str, cwd: str | None = None, **more: object) -> str | Failure: self.git_calls.append((args, cwd)) + self.git_timeouts.append((args[0], more.get("timeout"))) return answered.get(args[0], Failure("no git")) fake.effects = lambda made=fake.effects(): dataclasses.replace(made, git=git) # type: ignore[method-assign] return fake + def recorded(self, **answers: object) -> Forge: + """A forge like `awaiting`'s, in a checkout that records ABC-12 as the base of ABC-20's branch.""" + forge = awaiting(**answers) + forge.answers["git config"] = "ABC-12\n" + return forge + def test_a_chunk_that_starts_levels_the_branch_and_records_it_as_the_chunks_base(self) -> None: fake = self.with_git(FakeEffects()) out = io.StringIO() with contextlib.redirect_stdout(out): self.assertEqual(main.epic_based("ABC-20", self.checkout, self.epic_run(fake)), "ABC-12") - self.assertEqual(self.git_calls, [(args, self.checkout) for args in (("symbolic-ref", "--short", "refs/remotes/origin/HEAD"), ("fetch", "--quiet", "origin"), ("rev-parse", "--quiet", "--verify", "refs/remotes/origin/ABC-12"), ("rev-list", "--count", "origin/ABC-12..origin/main"), ("config", "branch.ABC-20.chunk-base", "ABC-12"))]) + self.assertEqual(self.git_calls, [(args, self.checkout) for args in (("symbolic-ref", "--short", "refs/remotes/origin/HEAD"), ("fetch", "--quiet", "origin"), ("ls-remote", "--heads", "origin", "refs/heads/ABC-12"), ("rev-list", "--count", "origin/ABC-12..origin/main"), ("config", "branch.ABC-20.chunk-base", "ABC-12"))]) self.assertIn("origin/ABC-12 already holds origin/main; ABC-20's pull request goes into ABC-12\n", out.getvalue()) + def test_a_call_to_origin_that_levels_the_branch_is_stopped_after_a_minute_so_no_window_hangs_before_its_first_cycle(self) -> None: + main.epic_based("ABC-20", self.checkout, self.epic_run(self.with_git(FakeEffects(), ls_remote="", push=""))) + self.assertEqual([timed for timed in self.git_timeouts if timed[0] in ("fetch", "ls-remote", "push")], [("fetch", 60), ("ls-remote", 60), ("push", 60), ("fetch", 60)]) + def test_says_the_default_branchs_whole_name_however_it_ends(self) -> None: out = io.StringIO() with contextlib.redirect_stdout(out): @@ -1083,7 +1095,7 @@ class AnEpicsWorkingBranch(DoEpicTest): def test_its_issue_moves_as_the_pull_request_opens_and_merges_and_no_person_merges_it(self) -> None: self.settings('jira-unblocking-statuses = ["Release Ready"]\njira-automation-moves-issues = true\njira-pull-request-open-status = "Code Review"\njira-pull-request-merged-status = "Release Ready"\n') - self.forges = [awaiting(), awaiting(), awaiting(decision="APPROVED")] + self.forges = [self.recorded(base="ABC-12"), self.recorded(base="ABC-12"), self.recorded(base="ABC-12", decision="APPROVED")] fake = self.with_git(FakeEffects(streams=[self.worker(), self.worker()], claude=[judged("finished"), judged("finished")], windows=PARKED_WINDOW)) code, out, err = self.run_main(["--chunk", "ABC-20", "ABC-12"], fake, MODELS) prompts = [what[what.index("-p") + 1] for seam, what in fake.calls if seam == "stream_claude"] @@ -1097,10 +1109,42 @@ class AnEpicsWorkingBranch(DoEpicTest): self.assertIn("The chunk's pull request goes into the epic's working branch `ABC-12`", prompts[0]) self.assertIn(("config", "branch.ABC-20.chunk-base", "ABC-12"), [args for args, _ in self.git_calls]) + def test_a_chunk_parked_on_a_persons_merge_into_the_default_branch_stays_parked_until_its_pull_request_goes_into_the_branch(self) -> None: + self.settings('jira-unblocking-statuses = ["Release Ready"]\njira-pull-request-merged-status = "Release Ready"\n') + session = main.session_named("A ABC-12 do-epic ABC-20", paths_from(self.home).claude_code_transcripts) + parked.park(self.home / ".local/state/do-epic/ABC-12", parked.Parked("ABC-20", "/code/api-ABC-20", "7", str(session.id), "your merge")) + self.worker()(["--session-id", str(session.id)]) + # The pull request was opened into main before the setting was on; a person then changes its base to ABC-12. + self.forges = [self.recorded(decision="APPROVED"), self.recorded(decision="APPROVED"), self.recorded(base="ABC-12", decision="APPROVED")] + fake = self.with_git(FakeEffects(streams=[self.worker()], claude=[judged("finished")], windows="ABC-20 awaits your merge\t0\t@9\n")) + code, out, err = self.run_main(["--chunk", "ABC-20", "ABC-12"], fake, MODELS) + prompts = [what[what.index("-p") + 1] for seam, what in fake.calls if seam == "stream_claude"] + self.assertEqual(code, 0, err) + self.assertEqual(out.count("cannot read the gate: pull request #7 merges into main on the forge, but this checkout takes ABC-12 as the base of its branch; a person settles which holds, and changes the pull request's base on the forge or the branch's `chunk-base` in git's config; trying again every 60 seconds\n"), 1) + self.assertEqual(len(prompts), 1) + self.assertTrue(prompts[0].startswith("do-epic parked the chunk while its pull request awaited a person's merge. It resumes now: pull request #7 passed its checks and is approved. "), prompts[0]) + + def test_a_chunk_that_started_before_the_setting_was_on_keeps_its_merge_for_a_person(self) -> None: + self.settings('jira-unblocking-statuses = ["Release Ready"]\njira-pull-request-open-status = "Code Review"\njira-pull-request-merged-status = "Release Ready"\n') + set_on = self.repo_config.read_text(encoding="utf-8") + self.repo_config.write_text(set_on.replace('epic-branch = "{epic}"\n', ""), encoding="utf-8") + worker = self.worker() + + def turns_the_setting_on(args: list[str]) -> object: + self.repo_config.write_text(set_on, encoding="utf-8") + return worker(args) + self.forges = [awaiting(decision="APPROVED"), awaiting(decision="APPROVED"), awaiting(pulls=""), CLEANED] + fake = self.with_git(FakeEffects(streams=[turns_the_setting_on, self.worker()], claude=[judged("unfinished"), judged("finished")], windows="ABC-20 awaits your merge\t0\t@9\n")) + code, out, err = self.run_main(["--chunk", "ABC-20", "ABC-12"], fake, MODELS) + self.assertEqual(code, 0, err) + self.assertNotIn("config", [args[0] for args, _ in self.git_calls], "no base recorded, so the pull request goes into the default branch") + self.assertIn("parked: pull request #7 awaits your merge\n", out) + self.assertNotIn("move ABC-20 to Code Review", self.store.writes) + def test_an_issue_that_cannot_move_to_the_open_status_says_so_and_its_chunk_goes_on(self) -> None: self.settings('jira-unblocking-statuses = ["Release Ready"]\njira-pull-request-open-status = "Code Review"\njira-pull-request-merged-status = "Release Ready"\n') self.backend = lambda config, key: (StepwiseStore(self.store, {"Code Review": "QA"}), EpicRecord(Description(self.description))) # type: ignore[method-assign,arg-type] - self.forges = [awaiting(), awaiting(), awaiting(decision="APPROVED")] + self.forges = [self.recorded(base="ABC-12"), self.recorded(base="ABC-12"), self.recorded(base="ABC-12", decision="APPROVED")] fake = self.with_git(FakeEffects(streams=[self.worker(), self.worker()], claude=[judged("finished"), judged("finished")], windows=PARKED_WINDOW)) code, out, err = self.run_main(["--chunk", "ABC-20", "ABC-12"], fake, MODELS) self.assertEqual(code, 0, err) @@ -1109,7 +1153,7 @@ class AnEpicsWorkingBranch(DoEpicTest): def test_a_finished_chunk_whose_issue_has_no_pull_request_into_the_epic_branch_moves_it_to_done(self) -> None: self.settings('jira-unblocking-statuses = ["Release Ready"]\njira-pull-request-merged-status = "Release Ready"\n') with contextlib.redirect_stdout(io.StringIO()): - self.assertEqual(main.handed_on("ABC-20", self.epic_run(FakeEffects(windows="")), humans=True).code, 0) + self.assertEqual(main.handed_on("ABC-20", self.epic_run(FakeEffects(windows="")), main.Landed.WITH_A_HUMAN).code, 0) self.assertEqual((self.store.issues[0].status, self.store.issues[0].done), ("Done", True)) # A chunk not marked as in a repo moves its issue to done itself, even where the automation moves issues. self.settings('jira-unblocking-statuses = ["Release Ready"]\njira-automation-moves-issues = true\n') @@ -1124,11 +1168,11 @@ class AnEpicsWorkingBranch(DoEpicTest): lagging = LaggingStore(self.store, before) out = io.StringIO() with contextlib.redirect_stdout(out): - stop = main.handed_on("ABC-20", dataclasses.replace(self.epic_run(FakeEffects(windows="")), store=lagging), in_repo=True, to_epic_branch=True) + stop = main.handed_on("ABC-20", dataclasses.replace(self.epic_run(FakeEffects(windows="")), store=lagging), main.Landed.ON_EPIC_BRANCH) self.assertEqual(stop.code, 0, stop.reason) self.assertIn("the chunk finished; started ABC-22\n", out.getvalue()) unread = LaggingStore(self.store, Failure("Jira answered 503")) - stop = main.handed_on("ABC-22", dataclasses.replace(self.epic_run(FakeEffects(windows="")), store=unread), in_repo=True, to_epic_branch=True) + stop = main.handed_on("ABC-22", dataclasses.replace(self.epic_run(FakeEffects(windows="")), store=unread), main.Landed.ON_EPIC_BRANCH) self.assertEqual(stop, main.Stop(1, "the chunk finished, but the next could not start: Jira answered 503")) def test_the_epics_finishing_is_due_once_every_issue_has_the_merged_status(self) -> None: @@ -1139,13 +1183,13 @@ class AnEpicsWorkingBranch(DoEpicTest): def test_without_a_merged_status_a_finished_chunk_stops_where_automation_moves_issues_and_is_done_where_it_doesnt(self) -> None: self.settings('jira-unblocking-statuses = ["Release Ready"]\njira-automation-moves-issues = true\n') - stop = main.handed_on("ABC-20", self.epic_run(FakeEffects()), in_repo=True, to_epic_branch=True) + stop = main.handed_on("ABC-20", self.epic_run(FakeEffects()), main.Landed.ON_EPIC_BRANCH) self.assertEqual(stop, main.Stop(1, "the chunk finished, but its issue stays as it is: its pull request merged into the epic's working branch, where the team's automation moves no issue, and do-epic's config names no `jira-pull-request-merged-status` to move it to. Move ABC-20 in Jira, then run `do-epic ABC-12` to start the chunks it blocks")) self.assertEqual(self.store.writes, []) self.settings("") out = io.StringIO() with contextlib.redirect_stdout(out): - self.assertEqual(main.handed_on("ABC-20", self.epic_run(FakeEffects(windows="")), in_repo=True, to_epic_branch=True).code, 0) + self.assertEqual(main.handed_on("ABC-20", self.epic_run(FakeEffects(windows="")), main.Landed.ON_EPIC_BRANCH).code, 0) self.assertTrue(self.store.issues[0].done) def test_an_issue_whose_workflow_reaches_the_merged_status_only_through_the_open_status_moves_through_it(self) -> None: @@ -1153,18 +1197,18 @@ class AnEpicsWorkingBranch(DoEpicTest): epic = self.epic_run(FakeEffects(windows="")) stepwise = StepwiseStore(self.store, {"Release Ready": "Code Review"}) with contextlib.redirect_stdout(io.StringIO()): - stop = main.handed_on("ABC-20", dataclasses.replace(epic, store=stepwise), in_repo=True, to_epic_branch=True) + stop = main.handed_on("ABC-20", dataclasses.replace(epic, store=stepwise), main.Landed.ON_EPIC_BRANCH) self.assertEqual(stop.code, 0, stop.reason) self.assertEqual(self.store.writes, ["move ABC-20 to Code Review", "move ABC-20 to Release Ready"]) # An issue the open status doesn't lead on from stops on the move it asked for. stuck = StepwiseStore(self.store, {"Release Ready": "QA", "Code Review": "QA"}) - stop = main.handed_on("ABC-22", dataclasses.replace(epic, store=stuck), in_repo=True, to_epic_branch=True) + stop = main.handed_on("ABC-22", dataclasses.replace(epic, store=stuck), main.Landed.ON_EPIC_BRANCH) self.assertTrue(stop.reason.startswith("the chunk finished, but its issue could not move to 'Release Ready': ABC-22 has no transition to 'Release Ready'"), stop.reason) def test_an_issue_that_cannot_move_to_the_merged_status_stops_and_says_what_to_do(self) -> None: self.settings('jira-unblocking-statuses = ["Release Ready"]\njira-pull-request-merged-status = "Release Ready"\n') self.store.fails = "Jira answered 503" - stop = main.handed_on("ABC-20", self.epic_run(FakeEffects()), in_repo=True, to_epic_branch=True) + stop = main.handed_on("ABC-20", self.epic_run(FakeEffects()), main.Landed.ON_EPIC_BRANCH) self.assertEqual(stop.code, 1) self.assertTrue(stop.reason.startswith("the chunk finished, but its issue could not move to 'Release Ready': "), stop.reason) self.assertTrue(stop.reason.endswith("once it has that status in Jira, run `do-epic ABC-12` to start the chunks it blocks"), stop.reason) diff --git a/bin/forge/pulls.py b/bin/forge/pulls.py index dba37458..d93479bc 100644 --- a/bin/forge/pulls.py +++ b/bin/forge/pulls.py @@ -103,6 +103,37 @@ def unmergeable(forge: Forge, number: str, run: Run) -> str | None | Failure: return Failure(f"tea api answered pull request {number} in a shape that can't be read: {str(pull)[:200]}") +def other_base(forge: Forge, number: str, base: str, run: Run) -> Failure | None: + """A Failure, naming both branches, where the forge merges pull request number into another branch than base, the one its checkout takes. None where the two agree. + + A tool decides from the checkout's base who merges the pull request, so it doesn't act on one the forge would merge elsewhere.""" + onto = base_branch(forge, number, run) + if isinstance(onto, Failure) or onto == base: + return onto if isinstance(onto, Failure) else None + return Failure(f"pull request #{number} merges into {onto} on the forge, but this checkout takes {base} as the base of its branch; a person settles which holds, and changes the pull request's base on the forge or the branch's `chunk-base` in git's config") + + +def base_branch(forge: Forge, number: str, run: Run) -> str | Failure: + """The branch the forge merges pull request number into.""" + if forge == "gh": + viewed = run(["gh", "pr", "view", number, "--json", "baseRefName"]) + if isinstance(viewed, Failure): + return viewed + try: + name = json.loads(viewed)["baseRefName"] + except (ValueError, KeyError, TypeError): + name = None + return name if name and isinstance(name, str) else Failure(f"gh pr view printed no base branch: {viewed.strip()[:200]}") + pull = gitea(run, f"repos/{{owner}}/{{repo}}/pulls/{number}") + if isinstance(pull, Failure): + return pull + try: + name = pull["base"]["ref"] + except (KeyError, TypeError): + name = None + return name if name and isinstance(name, str) else 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. diff --git a/bin/forge/test_forge.py b/bin/forge/test_forge.py index 8798bb97..ead6905c 100644 --- a/bin/forge/test_forge.py +++ b/bin/forge/test_forge.py @@ -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, Pending, POLL_SECONDS, ready_pulls, ReadyPull, required_checks, unmergeable, WAIT_SECONDS +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 failure import Failure from forge.commands import forge_of, gitea, runner, url_beside @@ -86,6 +86,34 @@ class Unmergeable(unittest.TestCase): 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}") +class OtherBase(unittest.TestCase): + ELSEWHERE = "pull request #7 merges into main on the forge, but this checkout takes ABC-12 as the base of its branch; a person settles which holds, and changes the pull request's base on the forge or the branch's `chunk-base` in git's config" + + def test_a_github_pull_request_into_the_base_given_passes_and_one_into_another_fails_naming_both(self): + commands: list[list[str]] = [] + self.assertIsNone(other_base("gh", "7", "main", lambda command: commands.append(command) or '{"baseRefName": "main"}')) + self.assertEqual(commands, [["gh", "pr", "view", "7", "--json", "baseRefName"]]) + self.assertEqual(other_base("gh", "7", "ABC-12", lambda command: '{"baseRefName": "main"}'), Failure(self.ELSEWHERE)) + + def test_a_gitea_pull_request_into_the_base_given_passes_and_one_into_another_fails_naming_both(self): + commands: list[list[str]] = [] + self.assertIsNone(other_base("tea", "7", "main", lambda command: commands.append(command) or '{"number": 7, "base": {"ref": "main"}}')) + self.assertEqual(commands, [["tea", "api", "repos/{owner}/{repo}/pulls/7"]]) + self.assertEqual(other_base("tea", "7", "ABC-12", lambda command: '{"number": 7, "base": {"ref": "main"}}'), Failure(self.ELSEWHERE)) + + def test_fails_where_the_forge_cannot_be_read_or_names_no_base(self): + unread = Failure("no network") + for forge in ("gh", "tea"): + with self.subTest(forge=forge): + self.assertIs(other_base(forge, "7", "main", lambda command: unread), unread) + self.assertEqual(other_base("gh", "7", "main", lambda command: LONG).reason, f"gh pr view printed no base branch: {LONG[:200]}") + self.assertEqual(other_base("gh", "7", "main", lambda command: "not json").reason, "gh pr view printed no base branch: not json") + self.assertEqual(other_base("gh", "7", "main", lambda command: '{"baseRefName": ""}').reason, 'gh pr view printed no base branch: {"baseRefName": ""}') + self.assertEqual(other_base("tea", "7", "main", lambda command: LONG).reason, f"tea api answered pull request 7 in a shape that can't be read: {LONG[:200]}") + self.assertEqual(other_base("tea", "7", "main", lambda command: '{"number": 7, "base": {}}').reason, "tea api answered pull request 7 in a shape that can't be read: {'number': 7, 'base': {}}") + self.assertEqual(other_base("tea", "7", "main", lambda command: '{"number": 7, "base": {"ref": null}}').reason, "tea api answered pull request 7 in a shape that can't be read: {'number': 7, 'base': {'ref': None}}") + + 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) diff --git a/bin/git_cli/epic_branch.py b/bin/git_cli/epic_branch.py index 50b757c2..491735c3 100644 --- a/bin/git_cli/epic_branch.py +++ b/bin/git_cli/epic_branch.py @@ -8,13 +8,35 @@ from git_cli.git_repo import Git, git def levelled(branch: str, default: str, cwd: str | None = None, timeout: float | None = None, run: Git = git, again: bool = True) -> str | Failure: """Brings origin's branch level with default, the default branch's ref, such as `origin/main`, and fetches it: what it did, as a sentence. Where origin has no such branch, it makes one at default. - It merges with a merge commit, never a rebase, since several chunks share the branch, and without a checkout, since no worktree has the branch checked out. A Failure where the two don't merge cleanly: a person resolves that, never the loop. A call to origin still running after timeout seconds fails. Where the merge's push fails, as when another chunk pushed its own merge first, it fetches and levels once more, unless again is False.""" - fetched = run("fetch", "--quiet", "origin", cwd=cwd, timeout=timeout) - if isinstance(fetched, Failure): - return fetched + A call to origin still running after timeout seconds fails. again is False in the second try `merged` makes after a push that failed.""" + held = fetched(cwd, timeout, run) or on_origin(branch, cwd, timeout, run) + if isinstance(held, Failure): + return held + if not held: + return pushed(default, branch, cwd, timeout, run) or f"Made origin/{branch} off {default}." + return merged(branch, default, cwd, timeout, run, again) + + +def fetched(cwd: str | None, timeout: float | None, run: Git) -> Failure | None: + """Fetches origin, and once more where that fails: chunks that start at once fetch into one checkout, and git fails the fetch that finds a ref another one just moved.""" + fetch = ("fetch", "--quiet", "origin") + if not isinstance(run(*fetch, cwd=cwd, timeout=timeout), Failure): + return None + second = run(*fetch, cwd=cwd, timeout=timeout) + return second if isinstance(second, Failure) else None + + +def on_origin(branch: str, cwd: str | None, timeout: float | None, run: Git) -> bool | Failure: + """Whether origin has branch, asked of origin itself: a fetch keeps this checkout's `origin/<branch>` after origin deleted the branch.""" + listed = run("ls-remote", "--heads", "origin", f"refs/heads/{branch}", cwd=cwd, timeout=timeout) + return listed if isinstance(listed, Failure) else bool(listed.strip()) + + +def merged(branch: str, default: str, cwd: str | None, timeout: float | None, run: Git, again: bool) -> str | Failure: + """Merges default into origin's branch, as this checkout fetched it, and pushes the merge: what it did, as a sentence. + + It merges with a merge commit, never a rebase, since several chunks share the branch, and without a checkout, since no worktree has the branch checked out. A Failure where the two don't merge cleanly: a person resolves that, never the loop. Where the push fails, as when another chunk pushed its own merge first, it levels the branch once more from a fresh fetch, unless again is False.""" ours = f"origin/{branch}" - if isinstance(run("rev-parse", "--quiet", "--verify", f"refs/remotes/{ours}", cwd=cwd), Failure): - return pushed(default, branch, cwd, timeout, run) or f"Made {ours} off {default}." behind = run("rev-list", "--count", f"{ours}..{default}", cwd=cwd) if isinstance(behind, Failure): return behind @@ -47,8 +69,5 @@ def unmerged(failed: Failure, branch: str, default: str) -> Failure: def pushed(commit: str, branch: str, cwd: str | None, timeout: float | None, run: Git) -> Failure | None: """Pushes commit as origin's branch, without force, so a push that raced another's fails, and fetches it.""" - for args in (["push", "--quiet", "origin", f"{commit}:refs/heads/{branch}"], ["fetch", "--quiet", "origin"]): - done = run(*args, cwd=cwd, timeout=timeout) - if isinstance(done, Failure): - return done - return None + done = run("push", "--quiet", "origin", f"{commit}:refs/heads/{branch}", cwd=cwd, timeout=timeout) + return done if isinstance(done, Failure) else fetched(cwd, timeout, run) diff --git a/bin/git_cli/test_epic_branch.py b/bin/git_cli/test_epic_branch.py index 59d89b8b..ab2ffa8f 100644 --- a/bin/git_cli/test_epic_branch.py +++ b/bin/git_cli/test_epic_branch.py @@ -16,16 +16,22 @@ def sh(cwd: pathlib.Path, *args: str) -> str: return subprocess.run(["git", "-c", "user.name=T", "-c", "user.email=t@example.com", *args], cwd=cwd, check=True, capture_output=True, text=True).stdout.strip() -def scripted(calls: list[tuple[str, ...]], **answers: str | Failure) -> git_repo.Git: - """A git that records each command in calls and answers it by its first word: as answers has it, else as a checkout where origin/ABC-1 lacks a commit of origin/main that merges in cleanly.""" - answered = {"fetch": "", "rev-parse": "abc\n", "rev-list": "1\n", "merge-tree": "7ree\n", "commit-tree": "c0mm17\n", "push": ""} | {name.replace("_", "-"): answer for name, answer in answers.items()} +def scripted(calls: list[tuple[str, ...]], **answers: str | Failure | list[str | Failure]) -> git_repo.Git: + """A git that records each command in calls and answers it by its first word: as answers has it, else as a checkout where origin/ABC-1 lacks a commit of origin/main that merges in cleanly. A list answers one call with each entry, in order, and every later call with its last.""" + answered = {"fetch": "", "ls-remote": "abc\trefs/heads/ABC-1\n", "rev-list": "1\n", "merge-tree": "7ree\n", "commit-tree": "c0mm17\n", "push": ""} | {name.replace("_", "-"): answer for name, answer in answers.items()} def run(*args: str, cwd: str | None = None, timeout: float | None = None, guarded: bool = False, errors: str = "strict") -> str | Failure: calls.append(args) - return answered[args[0]] + answer = answered[args[0]] + if not isinstance(answer, list): + return answer + return answer.pop(0) if len(answer) > 1 else answer[0] return run +LOCKED = Failure("git fetch --quiet origin: error: cannot lock ref 'refs/remotes/origin/main': is at 2222 but expected 1111") + + class Levelled(unittest.TestCase): def setUp(self) -> None: folder = tempfile.TemporaryDirectory() @@ -53,6 +59,25 @@ class Levelled(unittest.TestCase): self.assertEqual(sh(self.origin, "rev-parse", "ABC-1"), sh(self.origin, "rev-parse", "main")) self.assertEqual(sh(self.clone, "rev-parse", "origin/ABC-1"), sh(self.origin, "rev-parse", "main"), "fetched, so a worktree can branch off it") + def test_makes_a_fresh_branch_off_the_default_branch_where_origin_deleted_the_one_this_checkout_still_tracks(self) -> None: + self.commit("ABC-1", "b.txt", "epic\n") + self.commit("main", "b.txt", "epic\n") + sh(self.origin, "branch", "--quiet", "-D", "ABC-1") + self.assertEqual(levelled("ABC-1", "origin/main", cwd=str(self.clone)), "Made origin/ABC-1 off origin/main.") + self.assertEqual(sh(self.origin, "rev-parse", "ABC-1"), sh(self.origin, "rev-parse", "main"), "not the commits the deleted branch held") + self.assertEqual(sh(self.clone, "rev-parse", "origin/ABC-1"), sh(self.origin, "rev-parse", "main")) + + def test_asks_origin_for_exactly_the_branch(self) -> None: + calls: list[tuple[str, ...]] = [] + levelled("ABC-1", "origin/main", run=scripted(calls)) + self.assertEqual(calls[1], ("ls-remote", "--heads", "origin", "refs/heads/ABC-1")) + + def test_an_origin_that_cannot_say_whether_it_has_the_branch_fails_with_gits_reason(self) -> None: + calls: list[tuple[str, ...]] = [] + unreachable = Failure("git ls-remote --heads origin refs/heads/ABC-1: fatal: unable to access origin") + self.assertEqual(levelled("ABC-1", "origin/main", run=scripted(calls, ls_remote=unreachable)), unreachable) + self.assertEqual([args[0] for args in calls], ["fetch", "ls-remote"], "makes no branch on an origin it could not ask") + def test_leaves_a_branch_that_holds_the_default_branch(self) -> None: self.commit("ABC-1", "b.txt", "epic\n") before = sh(self.origin, "rev-parse", "ABC-1") @@ -123,10 +148,23 @@ class Levelled(unittest.TestCase): calls: list[tuple[str, ...]] = [] declined = Failure("git push --quiet origin c0mm17:refs/heads/ABC-1: ! [remote rejected] c0mm17 -> ABC-1 (protected branch hook declined)") self.assertEqual(levelled("ABC-1", "origin/main", run=scripted(calls, push=declined)), declined) - self.assertEqual([args[0] for args in calls], ["fetch", "rev-parse", "rev-list", "merge-tree", "commit-tree", "push"] * 2) + self.assertEqual([args[0] for args in calls], ["fetch", "ls-remote", "rev-list", "merge-tree", "commit-tree", "push"] * 2) + + def test_a_fetch_that_fails_is_tried_once_more_since_a_chunk_starting_beside_it_may_hold_the_ref(self) -> None: + merge = ["rev-list", "merge-tree", "commit-tree", "push"] + for fetches, names in (([LOCKED, ""], ["fetch", "fetch", "ls-remote", *merge, "fetch"]), (["", LOCKED, ""], ["fetch", "ls-remote", *merge, "fetch", "fetch"])): + with self.subTest(fetches=fetches): + calls: list[tuple[str, ...]] = [] + self.assertEqual(levelled("ABC-1", "origin/main", run=scripted(calls, fetch=list(fetches))), "Merged origin/main into origin/ABC-1.") + self.assertEqual([args[0] for args in calls], names) + + def test_a_fetch_that_fails_twice_fails_with_gits_reason(self) -> None: + calls: list[tuple[str, ...]] = [] + self.assertEqual(levelled("ABC-1", "origin/main", run=scripted(calls, fetch=LOCKED)), LOCKED) + self.assertEqual([args[0] for args in calls], ["fetch", "fetch"]) def test_every_call_to_origin_stops_after_the_timeout_given(self) -> None: - for answers, origin_calls in (({"rev_parse": Failure("git rev-parse: no such ref")}, ["fetch", "push", "fetch"]), ({"push": Failure("git push: rejected")}, ["fetch", "push", "fetch", "push"])): + for answers, origin_calls in (({"ls_remote": ""}, ["fetch", "ls-remote", "push", "fetch"]), ({"push": Failure("git push: rejected")}, ["fetch", "ls-remote", "push", "fetch", "ls-remote", "push"])): with self.subTest(origin_calls=origin_calls): timed: list[tuple[str, float | None]] = [] answering = scripted([], **answers) @@ -135,7 +173,7 @@ class Levelled(unittest.TestCase): timed.append((args[0], timeout)) return answering(*args, cwd=cwd, timeout=timeout, guarded=guarded, errors=errors) levelled("ABC-1", "origin/main", timeout=5, run=run) - self.assertEqual([(name, timeout) for name, timeout in timed if name in ("fetch", "push")], [(name, 5) for name in origin_calls]) + self.assertEqual([(name, timeout) for name, timeout in timed if name in ("fetch", "ls-remote", "push")], [(name, 5) for name in origin_calls]) class ChunkBase(unittest.TestCase): diff --git a/docs/decisions.md b/docs/decisions.md index 9b4faeed..0be70ff5 100644 --- a/docs/decisions.md +++ b/docs/decisions.md @@ -166,8 +166,9 @@ Valid while `~/.claude` is a checkout of this repo. - **The setting.** `epic-branch` in a project's config is a template with `{epic}` for the epic's key, such as `"{epic}"`. Left out, everything works as before. Under chunk-preflight the key is that of the Jira epic `plan` names, so a plan kept in a file has no working branch. Under do-epic it is the key of the epic do-epic drives. - **A chunk's base is recorded in git.** `git config branch.<chunk's branch>.chunk-base <epic's working branch>`, in the checkout's shared config (`git_repo.CHUNK_BASE`). `git_repo.default_ref` answers `origin/<epic's working branch>` on such a branch, so each tool that measures a chunk against its base reads the chunk's own commits. Deleting the branch removes the record. -- **The branch stays level by a merge commit, made without a checkout.** `epic_branch.levelled` makes the branch on origin off the default branch, or merges the default branch into it with `git merge-tree --write-tree`, which needs git 2.38 or later, and `git commit-tree`, and pushes without force. chunk-preflight runs it before it makes a chunk's worktree, do-epic as a chunk's window starts, and `chunk-pr open-epic` before it opens the epic's pull request. `open-epic` refuses where origin has no such branch, since a branch made there would be a stray one. A conflict stops the chunk and asks a person to merge by hand: a resolution picks between two teams' changes, which is theirs to pick. +- **The branch stays level by a merge commit, made without a checkout.** `epic_branch.levelled` makes the branch on origin off the default branch, or merges the default branch into it with `git merge-tree --write-tree`, which needs git 2.38 or later, and `git commit-tree`, and pushes without force. It asks origin itself whether it has the branch, with `git ls-remote`, since a fetch keeps a checkout's `origin/<branch>` after origin deleted the branch. So a chunk that starts after the epic's branch merged and was deleted gets a fresh branch off the default branch, never the commits the deleted one held. chunk-preflight runs it before it makes a chunk's worktree, do-epic as a chunk's window starts, and `chunk-pr open-epic` before it opens the epic's pull request. `open-epic` refuses where origin has no such branch, since a branch made there would be a stray one. A conflict stops the chunk and asks a person to merge by hand: a resolution picks between two teams' changes, which is theirs to pick. - **The chunk merges without a person.** `chunk-pr merge` into an epic's working branch ignores `merge = "human"` and `wait-for-approval`, and do-epic parks no such chunk on a person's merge. lilleman's call: the team's rule against merging without a go-ahead covers the default branch and protected branches. The pull request from the epic's working branch into the default branch is opened ready and never merged by the loop. do-epic's finishing chunk opens it with `chunk-pr open-epic`; under loop-claude the human runs that command. +- **The forge's base must agree with the recorded one.** The record says whether a person merges, and the forge says where the pull request lands, so the two must not differ. `chunk-pr ready` and `merge`, and do-epic's gate, read the pull request's base from the forge (`forge.pulls.other_base`). They fail, naming both branches, where it differs from the recorded base, or from the default branch where the checkout records none. A person then changes the pull request's base on the forge, or the record. chunk-preflight and do-epic record the base for a chunk they continue too, since the setting sends every chunk of the epic into the working branch. So a pull request that was open into the default branch when the setting was turned on fails this check until a person changes its base or merges it. do-epic parks a chunk, and moves its issue, by the base it recorded as the chunk's window started, never by the config alone. - **A review is asked for by a comment.** An auto-reviewer may review on its own only pull requests into the default branch. `review-request-comment` holds the comment that asks it, which `chunk-pr ready` and `chunk-pr push` post on a pull request into an epic's working branch. The worker waits for the review as for any auto-reviewer. - **do-epic moves the issue itself.** A team's automation fires only for pull requests into the default branch. `jira-pull-request-open-status` names the status of an issue whose chunk parks with its pull request open, and `jira-pull-request-merged-status` the one once it has merged. The first move is made as the chunk parks, since that is where do-epic learns of the pull request; one that opens and merges within a single run of the worker skips it. Where the workflow then offers no way straight to the merged status, do-epic moves the issue through the open status first. Each setting names a status, and do-epic takes whichever transition leads there. - **An issue at the merged status counts as done for the epic's end.** The release reviews and the finishing chunk come before a person merges the epic's working branch, and Jira counts the issues done only after. So `finish.landed` reads such an issue as done, wherever do-epic's config names the status. Without the setting, the finishing waits for Jira as before.