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
+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"