tests / unittest (push) Successful in 19s
Callers use Civilytics/docs-review/.gitea/workflows/review.yml@v1 with secrets: inherit. Stage 1 on every pull request; for drafter: claude, a 180-second wait and then the sandboxed drafter; closed pull requests close their docs PR; schedule and dispatch run the monthly pass. Tested live against Gitea 28.3 (drafter none, a misspelled drafter, claude, close). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
142 lines
5.5 KiB
Python
142 lines
5.5 KiB
Python
"""The `monthly` flow: whole-repository stage 1, then an issue or a drafted PR."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import re
|
|
from collections.abc import Mapping
|
|
from pathlib import Path
|
|
|
|
from docs_review import git
|
|
from docs_review.draft import build_prompt, parse_summary, render_pr_body
|
|
from docs_review.drafter import draft_in_sandbox
|
|
from docs_review.flags import Flag, compute_flags, group_uncovered, whole_repo_changes
|
|
from docs_review.inputs import Inputs, expand_patterns
|
|
from docs_review.report import MONTHLY_MARKER, render_issue, stage1_json
|
|
from docs_review.run_pr import TASK_PROMPT, read_guide, read_texts
|
|
|
|
MONTHLY_PREFIX = "docs-review/monthly-"
|
|
MONTH_PATTERN = re.compile(r"^\d{4}-(0[1-9]|1[0-2])$")
|
|
# Whole-repository mode counts every file as added and every input as changed, so only
|
|
# these two kinds mean anything.
|
|
MONTHLY_KINDS = ("uncovered", "unguided")
|
|
NO_DIFF_PAYLOAD = (
|
|
"Monthly mode has no diff. Read the repository as it stands, starting from the "
|
|
"top-level documents."
|
|
)
|
|
MOVED_NOTE = "The monthly review moved to PR #{n}."
|
|
MERGE_CONFLICT_NOTE = (
|
|
"The monthly docs branch `{branch}` no longer merges with `{base}`; "
|
|
"resolve it by hand."
|
|
)
|
|
|
|
|
|
def valid_month(month: str) -> bool:
|
|
return bool(MONTH_PATTERN.match(month))
|
|
|
|
|
|
def _stage1(inputs: Inputs, cwd: Path):
|
|
docs = expand_patterns(inputs.docs, cwd)
|
|
generated = expand_patterns(inputs.generated, cwd)
|
|
repo_files = git.ls_files(cwd)
|
|
changes = whole_repo_changes(repo_files, inputs.source_dirs)
|
|
flags = compute_flags(changes, frozenset(), frozenset(), read_texts(docs, cwd),
|
|
read_texts(generated, cwd), inputs.source_dirs,
|
|
read_guide(inputs, cwd), repo_files=repo_files)
|
|
kept = [f for f in flags if f.kind in MONTHLY_KINDS]
|
|
return group_uncovered(kept), docs
|
|
|
|
|
|
def post_issue(forge, flags: tuple[Flag, ...], month: str, note: str | None = None) -> None:
|
|
"""Open or update the one monthly issue; with nothing to say, open nothing."""
|
|
existing = forge.find_open_issue(MONTHLY_MARKER)
|
|
if not flags and not note and existing is None:
|
|
return
|
|
body = render_issue(flags, month=month)
|
|
if note:
|
|
body += "\n" + note + "\n"
|
|
title = f"docs: monthly review {month}"
|
|
if existing is not None:
|
|
forge.edit_issue(existing["number"], title=title, body=body)
|
|
else:
|
|
forge.open_issue(title, body)
|
|
|
|
|
|
def _checkout(base: str, month: str, reuse: dict | None, ident, cwd: Path) -> str | None:
|
|
"""The branch to draft on, or None when a reused branch no longer merges."""
|
|
git.fetch(base, cwd)
|
|
if reuse is None:
|
|
branch = f"{MONTHLY_PREFIX}{month}"
|
|
git.checkout_new(branch, f"origin/{base}", cwd)
|
|
return branch
|
|
branch = reuse["head"]["ref"]
|
|
git.fetch(branch, cwd)
|
|
git.checkout_new(branch, "FETCH_HEAD", cwd)
|
|
try:
|
|
git.merge(f"origin/{base}", ident, cwd)
|
|
except git.GitError:
|
|
try:
|
|
git.run(["rev-parse", "-q", "--verify", "MERGE_HEAD"], cwd)
|
|
except git.GitError:
|
|
raise # not a conflict: nothing to abort
|
|
git.run(["merge", "--abort"], cwd)
|
|
return None
|
|
return branch
|
|
|
|
|
|
def _commit_message(inputs: Inputs, month: str) -> str:
|
|
tail = f" (#{inputs.tracking_issue})" if inputs.tracking_issue else ""
|
|
return f"docs({inputs.workstream}): monthly review {month}{tail}"
|
|
|
|
|
|
def _deliver(forge, inputs, base, month, branch, reuse, ident, flags, result, changed, cwd,
|
|
env):
|
|
git.commit(changed, _commit_message(inputs, month), ident, cwd)
|
|
git.push(branch, cwd, env.get("DOCS_REVIEW_BOT_TOKEN"),
|
|
env.get("GITHUB_SERVER_URL"))
|
|
head = git.run(["rev-parse", "HEAD"], cwd).strip()
|
|
body = render_pr_body(parse_summary(result.text), result, flags, head, changed)
|
|
title = f"docs: monthly review {month}"
|
|
if reuse is not None:
|
|
number = reuse["number"]
|
|
forge.edit_pr(number, title=title, body=body)
|
|
else:
|
|
number = forge.open_pr(head=branch, base=base, title=title, body=body)
|
|
issue = forge.find_open_issue(MONTHLY_MARKER)
|
|
if issue is not None:
|
|
forge.edit_issue(issue["number"],
|
|
body=MONTHLY_MARKER + "\n" + MOVED_NOTE.format(n=number) + "\n")
|
|
forge.close_issue(issue["number"])
|
|
|
|
|
|
def run_monthly(inputs: Inputs, forge, env: Mapping[str, str], cwd: Path,
|
|
month: str, base: str) -> int:
|
|
flags, docs = _stage1(inputs, cwd)
|
|
if inputs.drafter == "none":
|
|
post_issue(forge, flags, month)
|
|
return 0
|
|
ident = forge.identity()
|
|
reuse = forge.find_open_pr_prefix(MONTHLY_PREFIX)
|
|
branch = _checkout(base, month, reuse, ident, cwd)
|
|
if branch is None:
|
|
post_issue(forge, flags, month,
|
|
MERGE_CONFLICT_NOTE.format(branch=reuse["head"]["ref"], base=base))
|
|
return 0
|
|
flags_json = json.dumps(stage1_json(flags, mode="monthly", head_sha=month))
|
|
prompt = build_prompt(
|
|
TASK_PROMPT.read_text(encoding="utf-8"), mode="monthly", pr=None,
|
|
flags_json=flags_json, payload=NO_DIFF_PAYLOAD, docs=docs,
|
|
top_level=inputs.top_level, guide_path=inputs.doc_guide,
|
|
)
|
|
drafted = draft_in_sandbox(inputs, prompt, cwd, env)
|
|
if drafted.note is not None:
|
|
post_issue(forge, flags, month, drafted.note)
|
|
return 1
|
|
result, changed = drafted.result, list(drafted.changed)
|
|
if not changed:
|
|
post_issue(forge, flags, month)
|
|
return 0
|
|
_deliver(forge, inputs, base, month, branch, reuse, ident, flags, result, changed, cwd,
|
|
env)
|
|
return 0
|