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:
+67
-34
@@ -5,15 +5,15 @@ A rerun takes the tag an earlier run pushed, once it checks the tag lists the sa
|
||||
import dataclasses
|
||||
import pathlib
|
||||
import re
|
||||
import subprocess
|
||||
import typing
|
||||
|
||||
from git_cli.git_repo import default_ref, git
|
||||
from deploy_release.cut import delete_release_branch, drop_unpushed_cut_tags, land, push_branch, ready_release, said, same_origin, scratch_worktree
|
||||
from deploy_release.cut import delete_release_branch, drop_unpushed_cut_tags, land, push_branch, ready_release, rewritten, same_origin, scratch_worktree
|
||||
from deploy_release.deploy import deploy
|
||||
from failure import Failure
|
||||
from forge.commands import forge_of
|
||||
from plan.release import newest_tag, tagged, UNRELEASED_TITLE, version
|
||||
from deploy_release.trace import Say
|
||||
from text_files.markdown_text import section
|
||||
|
||||
# The files a patch leaves as the tag holds them: the changelog, whose section the cut writes itself; the plan; and the records each chunk appends to, which seldom apply on an older tag.
|
||||
@@ -36,29 +36,42 @@ class Patch:
|
||||
entries: list[str]
|
||||
|
||||
|
||||
def cut_patch(clone: str, checkout: str, commits: str, sleep: typing.Callable[[float], None]) -> str | Failure:
|
||||
"""What the patch cut did, as the tool prints it; a Failure with git's, the forge's, ci.sh's or the tool's reason where a step stopped. commits is a range as git log reads it, or one commit."""
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class CiRan:
|
||||
code: int
|
||||
# stdout and stderr, as ci.sh interleaved them.
|
||||
output: str
|
||||
|
||||
|
||||
class Ci(typing.Protocol):
|
||||
"""Runs the ci.sh that worktree holds, as ci_script.ci_runner's does: a Failure where it could not start or was stopped."""
|
||||
|
||||
def __call__(self, worktree: pathlib.Path) -> CiRan | Failure: ...
|
||||
|
||||
|
||||
def cut_patch(clone: str, checkout: str, commits: str, sleep: typing.Callable[[float], None], say: Say, ci: Ci) -> str | Failure:
|
||||
"""What the patch cut did, as the tool prints it; a Failure with git's, the forge's, ci.sh's or the tool's reason where a step stopped. commits is a range as git log reads it, or one commit. say takes each step as it finishes, and ci runs the patch's ci.sh."""
|
||||
patch = planned(clone, checkout, commits)
|
||||
if isinstance(patch, Failure):
|
||||
return patch
|
||||
for commit in patch.commits:
|
||||
subject = git("log", "-1", "--format=%h %s", commit, cwd=clone)
|
||||
said(f"ships {'' if isinstance(subject, Failure) else subject.strip()}")
|
||||
say(f"ships {'' if isinstance(subject, Failure) else subject.strip()}")
|
||||
tag, branch = f"v{patch.named}", f"release-{patch.named}"
|
||||
pushed = git("ls-remote", "--tags", "--refs", "origin", f"refs/tags/{tag}", cwd=clone)
|
||||
if isinstance(pushed, Failure):
|
||||
return pushed
|
||||
made = tagged_entries(clone, patch) if pushed.strip() else tag_patch(clone, patch)
|
||||
made = tagged_entries(clone, patch) if pushed.strip() else tag_patch(clone, patch, say, ci)
|
||||
if isinstance(made, Failure):
|
||||
return made
|
||||
build = lambda replacing: push_merge_back(clone, patch, replacing)
|
||||
if isinstance(landed := land(clone, patch.ref, branch, f"Merge release {patch.named} into main", MERGE_BACK_BODY, build, sleep), Failure):
|
||||
if isinstance(landed := land(clone, patch.ref, branch, f"Merge release {patch.named} into main", MERGE_BACK_BODY, build, sleep, say), Failure):
|
||||
return landed
|
||||
if isinstance(fetched := git("fetch", "--quiet", "origin", cwd=clone), Failure):
|
||||
return fetched
|
||||
if isinstance(git("merge-base", "--is-ancestor", tag, patch.ref, cwd=clone), Failure):
|
||||
return Failure(f"the merge-back merged, but {patch.ref} doesn't hold {tag}, so the next cut can't read it as cut")
|
||||
delete_release_branch(clone, branch)
|
||||
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.")
|
||||
@@ -88,12 +101,12 @@ def planned(clone: str, checkout: str, commits: str) -> Patch | Failure:
|
||||
if base is None or cut is None:
|
||||
return Failure(f"{ref}'s newest tag, {base}, names no release x.y.z to cut a patch of" if base else f"{ref} holds no v* tag to cut a patch of")
|
||||
named = version((cut[0], cut[1], cut[2] + 1))
|
||||
# A patch holds only fixes, and the release review reads the whole project, so a patch is cut without one.
|
||||
release = ready_release(clone, ref, review=False)
|
||||
if isinstance(release, Failure):
|
||||
return release
|
||||
if version(release) != named:
|
||||
return Failure(f"the next release in todo.md is {version(release)}, not {named}, the patch of {base}: file the fix under {named}")
|
||||
ready = ready_release(clone, ref)
|
||||
if isinstance(ready, Failure):
|
||||
return ready
|
||||
# A patch holds only fixes, and the release review reads the whole project, so a patch is cut whether or not ready.reviewed.
|
||||
if version(ready.release) != named:
|
||||
return Failure(f"the next release in todo.md is {version(ready.release)}, not {named}, the patch of {base}: file the fix under {named}")
|
||||
picked = patch_commits(clone, ref, base, commits)
|
||||
if isinstance(picked, Failure):
|
||||
return picked
|
||||
@@ -118,7 +131,7 @@ def patch_commits(clone: str, ref: str, base: str, commits: str) -> list[str] |
|
||||
|
||||
|
||||
def changelog_entries(clone: str, ref: str, commits: list[str]) -> list[str] | Failure:
|
||||
"""The entries under ref's ## Unreleased in CHANGELOG.md that commits added, in the order ref lists them; a Failure where they added none."""
|
||||
"""The entries under ref's ## Unreleased in CHANGELOG.md that commits added, in the order ref lists them; a Failure where they added none, or where ref's CHANGELOG.md has no such section, which the merge-back moves the entries out of."""
|
||||
added: set[str] = set()
|
||||
for commit in commits:
|
||||
diff = git("diff", f"{commit}^", commit, "--", "CHANGELOG.md", cwd=clone)
|
||||
@@ -128,7 +141,11 @@ def changelog_entries(clone: str, ref: str, commits: list[str]) -> list[str] | F
|
||||
changelog = git("show", f"{ref}:CHANGELOG.md", cwd=clone)
|
||||
if isinstance(changelog, Failure):
|
||||
return changelog
|
||||
entries = [line for line in section(changelog, UNRELEASED_TITLE) or [] if line in added]
|
||||
unreleased = section(changelog, UNRELEASED_TITLE)
|
||||
# The merge-back finds the heading with unreleased_start, after the tag is pushed, so that is asked here too.
|
||||
if unreleased is None or unreleased_start(changelog.split("\n")) is None:
|
||||
return Failure(f"CHANGELOG.md on {ref} has no ## Unreleased section, which the patch's entries come from: add it on {ref}")
|
||||
entries = [line for line in unreleased if line in added]
|
||||
return entries or Failure(f"the commits add no entry under ## Unreleased in CHANGELOG.md on {ref}: name the commit that finished the fix's item too")
|
||||
|
||||
|
||||
@@ -143,7 +160,7 @@ def tagged_entries(clone: str, patch: Patch) -> Failure | None:
|
||||
return None
|
||||
|
||||
|
||||
def tag_patch(clone: str, patch: Patch) -> Failure | None:
|
||||
def tag_patch(clone: str, patch: Patch, say: Say, ci: Ci) -> Failure | None:
|
||||
"""In a worktree at the base tag, apply patch's commits but for LEFT_AS_TAGGED, head CHANGELOG.md with its section, commit that titled `Cut release <version>`, test it with ci.sh, then tag it and push the tag."""
|
||||
title, tag = f"Cut release {patch.named}", f"v{patch.named}"
|
||||
with scratch_worktree(clone, patch.base) as worktree:
|
||||
@@ -152,21 +169,22 @@ def tag_patch(clone: str, patch: Patch) -> Failure | None:
|
||||
for commit in patch.commits:
|
||||
if isinstance(applied := apply_commit(clone, worktree, commit, patch), Failure):
|
||||
return applied
|
||||
changelog = worktree / "CHANGELOG.md"
|
||||
changelog.write_text(headed(changelog.read_text() if changelog.exists() else "# Changelog\n", patch.entries, patch.named))
|
||||
head = lambda changelog: headed("# Changelog\n" if changelog is None else changelog, patch.entries, patch.named)
|
||||
if isinstance(written := rewritten(worktree / "CHANGELOG.md", f"head CHANGELOG.md with ## {patch.named} in the patch's worktree, so nothing is tagged", head), Failure):
|
||||
return written
|
||||
if isinstance(committed := git("commit", "--quiet", "--all", "--message", title, cwd=str(worktree)), Failure):
|
||||
return committed
|
||||
said(f"applied the commits on {patch.base}; testing them with ci.sh")
|
||||
if isinstance(tested := run_ci(worktree, patch.ref), Failure):
|
||||
say(f"applied the commits on {patch.base}; testing them with ci.sh")
|
||||
if isinstance(tested := run_ci(worktree, patch.ref, ci), Failure):
|
||||
return tested
|
||||
said("ci.sh passed")
|
||||
say("ci.sh passed")
|
||||
if isinstance(made := git("tag", "--annotate", "--message", title, tag, "HEAD", cwd=str(worktree)), Failure):
|
||||
return made
|
||||
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}")
|
||||
say(f"pushed the tag {tag}")
|
||||
return None
|
||||
|
||||
|
||||
@@ -187,7 +205,8 @@ def apply_commit(clone: str, worktree: pathlib.Path, commit: str, patch: Patch)
|
||||
if not diff:
|
||||
return None
|
||||
patch_file = worktree.parent / "commit.patch"
|
||||
patch_file.write_text(diff)
|
||||
if isinstance(written := rewritten(patch_file, f"write {commit[:12]}'s changes to a file to apply, so nothing is tagged", lambda _: diff), Failure):
|
||||
return written
|
||||
if isinstance(applied := git("apply", "--3way", "--index", str(patch_file), cwd=str(worktree)), Failure):
|
||||
subject = git("log", "-1", "--format=%s", commit, cwd=clone)
|
||||
return Failure(
|
||||
@@ -199,14 +218,16 @@ def apply_commit(clone: str, worktree: pathlib.Path, commit: str, patch: Patch)
|
||||
return None
|
||||
|
||||
|
||||
def run_ci(worktree: pathlib.Path, ref: str) -> Failure | None:
|
||||
"""None where the patch's ci.sh passes on its committed head; a Failure with the end of its output where it fails."""
|
||||
def run_ci(worktree: pathlib.Path, ref: str, ci: Ci) -> Failure | None:
|
||||
"""None where the patch's ci.sh passes on its committed head, run by ci; a Failure with the end of its output where it fails, or with ci's reason where it could not run or was stopped."""
|
||||
if not (worktree / "ci.sh").exists():
|
||||
return Failure("the patch holds no ci.sh to test it with")
|
||||
ran = subprocess.run(["sh", "ci.sh"], capture_output=True, cwd=worktree, text=True, errors="replace")
|
||||
if ran.returncode == 0:
|
||||
ran = ci(worktree)
|
||||
if isinstance(ran, Failure):
|
||||
return Failure(f"{ran.reason}, so nothing is tagged. Rerun once that is fixed.")
|
||||
if ran.code == 0:
|
||||
return None
|
||||
tail = "\n".join((ran.stdout + ran.stderr).strip().splitlines()[-30:])
|
||||
tail = "\n".join(ran.output.strip().splitlines()[-30:])
|
||||
return Failure(f"ci.sh failed on the patch, so nothing is tagged. Fix it on {ref}, then rerun naming the fix's commits too. The end of its output:\n{tail}")
|
||||
|
||||
|
||||
@@ -217,19 +238,31 @@ def push_merge_back(clone: str, patch: Patch, replacing: str | None) -> Failure
|
||||
return worktree
|
||||
if isinstance(merged := git("merge", "--quiet", "--no-ff", "--no-commit", "--strategy", "ours", f"v{patch.named}", cwd=str(worktree)), Failure):
|
||||
return merged
|
||||
changelog = worktree / "CHANGELOG.md"
|
||||
changelog.write_text(moved_entries(changelog.read_text(), patch.entries, patch.named))
|
||||
# The tag is pushed by now, so each stop here says that a rerun with the same commits takes it and builds only this merge-back again.
|
||||
rerun = f"v{patch.named} is pushed, and a rerun with the same commits merges it back"
|
||||
def move(changelog: str | None) -> str | Failure:
|
||||
moved = moved_entries(changelog or "", patch.entries, patch.named)
|
||||
return Failure(f"{moved.reason}, on {patch.ref}: add the heading there; {rerun}") if isinstance(moved, Failure) else moved
|
||||
if isinstance(written := rewritten(worktree / "CHANGELOG.md", f"move the entries of {patch.named} out of ## Unreleased in the merge-back's worktree; {rerun}", move), Failure):
|
||||
return written
|
||||
if isinstance(committed := git("commit", "--quiet", "--all", "--message", f"Merge release {patch.named} into main", cwd=str(worktree)), Failure):
|
||||
return committed
|
||||
return push_branch(worktree, f"release-{patch.named}", replacing)
|
||||
|
||||
|
||||
def moved_entries(changelog: str, entries: list[str], named: str) -> str:
|
||||
"""changelog with entries taken out of its ## Unreleased section and put in a ## named section right below that section."""
|
||||
def moved_entries(changelog: str, entries: list[str], named: str) -> str | Failure:
|
||||
"""changelog with entries taken out of its ## Unreleased section and put in a ## named section right below that section; a Failure where it has no such heading."""
|
||||
lines = changelog.split("\n")
|
||||
start = next(index for index, line in enumerate(lines) if re.fullmatch(f"## {UNRELEASED_TITLE}", line))
|
||||
start = unreleased_start(lines)
|
||||
if start is None:
|
||||
return Failure(f"CHANGELOG.md has no ## Unreleased heading to move the entries of {named} from")
|
||||
end = next((index for index in range(start + 1, len(lines)) if lines[index].startswith("## ")), len(lines))
|
||||
kept = [line for line in lines[start + 1:end] if line not in entries]
|
||||
while kept and not kept[-1].strip():
|
||||
kept.pop()
|
||||
return "\n".join([*lines[:start + 1], *kept, "", f"## {named}", "", *entries, "", *lines[end:]])
|
||||
|
||||
|
||||
def unreleased_start(lines: list[str]) -> int | None:
|
||||
"""The index in lines of a changelog's ## Unreleased heading; None where it has none."""
|
||||
return next((index for index, line in enumerate(lines) if re.fullmatch(f"## {UNRELEASED_TITLE}", line)), None)
|
||||
|
||||
Reference in New Issue
Block a user