File size: 3,149 Bytes
914f53d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | import type { Octokit } from '@octokit/rest';
import type { Finding } from './types.js';
/**
* Post blocking findings as inline PR review comments anchored at file/line,
* so humans act on them where they read code instead of cross-referencing the
* state-JSON ledger. Each comment carries a per-finding marker so re-reports
* across cycles never duplicate. Findings that GitHub rejects (line outside
* the diff) stay summary-only — the ledger comment already lists them.
*/
const FINDING_MARKER_PREFIX = '<!-- pr-agent-finding:';
function findingMarker(id: string): string {
return `${FINDING_MARKER_PREFIX}${id} -->`;
}
function commentBody(finding: Finding): string {
return `${findingMarker(finding.id)}
**PR Agent — blocking finding \`${finding.id}\`** (${finding.source})
${finding.message}
<sub>Reply \`agent-resolve ${finding.id}\` to dismiss as a false positive.</sub>`;
}
export async function postInlineFindings(
octokit: Octokit,
owner: string,
repo: string,
prNumber: number,
headSha: string,
findings: Finding[],
): Promise<void> {
const candidates = findings.filter(
(f) => f.severity === 'blocking' && f.file && typeof f.line === 'number' && f.line > 0,
);
if (candidates.length === 0) return;
const existing: string[] = [];
let page = 1;
for (;;) {
const { data } = await octokit.pulls.listReviewComments({
owner,
repo,
pull_number: prNumber,
per_page: 100,
page,
});
existing.push(...data.map((c) => c.body ?? ''));
if (data.length < 100) break;
page++;
}
const fresh = candidates.filter(
(f) => !existing.some((body) => body.includes(findingMarker(f.id))),
);
if (fresh.length === 0) return;
const comments = fresh.map((f) => ({
path: f.file,
line: f.line!,
side: 'RIGHT' as const,
body: commentBody(f),
}));
try {
// One review containing every comment: a single notification for humans.
await octokit.pulls.createReview({
owner,
repo,
pull_number: prNumber,
commit_id: headSha,
event: 'COMMENT',
comments,
});
console.log(`Posted ${comments.length} inline finding comment(s)`);
return;
} catch (err) {
// GitHub 422s the whole review when any single anchor falls outside the
// diff. Retry per-comment so valid anchors still land.
console.warn(
'Batch inline review rejected; retrying per comment:',
(err as Error).message,
);
}
let posted = 0;
for (const c of comments) {
try {
await octokit.pulls.createReviewComment({
owner,
repo,
pull_number: prNumber,
commit_id: headSha,
path: c.path,
line: c.line,
side: c.side,
body: c.body,
});
posted++;
} catch (err) {
console.warn(
`Inline comment rejected for ${c.path}:${c.line} (likely outside diff):`,
(err as Error).message,
);
}
}
console.log(`Posted ${posted}/${comments.length} inline finding comment(s)`);
}
|