66# This workflow holds no model credentials and never checks out PR code. It
77# validates the payload, drops reviews of a head that is no longer current,
88# then edits a single summary comment in place and adds inline comments for
9- # findings not posted before. It never approves or requests changes.
9+ # findings not posted before. It never approves or requests changes. A
10+ # payload can also add or remove labels from a fixed allowlist, e.g. marking a
11+ # PR the bot skipped (a dependency bot PR or a mechanical tooling update) with
12+ # no comment at all. It can delete the bot's own comments (a whole review, or
13+ # single findings it has withdrawn), and reply in the threads of its own
14+ # findings. It only ever deletes or replies to comments by github-actions[bot].
1015name : react/metro/review-post
1116
1217on :
3641 const zlib = require('node:zlib');
3742 const MARKER = '<!-- metro-review-bot -->';
3843 const BOT = 'github-actions[bot]';
44+ // The only labels a payload may add or remove.
45+ const LABELS = {
46+ 'Auto review: Skipped': {
47+ color: 'ededed',
48+ description: 'The Metro review bot skipped this PR (a dependency bot PR or a mechanical tooling update)',
49+ },
50+ };
3951
4052 // #region Validate the payload
4153
@@ -50,11 +62,26 @@ jobs:
5062 if (p.v !== 1) return fail('unsupported version');
5163 if (!Number.isInteger(p.pr) || p.pr < 1) return fail('bad pr');
5264 if (!/^[0-9a-f]{40}$/.test(p.head_sha ?? '')) return fail('bad head_sha');
53- if (typeof p.summary !== 'string' || !p.summary.startsWith(MARKER) || p.summary.length > 60000) {
65+ const addLabels = Array.isArray(p.labels?.add) ? p.labels.add : [];
66+ const removeLabels = Array.isArray(p.labels?.remove) ? p.labels.remove : [];
67+ if ([...addLabels, ...removeLabels].some(l => !Object.hasOwn(LABELS, l))) return fail('label not allowed');
68+ const hasSummary = p.summary != null;
69+ if (hasSummary && (typeof p.summary !== 'string' || !p.summary.startsWith(MARKER) || p.summary.length > 60000)) {
5470 return fail('bad summary');
5571 }
5672 const comments = Array.isArray(p.comments) ? p.comments : [];
73+ const deleteIds = Array.isArray(p.delete_ids) ? p.delete_ids : [];
74+ const replies = Array.isArray(p.replies) ? p.replies : [];
75+ const retract = p.retract === true;
76+ if (!hasSummary && (comments.length || deleteIds.length || replies.length || (!retract && !addLabels.length && !removeLabels.length))) {
77+ return fail('a payload without a summary may only change labels or retract');
78+ }
79+ if (retract && (hasSummary || comments.length)) return fail('retract can only be combined with labels');
5780 if (comments.length > 15) return fail('too many comments');
81+ if (deleteIds.length > 30 || deleteIds.some(id => !/^[0-9a-f]{8}$/.test(id))) return fail('bad delete_ids');
82+ if (replies.length > 15 || replies.some(r => !/^[0-9a-f]{8}$/.test(r?.id ?? '') || typeof r.body !== 'string' || r.body.length > 3000)) {
83+ return fail('bad replies');
84+ }
5885 for (const c of comments) {
5986 const ok =
6087 /^[0-9a-f]{8}$/.test(c.id ?? '') &&
@@ -95,6 +122,54 @@ jobs:
95122 return core.notice(`#${p.pr} moved to ${pr.head.sha.slice(0, 7)}; stale review of ${p.head_sha.slice(0, 7)} dropped`);
96123 }
97124
125+ // #endregion
126+ // #region Labels
127+
128+ for (const name of addLabels) {
129+ try {
130+ await github.rest.issues.getLabel({owner, repo, name});
131+ } catch (e) {
132+ if (e.status !== 404) throw e;
133+ await github.rest.issues.createLabel({owner, repo, name, ...LABELS[name]});
134+ }
135+ }
136+ if (addLabels.length) await github.rest.issues.addLabels({owner, repo, issue_number: p.pr, labels: addLabels});
137+ for (const name of removeLabels) {
138+ try {
139+ await github.rest.issues.removeLabel({owner, repo, issue_number: p.pr, name});
140+ } catch (e) {
141+ if (e.status !== 404) throw e;
142+ }
143+ }
144+ const labelNote = [addLabels.length ? `labels added: ${addLabels.join(', ')}` : null, removeLabels.length ? `labels removed: ${removeLabels.join(', ')}` : null].filter(Boolean).join('; ');
145+
146+ // #endregion
147+ // #region Retract and delete
148+
149+ const issueComments = await github.paginate(github.rest.issues.listComments, {owner, repo, issue_number: p.pr, per_page: 100});
150+ const ownSummaries = issueComments.filter(c => c.user.login === BOT && c.body.startsWith(MARKER));
151+ const reviewComments = await github.paginate(github.rest.pulls.listReviewComments, {owner, repo, pull_number: p.pr, per_page: 100});
152+ const own = reviewComments.filter(c => c.user.login === BOT);
153+ // A finding's inline comment carries its id in the footer. The bot's
154+ // replies in a thread don't.
155+ const findingId = c => c.body.match(/id `([0-9a-f]{8})`/)?.[1];
156+ const deleted = new Set();
157+ for (const c of own) {
158+ const id = findingId(c);
159+ if (retract || (id && deleteIds.includes(id))) {
160+ await github.rest.pulls.deleteReviewComment({owner, repo, comment_id: c.id});
161+ deleted.add(c.id);
162+ }
163+ }
164+ if (retract) {
165+ for (const c of ownSummaries) await github.rest.issues.deleteComment({owner, repo, comment_id: c.id});
166+ return core.notice(`#${p.pr}: review retracted (${ownSummaries.length} summary and ${deleted.size} inline comment(s) deleted)${labelNote ? `; ${labelNote}` : ''}`);
167+ }
168+ if (!hasSummary) return core.notice(`#${p.pr}: ${labelNote}`);
169+
170+ // #endregion
171+ // #region Post
172+
98173 // Head-side lines a comment can anchor to, per file.
99174 const anchors = new Map();
100175 for (const f of await github.paginate(github.rest.pulls.listFiles, {owner, repo, pull_number: p.pr, per_page: 100})) {
@@ -108,25 +183,16 @@ jobs:
108183 anchors.set(f.filename, lines);
109184 }
110185
111- // #endregion
112- // #region Post
113-
114- const issueComments = await github.paginate(github.rest.issues.listComments, {owner, repo, issue_number: p.pr, per_page: 100});
115- const existing = issueComments.find(c => c.user.login === BOT && c.body.startsWith(MARKER));
186+ const existing = ownSummaries[0];
116187 const summary = defuse(p.summary);
117188 if (existing) {
118189 await github.rest.issues.updateComment({owner, repo, comment_id: existing.id, body: summary});
119190 } else {
120191 await github.rest.issues.createComment({owner, repo, issue_number: p.pr, body: summary});
121192 }
122193
123- const reviewComments = await github.paginate(github.rest.pulls.listReviewComments, {owner, repo, pull_number: p.pr, per_page: 100});
124- const posted = new Set(
125- reviewComments
126- .filter(c => c.user.login === BOT)
127- .map(c => c.body.match(/id `([0-9a-f]{8})`/)?.[1])
128- .filter(Boolean),
129- );
194+ const remaining = own.filter(c => !deleted.has(c.id));
195+ const posted = new Set(remaining.map(findingId).filter(Boolean));
130196 const fresh = comments.filter(c => !posted.has(c.id) && anchors.get(c.path)?.has(c.line));
131197 const skipped = comments.length - fresh.length;
132198 if (fresh.length) {
@@ -139,6 +205,15 @@ jobs:
139205 comments: fresh.map(c => ({path: c.path, line: c.line, side: 'RIGHT', body: defuse(c.body)})),
140206 });
141207 }
142- core.notice(`#${p.pr}: summary ${existing ? 'updated' : 'created'}, ${fresh.length} inline comment(s) posted, ${skipped} already posted or unanchorable`);
208+ // Replies go in the thread of the bot's own finding with that id.
209+ let replied = 0;
210+ for (const r of replies) {
211+ const target = remaining.find(c => findingId(c) === r.id && c.in_reply_to_id == null);
212+ if (!target) continue;
213+ await github.rest.pulls.createReplyForReviewComment({owner, repo, pull_number: p.pr, comment_id: target.id, body: defuse(r.body)});
214+ replied++;
215+ }
216+ const extra = [deleted.size ? `${deleted.size} deleted` : null, replied ? `${replied} replied` : null, labelNote || null].filter(Boolean).join('; ');
217+ core.notice(`#${p.pr}: summary ${existing ? 'updated' : 'created'}, ${fresh.length} inline comment(s) posted, ${skipped} already posted or unanchorable${extra ? `; ${extra}` : ''}`);
143218
144219 // #endregion
0 commit comments