Files
claude-config/bin/do_epic/finish.py
T

105 lines
8.7 KiB
Python

"""How an epic ends (docs/decisions.md → `do-epic: drives one Jira epic …` → "Finishing").
Once the epic holds issues and none is open, its finishing chunk runs, in a window named for the epic's key. It has no issue: `task` is its task, which follows the epic's state.
1. While a repo the epic changed lacks a release review, the chunk files one issue per such repo, labelled REVIEW_LABEL, and ends. Each such issue runs as any issue's chunk does, and so does each issue its review files.
2. With each repo reviewed, the human confirms each goal. For a goal they don't confirm, the chunk files the set of items they approve, and ends.
3. With every goal confirmed, `do-epic <EPIC-KEY> --close-epic` posts the summary on the epic and closes it.
The chunk starts again each time the epic holds no open issue, until the epic is closed. A repo is reviewed once per epic, as a release is: the issues filed after its review don't bring another."""
import dataclasses
import pathlib
import typing
from do_epic.chunk_prompt import DRIVER
from do_epic.backend.protocol import EpicIssue, EpicStore, REVIEW_LABEL
from failure import Failure
from do_epic import findings
from do_epic.split import finished
from do_epic.repo_pick import label_of, LabelRepos, recorded, Repo
from text_files.markdown_text import listed
from text_files.piped_text import piped
from plan.release import REVIEW_TITLE
# Not `--close`, which reads as closing the run or one issue: three readers, seated as the human, as a chunk's worker and as one of the client's team, picked this one from four, 2-1 over it.
CLOSE = "--close-epic"
USAGE = f"do-epic <EPIC-KEY> {CLOSE} <<'EOF' summary EOF"
HELP = f"""Once every issue is done, the epic's finishing chunk runs, in a window named for the epic.
It files a release review of each repo the epic changed, as an issue labelled `{REVIEW_LABEL}`, then asks you to confirm each goal.
{CLOSE} posts the markdown piped in on the epic as its summary, then closes the epic. It refuses while an issue is open or a repo lacks its review.
It closes the epic by the transition `jira-finish-transition` names, to the status `jira-finish-status` names."""
def landed(issues: list[EpicIssue], merged_status: str | None) -> list[EpicIssue]:
"""issues, each counted done where it has the status merged_status, which do-epic's `jira-pull-request-merged-status` names: its work has merged into the epic's working branch, and Jira counts it done only once a person has merged that branch into the default branch. The epic's release reviews and finishing come before that merge, so they can't wait for it (docs/decisions.md → `epic-branch: an epic's chunks branch off and merge into a working branch …`). None names no such status."""
if merged_status is None:
return issues
return [dataclasses.replace(issue, done=True) if issue.status.casefold() == merged_status.casefold() else issue for issue in issues]
def due(issues: list[EpicIssue]) -> bool:
"""Whether the epic's finishing comes next: it holds an issue, and none is open."""
return bool(issues) and not still_open(issues)
def still_open(issues: list[EpicIssue]) -> list[str]:
"""The key of each issue that isn't done; an item split by repo is done once its sub-tasks are."""
return [issue.key for issue in issues if not finished(issues, issue.key)]
def repo_of(issue: EpicIssue, team: LabelRepos) -> str | None:
"""The name of the repo the issue's labels record; None where they record none."""
pick = recorded(issue, team)
return pick.name if isinstance(pick, Repo) else None
def unreviewed(issues: list[EpicIssue], team: LabelRepos) -> list[str]:
"""The repos the epic changed that have no release review, sorted. The epic changed a repo where a done issue names it, and a repo has its review once an issue labelled REVIEW_LABEL names it, open or done."""
reviewed = {repo_of(issue, team) for issue in issues if REVIEW_LABEL in issue.labels}
changed = {repo_of(issue, team) for issue in issues if issue.done and REVIEW_LABEL not in issue.labels}
return sorted(repo for repo in changed - reviewed if repo is not None)
def task(epic: str, issues: list[EpicIssue], team: LabelRepos, launcher: pathlib.Path, language: str) -> str:
"""The finishing chunk's task, for the epic with the key epic as issues has it. launcher is the do-epic that runs the chunk, and language the one the epic's Jira text is in."""
opening = f"Finish the epic {epic}, which holds no open issue, as one chunk that changes no repo: work in the folder you start in."
if lacking := unreviewed(issues, team):
return (
f"{opening} Each repo the epic changed takes a release review first, and {listed(lacking, 'and')} {'has' if len(lacking) == 1 else 'have'} none. "
f"File one issue per such repo, with `{launcher} {epic} {findings.FILE} \"<summary>\" {findings.LABEL} <label> {findings.LABEL} {REVIEW_LABEL}` and its detail on stdin, both in {language}, the epic's language. "
f"The `<label>` is {listed([f'`{label_of(Repo(name), team)}` for {name}' for name in lacking], 'and')}. "
f"The detail asks for a release review of the whole repo, as the epic's issues left it: the one `~/.claude/skills/larv-review/SKILL.md` → \"Release review\" describes. The epic's key stands in place of the release there, so the review's branch opens with an empty commit titled `{REVIEW_TITLE}{epic}`. "
"Where filing fails and says the issue may exist, ask the human to look before you file it again. "
f"The chunk is finished once each issue is filed: {DRIVER} works them, and starts this chunk again once the epic holds no open issue."
)
return (
f"{opening} Each repo it changed has had its release review. Four steps remain, in this order.\n\n"
"1. Ask the human to confirm each goal under the description's ## Goals: that an achieve goal is achieved, and that no defect stands against a maintain goal. Ask one goal per question, each with the evidence of what shipped. "
"A goal is met only where the human confirms it: never take one as met from the evidence alone. Don't ask again about a goal a decision in the description records as confirmed.\n"
"2. For a goal the human doesn't confirm, propose a set of items that would meet it, and ask the human to approve the set. File each item they approve. "
f"Then the chunk is finished: {DRIVER} works the items, and starts this chunk again once the epic holds no open issue.\n"
f"3. Once the human has confirmed every goal, run `project-config epic-branch` in the checkout of each repo the epic changed. Where it fails, the repo keeps no working branch for the epic: skip it. Where it prints a template, run `chunk-pr open-epic {epic} \"<title>\"` in that checkout, which makes the epic's working branch level with the default branch and opens the pull request from it into the default branch. Never merge that pull request: a person does.\n"
f"4. Then write a summary in {language}, the epic's language, for the people who work the epic: what shipped in each repo, each pull request step 3 opened, and each goal's evidence. "
f"Post it and close the epic with `{launcher} {epic} {CLOSE}`, the summary on stdin. That is the chunk's very last step, and the chunk is finished once it succeeds.\n\n"
f"{findings.instructions(epic, launcher, language, team)} Record each goal's confirmation as such a decision.\n\n"
"The epic's issues, each done:\n\n" + "\n".join(f"- {issue.key}{f' ({repo})' if (repo := repo_of(issue, team)) else ''}: {issue.summary}" for issue in issues)
)
def closed(epic: str, store: EpicStore, team: LabelRepos, stdin: typing.TextIO, merged_status: str | None = None) -> str | Failure:
"""Posts the summary piped in on the epic with the key epic and closes it, and answers the line to print; a Failure, with nothing posted, while the epic isn't ready to close."""
# Whether the human confirmed each goal isn't checked here: a decision that records it is the worker's own text, so only the task and the judge, who reads the human's answers, can hold the worker to it.
issues = store.read()
if isinstance(issues, Failure):
return Failure(f"cannot close the epic {epic}: {issues.reason}")
issues = landed(issues, merged_status)
already = store.closed()
if already:
return Failure(f"cannot close the epic {epic}: {already.reason if isinstance(already, Failure) else 'it is closed already'}")
if waiting := still_open(issues):
return Failure(f"cannot close the epic {epic}: {listed(waiting, 'and')} {'is' if len(waiting) == 1 else 'are'} open")
if lacking := unreviewed(issues, team):
return Failure(f"cannot close the epic {epic}: no issue labelled {REVIEW_LABEL} reviews {listed(lacking, 'or')}")
done = store.close(piped(stdin))
return Failure(f"cannot close the epic {epic}: {done.reason}") if isinstance(done, Failure) else f"Posted the summary on the epic {epic} and closed it."