feat: reusable review workflow
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>
This commit is contained in:
jaredandClaude Opus 5.5 committed 2026-11-12 15:12:57 -05:00
1 parent ade847ba5b
commit f39409e62c
11 files changed
+466 -26

No files matched your search

+30
View File
@@ -26,6 +26,8 @@ CREDENTIAL_NOTE = "drafted edits contained a credential value; nothing committed
DIRTY_NOTE = "working tree not clean before drafting"
EXPORT_NOTE = "drafter failed: could not export the tree for drafting: {reason}"
EXPORT_REASON_MAX = 200
NO_CREDENTIAL_NOTE = "drafter: claude but no Claude credential is set"
CREDENTIAL_VARS = ("CLAUDE_CODE_OAUTH_TOKEN", "ANTHROPIC_API_KEY")
REFUSED_NOTE = "drafter failed: refusing to copy back `{path}` (symlink or outside the repository)"
@@ -78,6 +80,31 @@ def write_system_prompt(cwd: Path, guide_path: str | None,
return Path(name)
def has_claude_credential(env: Mapping[str, str]) -> bool:
return any((env.get(k) or "").strip() for k in CREDENTIAL_VARS)
def write_step_summary(result: DraftResult, env: Mapping[str, str]) -> None:
"""Print the run's cost and turns, whichever Claude reported, and append them to
$GITHUB_STEP_SUMMARY when the runner sets it."""
lines = []
if result.cost_usd is not None:
lines.append(f"- total_cost_usd: {result.cost_usd:.2f}")
if result.turns is not None:
lines.append(f"- num_turns: {result.turns}")
if not lines:
return
print("drafter " + ", ".join(l[2:] for l in lines))
path = env.get("GITHUB_STEP_SUMMARY")
if not path:
return
try:
with open(path, "a", encoding="utf-8") as fh:
fh.write("### docs-review drafter\n\n" + "\n".join(lines) + "\n")
except OSError as err:
print(f"warning: could not write the step summary: {err}", file=sys.stderr)
def run_drafter(inputs: Inputs, prompt: str, cwd: Path, env: Mapping[str, str],
system_prompt_file: Path | None) -> DraftResult:
"""Run the configured drafter in cwd. Task 12 adds `local`."""
@@ -129,6 +156,8 @@ class Drafted:
def draft_in_sandbox(inputs: Inputs, prompt: str, cwd: Path,
env: Mapping[str, str]) -> Drafted:
"""Run the drafter on an export of HEAD and copy its allowed changes into cwd."""
if inputs.drafter == "claude" and not has_claude_credential(env):
return Drafted(None, (), NO_CREDENTIAL_NOTE)
if git.run(["status", "--porcelain"], cwd).strip():
return Drafted(None, (), DIRTY_NOTE)
box = prompt_file = None
@@ -143,6 +172,7 @@ def draft_in_sandbox(inputs: Inputs, prompt: str, cwd: Path,
before = sandbox.snapshot(box)
prompt_file = write_system_prompt(box, inputs.doc_guide, env)
result = run_drafter(inputs, prompt, box, env, prompt_file)
write_step_summary(result, env)
if not result.ok:
return Drafted(result, (), f"drafter failed: {result.error}")
change = sandbox.compare(before, sandbox.snapshot(box))