fix: refresh reviews after new commits

This commit is contained in:
lezi-fun committed 2026-08-14 09:16:07 +08:00
1 parent ed3e1bd72c
commit 4d73d1f30c
12 files changed
+565 -29

No files matched your search

+48
View File
@@ -0,0 +1,48 @@
import assert from "node:assert/strict";
import test from "node:test";
import type { Octokit } from "@octokit/rest";
import {
formatPermissionDeniedMessage,
postPermissionDeniedComment
} from "../src/github/commandFeedback.js";
test("permission denied feedback is friendly and actionable", () => {
const body = formatPermissionDeniedMessage("contributor", "/recheck");
assert.match(body, /^Hi! @contributor, we're sorry/);
assert.match(body, /permission to run `\/recheck`/);
assert.match(body, /write.*maintain.*admin/i);
assert.match(body, /ask a repository maintainer/i);
});
test("permission denied feedback is deduplicated by source comment", async () => {
const created: string[] = [];
const comments: Array<{ body: string }> = [];
const octokit = {
paginate: async () => comments,
rest: {
issues: {
listComments: async () => ({ data: comments }),
createComment: async ({ body }: { body: string }) => {
created.push(body);
comments.push({ body });
return { data: { id: 1, body } };
}
}
}
} as unknown as Octokit;
const params = {
owner: "forumlify",
repo: "public",
pullNumber: 17,
sourceCommentId: 123,
commenterLogin: "contributor",
command: "/conflict" as const
};
await postPermissionDeniedComment(octokit, params);
await postPermissionDeniedComment(octokit, params);
assert.equal(created.length, 1);
assert.match(created[0]!, /comment=123/);
assert.match(created[0]!, /permission to run `\/conflict`/);
});
+126
View File
@@ -0,0 +1,126 @@
import assert from "node:assert/strict";
import test from "node:test";
import type { Octokit } from "@octokit/rest";
import {
beginCommitReviewProgress,
finishCommitReviewProgress
} from "../src/review/processor.js";
const headSha = "1234567890abcdef1234567890abcdef12345678";
const reviewMarker = "<!-- ghbot-review:v1 mode=normal outcome=pass requires-admin=false review=0 change=0 -->";
test("new commit progress is created once and reused for the same head", async () => {
const comments: Array<{ id: number; body: string }> = [];
const created: string[] = [];
const updated: Array<{ comment_id: number; body: string }> = [];
const pullsGet = async () => ({
data: {
state: "open",
draft: false,
head: { sha: headSha }
}
});
const issuesListComments = async () => ({ data: comments });
const octokit = {
paginate: async (method: unknown) => {
assert.equal(method, issuesListComments);
return comments;
},
rest: {
pulls: { get: pullsGet },
issues: {
listComments: issuesListComments,
createComment: async ({ body }: { body: string }) => {
created.push(body);
const comment = { id: 77, body };
comments.push(comment);
return { data: comment };
},
updateComment: async ({ comment_id, body }: { comment_id: number; body: string }) => {
updated.push({ comment_id, body });
const comment = comments.find((item) => item.id === comment_id);
if (comment) {
comment.body = body;
}
return { data: { id: comment_id, body } };
}
}
}
} as unknown as Octokit;
const params = { owner: "forumlify", repo: "public", pullNumber: 17 };
const first = await beginCommitReviewProgress(octokit, params);
const second = await beginCommitReviewProgress(octokit, params);
assert.deepEqual(first, { commentId: 77, headSha });
assert.deepEqual(second, first);
assert.equal(created.length, 1);
assert.equal(updated.length, 1);
assert.match(created[0]!, /ghbot-review-progress:v1 head=1234567890abcdef/);
assert.match(created[0]!, /New commit `1234567890ab` detected/);
});
test("completed progress updates the same comment after a review is published for the head", async () => {
const updated: Array<{ comment_id: number; body: string }> = [];
const reviews = [
{ commit_id: "f".repeat(40), body: reviewMarker },
{ commit_id: headSha, body: reviewMarker }
];
const pullsListReviews = async () => ({ data: reviews });
const octokit = {
paginate: async (method: unknown) => {
assert.equal(method, pullsListReviews);
return reviews;
},
rest: {
pulls: { listReviews: pullsListReviews },
issues: {
updateComment: async ({ comment_id, body }: { comment_id: number; body: string }) => {
updated.push({ comment_id, body });
return { data: { id: comment_id, body } };
}
}
}
} as unknown as Octokit;
await finishCommitReviewProgress(octokit, {
owner: "forumlify",
repo: "public",
pullNumber: 17,
commentId: 77,
headSha
});
assert.equal(updated.length, 1);
assert.equal(updated[0]!.comment_id, 77);
assert.match(updated[0]!.body, /Automated review completed/);
assert.match(updated[0]!.body, /`ghbot review` check now reflect this commit/);
});
test("completed progress reports a stale run when the reviewed head was not published", async () => {
const updated: string[] = [];
const pullsListReviews = async () => ({ data: [] });
const octokit = {
paginate: async () => [],
rest: {
pulls: { listReviews: pullsListReviews },
issues: {
updateComment: async ({ body }: { body: string }) => {
updated.push(body);
return { data: { id: 77, body } };
}
}
}
} as unknown as Octokit;
await finishCommitReviewProgress(octokit, {
owner: "forumlify",
repo: "public",
pullNumber: 17,
commentId: 77,
headSha
});
assert.match(updated[0]!, /ended without publishing a result/);
assert.match(updated[0]!, /newest commit will be reviewed separately/);
});
+86
View File
@@ -0,0 +1,86 @@
import assert from "node:assert/strict";
import test from "node:test";
import type { Octokit } from "@octokit/rest";
import {
formatSupersededReviewBody,
supersedePreviousBotReviews
} from "../src/review/processor.js";
const marker = "<!-- ghbot-review:v1 mode=normal outcome=block requires-admin=false review=1 change=1 -->";
test("superseded review body removes old findings and points to the current commit", () => {
const body = formatSupersededReviewBody({
originalMarker: marker,
oldCommitId: "a".repeat(40),
currentCommitId: "b".repeat(40)
});
assert.match(body, /Superseded automated review/);
assert.match(body, /`aaaaaaaaaaaa`/);
assert.match(body, /`bbbbbbbbbbbb`/);
assert.doesNotMatch(body, /Required changes:/);
assert.match(body, /inline `review` and `change` comments were removed/);
});
test("old bot reviews are cleaned only while the current review is preserved", async () => {
const deletedComments: number[] = [];
const updatedReviews: Array<{ review_id: number; body: string }> = [];
const dismissedReviews: number[] = [];
const reviews = [
{ id: 10, user: { type: "Bot" }, body: marker, state: "CHANGES_REQUESTED", commit_id: "a".repeat(40) },
{ id: 20, user: { type: "Bot" }, body: marker, state: "APPROVED", commit_id: "b".repeat(40) },
{ id: 30, user: { type: "User" }, body: marker, state: "COMMENTED", commit_id: "c".repeat(40) },
{ id: 40, user: { type: "Bot" }, body: marker, state: "DISMISSED", commit_id: "d".repeat(40) },
{ id: 50, user: { type: "Bot" }, body: "Unrelated automation", state: "COMMENTED", commit_id: "e".repeat(40) },
{ id: 60, user: { type: "Bot" }, body: marker, state: "CHANGES_REQUESTED", commit_id: "b".repeat(40) }
];
const octokit = {
paginate: async (method: unknown, params: { review_id?: number }) => {
if (method === pulls.listReviews) {
return reviews;
}
if (params.review_id === 10) {
return [{ id: 101 }, { id: 102 }];
}
if (params.review_id === 40) {
return [{ id: 401 }];
}
if (params.review_id === 60) {
return [{ id: 601 }];
}
return [];
},
rest: {
pulls: {
listReviews: async () => ({ data: reviews }),
listCommentsForReview: async () => ({ data: [] }),
deleteReviewComment: async ({ comment_id }: { comment_id: number }) => {
deletedComments.push(comment_id);
return { data: undefined };
},
updateReview: async ({ review_id, body }: { review_id: number; body: string }) => {
updatedReviews.push({ review_id, body });
return { data: {} };
},
dismissReview: async ({ review_id }: { review_id: number }) => {
dismissedReviews.push(review_id);
return { data: {} };
}
}
}
} as unknown as Octokit;
const pulls = (octokit.rest as unknown as { pulls: { listReviews: unknown } }).pulls;
await supersedePreviousBotReviews(octokit, {
owner: "forumlify",
repo: "public",
pullNumber: 17,
currentReviewId: 20,
currentCommitId: "b".repeat(40)
});
assert.deepEqual(deletedComments, [101, 102, 401, 601]);
assert.deepEqual(updatedReviews.map((item) => item.review_id), [10, 40, 60]);
assert.match(updatedReviews[0]!.body, /bbbbbbbbbbbb/);
assert.deepEqual(dismissedReviews, [10, 60]);
assert.ok(pulls.listReviews);
});
+11
View File
@@ -0,0 +1,11 @@
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import test from "node:test";
test("pull_request_target checks out a PR worktree only for same-repository heads", async () => {
const workflow = await fs.readFile(".github/workflows/review-reusable.yml", "utf8");
const caller = await fs.readFile(".github/workflows/review.yml", "utf8");
assert.match(workflow, /pull_head_repository == format\('\{0\}\/\{1\}'/);
assert.doesNotMatch(workflow, /allow-unsafe-pr-checkout:\s*true/);
assert.match(caller, /pull_head_repository:\s*\$\{\{ github\.event\.pull_request\.head\.repo\.full_name \|\| '' \}\}/);
});