Answer product-owner round 1: 2 🔴, 2 🟡 (0 misread); rebuild a branch main moved past, drop a tag whose push failed, refuse another project's clone, say which pull request it waits for
This commit is contained in:
+47
-15
@@ -5,6 +5,7 @@ Each step first checks whether an earlier run did it, so a rerun continues where
|
||||
import pathlib
|
||||
import re
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
from release_standing import chunk_item, Cut, default_plan, Next, ReadyToCut, ReviewDue, Unplanned
|
||||
@@ -19,6 +20,8 @@ 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(same := same_origin(clone, checkout), Failure):
|
||||
return same
|
||||
if isinstance(fetched := git("fetch", "--quiet", "--tags", "origin", cwd=clone), Failure):
|
||||
return fetched
|
||||
ref = default_ref(cwd=clone)
|
||||
@@ -41,17 +44,31 @@ 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
|
||||
tagged = tag_commit(tag, title, merged, clone)
|
||||
if isinstance(tagged, Failure):
|
||||
return tagged
|
||||
made = tag_commit(tag, title, merged, clone)
|
||||
if isinstance(made, 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)
|
||||
return pushed
|
||||
moved = deploy(checkout)
|
||||
if isinstance(moved, Failure):
|
||||
return Failure(f"cut and pushed {tag}, but moving {checkout} stopped: {moved.reason}")
|
||||
return Failure(f"cut and pushed {tag}, but moving {checkout} stopped: {moved.reason}. Fix that, then run a bare deploy-release.")
|
||||
return f"Cut {tag}.\n\n{moved}"
|
||||
|
||||
|
||||
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 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)
|
||||
@@ -68,7 +85,7 @@ def ready_release(clone: str, ref: str) -> Release | Failure:
|
||||
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")
|
||||
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 _:
|
||||
@@ -76,7 +93,7 @@ def ready_release(clone: str, ref: str) -> Release | Failure:
|
||||
|
||||
|
||||
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."""
|
||||
"""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
|
||||
@@ -93,8 +110,14 @@ def land(clone: str, ref: str, named: str, title: str) -> Failure | None:
|
||||
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):
|
||||
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
|
||||
head = branch_head(branch, clone) or Failure(f"pushed {branch}, but origin holds no such branch")
|
||||
if isinstance(head, Failure):
|
||||
@@ -106,6 +129,7 @@ def land(clone: str, ref: str, named: str, title: str) -> Failure | None:
|
||||
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
|
||||
print(f"deploy-release: waiting for the checks of pull request {number}", file=sys.stderr)
|
||||
if isinstance(passed := forge.checks_passed(on, number, head, run), Failure):
|
||||
return passed
|
||||
return forge.merge(on, number, head, run)
|
||||
@@ -117,8 +141,15 @@ def branch_head(branch: str, clone: str) -> str | None | Failure:
|
||||
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."""
|
||||
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):
|
||||
@@ -130,17 +161,18 @@ def push_cut_commit(clone: str, ref: str, branch: str, named: str, title: str) -
|
||||
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))
|
||||
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)
|
||||
|
||||
|
||||
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."""
|
||||
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 None if existing.strip() == commit else Failure(f"{tag} already tags {existing.strip()[:12]}, not the merged commit {commit[:12]}")
|
||||
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 None
|
||||
return made if isinstance(made, Failure) else True
|
||||
|
||||
Reference in New Issue
Block a user