Files
claude-config/bin/deploy_release/cut.py
T

227 lines
11 KiB
Python

"""`deploy-release --cut`: cut the next release once it is ready, through a pull request, then tag it and move ~/.claude to it.
Each step first checks whether an earlier run did it, so a rerun continues where the last run stopped.
"""
import pathlib
import shutil
import sys
import tempfile
import typing
from release_plan.release_standing import chunk_item, Cut, default_plan, Next, ReadyToCut, ReviewDue, Unplanned
from project_config.settings import DEFAULT_FLOOR
from git_cli.git_repo import default_ref, git
from deploy_release.deploy import deploy
from failure import Failure
from plan_store.todo_md import FilePlan
from record_store.stores import FileStore
from forge.commands import forge_of, runner
from plan.release import newest_tag, Release, UNRELEASED_HEADING, version
from forge import pulls
from deploy_release import release_pull
from project_config import templates
def cut(clone: str, checkout: str, sleep: typing.Callable[[float], None]) -> str | Failure:
"""What the cut did, as the tool prints it; a Failure with git's, the forge's or the tool's reason where a step stopped."""
if isinstance(same := same_origin(clone, checkout), 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
release = ready_release(clone, ref)
if isinstance(release, Failure):
return release
named = version(release)
title, tag = f"Cut release {named}", f"v{named}"
merged = commit_titled(title, ref, clone)
if merged is None:
landed = land(clone, ref, named, title, sleep)
if isinstance(landed, Failure):
return landed
if isinstance(fetched := git("fetch", "--quiet", "origin", cwd=clone), Failure):
return fetched
merged = commit_titled(title, ref, clone)
if merged is None:
return Failure(f"the pull request merged, but {ref} holds no commit titled {title!r}")
if isinstance(merged, Failure):
return merged
if isinstance(made := git("tag", "--annotate", "--message", title, tag, merged, cwd=clone), Failure):
return made
said(f"tagged {merged[:12]} {tag}")
if isinstance(pushed := git("push", "--quiet", "origin", f"refs/tags/{tag}", cwd=clone), Failure):
# Until a rerun, chunk-preflight would read a tag only the clone holds as cut.
git("tag", "--delete", tag, cwd=clone)
return pushed
said(f"pushed the tag {tag}")
delete_release_branch(clone, f"release-{named}")
moved = deploy(checkout)
if isinstance(moved, Failure):
return Failure(f"cut and pushed {tag}, but moving {checkout} stopped: {moved.reason}. Fix that, then run a bare deploy-release.")
return f"{moved}\n\nDone: {tag} is cut and pushed, and {checkout} is on it."
def said(step: str) -> None:
"""Say on stderr that step finished, so the human sees how far the cut got before the changelog fills the screen."""
print(f"deploy-release: {step}", file=sys.stderr)
def delete_release_branch(clone: str, branch: str) -> None:
"""Delete origin's merged branch, where it is still there; only warn where that fails, since the release is cut by then."""
listed = branch_head(branch, clone)
if listed is None:
return
deleted = listed if isinstance(listed, Failure) else git("push", "--quiet", "origin", "--delete", f"refs/heads/{branch}", cwd=clone)
if isinstance(deleted, Failure):
said(f"warning: the branch {branch} stays on origin: {deleted.reason}")
else:
said(f"deleted the branch {branch} on origin")
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)]
failed = next((url for url in urls if isinstance(url, Failure)), None)
if failed is not None:
return failed
if urls[0] != urls[1]:
return Failure(f"run --cut in a clone of {checkout}'s origin, {str(urls[1]).strip()}; this directory's origin is {str(urls[0]).strip()}")
return None
def clone_plan(clone: str) -> FilePlan:
"""The clone's plan, at the path this repo keeps it. ready_release only reads it; FilePlan also takes the files and the floor a write would rank it against, and the clone has no config to set the floor."""
goals, decisions, history = (FileStore(pathlib.Path(clone, path), path) for path in ("README.md", "docs/decisions.md", "docs/comprehension-history.md"))
return FilePlan(pathlib.Path(clone, "todo.md"), "todo.md", goals, decisions, history, DEFAULT_FLOOR)
def ready_release(clone: str, ref: str) -> Release | Failure:
"""The next release, as chunk-preflight works it out, where it is ready to cut: it holds no item in todo.md, its release review has landed and changes are unreleased."""
# No project's config names the clone, so its changelog is read at the fixed path, on ref like the plan, and its titles as they stand.
plan = default_plan(clone, ref, clone_plan(clone), FileStore(pathlib.Path(clone, "CHANGELOG.md"), "CHANGELOG.md"), templates.BARE)
if isinstance(plan.plan, Failure):
return Failure(f"todo.md on {ref} cannot be read: {plan.plan.reason}")
match chunk_item(plan.plan.items, plan.cut):
case ReadyToCut(release=release) if release is not None:
return release
case ReadyToCut():
return Failure("todo.md names no release after the last one cut")
case Next(release=release) if release is not None:
return Failure(f"release {version(release)} still holds items in todo.md")
case ReviewDue(release=release):
return Failure(f"the release review of {version(release) if release else 'the next release'} hasn't landed")
case Unplanned():
return Failure("every change is released already: nothing has landed since the last release cut")
case Cut(release=release):
return Failure(f"todo.md files items under {version(release)}, which is cut already")
case _:
return Failure("cannot tell which release is next")
def commit_titled(title: str, ref: str, clone: str) -> str | None | Failure:
"""The commit titled title on ref, after the newest tag; None where there is none."""
tag = newest_tag(clone, merged_into=ref)
if isinstance(tag, Failure):
return tag
log = git("log", "--format=%H %s", f"{tag}..{ref}" if tag else ref, cwd=clone)
if isinstance(log, Failure):
return log
return next((line.split(" ", 1)[0] for line in log.splitlines() if line.split(" ", 1)[1:] == [title]), None)
def land(clone: str, ref: str, named: str, title: str, sleep: typing.Callable[[float], None]) -> Failure | None:
"""Push the branch release-<named> with the cut commit and open its pull request, where an earlier run hasn't; then wait for its checks and merge it."""
url = git("config", "--get", "remote.origin.url", cwd=clone)
if isinstance(url, Failure):
return url
on, run, branch, base = forge_of(url), runner(clone), f"release-{named}", ref.removeprefix("origin/")
head = branch_head(branch, clone)
if isinstance(head, Failure):
return head
behind = head is not None and behind_main(branch, head, ref, clone)
if isinstance(behind, Failure):
return behind
if head is None or behind:
# Where main moved on, such as to fix a failed check, the branch could no longer merge fast-forward, so it is rebuilt on main.
if isinstance(pushed := push_cut_commit(clone, ref, branch, named, title, head), Failure):
return pushed
said(f"pushed the branch {branch}")
head = branch_head(branch, clone) or Failure(f"pushed {branch}, but origin holds no such branch")
if isinstance(head, Failure):
return head
number = pulls.open_pull(on, branch, head, run)
if number is None:
if isinstance(created := release_pull.create_pull(on, branch, base, title, run), Failure):
return created
number = pulls.open_pull(on, branch, head, run) or Failure(f"opened a pull request for {branch}, but the forge lists none from origin at {head[:12]}")
if isinstance(number, str):
said(f"opened pull request {number}")
if isinstance(number, Failure):
return number
required = pulls.required_checks(on, base, run)
if isinstance(required, Failure):
return required
said(f"waiting for the checks of pull request {number}" + (f"; {base} requires {', '.join(sorted(required))}" if required else ""))
if isinstance(passed := pulls.checks_passed(on, number, head, required, run, sleep), Failure):
return passed
said(f"the checks of pull request {number} passed")
if isinstance(merged := release_pull.merge(on, number, head, run), Failure):
return merged
said(f"merged pull request {number}")
return None
def branch_head(branch: str, clone: str) -> str | None | Failure:
"""The commit origin's branch points at; None where origin has no such branch."""
listed = git("ls-remote", "--heads", "origin", f"refs/heads/{branch}", cwd=clone)
return listed if isinstance(listed, Failure) else next(iter(listed.split()), None)
def behind_main(branch: str, head: str, ref: str, clone: str) -> bool | Failure:
"""Whether origin's branch, at head, lacks a commit ref holds."""
if isinstance(fetched := git("fetch", "--quiet", "origin", f"refs/heads/{branch}", cwd=clone), Failure):
return fetched
return isinstance(git("merge-base", "--is-ancestor", ref, head, cwd=clone), Failure)
def push_cut_commit(clone: str, ref: str, branch: str, named: str, title: str, replacing: str | None) -> Failure | None:
"""Commit ref's CHANGELOG.md with ## Unreleased renamed to named, in a worktree of its own so the clone's checkout stays as it is, and push it as branch: a new one, or in place of replacing, the commit origin's branch points at."""
worktree = pathlib.Path(tempfile.mkdtemp(prefix="deploy-release-")) / "cut"
try:
if isinstance(added := git("worktree", "add", "--quiet", "--detach", str(worktree), ref, cwd=clone), Failure):
return added
changelog = worktree / "CHANGELOG.md"
renamed, count = UNRELEASED_HEADING.subn(f"## {named}", changelog.read_text() if changelog.exists() else "", count=1)
if not count:
return Failure(f"CHANGELOG.md on {ref} has no ## Unreleased section to rename")
changelog.write_text(renamed)
if isinstance(committed := git("commit", "--quiet", "-am", title, cwd=str(worktree)), Failure):
return committed
lease = [f"--force-with-lease=refs/heads/{branch}:{replacing}"] if replacing else []
pushed = git("push", "--quiet", *lease, "origin", f"HEAD:refs/heads/{branch}", cwd=str(worktree))
return pushed if isinstance(pushed, Failure) else None
finally:
git("worktree", "remove", "--force", str(worktree), cwd=clone)
shutil.rmtree(worktree.parent, ignore_errors=True)