Skip to content

Commit d593882

Browse files
committed
Post inline audit comments in batches and never skip the summary
A large review can get a 502 from GitHub after the review was created, which stopped the job before the summary comment. Post in batches of 20, log a failed batch as a warning, and always continue to the summary.
1 parent 816fbbe commit d593882

1 file changed

Lines changed: 28 additions & 12 deletions

File tree

‎scripts/audit/post-review.mjs‎

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,26 +176,42 @@ export async function postReview({ github, context, core, reportPath, headSha, h
176176
const posted = await existingFindingIds({ github, context, prNumber });
177177
const { comments, overflow } = buildReview(report, lineMap, posted);
178178

179-
if (comments.length > 0) {
180-
await github.rest.pulls.createReview({
181-
...context.repo,
182-
pull_number: prNumber,
183-
commit_id: headSha,
184-
event: "COMMENT",
185-
body: `Example audit: ${comments.length} inline suggestion(s). The summary comment on this PR has the full list.`,
186-
comments
187-
});
188-
core.info(`Posted ${comments.length} inline comment(s).`);
179+
// Post inline comments in small batches. Large reviews can make GitHub
180+
// return a 502 after it has already created the review, so a failure here
181+
// must never prevent the summary comment below. The next run dedupes by
182+
// finding id, so a batch that half-succeeded is not repeated.
183+
const BATCH = 20;
184+
let postedCount = 0;
185+
for (let i = 0; i < comments.length; i += BATCH) {
186+
const batch = comments.slice(i, i + BATCH);
187+
try {
188+
await github.rest.pulls.createReview({
189+
...context.repo,
190+
pull_number: prNumber,
191+
commit_id: headSha,
192+
event: "COMMENT",
193+
body: i === 0 ? `Example audit: ${comments.length} inline comment(s). The summary comment on this PR has the full list.` : `Example audit, continued (${i + 1} to ${i + batch.length} of ${comments.length}).`,
194+
comments: batch
195+
});
196+
postedCount += batch.length;
197+
} catch (err) {
198+
core.warning(`Inline review batch failed (${err.status ?? ""} ${err.message}); continuing with the summary comment.`);
199+
}
189200
}
201+
if (postedCount > 0) core.info(`Posted ${postedCount} inline comment(s).`);
190202

191203
let body = toMarkdown(report, { forComment: true });
192204
if (checkConclusion === "failure" && report.mode === "enforcing") {
193205
body = body.replace("<!-- hub-audit -->\n", "<!-- hub-audit -->\n> The **Audit examples** check failed because of the items under *Fix before merge*. Push a fix and it re-runs automatically.\n\n");
194206
}
195207
const notInline = overflow.filter((f) => !posted.has(f.id) && f.line === 0).length;
196208
if (notInline > 0 && comments.length > 0) body += `\n${notInline} finding(s) are file-level and appear only in this summary.\n`;
197-
await upsertStickyComment({ github, context, prNumber, body });
198-
core.info(`Updated summary comment on #${prNumber}.`);
209+
try {
210+
await upsertStickyComment({ github, context, prNumber, body });
211+
core.info(`Updated summary comment on #${prNumber}.`);
212+
} catch (err) {
213+
core.setFailed(`Could not post the summary comment: ${err.status ?? ""} ${err.message}`);
214+
}
199215
}
200216

201217
function str(v, max) {

0 commit comments

Comments
 (0)