Answer principles round 1: 0 🔴, 1 🟡 (0 misread); bound and stream a patch cut's ci.sh, keep a trace of each deploy-release run, fail on file errors and a missing Unreleased heading, read the plan once per cut, one home for the review title, inject task-bench's container names, one spelling of its task kind
CI / tests (pull_request) Has been cancelled
CI / tests (pull_request) Has been cancelled
This commit is contained in:
+56
-40
@@ -3,9 +3,9 @@
|
||||
Each step first checks whether an earlier run did it, so a rerun continues where the last run stopped.
|
||||
"""
|
||||
import contextlib
|
||||
import dataclasses
|
||||
import pathlib
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
import typing
|
||||
|
||||
@@ -20,14 +20,23 @@ from forge.commands import forge_of, runner
|
||||
from plan.release import newest_tag, Release, tagged, UNRELEASED_HEADING, version
|
||||
from forge import pulls
|
||||
from deploy_release import release_pull
|
||||
from deploy_release.trace import Say
|
||||
from project_config import templates
|
||||
|
||||
|
||||
CUT_BODY = "Rename ## Unreleased in CHANGELOG.md to the version."
|
||||
|
||||
|
||||
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."""
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Ready:
|
||||
"""The next release, which holds no item in todo.md while changes are unreleased."""
|
||||
release: Release
|
||||
# Whether its release review has landed.
|
||||
reviewed: bool
|
||||
|
||||
|
||||
def cut(clone: str, checkout: str, sleep: typing.Callable[[float], None], say: Say) -> 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. say takes each step as it finishes."""
|
||||
if isinstance(same := same_origin(clone, checkout), Failure):
|
||||
return same
|
||||
if isinstance(fetched := git("fetch", "--quiet", "--tags", "origin", cwd=clone), Failure):
|
||||
@@ -37,20 +46,20 @@ def cut(clone: str, checkout: str, sleep: typing.Callable[[float], None]) -> str
|
||||
ref = default_ref(cwd=clone)
|
||||
if isinstance(ref, Failure):
|
||||
return ref
|
||||
ready = ready_release(clone, ref)
|
||||
if isinstance(ready, Failure):
|
||||
return ready
|
||||
named = version(ready.release)
|
||||
# The patch refusal comes before the review's: a patch takes no release review, so "hasn't landed" would send the human the wrong way.
|
||||
release = ready_release(clone, ref, review=False)
|
||||
if isinstance(release, Failure):
|
||||
return release
|
||||
named = version(release)
|
||||
if isinstance(patched := patch_refusal(clone, ref, release), Failure):
|
||||
if isinstance(patched := patch_refusal(clone, ref, ready.release), Failure):
|
||||
return patched
|
||||
if isinstance(reviewed := ready_release(clone, ref), Failure):
|
||||
return reviewed
|
||||
if not ready.reviewed:
|
||||
return Failure(f"the release review of {named} hasn't landed")
|
||||
title, tag, branch = f"Cut release {named}", f"v{named}", f"release-{named}"
|
||||
merged = commit_titled(title, ref, clone)
|
||||
if merged is None:
|
||||
build = lambda replacing: push_cut_commit(clone, ref, branch, named, title, replacing)
|
||||
landed = land(clone, ref, branch, title, CUT_BODY, build, sleep)
|
||||
landed = land(clone, ref, branch, title, CUT_BODY, build, sleep, say)
|
||||
if isinstance(landed, Failure):
|
||||
return landed
|
||||
if isinstance(fetched := git("fetch", "--quiet", "origin", cwd=clone), Failure):
|
||||
@@ -62,13 +71,13 @@ def cut(clone: str, checkout: str, sleep: typing.Callable[[float], None]) -> str
|
||||
return merged
|
||||
if isinstance(made := git("tag", "--annotate", "--message", title, tag, merged, cwd=clone), Failure):
|
||||
return made
|
||||
said(f"tagged {merged[:12]} {tag}")
|
||||
say(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, branch)
|
||||
say(f"pushed the tag {tag}")
|
||||
delete_release_branch(clone, branch, say)
|
||||
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.")
|
||||
@@ -86,21 +95,16 @@ def patch_refusal(clone: str, ref: str, release: Release) -> Failure | None:
|
||||
return None
|
||||
|
||||
|
||||
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:
|
||||
def delete_release_branch(clone: str, branch: str, say: Say) -> 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}")
|
||||
say(f"warning: the branch {branch} stays on origin: {deleted.reason}")
|
||||
else:
|
||||
said(f"deleted the branch {branch} on origin")
|
||||
say(f"deleted the branch {branch} on origin")
|
||||
|
||||
|
||||
def drop_unpushed_cut_tags(clone: str) -> Failure | None:
|
||||
@@ -137,23 +141,23 @@ def clone_plan(clone: str) -> FilePlan:
|
||||
return FilePlan(pathlib.Path(clone, "todo.md"), "todo.md", goals, decisions, history, DEFAULT_FLOOR)
|
||||
|
||||
|
||||
def ready_release(clone: str, ref: str, review: bool = True) -> 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, unless review is False, and changes are unreleased."""
|
||||
def ready_release(clone: str, ref: str) -> Ready | Failure:
|
||||
"""The next release, as chunk-preflight works it out, where it holds no item in todo.md and changes are unreleased, with whether its release review has landed: the caller decides whether the cut needs one."""
|
||||
# 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
|
||||
return Ready(release, reviewed=True)
|
||||
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) if not review and release is not None:
|
||||
return release
|
||||
case ReviewDue(release=release):
|
||||
return Failure(f"the release review of {version(release) if release else 'the next release'} hasn't landed")
|
||||
case ReviewDue(release=release) if release is not None:
|
||||
return Ready(release, reviewed=False)
|
||||
case ReviewDue():
|
||||
return Failure("the release review of 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):
|
||||
@@ -173,7 +177,7 @@ def commit_titled(title: str, ref: str, clone: str) -> str | None | Failure:
|
||||
return next((line.split(" ", 1)[0] for line in log.splitlines() if line.split(" ", 1)[1:] == [title]), None)
|
||||
|
||||
|
||||
def land(clone: str, ref: str, branch: str, title: str, body: str, build: typing.Callable[[str | None], Failure | None], sleep: typing.Callable[[float], None]) -> Failure | None:
|
||||
def land(clone: str, ref: str, branch: str, title: str, body: str, build: typing.Callable[[str | None], Failure | None], sleep: typing.Callable[[float], None], say: Say) -> Failure | None:
|
||||
"""Push branch and open its pull request, titled title with body, where an earlier run hasn't; then wait for its checks and merge it.
|
||||
|
||||
build pushes branch's commit, made on ref. It takes the commit origin's branch points at, which the push replaces, or None where origin has no such branch yet."""
|
||||
@@ -191,7 +195,7 @@ def land(clone: str, ref: str, branch: str, title: str, body: str, build: typing
|
||||
# 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 := build(head), Failure):
|
||||
return pushed
|
||||
said(f"pushed the branch {branch}")
|
||||
say(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
|
||||
@@ -201,19 +205,19 @@ def land(clone: str, ref: str, branch: str, title: str, body: str, build: typing
|
||||
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}")
|
||||
say(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 ""))
|
||||
say(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")
|
||||
say(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}")
|
||||
say(f"merged pull request {number}")
|
||||
return None
|
||||
|
||||
|
||||
@@ -235,16 +239,28 @@ def push_cut_commit(clone: str, ref: str, branch: str, named: str, title: str, r
|
||||
with scratch_worktree(clone, ref) as worktree:
|
||||
if isinstance(worktree, Failure):
|
||||
return worktree
|
||||
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)
|
||||
def renamed(changelog: str | None) -> str | Failure:
|
||||
text, count = UNRELEASED_HEADING.subn(f"## {named}", changelog or "", count=1)
|
||||
return text if count else Failure(f"CHANGELOG.md on {ref} has no ## Unreleased section to rename")
|
||||
if isinstance(written := rewritten(worktree / "CHANGELOG.md", f"rename ## Unreleased to ## {named} in the cut's worktree, so nothing is pushed", renamed), Failure):
|
||||
return written
|
||||
if isinstance(committed := git("commit", "--quiet", "-am", title, cwd=str(worktree)), Failure):
|
||||
return committed
|
||||
return push_branch(worktree, branch, replacing)
|
||||
|
||||
|
||||
def rewritten(file: pathlib.Path, doing: str, rewrite: typing.Callable[[str | None], str | Failure]) -> Failure | None:
|
||||
"""Replace file's text with what rewrite makes of it, or of None where there is no such file. A Failure where rewrite gives one, which leaves the file, or where the file can't be read or written: that one says doing, the step that stopped, and names the file."""
|
||||
try:
|
||||
text = rewrite(file.read_text(encoding="utf-8") if file.exists() else None)
|
||||
if isinstance(text, Failure):
|
||||
return text
|
||||
file.write_text(text, encoding="utf-8")
|
||||
except (OSError, UnicodeError) as failure:
|
||||
return Failure(f"could not {doing}: {failure}" + ("" if str(file) in str(failure) else f" ({file})"))
|
||||
return None
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def scratch_worktree(clone: str, at: str) -> typing.Iterator[pathlib.Path | Failure]:
|
||||
"""A worktree of clone, detached at at, in a folder of its own that is removed afterwards, so the clone's checkout stays as it is; a Failure where git can't add it."""
|
||||
|
||||
Reference in New Issue
Block a user