fix: separate review phases and resolve stale threads
This commit is contained in:
1 parent
e690b530c8
commit
ceec15bf28
5 files changed
+416
-66
No files matched your search
+1
-1
@@ -46,7 +46,7 @@ goose 的审核结果固定包含四个顶层字段:
|
|||||||
|
|
||||||
新 commit 触发 `synchronize` 后,workflow 会恢复该 PR 最新的缓存。goose 同时收到旧审核结果和当前完整 PR diff,重新验证所有旧问题、移除已经修复的问题,并检查新 commit 引入的回归。旧的合并结论不会在没有新审核的情况下直接复用。
|
新 commit 触发 `synchronize` 后,workflow 会恢复该 PR 最新的缓存。goose 同时收到旧审核结果和当前完整 PR diff,重新验证所有旧问题、移除已经修复的问题,并检查新 commit 引入的回归。旧的合并结论不会在没有新审核的情况下直接复用。
|
||||||
|
|
||||||
每次 `synchronize` 都会先发布一条绑定当前 commit 的“开始审核”进度评论;审核完成、失败,或因为 PR 再次变化而过期时,会更新同一条评论。新审核成功发布后,ghbot 会删除旧机器人审核中的逐行 `review` 和 `change` 意见,dismiss 仍然生效的旧审核结论,并把旧审核正文压缩为 superseded 占位说明。GitHub 不允许删除已经提交的 review 记录本身,因此最终只有最新审核保留完整正文和有效状态。
|
每次 `synchronize` 都会先发布一条绑定当前 commit 的“开始审核”进度评论;审核完成、失败,或因为 PR 再次变化而过期时,会更新同一条评论。新审核成功发布后,ghbot 会把旧机器人审核中的逐行 `review` 和 `change` 线程标记为 resolved,dismiss 仍然生效的旧审核结论,并把旧审核正文压缩为 superseded 占位说明。如果 GitHub 无法通过 GraphQL 暴露某个旧线程,ghbot 才会删除这条无法映射的逐行评论作为降级处理。GitHub 不允许删除已经提交的 review 记录本身,因此最终只有最新审核保留完整正文和有效状态。
|
||||||
|
|
||||||
PR 标题、描述或 base branch 变化触发 `edited` 时也会重新审核。对象按仓库 ID 和 PR 编号隔离;`latest.json` 用于加速下一次审核,`reviews/<head-sha>.json` 留存每个成功审核过的 head。关闭或合并 PR 不会主动删除这些对象。缓存不保存 API key、完整 diff 或 prompt。
|
PR 标题、描述或 base branch 变化触发 `edited` 时也会重新审核。对象按仓库 ID 和 PR 编号隔离;`latest.json` 用于加速下一次审核,`reviews/<head-sha>.json` 留存每个成功审核过的 head。关闭或合并 PR 不会主动删除这些对象。缓存不保存 API key、完整 diff 或 prompt。
|
||||||
|
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ When Cloudflare R2 is configured, each successful review stores this metadata in
|
|||||||
|
|
||||||
When a new commit triggers `synchronize`, or PR metadata/base changes trigger `edited`, the latest cache for that PR is restored. goose receives an earlier-head result plus the current complete PR patch, revalidates old findings, removes fixed findings, and checks the newest content. The previous merge decision is never reused without a fresh review. An `edited` event on the same head still runs a fresh complete review so title, description, and base-branch changes are respected.
|
When a new commit triggers `synchronize`, or PR metadata/base changes trigger `edited`, the latest cache for that PR is restored. goose receives an earlier-head result plus the current complete PR patch, revalidates old findings, removes fixed findings, and checks the newest content. The previous merge decision is never reused without a fresh review. An `edited` event on the same head still runs a fresh complete review so title, description, and base-branch changes are respected.
|
||||||
|
|
||||||
For every `synchronize` event, ghbot posts a commit-scoped progress comment when review starts and updates that same comment when the run completes, fails, or becomes stale because the PR changed again. After the new review is published successfully, ghbot removes inline `review` and `change` comments from earlier bot reviews, dismisses any earlier active bot decision, and reduces its old summary to a superseded marker. GitHub does not allow a submitted review record itself to be deleted, so the newest review is the only full and active automated result.
|
For every `synchronize` event, ghbot posts a commit-scoped progress comment when review starts and updates that same comment when the run completes, fails, or becomes stale because the PR changed again. After the new review is published successfully, ghbot marks inline `review` and `change` threads from earlier bot reviews as resolved, dismisses any earlier active bot decision, and reduces its old summary to a superseded marker. If GitHub cannot expose a legacy thread through GraphQL, ghbot deletes only that unmappable inline comment as a fallback. GitHub does not allow a submitted review record itself to be deleted, so the newest review is the only full and active automated result.
|
||||||
|
|
||||||
Objects are isolated by repository ID and PR number. `latest.json` accelerates the next review, while `reviews/<head-sha>.json` preserves each successful head result. Closing or merging a PR does not proactively delete these objects. Cache data contains no API keys, full diff, or prompt.
|
Objects are isolated by repository ID and PR number. `latest.json` accelerates the next review, while `reviews/<head-sha>.json` preserves each successful head result. Closing or merging a PR does not proactively delete these objects. Cache data contains no API keys, full diff, or prompt.
|
||||||
|
|
||||||
|
|||||||
+56
-2
@@ -17,7 +17,28 @@ export function formatReviewBody(
|
|||||||
`## Automated review`,
|
`## Automated review`,
|
||||||
"",
|
"",
|
||||||
`Mode: ${mode}`,
|
`Mode: ${mode}`,
|
||||||
"",
|
""
|
||||||
|
];
|
||||||
|
|
||||||
|
if (decision.change.length > 0) {
|
||||||
|
lines.push(
|
||||||
|
`### Change request`,
|
||||||
|
"",
|
||||||
|
`${decision.change.length} blocking finding(s) were submitted first as a change request.`,
|
||||||
|
""
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (decision.review.length > 0) {
|
||||||
|
lines.push(
|
||||||
|
`### Review`,
|
||||||
|
"",
|
||||||
|
`${decision.review.length} non-blocking review note(s) were submitted after the change request.`,
|
||||||
|
""
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
lines.push(
|
||||||
`### Comment`,
|
`### Comment`,
|
||||||
"",
|
"",
|
||||||
decision.comment,
|
decision.comment,
|
||||||
@@ -35,7 +56,7 @@ export function formatReviewBody(
|
|||||||
"",
|
"",
|
||||||
`Required changes: ${decision.change.length}`,
|
`Required changes: ${decision.change.length}`,
|
||||||
`Review notes: ${decision.review.length}`
|
`Review notes: ${decision.review.length}`
|
||||||
];
|
);
|
||||||
|
|
||||||
if (decision.change.length > 0 && !decision.result.shouldClosePullRequest) {
|
if (decision.change.length > 0 && !decision.result.shouldClosePullRequest) {
|
||||||
lines.push("", "After updating the pull request, comment `/recheck` to run the review again.");
|
lines.push("", "After updating the pull request, comment `/recheck` to run the review again.");
|
||||||
@@ -55,6 +76,39 @@ export function formatReviewBody(
|
|||||||
return lines.join("\n");
|
return lines.join("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function formatFindingReviewBody(
|
||||||
|
decision: ReviewDecision,
|
||||||
|
category: "review" | "change",
|
||||||
|
unpostedFindings: CategorizedFinding[],
|
||||||
|
mode: ReviewMode,
|
||||||
|
disposition: ReviewDisposition
|
||||||
|
): string {
|
||||||
|
const findings = category === "change" ? decision.change : decision.review;
|
||||||
|
const lines = [
|
||||||
|
formatReviewStateMarker(mode, disposition, decision.review.length, decision.change.length),
|
||||||
|
`## ${category === "change" ? "Change request" : "Review"}`,
|
||||||
|
"",
|
||||||
|
category === "change"
|
||||||
|
? "These blocking findings must be fixed before the pull request can merge."
|
||||||
|
: "These are non-blocking review notes for the pull request author.",
|
||||||
|
"",
|
||||||
|
`Findings: ${findings.length}`
|
||||||
|
];
|
||||||
|
|
||||||
|
if (unpostedFindings.length > 0) {
|
||||||
|
lines.push("", "Findings that could not be attached inline:");
|
||||||
|
for (const finding of unpostedFindings) {
|
||||||
|
lines.push(
|
||||||
|
"",
|
||||||
|
`- ${finding.path}:${finding.line} [${finding.category}] ${finding.title}`,
|
||||||
|
` ${finding.body}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines.join("\n");
|
||||||
|
}
|
||||||
|
|
||||||
function formatDisposition(disposition: ReviewDisposition): string {
|
function formatDisposition(disposition: ReviewDisposition): string {
|
||||||
if (disposition.blocksMerge) {
|
if (disposition.blocksMerge) {
|
||||||
return "changes requested";
|
return "changes requested";
|
||||||
|
|||||||
+255
-61
@@ -13,7 +13,11 @@ import {
|
|||||||
saveReviewCache
|
saveReviewCache
|
||||||
} from "./cache.js";
|
} from "./cache.js";
|
||||||
import { GooseReviewer } from "./gooseReviewer.js";
|
import { GooseReviewer } from "./gooseReviewer.js";
|
||||||
import { formatReviewBody, type CategorizedFinding } from "./format.js";
|
import {
|
||||||
|
formatFindingReviewBody,
|
||||||
|
formatReviewBody,
|
||||||
|
type CategorizedFinding
|
||||||
|
} from "./format.js";
|
||||||
import {
|
import {
|
||||||
approvedLoginsForHead,
|
approvedLoginsForHead,
|
||||||
evaluateReviewDecision,
|
evaluateReviewDecision,
|
||||||
@@ -1031,40 +1035,118 @@ async function submitReview(
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const validLines = collectValidNewLines(params.files);
|
const validLines = collectValidNewLines(params.files);
|
||||||
const filesByPath = new Map(params.files.map((file) => [file.filename, file]));
|
const filesByPath = new Map(params.files.map((file) => [file.filename, file]));
|
||||||
const unpostedFindings: CategorizedFinding[] = [];
|
|
||||||
const categorizedFindings: CategorizedFinding[] = [
|
|
||||||
...params.decision.change.map((finding) => ({ ...finding, category: "change" as const })),
|
|
||||||
...params.decision.review.map((finding) => ({ ...finding, category: "review" as const }))
|
|
||||||
];
|
|
||||||
|
|
||||||
const comments = categorizedFindings.flatMap((finding) => {
|
|
||||||
const file = filesByPath.get(finding.path);
|
|
||||||
if (!file) {
|
|
||||||
unpostedFindings.push(finding);
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
const position = toDiffPosition(file, finding.line, validLines);
|
|
||||||
if (!position) {
|
|
||||||
unpostedFindings.push(finding);
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
path: position.path,
|
|
||||||
line: position.line,
|
|
||||||
side: position.side,
|
|
||||||
body: `**${finding.category === "change" ? "Required change" : "Review note"}: ${finding.title}**\n\n${finding.body}`
|
|
||||||
}
|
|
||||||
];
|
|
||||||
});
|
|
||||||
|
|
||||||
const disposition = evaluateReviewDecision(params.decision);
|
const disposition = evaluateReviewDecision(params.decision);
|
||||||
const event = disposition.event;
|
const currentReviewIds: number[] = [];
|
||||||
const body = formatReviewBody(params.decision, unpostedFindings, params.mode, disposition);
|
|
||||||
|
|
||||||
let currentReviewId: number;
|
for (const phase of buildReviewSubmissionPlan(params.decision, disposition)) {
|
||||||
|
const unpostedFindings: CategorizedFinding[] = [];
|
||||||
|
const comments = phase.findings.flatMap((finding) => {
|
||||||
|
const file = filesByPath.get(finding.path);
|
||||||
|
if (!file) {
|
||||||
|
unpostedFindings.push(finding);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const position = toDiffPosition(file, finding.line, validLines);
|
||||||
|
if (!position) {
|
||||||
|
unpostedFindings.push(finding);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
path: position.path,
|
||||||
|
line: position.line,
|
||||||
|
side: position.side,
|
||||||
|
body: `**${finding.category === "change" ? "Required change" : "Review note"}: ${finding.title}**\n\n${finding.body}`
|
||||||
|
}
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
const body = phase.phase === "final"
|
||||||
|
? formatReviewBody(params.decision, [], params.mode, disposition)
|
||||||
|
: formatFindingReviewBody(params.decision, phase.phase, unpostedFindings, params.mode, disposition);
|
||||||
|
const reviewId = await createGitHubReview(octokit, {
|
||||||
|
owner: params.owner,
|
||||||
|
repo: params.repo,
|
||||||
|
pullNumber: params.pullNumber,
|
||||||
|
commitId: params.commitId,
|
||||||
|
event: phase.event,
|
||||||
|
body,
|
||||||
|
comments,
|
||||||
|
logContext: {
|
||||||
|
owner: params.owner,
|
||||||
|
repo: params.repo,
|
||||||
|
pullNumber: params.pullNumber,
|
||||||
|
commitId: params.commitId
|
||||||
|
}
|
||||||
|
});
|
||||||
|
currentReviewIds.push(reviewId);
|
||||||
|
}
|
||||||
|
|
||||||
|
await supersedePreviousBotReviews(octokit, {
|
||||||
|
owner: params.owner,
|
||||||
|
repo: params.repo,
|
||||||
|
pullNumber: params.pullNumber,
|
||||||
|
currentReviewIds,
|
||||||
|
currentCommitId: params.commitId
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ReviewSubmissionPhase = {
|
||||||
|
phase: "change" | "review" | "final";
|
||||||
|
event: "APPROVE" | "COMMENT" | "REQUEST_CHANGES";
|
||||||
|
findings: CategorizedFinding[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export function buildReviewSubmissionPlan(
|
||||||
|
decision: ReviewDecision,
|
||||||
|
disposition: { event: "APPROVE" | "COMMENT" | "REQUEST_CHANGES" }
|
||||||
|
): ReviewSubmissionPhase[] {
|
||||||
|
const phases: ReviewSubmissionPhase[] = [];
|
||||||
|
if (decision.change.length > 0) {
|
||||||
|
phases.push({
|
||||||
|
phase: "change",
|
||||||
|
event: "REQUEST_CHANGES",
|
||||||
|
findings: decision.change.map((finding) => ({ ...finding, category: "change" as const }))
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (decision.review.length > 0) {
|
||||||
|
phases.push({
|
||||||
|
phase: "review",
|
||||||
|
event: "COMMENT",
|
||||||
|
findings: decision.review.map((finding) => ({ ...finding, category: "review" as const }))
|
||||||
|
});
|
||||||
|
}
|
||||||
|
phases.push({
|
||||||
|
phase: "final",
|
||||||
|
// Once a dedicated REQUEST_CHANGES review has been submitted, the final
|
||||||
|
// summary must stay a comment so GitHub does not group review notes into
|
||||||
|
// the change-request review.
|
||||||
|
event: decision.change.length > 0 ? "COMMENT" : disposition.event,
|
||||||
|
findings: []
|
||||||
|
});
|
||||||
|
return phases;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createGitHubReview(
|
||||||
|
octokit: Octokit,
|
||||||
|
params: {
|
||||||
|
owner: string;
|
||||||
|
repo: string;
|
||||||
|
pullNumber: number;
|
||||||
|
commitId: string;
|
||||||
|
event: "APPROVE" | "COMMENT" | "REQUEST_CHANGES";
|
||||||
|
body: string;
|
||||||
|
comments: Array<{
|
||||||
|
path: string;
|
||||||
|
line: number;
|
||||||
|
side: "LEFT" | "RIGHT";
|
||||||
|
body: string;
|
||||||
|
}>;
|
||||||
|
logContext: { owner: string; repo: string; pullNumber: number; commitId: string };
|
||||||
|
}
|
||||||
|
): Promise<number> {
|
||||||
try {
|
try {
|
||||||
const response = await withRetry("github.pulls.createReview", async () => {
|
const response = await withRetry("github.pulls.createReview", async () => {
|
||||||
return octokit.rest.pulls.createReview({
|
return octokit.rest.pulls.createReview({
|
||||||
@@ -1072,25 +1154,19 @@ async function submitReview(
|
|||||||
repo: params.repo,
|
repo: params.repo,
|
||||||
pull_number: params.pullNumber,
|
pull_number: params.pullNumber,
|
||||||
commit_id: params.commitId,
|
commit_id: params.commitId,
|
||||||
event,
|
event: params.event,
|
||||||
body,
|
body: params.body,
|
||||||
comments
|
comments: params.comments
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
currentReviewId = response.data.id;
|
return response.data.id;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (!shouldFallbackToCommentReview(error, event)) {
|
if (!shouldFallbackToCommentReview(error, params.event)) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.warn(
|
logger.warn(
|
||||||
{
|
{ error, ...params.logContext },
|
||||||
error,
|
|
||||||
owner: params.owner,
|
|
||||||
repo: params.repo,
|
|
||||||
pullNumber: params.pullNumber,
|
|
||||||
commitId: params.commitId
|
|
||||||
},
|
|
||||||
"Falling back to COMMENT review because the current token is not allowed to approve pull requests."
|
"Falling back to COMMENT review because the current token is not allowed to approve pull requests."
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -1101,20 +1177,12 @@ async function submitReview(
|
|||||||
pull_number: params.pullNumber,
|
pull_number: params.pullNumber,
|
||||||
commit_id: params.commitId,
|
commit_id: params.commitId,
|
||||||
event: "COMMENT",
|
event: "COMMENT",
|
||||||
body,
|
body: params.body,
|
||||||
comments
|
comments: params.comments
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
currentReviewId = response.data.id;
|
return response.data.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
await supersedePreviousBotReviews(octokit, {
|
|
||||||
owner: params.owner,
|
|
||||||
repo: params.repo,
|
|
||||||
pullNumber: params.pullNumber,
|
|
||||||
currentReviewId,
|
|
||||||
currentCommitId: params.commitId
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function upsertReviewCheckRun(
|
async function upsertReviewCheckRun(
|
||||||
@@ -1263,7 +1331,8 @@ export async function supersedePreviousBotReviews(
|
|||||||
owner: string;
|
owner: string;
|
||||||
repo: string;
|
repo: string;
|
||||||
pullNumber: number;
|
pullNumber: number;
|
||||||
currentReviewId: number;
|
currentReviewId?: number;
|
||||||
|
currentReviewIds?: number[];
|
||||||
currentCommitId: string;
|
currentCommitId: string;
|
||||||
}
|
}
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
@@ -1274,12 +1343,17 @@ export async function supersedePreviousBotReviews(
|
|||||||
per_page: 100
|
per_page: 100
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const currentReviewIds = new Set([
|
||||||
|
...(params.currentReviewIds ?? []),
|
||||||
|
...(params.currentReviewId ? [params.currentReviewId] : [])
|
||||||
|
]);
|
||||||
|
|
||||||
for (const review of reviews) {
|
for (const review of reviews) {
|
||||||
if (review.user?.type !== "Bot" || !parseReviewStateMarker(review.body)) {
|
if (review.user?.type !== "Bot" || !parseReviewStateMarker(review.body)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!review.id || review.id === params.currentReviewId) {
|
if (!review.id || currentReviewIds.has(review.id)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1292,12 +1366,51 @@ export async function supersedePreviousBotReviews(
|
|||||||
review_id: review.id,
|
review_id: review.id,
|
||||||
per_page: 100
|
per_page: 100
|
||||||
});
|
});
|
||||||
for (const comment of comments) {
|
const commentsWithNodeIds = comments.filter(
|
||||||
|
(comment): comment is typeof comment & { node_id: string } => Boolean(comment.node_id)
|
||||||
|
);
|
||||||
|
const commentsToDelete = new Set(comments.filter((comment) => !comment.node_id).map((comment) => comment.id));
|
||||||
|
if (commentsWithNodeIds.length > 0) {
|
||||||
|
let resolvedCommentNodeIds = new Set<string>();
|
||||||
|
try {
|
||||||
|
resolvedCommentNodeIds = await withRetry(
|
||||||
|
"github.graphql.resolveReviewThread.superseded",
|
||||||
|
async () => resolveReviewThreadsForComments(octokit, {
|
||||||
|
owner: params.owner,
|
||||||
|
repo: params.repo,
|
||||||
|
pullNumber: params.pullNumber,
|
||||||
|
commentNodeIds: commentsWithNodeIds.map((comment) => comment.node_id)
|
||||||
|
})
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
logger.warn(
|
||||||
|
{
|
||||||
|
error,
|
||||||
|
owner: params.owner,
|
||||||
|
repo: params.repo,
|
||||||
|
pullNumber: params.pullNumber,
|
||||||
|
reviewId: review.id
|
||||||
|
},
|
||||||
|
"Could not resolve superseded review threads through GraphQL; deleting only the affected inline comments."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// A comment not returned from the thread connection cannot be resolved
|
||||||
|
// by its REST id. Delete only that exceptional remainder so one stale
|
||||||
|
// API response does not leave old inline findings permanently active.
|
||||||
|
for (const comment of commentsWithNodeIds) {
|
||||||
|
if (!resolvedCommentNodeIds.has(comment.node_id)) {
|
||||||
|
commentsToDelete.add(comment.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const commentId of commentsToDelete) {
|
||||||
await withRetry("github.pulls.deleteReviewComment.superseded", async () => {
|
await withRetry("github.pulls.deleteReviewComment.superseded", async () => {
|
||||||
return octokit.rest.pulls.deleteReviewComment({
|
return octokit.rest.pulls.deleteReviewComment({
|
||||||
owner: params.owner,
|
owner: params.owner,
|
||||||
repo: params.repo,
|
repo: params.repo,
|
||||||
comment_id: comment.id
|
comment_id: commentId
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -1366,10 +1479,91 @@ export function formatSupersededReviewBody(params: {
|
|||||||
"",
|
"",
|
||||||
`The review for commit \`${shortSha(params.oldCommitId)}\` has been replaced by the automated review for commit \`${shortSha(params.currentCommitId)}\`.`,
|
`The review for commit \`${shortSha(params.oldCommitId)}\` has been replaced by the automated review for commit \`${shortSha(params.currentCommitId)}\`.`,
|
||||||
"",
|
"",
|
||||||
"Its previous inline `review` and `change` comments were removed. Refer to the latest commit review for the current result."
|
"Its previous inline `review` and `change` threads were marked as resolved. Refer to the latest commit review for the current result."
|
||||||
].join("\n");
|
].join("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function resolveReviewThreadsForComments(
|
||||||
|
octokit: Octokit,
|
||||||
|
params: { owner: string; repo: string; pullNumber: number; commentNodeIds: string[] }
|
||||||
|
): Promise<Set<string>> {
|
||||||
|
type ReviewThreadsResponse = {
|
||||||
|
repository?: {
|
||||||
|
pullRequest?: {
|
||||||
|
reviewThreads?: {
|
||||||
|
nodes?: Array<{
|
||||||
|
id: string;
|
||||||
|
isResolved: boolean;
|
||||||
|
comments?: { nodes?: Array<{ id: string }> } | null;
|
||||||
|
} | null>;
|
||||||
|
pageInfo: { hasNextPage: boolean; endCursor?: string | null };
|
||||||
|
} | null;
|
||||||
|
} | null;
|
||||||
|
} | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
const targetCommentNodeIds = new Set(params.commentNodeIds);
|
||||||
|
const matchedCommentNodeIds = new Set<string>();
|
||||||
|
let after: string | null = null;
|
||||||
|
|
||||||
|
do {
|
||||||
|
const result: ReviewThreadsResponse = await octokit.graphql<ReviewThreadsResponse>(
|
||||||
|
`query ReviewThreadsForSupersededReview($owner: String!, $repo: String!, $pullNumber: Int!, $after: String) {
|
||||||
|
repository(owner: $owner, name: $repo) {
|
||||||
|
pullRequest(number: $pullNumber) {
|
||||||
|
reviewThreads(first: 100, after: $after) {
|
||||||
|
pageInfo { hasNextPage endCursor }
|
||||||
|
nodes {
|
||||||
|
id
|
||||||
|
isResolved
|
||||||
|
comments(first: 100) {
|
||||||
|
nodes { id }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`,
|
||||||
|
{ owner: params.owner, repo: params.repo, pullNumber: params.pullNumber, after }
|
||||||
|
);
|
||||||
|
|
||||||
|
const threads = result.repository?.pullRequest?.reviewThreads;
|
||||||
|
if (!threads) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const thread of threads.nodes ?? []) {
|
||||||
|
if (!thread) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const threadCommentNodeIds = (thread.comments?.nodes ?? [])
|
||||||
|
.map((comment) => comment?.id)
|
||||||
|
.filter((commentId): commentId is string => typeof commentId === "string");
|
||||||
|
const matchingCommentNodeIds = threadCommentNodeIds.filter((commentId) => targetCommentNodeIds.has(commentId));
|
||||||
|
if (matchingCommentNodeIds.length === 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (const commentNodeId of matchingCommentNodeIds) {
|
||||||
|
matchedCommentNodeIds.add(commentNodeId);
|
||||||
|
}
|
||||||
|
if (!thread.isResolved) {
|
||||||
|
await octokit.graphql(
|
||||||
|
`mutation ResolveReviewThread($threadId: ID!) {
|
||||||
|
resolveReviewThread(input: { threadId: $threadId }) {
|
||||||
|
thread { isResolved }
|
||||||
|
}
|
||||||
|
}`,
|
||||||
|
{ threadId: thread.id }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
after = threads.pageInfo.hasNextPage ? threads.pageInfo.endCursor ?? null : null;
|
||||||
|
} while (after);
|
||||||
|
|
||||||
|
return matchedCommentNodeIds;
|
||||||
|
}
|
||||||
|
|
||||||
function extractReviewStateMarker(body: string | null | undefined): string {
|
function extractReviewStateMarker(body: string | null | undefined): string {
|
||||||
return body?.match(/<!-- ghbot-review:v1[^>]*-->/)?.[0] ?? "<!-- ghbot-review:v1 superseded=true -->";
|
return body?.match(/<!-- ghbot-review:v1[^>]*-->/)?.[0] ?? "<!-- ghbot-review:v1 superseded=true -->";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,41 @@ import assert from "node:assert/strict";
|
|||||||
import test from "node:test";
|
import test from "node:test";
|
||||||
import type { Octokit } from "@octokit/rest";
|
import type { Octokit } from "@octokit/rest";
|
||||||
import {
|
import {
|
||||||
|
buildReviewSubmissionPlan,
|
||||||
formatSupersededReviewBody,
|
formatSupersededReviewBody,
|
||||||
supersedePreviousBotReviews
|
supersedePreviousBotReviews
|
||||||
} from "../src/review/processor.js";
|
} 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 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", () => {
|
test("superseded review body removes old findings and points to the current commit", () => {
|
||||||
const body = formatSupersededReviewBody({
|
const body = formatSupersededReviewBody({
|
||||||
originalMarker: marker,
|
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, /`aaaaaaaaaaaa`/);
|
||||||
assert.match(body, /`bbbbbbbbbbbb`/);
|
assert.match(body, /`bbbbbbbbbbbb`/);
|
||||||
assert.doesNotMatch(body, /Required changes:/);
|
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 () => {
|
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);
|
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"]);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user