Say as each step of deploy-release --cut finishes, end on a Done line, delete the merged release branch, and wait for the checks main's protection requires

This commit is contained in:
2026-11-09 02:44:40 +01:00
parent daf6a38fc3
commit 94aa8bafbf
4 changed files with 114 additions and 20 deletions
+53 -7
View File
@@ -4,6 +4,7 @@ import pathlib
import subprocess
import sys
import tempfile
import typing
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
@@ -186,6 +187,8 @@ elif args[:2] in (["pulls", "create"], ["pr", "create"]):
save()
sys.exit("pull request already exists for these targets")
state["pulls"].append({"number": len(state["pulls"]) + 1, "head": head, "fork": False, "state": "open"})
elif forge == "tea" and args == ["api", "repos/{owner}/{repo}/branch_protections"]:
print("[]")
elif forge == "tea" and args[0] == "api" and args[1].endswith("/status"):
checks = state["checks"]
print(json.dumps({"state": "pending" if checks == "none" else checks, "total_count": 0 if checks == "none" else 1}))
@@ -356,9 +359,35 @@ class Cut(unittest.TestCase):
self.assertIn("origin", ran.stderr)
self.assertEqual(self.forge_state()["pulls"], [])
def test_says_which_pull_request_it_waits_for(self):
def test_says_as_each_step_finishes_and_ends_saying_the_release_is_cut_and_the_checkout_on_it(self):
ran = self.cut()
self.assertIn("pull request 1", ran.stderr)
self.assert_cut(ran)
merged = self.origin_git("rev-parse", "main").strip()
self.assertEqual(ran.stderr.splitlines(), [
"deploy-release: pushed the branch release-0.2.0",
"deploy-release: opened pull request 1",
"deploy-release: waiting for the checks of pull request 1",
"deploy-release: the checks of pull request 1 passed",
"deploy-release: merged pull request 1",
f"deploy-release: tagged {merged[:12]} v0.2.0",
"deploy-release: pushed the tag v0.2.0",
"deploy-release: deleted the branch release-0.2.0 on origin",
])
self.assertEqual(ran.stdout.splitlines()[-1], f"Done: v0.2.0 is cut and pushed, and {self.target} is on it.")
def test_deletes_the_merged_release_branch_on_origin(self):
self.assert_cut(self.cut())
self.assertEqual(self.origin_git("branch", "--list", "release-0.2.0"), "")
def test_only_warns_where_deleting_the_release_branch_fails(self):
hook = self.origin / "hooks" / "pre-receive"
hook.write_text("#!/bin/sh\nif grep -q '^[^ ]* 0000000000000000000000000000000000000000 refs/heads/'; then echo 'deletes are refused' >&2; exit 1; fi\n")
hook.chmod(0o755)
ran = self.cut()
self.assert_cut(ran)
self.assertIn("deploy-release: warning: the branch release-0.2.0 stays on origin:", ran.stderr)
self.assertIn("deletes are refused", ran.stderr)
self.assertEqual(ran.stdout.splitlines()[-1], f"Done: v0.2.0 is cut and pushed, and {self.target} is on it.")
def test_stops_where_the_tag_exists_on_another_commit_and_a_rerun_tags_the_merged_commit(self):
self.git(self.clone, "switch", "-q", "-c", "side")
@@ -443,18 +472,35 @@ class Cut(unittest.TestCase):
self.assertEqual(self.origin_git("log", "-1", "--format=%s", "main").strip(), "Release review of 0.2.0")
def tea(statuses: list[str], protections: str = "[]") -> typing.Callable[[list[str]], str]:
"""A tea answering each read of the head's status with the next of statuses, and of the branch protections with protections."""
answers = iter(statuses)
return lambda command: protections if command[-1].endswith("/branch_protections") else next(answers)
REQUIRED = '[{"rule_name": "main", "enable_status_check": true, "status_check_contexts": ["CI / tests (pull_request)"]}]'
class ChecksPassed(unittest.TestCase):
def test_waits_on_tea_while_a_check_is_pending(self):
statuses = iter(['{"state": "pending", "total_count": 1}', '{"state": "success", "total_count": 1}'])
slept = []
self.assertIsNone(checks_passed("tea", "1", "abc", lambda command: next(statuses), slept.append))
self.assertIsNone(checks_passed("tea", "1", "abc", "main", tea(['{"state": "pending", "total_count": 1}', '{"state": "success", "total_count": 1}']), slept.append))
self.assertEqual(slept, [POLL_SECONDS])
def test_takes_a_commit_with_no_status_on_tea_as_passed(self):
self.assertIsNone(checks_passed("tea", "1", "abc", lambda command: '{"state": "pending", "total_count": 0}', self.fail))
def test_takes_a_commit_with_no_status_on_tea_as_passed_where_the_base_requires_none(self):
self.assertIsNone(checks_passed("tea", "1", "abc", "main", tea(['{"state": "pending", "total_count": 0}']), self.fail))
def test_waits_on_tea_for_a_check_the_base_requires_until_it_reports_and_passes(self):
slept = []
statuses = ['{"state": "pending", "total_count": 0, "statuses": []}', '{"state": "success", "total_count": 1, "statuses": [{"context": "lint", "status": "success"}]}', '{"state": "success", "total_count": 2, "statuses": [{"context": "lint", "status": "success"}, {"context": "CI / tests (pull_request)", "status": "success"}]}']
self.assertIsNone(checks_passed("tea", "1", "abc", "main", tea(statuses, REQUIRED), slept.append))
self.assertEqual(slept, [POLL_SECONDS, POLL_SECONDS])
def test_ignores_on_tea_a_protection_of_another_branch(self):
self.assertIsNone(checks_passed("tea", "1", "abc", "develop", tea(['{"state": "pending", "total_count": 0}'], REQUIRED), self.fail))
def test_fails_on_tea_where_a_check_failed(self):
self.assertIsInstance(checks_passed("tea", "1", "abc", lambda command: '{"state": "failure", "total_count": 1}', self.fail), Failure)
self.assertIsInstance(checks_passed("tea", "1", "abc", "main", tea(['{"state": "failure", "total_count": 1}']), self.fail), Failure)
if __name__ == "__main__":