72 lines
3.3 KiB
Python
72 lines
3.3 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."""
|
|
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")
|
|
|
|
|
|
def reviewer_header(default: str, pass_: str, rerun: bool) -> Failure | None:
|
|
"""Prints the header of the prompt that launches pass_'s reviewer in its next round, or the relaunch of its current one where rerun."""
|
|
titles = branch_titles(default)
|
|
if isinstance(titles, Failure):
|
|
return titles
|
|
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_)}")
|
|
round_ = done + 1
|
|
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
|
|
diff = git("diff", base, "HEAD", errors="replace")
|
|
if isinstance(diff, Failure):
|
|
return diff
|
|
history = history_file()
|
|
if isinstance(history, Failure):
|
|
return history
|
|
diff_file, places_file = folder / f"branch-{head}.diff", folder / "places.md"
|
|
try:
|
|
diff_file.write_text(diff, encoding="utf-8")
|
|
places_file.write_text(places_report(history[1]) + "\n", encoding="utf-8")
|
|
except OSError as failure:
|
|
return Failure(f"could not write into the review folder {folder}: {failure}")
|
|
report = folder / f"{pass_}-round-{round_}.md"
|
|
lines = [
|
|
f"Pass: {pass_}, round {round_}.",
|
|
f"PR: {pull_named(branch)}.",
|
|
f"Worktree: {worktree}, on branch {branch}.",
|
|
f"Base: {base}, the merge base of HEAD and {default}.",
|
|
f"Report: write it to {report.with_name(report.stem + '-rerun.md')}, in place of {report}, which asked for this rerun." if rerun else f"Report: write it to {report}.",
|
|
f"Diff: {diff_file}",
|
|
*([f"Places: {places_file}"] if pass_ in TAKE_PLACES else []),
|
|
]
|
|
print("\n".join(lines))
|
|
return None
|
|
|
|
|
|
def pull_named(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 a pull request it can't name doesn't stop its launch."""
|
|
upstream = git("rev-parse", "@{upstream}")
|
|
origin = git("remote", "get-url", "origin")
|
|
if isinstance(upstream, Failure) or isinstance(origin, Failure):
|
|
return "none, the branch is not pushed"
|
|
checkout = top()
|
|
if isinstance(checkout, Failure):
|
|
return f"unknown, {checkout.reason}"
|
|
number = open_pull(forge_of(origin.strip()), branch, upstream.strip(), runner(checkout))
|
|
if isinstance(number, Failure):
|
|
return f"unknown, {number.reason}"
|
|
return f"#{number}" if number else "none open at the pushed head"
|