/** * Local review has no forge to post comments to, so drafted comments are * written to a markdown file instead — easy to paste into a PR/MR description, * a commit message, or hand to whoever opens the actual pull request. */ import { mkdir, writeFile } from "node:fs/promises"; import { join } from "node:path"; import type { DraftComment } from "./state.ts"; function sanitize(name: string): string { return name.replace(/[^a-zA-Z0-9._-]+/g, "-").replace(/^-+|-+$/g, "") || "review"; } export async function writeNotesFile(repoRoot: string, branch: string, drafts: DraftComment[]): Promise { const dir = join(repoRoot, ".pi-diff"); await mkdir(dir, { recursive: true }); const path = join(dir, `${sanitize(branch)}.md`); const byPath = new Map(); for (const draft of drafts) { const list = byPath.get(draft.path) ?? []; list.push(draft); byPath.set(draft.path, list); } const lines: string[] = [`# Diff notes — ${branch}`, "", `_${drafts.length} comment${drafts.length === 1 ? "" : "s"}, generated by pi-diff_`, ""]; for (const [path, items] of byPath) { lines.push(`## ${path}`, ""); for (const item of items.sort((a, b) => a.line - b.line)) { lines.push(`- **line ${item.line}:** ${item.body}`); } lines.push(""); } await writeFile(path, lines.join("\n"), "utf8"); return path; }