115 lines
4.9 KiB
Python
115 lines
4.9 KiB
Python
"""reviewer-header: write the branch diff and the places into the chunk's review folder, and print the header of a reviewer's prompt."""
|
|
import dataclasses
|
|
import pathlib
|
|
|
|
from git_cli.git_repo import branch_titles, git, top
|
|
from review_rules.round_titles import cap, round_counts
|
|
from failure import Failure
|
|
from chunk_preflight.choices import forge_of
|
|
from larv_review.history_commands import history_file, places_report
|
|
from review_rules.review_folder import made_review_folder
|
|
from deploy_release.forge import open_pull, runner
|
|
|
|
# The passes whose reviewer is given the places: skills/larv-review/SKILL.md → "Comprehension check" → "Places".
|
|
TAKE_PLACES = ("architecture", "stability")
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Launch:
|
|
"""What the header of a reviewer's prompt names."""
|
|
pass_: str
|
|
round_: int
|
|
rerun: bool
|
|
pull: str
|
|
worktree: str
|
|
branch: str
|
|
base: str
|
|
default: str
|
|
folder: pathlib.Path
|
|
diff: pathlib.Path
|
|
|
|
def asked(self) -> pathlib.Path:
|
|
"""The round's report, which asks for the rerun where there is one."""
|
|
return self.folder / f"{self.pass_}-round-{self.round_}.md"
|
|
|
|
def header(self) -> str:
|
|
asked = self.asked()
|
|
report = f"{asked.with_name(asked.stem + '-rerun.md')}, in place of {asked}, which asked for this rerun" if self.rerun else str(asked)
|
|
return "\n".join([
|
|
f"Pass: {self.pass_}, round {self.round_}.",
|
|
f"PR: {self.pull}.",
|
|
f"Worktree: {self.worktree}, on branch {self.branch}.",
|
|
f"Base: {self.base}, the merge base of HEAD and {self.default}.",
|
|
f"Report: write it to {report}.",
|
|
f"Diff: {self.diff}",
|
|
*([f"Places: {self.folder / 'places.md'}"] if self.pass_ in TAKE_PLACES else []),
|
|
])
|
|
|
|
|
|
def unanswered_round(titles: list[str], pass_: str) -> int | Failure:
|
|
"""The pass's round that no `Answer <pass> round <n>:` commit has answered yet; a Failure past the pass's cap."""
|
|
done = round_counts(titles).get(pass_, 0)
|
|
if done >= cap(pass_):
|
|
return Failure(f"{pass_} takes no more rounds: it has had {done}, and its cap is {cap(pass_)}")
|
|
return done + 1
|
|
|
|
|
|
def reviewer_header(default: str, pass_: str, rerun: bool) -> Failure | None:
|
|
"""Writes the branch diff and the places into the review folder, and prints the header that launches pass_'s reviewer in the pass's unanswered round, or its relaunch one rung up where rerun."""
|
|
titles = branch_titles(default)
|
|
if isinstance(titles, Failure):
|
|
return titles
|
|
round_ = unanswered_round(titles, pass_)
|
|
if isinstance(round_, Failure):
|
|
return round_
|
|
found = [top(), *(git(*args) for args in (["symbolic-ref", "--short", "HEAD"], ["rev-parse", "--short", "HEAD"], ["merge-base", "HEAD", default]))]
|
|
failed = next((answer for answer in found if isinstance(answer, Failure)), None)
|
|
if failed is not None:
|
|
return failed
|
|
worktree, branch, head, base = (str(answer).strip() for answer in found)
|
|
folder = made_review_folder(worktree)
|
|
if isinstance(folder, Failure):
|
|
return folder
|
|
launch = Launch(pass_, round_, rerun, pull_named(worktree, branch), worktree, branch, base, default, folder, folder / f"branch-{head}.diff")
|
|
if rerun and not launch.asked().exists():
|
|
return Failure(f"no report asked for a rerun of {pass_}'s round {round_}: {launch.asked()} is missing; relaunch before you commit the round's answers")
|
|
written = write_inputs(launch, base)
|
|
if isinstance(written, Failure):
|
|
return written
|
|
print(launch.header())
|
|
return None
|
|
|
|
|
|
def write_inputs(launch: Launch, base: str) -> Failure | None:
|
|
"""Writes the branch diff from base to HEAD and what `larv-review places` prints into the review folder."""
|
|
diff = git("diff", base, "HEAD", errors="replace")
|
|
if isinstance(diff, Failure):
|
|
return diff
|
|
history = history_file()
|
|
if isinstance(history, Failure):
|
|
return history
|
|
try:
|
|
for path, text in ((launch.diff, diff), (launch.folder / "places.md", places_report(history[1]) + "\n")):
|
|
# review-replay copies a file into a round's replay by its mtime, so rewriting one unchanged would drop it from every earlier round.
|
|
if not path.exists() or path.read_text(encoding="utf-8") != text:
|
|
path.write_text(text, encoding="utf-8")
|
|
except (OSError, UnicodeDecodeError) as failure:
|
|
return Failure(f"could not write into the review folder {launch.folder}: {failure}")
|
|
return None
|
|
|
|
|
|
def pull_named(worktree: str, branch: str) -> str:
|
|
"""The branch's open pull request at its pushed head, as `#<number>`, or why there is none to name.
|
|
|
|
A reviewer reads the branch in the worktree, so the launch goes ahead when no pull request can be named."""
|
|
upstream = git("rev-parse", "@{upstream}")
|
|
if isinstance(upstream, Failure):
|
|
return "none, the branch is not pushed"
|
|
origin = git("remote", "get-url", "origin")
|
|
if isinstance(origin, Failure):
|
|
return "unknown, the checkout has no origin remote"
|
|
number = open_pull(forge_of(origin.strip()), branch, upstream.strip(), runner(worktree))
|
|
if isinstance(number, Failure):
|
|
return f"unknown, {number.reason}"
|
|
return f"#{number}" if number else "none open at the pushed head"
|