"""Stage 1 output: JSON, the PR comment and the monthly issue body.""" from __future__ import annotations from collections.abc import Iterable from docs_review.flags import Flag STAGE1_MARKER = "" MONTHLY_MARKER = "" _SECTIONS = ( ("touched", "Touched", "These documents mention code or a name this PR changes."), ("dead", "Dead", "These documents mention a path this PR removes or renames."), ("uncovered", "Uncovered", "These source files are not mentioned in any document."), ("stale_generated", "Stale generated", "These generated documents depend on an input that changed."), ("unguided", "Unguided", "These documents are not described in the documentation guide."), ) def stage1_json(flags: Iterable[Flag], *, mode: str, head_sha: str) -> dict: return { "mode": mode, "head_sha": head_sha, "flags": [ {"kind": f.kind, "doc": f.doc, "subject": f.subject, "detail": f.detail} for f in flags ], } def _line(f: Flag) -> str: if f.doc is None: return f"- `{f.subject}` ({f.detail})" return f"- `{f.doc}`: `{f.subject}` ({f.detail})" def render_sections(flags: Iterable[Flag]) -> list[str]: flags = tuple(flags) out: list[str] = [] for kind, title, blurb in _SECTIONS: members = [f for f in flags if f.kind == kind] if members: out.append("\n".join([f"### {title}", blurb, ""] + [_line(f) for f in members])) return out def render_comment( flags: Iterable[Flag], *, head_sha: str, note: str | None = None ) -> str | None: parts = render_sections(flags) if not parts and not note: return None if note: parts.append(note) parts.append(f"Checked at {head_sha}.") return STAGE1_MARKER + "\n" + "\n\n".join(parts) + "\n" def render_clean(head_sha: str) -> str: return STAGE1_MARKER + f"Nothing flagged as of {head_sha}." def render_issue(flags: Iterable[Flag], *, month: str) -> str: parts = render_sections(flags) or [f"Nothing flagged as of {month}."] return MONTHLY_MARKER + "\n" + f"Documents to review for {month}.\n\n" + "\n\n".join(parts) + "\n"