Answer stability round 1: 1 🔴, 5 🟡 (0 misread); hold a pull request's base on the forge to the recorded one, ask origin for the epic branch, and name where a chunk's work landed
CI / tests (pull_request) Successful in 3m46s

This commit is contained in:
Lilleman
2026-11-20 21:42:32 +01:00
parent 525f3f8836
commit d464f271bf
13 changed files with 352 additions and 74 deletions
+25 -10
View File
@@ -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:
+64 -5
View File
@@ -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"