Answer stability round 1: 1 🔴, 3 🟡 (0 misread); drop cut tags no run pushed, split the fork and checks tests, test the usage error

This commit is contained in:
2026-11-08 10:55:44 +01:00
parent db993f159e
commit 10dd5fb527
3 changed files with 80 additions and 43 deletions
+22 -14
View File
@@ -24,6 +24,8 @@ def cut(clone: str, checkout: str) -> str | Failure:
return same
if isinstance(fetched := git("fetch", "--quiet", "--tags", "origin", cwd=clone), Failure):
return fetched
if isinstance(dropped := drop_unpushed_cut_tags(clone), Failure):
return dropped
ref = default_ref(cwd=clone)
if isinstance(ref, Failure):
return ref
@@ -44,13 +46,11 @@ def cut(clone: str, checkout: str) -> str | Failure:
return Failure(f"the pull request merged, but {ref} holds no commit titled {title!r}")
if isinstance(merged, Failure):
return merged
made = tag_commit(tag, title, merged, clone)
if isinstance(made, Failure):
if isinstance(made := git("tag", "--annotate", "--message", title, tag, merged, cwd=clone), Failure):
return made
if isinstance(pushed := git("push", "--quiet", "origin", f"refs/tags/{tag}", cwd=clone), Failure):
# A tag only the clone holds would read as cut, so every rerun would refuse.
if made:
git("tag", "--delete", tag, cwd=clone)
# Until a rerun, chunk-preflight would read a tag only the clone holds as cut.
git("tag", "--delete", tag, cwd=clone)
return pushed
moved = deploy(checkout)
if isinstance(moved, Failure):
@@ -58,6 +58,23 @@ def cut(clone: str, checkout: str) -> str | Failure:
return f"Cut {tag}.\n\n{moved}"
def drop_unpushed_cut_tags(clone: str) -> Failure | None:
"""Delete each v* tag the clone holds but origin doesn't, where it tags a commit titled `Cut release <its version>`: one a run made and never pushed, such as a run that died between the two. The clone reads cut releases from its own tags, so such a tag would read as a cut nobody published."""
local = git("tag", "--list", "v*", "--format=%(refname:short) %(*subject)", cwd=clone)
remote = git("ls-remote", "--tags", "--refs", "origin", "refs/tags/v*", cwd=clone)
if isinstance(local, Failure):
return local
if isinstance(remote, Failure):
return remote
pushed = {line.split("refs/tags/", 1)[-1] for line in remote.splitlines()}
for line in local.splitlines():
tag, _, subject = line.partition(" ")
if tag not in pushed and subject == f"Cut release {tag.removeprefix('v')}":
if isinstance(deleted := git("tag", "--delete", tag, cwd=clone), Failure):
return deleted
return None
def same_origin(clone: str, checkout: str) -> Failure | None:
"""A Failure unless clone is a clone of the origin checkout takes its releases from, so --cut never cuts another project."""
urls = [git("ls-remote", "--get-url", "origin", cwd=where) for where in (clone, checkout)]
@@ -167,12 +184,3 @@ def push_cut_commit(clone: str, ref: str, branch: str, named: str, title: str, r
finally:
git("worktree", "remove", "--force", str(worktree), cwd=clone)
shutil.rmtree(worktree.parent, ignore_errors=True)
def tag_commit(tag: str, title: str, commit: str, clone: str) -> bool | Failure:
"""Tag commit as tag, annotated with title, unless an earlier run did: whether this call made the tag. A Failure where tag already names another commit."""
existing = git("rev-parse", "--verify", "--quiet", f"refs/tags/{tag}^{{commit}}", cwd=clone)
if not isinstance(existing, Failure):
return False if existing.strip() == commit else Failure(f"{tag} already tags {existing.strip()[:12]}, not the merged commit {commit[:12]}")
made = git("tag", "--annotate", "--message", title, tag, commit, cwd=clone)
return made if isinstance(made, Failure) else True
+2 -2
View File
@@ -28,8 +28,8 @@ landed and changes are unreleased. The release names the version; the tool doesn
3. It tags the merged commit v<version>, annotated `Cut release <version>`, pushes only the tag,
then moves <checkout> to it as above.
Where a step fails it stops, exit 1, with git's or the forge's reason, and leaves the pull request
and the branch as they are. A rerun continues: it takes the branch's open pull request, a cut commit
already merged and a tag already on it. Once the tag is pushed, the release is cut: where only the
and the branch as they are. A rerun continues: it takes the branch's open pull request and a cut
commit already merged, and drops a cut tag an earlier run made but never pushed. Once the tag is pushed, the release is cut: where only the
move stopped, fix what it names and run a bare deploy-release.
"""
import os
+56 -27
View File
@@ -8,7 +8,7 @@ import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from deploy_release.forge import checks_passed
from deploy_release.forge import checks_passed, POLL_SECONDS
from failure import Failure
from test_env import isolated_env
@@ -285,8 +285,7 @@ class Cut(unittest.TestCase):
self.assertEqual(self.git(self.clone, "status", "--porcelain"), "")
def test_cuts_through_gh_on_a_github_origin(self):
self.git(self.clone, "remote", "set-url", "origin", "https://github.com/o/r.git")
self.git(self.clone, "config", f"url.{self.origin}.insteadOf", "https://github.com/o/r.git")
self.use_github()
ran = self.cut()
self.assert_cut(ran)
calls = self.forge_state()["calls"]
@@ -376,25 +375,52 @@ class Cut(unittest.TestCase):
self.assert_cut(self.cut())
self.assertEqual(len(self.forge_state()["pulls"]), 1, "the rerun finds the merged commit, so it opens no second pull request")
def test_merges_only_origins_own_pull_request_where_a_fork_opened_one_from_a_branch_of_the_same_name(self):
for forge_name in ("tea", "gh"):
with self.subTest(forge_name):
self.setUp()
if forge_name == "gh":
self.git(self.clone, "remote", "set-url", "origin", "https://github.com/o/r.git")
self.git(self.clone, "config", f"url.{self.origin}.insteadOf", "https://github.com/o/r.git")
self.set_checks("failure")
self.cut()
self.git(self.clone, "switch", "-q", "-c", "fork", "origin/release-0.2.0")
self.commit({"settings.json": "{\"hooks\": \"evil\"}\n"}, "Cut release 0.2.0")
self.git(self.clone, "push", "-q", "origin", "fork:refs/forks/release-0.2.0")
self.git(self.clone, "switch", "-q", "main")
state = self.forge_state()
fork = {"number": 9, "head": "release-0.2.0", "sha": self.git(self.clone, "rev-parse", "fork").strip(), "fork": True, "state": "open"}
self.forge_state({**state, "pulls": [fork, *state["pulls"]], "checks": "success"})
self.assert_cut(self.cut())
self.assertEqual([found["state"] for found in self.forge_state()["pulls"]], ["open", "merged"])
self.assertNotIn("evil", self.origin_git("show", "main:settings.json") if "settings.json" in self.origin_git("ls-tree", "--name-only", "main") else "")
def use_github(self) -> None:
"""Point the clone's origin at a GitHub URL that git reads as the local origin, so the cut runs gh."""
self.git(self.clone, "remote", "set-url", "origin", "https://github.com/o/r.git")
self.git(self.clone, "config", f"url.{self.origin}.insteadOf", "https://github.com/o/r.git")
def assert_fork_pull_ignored(self) -> None:
"""After a failed first run, a fork opens a pull request from its own release-0.2.0, listed first; the rerun merges only origin's."""
self.set_checks("failure")
self.cut()
self.git(self.clone, "switch", "-q", "-c", "fork", "origin/release-0.2.0")
self.commit({"settings.json": "{}\n"}, "Cut release 0.2.0")
self.git(self.clone, "push", "-q", "origin", "fork:refs/forks/release-0.2.0")
self.git(self.clone, "switch", "-q", "main")
state = self.forge_state()
fork = {"number": 9, "head": "release-0.2.0", "sha": self.git(self.clone, "rev-parse", "fork").strip(), "fork": True, "state": "open"}
self.forge_state({**state, "pulls": [fork, *state["pulls"]], "checks": "success"})
self.assert_cut(self.cut())
self.assertEqual([found["state"] for found in self.forge_state()["pulls"]], ["open", "merged"])
self.assertNotIn("settings.json", self.origin_git("ls-tree", "--name-only", "main"))
def test_merges_only_origins_own_pull_request_on_tea_where_a_fork_opened_one_from_a_branch_of_the_same_name(self):
self.assert_fork_pull_ignored()
def test_merges_only_origins_own_pull_request_on_gh_where_a_fork_opened_one_from_a_branch_of_the_same_name(self):
self.use_github()
self.assert_fork_pull_ignored()
def test_a_rerun_drops_a_cut_tag_an_earlier_run_made_but_never_pushed(self):
self.set_checks("failure")
self.cut()
self.set_checks("success")
hook = self.origin / "hooks" / "pre-receive"
hook.write_text("#!/bin/sh\nif grep -q refs/tags/; then exit 1; fi\n")
hook.chmod(0o755)
self.cut()
# A run that died between tagging and pushing left this tag behind.
self.git(self.clone, "fetch", "-q", "origin")
self.git(self.clone, "tag", "-a", "v0.2.0", "-m", "Cut release 0.2.0", "origin/main")
hook.unlink()
self.assert_cut(self.cut())
def test_takes_no_option_but_cut(self):
ran = subprocess.run([sys.executable, str(TOOL), "--cut", "--force"], capture_output=True, cwd=self.clone, env=self.env, text=True)
self.assertEqual(ran.returncode, 1)
self.assertIn("usage", ran.stderr)
self.assertEqual(self.forge_state()["calls"], [])
def test_stops_where_origins_open_pull_request_has_another_head_than_the_branch(self):
self.set_checks("failure")
@@ -409,14 +435,17 @@ class Cut(unittest.TestCase):
class ChecksPassed(unittest.TestCase):
def test_waits_on_tea_while_a_check_is_pending_and_takes_none_as_passed(self):
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.assertEqual(len(slept), 1)
self.assertIsNone(checks_passed("tea", "1", "abc", lambda command: '{"state": "pending", "total_count": 0}', slept.append))
failed = checks_passed("tea", "1", "abc", lambda command: '{"state": "failure", "total_count": 1}', slept.append)
self.assertIsInstance(failed, Failure)
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_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)
if __name__ == "__main__":