147 lines
6.9 KiB
Python
147 lines
6.9 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 re
|
|
import shutil
|
|
import tempfile
|
|
|
|
from release_standing import chunk_item, Cut, default_plan, Next, ReadyToCut, ReviewDue, Unplanned
|
|
from git_repo import default_ref, git
|
|
from deploy_release.deploy import deploy
|
|
from failure import Failure
|
|
from deploy_release import forge
|
|
from chunk_preflight.choices import forge_of
|
|
from todo_table import items
|
|
from release import newest_tag, Release, version
|
|
|
|
|
|
def cut(clone: str, checkout: str) -> 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(fetched := git("fetch", "--quiet", "--tags", "origin", cwd=clone), Failure):
|
|
return fetched
|
|
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)
|
|
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
|
|
tagged = tag_commit(tag, title, merged, clone)
|
|
if isinstance(tagged, Failure):
|
|
return tagged
|
|
if isinstance(pushed := git("push", "--quiet", "origin", f"refs/tags/{tag}", cwd=clone), Failure):
|
|
return pushed
|
|
moved = deploy(checkout)
|
|
if isinstance(moved, Failure):
|
|
return Failure(f"cut and pushed {tag}, but moving {checkout} stopped: {moved.reason}")
|
|
return f"Cut {tag}.\n\n{moved}"
|
|
|
|
|
|
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."""
|
|
plan = default_plan(clone, ref)
|
|
found = items(plan.todo) if isinstance(plan.todo, str) else plan.todo
|
|
if isinstance(found, Failure):
|
|
return Failure(f"todo.md on {ref} cannot be read: {found.reason}")
|
|
match chunk_item(found, 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("no change is unreleased 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 on ref since the newest tag whose title is title; 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) -> 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), forge.runner(clone), f"release-{named}", ref.removeprefix("origin/")
|
|
head = branch_head(branch, clone)
|
|
if head is None:
|
|
if isinstance(pushed := push_cut_commit(clone, ref, branch, named, title), Failure):
|
|
return pushed
|
|
head = branch_head(branch, clone) or Failure(f"pushed {branch}, but origin holds no such branch")
|
|
if isinstance(head, Failure):
|
|
return head
|
|
number = forge.open_pull(on, branch, head, run)
|
|
if number is None:
|
|
if isinstance(created := forge.create_pull(on, branch, base, title, run), Failure):
|
|
return created
|
|
number = forge.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, Failure):
|
|
return number
|
|
if isinstance(passed := forge.checks_passed(on, number, head, run), Failure):
|
|
return passed
|
|
return forge.merge(on, number, head, run)
|
|
|
|
|
|
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 push_cut_commit(clone: str, ref: str, branch: str, named: str, title: str) -> 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."""
|
|
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 = re.subn(r"^## Unreleased$", f"## {named}", changelog.read_text() if changelog.exists() else "", count=1, flags=re.MULTILINE)
|
|
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
|
|
pushed = git("push", "--quiet", "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)
|
|
|
|
|
|
def tag_commit(tag: str, title: str, commit: str, clone: str) -> Failure | None:
|
|
"""Tag commit as tag, annotated with title, unless an earlier run did; 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 None 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 None
|