Treat a recorded base that is origin's default branch as the default branch, and wait for the forge to show a pushed head before push asks for a review

This commit is contained in:
2026-11-27 15:07:35 +01:00
parent 411819c38d
commit 32aee19da2
2 changed files with 72 additions and 16 deletions
+36 -2
View File
@@ -24,6 +24,8 @@ GITHUB = "git@github.com:me/project.git"
GITEA = "ssh://git@gitea.example.org:22/me/project.git"
HEAD = "c0ffee"
PUSH = ["git", "push", "--quiet", "--set-upstream", "origin", "item-7"]
OLD = "badc0de"
OLD_HEAD = ["git", "ls-remote", "origin", "refs/heads/item-7"]
GH_LIST = ["gh", "pr", "list", "--head", "item-7", "--json", "number,headRefOid,isCrossRepository", "--template", "{{range .}}{{.number}}\t{{.headRefOid}}\t{{.isCrossRepository}}\n{{end}}"]
TEA_LIST = ["tea", "api", "repos/{owner}/{repo}/pulls?state=open&limit=50"]
GH_SETTINGS = ["gh", "repo", "view", "--json", "mergeCommitAllowed,rebaseMergeAllowed,squashMergeAllowed"]
@@ -61,6 +63,8 @@ class Checkout:
self.chunk_base = ""
# The branch the forge merges the pull request into on each read; the last one repeats. Empty where the forge has the checkout's: chunk_base, else main.
self.bases: list[str] = []
# The heads GitHub lists pull request #12 at on each read; the last one repeats. Empty where it lists HEAD.
self.listed_heads: list[str] = []
# What GitHub reports as the pull request's review decision on each read; the last one repeats.
self.decisions = ["APPROVED"]
# Set, what GitHub prints instead of a decision's JSON.
@@ -101,7 +105,7 @@ class Checkout:
if command[2:3] == ["create"]:
self.open = True
if command == GH_LIST:
return f"12\t{HEAD}\tfalse\n" if self.open else ""
return f"12\t{self.listed_heads.pop(0) if len(self.listed_heads) > 1 else self.listed_heads[0] if self.listed_heads else HEAD}\tfalse\n" if self.open else ""
if command == TEA_LIST:
return json.dumps([{"number": 12, "head": {"ref": "item-7", "label": "item-7", "sha": HEAD, "repo": {"full_name": "me/project"}}, "base": {"repo": {"full_name": "me/project"}}}] if self.open else [])
if command == TEA_STATUS:
@@ -123,7 +127,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, GH_BASE, TEA_LIST, TEA_PULL, GH_SETTINGS, TEA_SETTINGS, OWED, GH_CHECKS, TEA_BRANCH, TEA_STATUS, GH_THREADS]
reads = [OLD_HEAD, 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]
@@ -1078,6 +1082,14 @@ class EpicBranch(unittest.TestCase):
self.assertEqual(step(["merge"], checkout, checkout.config, parks=True), "Merged pull request #12 (merge).")
self.assertNotIn(GH_DECISION, checkout.ran)
def test_a_recorded_base_that_is_origins_default_branch_leaves_the_pull_request_to_a_person(self):
checkout = self.checkout()
checkout.chunk_base = "main\n"
checkout.answers[(PROJECT_CONFIG, "merge")] = "human\n"
done = step(["merge"], checkout, checkout.config)
self.assertIn("awaits a person's merge", str(done))
self.assertFalse(any("merge" in command[1:4] for command in checkout.acted() if command[0] != "git"), checkout.acted())
def test_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):
@@ -1185,6 +1197,28 @@ class EpicBranch(unittest.TestCase):
self.assertEqual(step(["push"], checkout, checkout.config), "Pushed item-7.")
self.assertEqual(checkout.acted(), [PUSH])
def test_push_waits_for_the_forge_to_show_the_new_head_before_it_asks_for_a_review(self):
checkout, slept = self.checkout(), []
checkout.answers[tuple(OLD_HEAD)] = f"{OLD}\trefs/heads/item-7\n"
checkout.listed_heads = [OLD, OLD, OLD, OLD, HEAD]
self.assertEqual(step(["push"], checkout, checkout.config, sleep=slept.append), "Pushed item-7, and asked for a review of pull request #12 with “@reviewer review”.")
self.assertEqual(slept, [5, 5])
def test_push_fails_naming_the_pull_request_where_the_forge_never_shows_the_new_head(self):
checkout, slept = self.checkout(), []
checkout.answers[tuple(OLD_HEAD)] = f"{OLD}\trefs/heads/item-7\n"
checkout.listed_heads = [OLD]
done = step(["push"], checkout, checkout.config, sleep=slept.append)
self.assertEqual(done, Failure(f"pushed item-7, but the forge still shows pull request #12 at {OLD[:12]}, not at {HEAD[:12]}, after 60 seconds, so no review was asked for; run chunk-pr push again"))
self.assertEqual(sum(slept), 60)
self.assertNotIn(self.COMMENT_GH, checkout.ran)
def test_push_waits_for_no_pull_request_that_was_not_open_at_the_old_head(self):
checkout, slept = self.checkout(open=False), []
checkout.answers[tuple(OLD_HEAD)] = f"{OLD}\trefs/heads/item-7\n"
self.assertEqual(step(["push"], checkout, checkout.config, sleep=slept.append), "Pushed item-7.")
self.assertEqual(slept, [])
def test_a_failed_push_asks_for_no_review(self):
checkout = self.checkout()
checkout.answers[tuple(PUSH)] = Failure("git push: rejected")