co-maintainer 0.6.4 → 0.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "co-maintainer",
3
- "version": "0.6.4",
3
+ "version": "0.6.5",
4
4
  "description": "An imitator of yours, A PR review tool like co-maintainer.",
5
5
  "keywords": [
6
6
  "ai",
@@ -200,6 +200,38 @@ function inlineCommentBody(finding, suggestion = false) {
200
200
  return heading;
201
201
  return `${heading}\n\n${rest}`;
202
202
  }
203
+ function rowFinding(row) {
204
+ return {
205
+ path: row.path ?? "",
206
+ from: row.line_from ?? 0,
207
+ to: row.line_to ?? row.line_from ?? 0,
208
+ heading: row.title,
209
+ excerpt: row.body_md,
210
+ };
211
+ }
212
+ function sameBody(a, b) {
213
+ const clean = (text) => text.replace(/\r\n/g, "\n").trim();
214
+ return clean(a) === clean(b);
215
+ }
216
+ /** Whether a posted comment already says what this finding says. The comment
217
+ * may have gone out with its suggestion block, so both forms count. */
218
+ function repeatsComment(row, posted) {
219
+ const finding = rowFinding(row);
220
+ return (sameBody(posted, inlineCommentBody(finding)) ||
221
+ sameBody(posted, inlineCommentBody(finding, true)));
222
+ }
223
+ /** The pull request's review comments by id. An unreadable list comes back
224
+ * empty, so a repeat is posted again rather than going unsaid. */
225
+ async function postedCommentBodies(client, job, log) {
226
+ try {
227
+ const listed = await client.pages(`repos/${job.repo}/pulls/${job.pr_number}/comments`);
228
+ return new Map(listed.map((item) => [String(item.id ?? ""), String(item.body ?? "")]));
229
+ }
230
+ catch (error) {
231
+ log("info", `could not read the earlier comments: ${redact(String(error))}`);
232
+ return new Map();
233
+ }
234
+ }
203
235
  /** A review job's options: the config, with the job's pinned model (a
204
236
  * "Retry with the same model") over the configured one. */
205
237
  export function reviewOptionsForJob(repo, prNumber, args) {
@@ -350,23 +382,31 @@ async function finishCheck(client, job, checkRunId, conclusion, title, findings,
350
382
  }
351
383
  async function publish(client, job, reviewId, headSha, files, log, metadata, mode = DEFAULT_REVIEW_BLOCKING) {
352
384
  const stored = listFindingsForReview(reviewId);
353
- const replies = stored.filter((row) => row.thread_comment_id);
354
- const freshRows = stored.filter((row) => row.state === "new" && !row.thread_comment_id);
355
- const fresh = freshRows.map((row) => ({
356
- path: row.path ?? "",
357
- from: row.line_from ?? 0,
358
- to: row.line_to ?? row.line_from ?? 0,
359
- heading: row.title,
360
- excerpt: row.body_md,
361
- }));
385
+ // GitHub wraps every `in_reply_to` comment in a review of its own, so a round
386
+ // posts no replies. A repeat stays on its thread and in the summary, and a
387
+ // finding whose text changed goes inline on the one review.
388
+ const threaded = stored.filter((row) => row.thread_comment_id);
389
+ const earlier = threaded.length > 0
390
+ ? await postedCommentBodies(client, job, log)
391
+ : new Map();
392
+ const repeats = threaded.filter((row) => {
393
+ const posted = earlier.get(row.thread_comment_id ?? "");
394
+ return posted !== undefined && repeatsComment(row, posted);
395
+ });
396
+ const freshRows = stored.filter((row) => !repeats.includes(row) && (row.state === "new" || row.thread_comment_id));
397
+ const fresh = freshRows.map(rowFinding);
362
398
  const { inline, leftover } = splitInline(fresh, files);
363
- const anchorByRow = new Map();
364
- for (const { finding, anchor } of inline) {
399
+ const commentByRow = new Map();
400
+ for (const { finding, anchor, suggestion } of inline) {
365
401
  const row = freshRows.find((item) => item.path === finding.path &&
366
402
  item.line_from === finding.from &&
367
403
  item.line_to === finding.to);
368
- if (row)
369
- anchorByRow.set(row.id, anchor);
404
+ if (row) {
405
+ commentByRow.set(row.id, {
406
+ anchor,
407
+ body: inlineCommentBody(finding, suggestion),
408
+ });
409
+ }
370
410
  }
371
411
  const freshSummaryKeys = new Set([...leftover, ...inline.map(({ finding }) => finding)].map((finding) => `${finding.path}\0${finding.from}\0${finding.to}`));
372
412
  const summaryFindings = [
@@ -386,7 +426,7 @@ async function publish(client, job, reviewId, headSha, files, log, metadata, mod
386
426
  excerpt: row.body_md,
387
427
  })),
388
428
  ];
389
- const body = reviewBody(summaryFindings, metadata, inline.length + replies.length);
429
+ const body = reviewBody(summaryFindings, metadata, inline.length + repeats.length);
390
430
  const comments = inline.map(({ finding, anchor, suggestion }) => ({
391
431
  path: finding.path,
392
432
  body: inlineCommentBody(finding, suggestion),
@@ -401,19 +441,8 @@ async function publish(client, job, reviewId, headSha, files, log, metadata, mod
401
441
  comments.some((comment) => hasForbiddenCopy(comment.body))) {
402
442
  throw new Error("review body contains forbidden copy characters");
403
443
  }
404
- for (const row of replies) {
405
- const text = inlineCommentBody({
406
- path: row.path ?? "",
407
- from: row.line_from ?? 0,
408
- to: row.line_to ?? row.line_from ?? 0,
409
- heading: row.title,
410
- excerpt: row.body_md,
411
- });
412
- const posted = await post(client, `repos/${job.repo}/pulls/${job.pr_number}/comments`, {
413
- body: text,
414
- in_reply_to: Number(row.thread_comment_id),
415
- });
416
- setFindingPosted(row.id, String(posted.id ?? ""), row.thread_comment_id ?? undefined);
444
+ for (const row of repeats) {
445
+ setFindingPosted(row.id, row.thread_comment_id ?? "");
417
446
  }
418
447
  const payload = {
419
448
  commit_id: headSha,
@@ -432,11 +461,13 @@ async function publish(client, job, reviewId, headSha, files, log, metadata, mod
432
461
  try {
433
462
  const listed = await client.pages(`repos/${job.repo}/pulls/${job.pr_number}/comments`);
434
463
  for (const row of stored) {
435
- const anchor = anchorByRow.get(row.id);
436
- if (row.posted_comment_id || !row.path || !anchor)
464
+ const comment = commentByRow.get(row.id);
465
+ if (row.posted_comment_id || !row.path || !comment)
437
466
  continue;
438
467
  const hit = listed.find((item) => String(item.path ?? "") === row.path &&
439
- Number(item.line ?? item.original_line ?? 0) === anchor.line);
468
+ Number(item.line ?? item.original_line ?? 0) ===
469
+ comment.anchor.line &&
470
+ sameBody(String(item.body ?? ""), comment.body));
440
471
  if (hit?.id)
441
472
  setFindingPosted(row.id, String(hit.id));
442
473
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "co-maintainer",
3
- "version": "0.6.4",
3
+ "version": "0.6.5",
4
4
  "description": "An imitator of yours, A PR review tool like co-maintainer.",
5
5
  "keywords": [
6
6
  "ai",