fix: separate review phases and resolve stale threads

This commit is contained in:
lezi-fun committed 2026-08-18 18:10:34 +08:00
1 parent e690b530c8
commit ceec15bf28
5 files changed
+416 -66

No files matched your search

+103 -1
View File
@@ -2,12 +2,41 @@ import assert from "node:assert/strict";
import test from "node:test";
import type { Octokit } from "@octokit/rest";
import {
buildReviewSubmissionPlan,
formatSupersededReviewBody,
supersedePreviousBotReviews
} from "../src/review/processor.js";
import type { ReviewDecision } from "../src/types.js";
const marker = "<!-- ghbot-review:v1 mode=normal outcome=block requires-admin=false review=1 change=1 -->";
const decisionWithBothFindingKinds: ReviewDecision = {
review: [{ path: "src/review.ts", line: 12, title: "Clarify this branch", body: "This is non-blocking." }],
change: [{ path: "src/review.ts", line: 8, title: "Fix the null case", body: "This blocks merge." }],
comment: "The pull request needs one required fix and has one review note.",
result: {
canMerge: false,
summary: "One required fix remains.",
shouldClosePullRequest: false,
closeReason: ""
}
};
test("review submission keeps change requests, review notes, and final comment separate", () => {
const phases = buildReviewSubmissionPlan(decisionWithBothFindingKinds, {
event: "REQUEST_CHANGES"
});
assert.deepEqual(
phases.map(({ phase, event, findings }) => ({ phase, event, findings: findings.map((finding) => finding.category) })),
[
{ phase: "change", event: "REQUEST_CHANGES", findings: ["change"] },
{ phase: "review", event: "COMMENT", findings: ["review"] },
{ phase: "final", event: "COMMENT", findings: [] }
]
);
});
test("superseded review body removes old findings and points to the current commit", () => {
const body = formatSupersededReviewBody({
originalMarker: marker,
@@ -18,7 +47,7 @@ test("superseded review body removes old findings and points to the current comm
assert.match(body, /`aaaaaaaaaaaa`/);
assert.match(body, /`bbbbbbbbbbbb`/);
assert.doesNotMatch(body, /Required changes:/);
assert.match(body, /inline `review` and `change` comments were removed/);
assert.match(body, /inline `review` and `change` threads were marked as resolved/);
});
test("old bot reviews are cleaned only while the current review is preserved", async () => {
@@ -95,3 +124,76 @@ test("old bot reviews are cleaned only while the current review is preserved", a
]);
assert.ok(pulls.listReviews);
});
test("superseded inline comments are marked resolved when GitHub exposes review threads", async () => {
const deletedComments: number[] = [];
const resolvedThreads: string[] = [];
const reviews = [
{ id: 10, node_id: "PRR_10", user: { type: "Bot" }, body: marker, state: "CHANGES_REQUESTED", commit_id: "a".repeat(40) },
{ id: 20, node_id: "PRR_20", user: { type: "Bot" }, body: marker, state: "COMMENTED", 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, node_id: "PRC_101" }, { id: 102, node_id: "PRC_102" }];
}
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 () => ({ data: {} }),
dismissReview: async () => ({ data: {} })
}
},
graphql: async (query: string, variables: { threadId?: string }) => {
if (query.includes("query ReviewThreadsForSupersededReview")) {
return {
repository: {
pullRequest: {
reviewThreads: {
nodes: [
{
id: "THREAD_PRC_101",
isResolved: false,
comments: { nodes: [{ id: "PRC_101" }] }
},
{
id: "THREAD_PRC_102",
isResolved: false,
comments: { nodes: [{ id: "PRC_102" }] }
}
],
pageInfo: { hasNextPage: false, endCursor: null }
}
}
}
};
}
if (query.includes("mutation ResolveReviewThread")) {
resolvedThreads.push(variables.threadId!);
}
return { resolveReviewThread: { thread: { isResolved: true } } };
}
} 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, []);
assert.deepEqual(resolvedThreads, ["THREAD_PRC_101", "THREAD_PRC_102"]);
});