diff --git a/README.md b/README.md index e369ea9..c9d937f 100644 --- a/README.md +++ b/README.md @@ -90,15 +90,19 @@ a pull request comment, or from the CLI: | `@gitdogbot resolve` | `gitdog reviews resolve ` | Resolves every Gitdog review thread | | `@gitdogbot help` | `gitdog reviews --help` | Lists the commands | -A command is a comment line that starts with the mention, in the pull request -conversation; replies inside a review thread are not read. Collaborators can run +A command is a comment line that starts with the mention, or follows another +review bot's command on the same line (`@coderabbitai review @gitdogbot +review`), in the pull request conversation; replies inside a review thread are +not read. `@gitdog` works as shorthand, but it also notifies the GitHub user +of that name, so `@gitdogbot` is preferred. While a review runs, Gitdog keeps +one status comment on the pull request up to date: reviewing, ready for +approval, reviewed, or why it could not finish. Collaborators can run every command; the pull request author, who may work from a fork, can run all but a full review. A review that failed or ran out of tokens can be requested again, and pull requests opened by bots such as Dependabot are not reviewed. In an issue or discussion, mention `@gitdogbot` with a question to get a reply -grounded in the repository. When the account runs out of tokens, Gitdog says so -on the pull request at most once a day. Someone who sends more than 10 commands +grounded in the repository. When a review cannot finish, for example because the account ran out of tokens, its status comment says why; for automatic reviews Gitdog posts at most one such comment a day per pull request. Someone who sends more than 10 commands an hour or 40 a day to one repository is paused there for 24 hours. ## Get started diff --git a/convex/github/helpers.ts b/convex/github/helpers.ts index cda6fae..575c917 100644 --- a/convex/github/helpers.ts +++ b/convex/github/helpers.ts @@ -486,6 +486,34 @@ export async function addIssueComment( return data.id; } +// Replace the body of an issue or pull request comment +export async function updateIssueComment( + owner: string, + repo: string, + commentId: number, + body: string, + token: string, +): Promise { + const response = await fetchGitHubApi( + `https://api.github.com/repos/${owner}/${repo}/issues/comments/${commentId}`, + { + method: "PATCH", + headers: { + Accept: "application/vnd.github.v3+json", + Authorization: `Bearer ${token}`, + "User-Agent": "Gitdog", + "Content-Type": "application/json", + }, + body: JSON.stringify({ body }), + }, + ); + if (!response.ok) { + await response.body?.cancel().catch(() => undefined); + throw new Error(`Failed to update issue comment (HTTP ${response.status})`); + } + await response.body?.cancel().catch(() => undefined); +} + // Update issue title and/or body export async function updateIssue( owner: string, diff --git a/convex/pullRequests/commands.ts b/convex/pullRequests/commands.ts index b46731d..f987c28 100644 --- a/convex/pullRequests/commands.ts +++ b/convex/pullRequests/commands.ts @@ -2,6 +2,22 @@ import { getConfiguredGitHubAppSlug } from "../github/botIdentity"; export type ReviewCommand = "review" | "full_review" | "resolve" | "help"; +// Review bots whose commands another may follow on the same line. Any +// GitHub App account, named "...[bot]", counts too. +const REVIEW_BOTS = [ + "coderabbitai", + "copilot", + "gemini-code-assist", + "sourcery-ai", + "greptile-apps", + "greptileai", + "codeant-ai", + "qodo-merge", + "codiumai-agent", + "cursor", + "[\\w-]+\\[bot\\]", +].join("|"); + // Commands one commenter may send to one repository before being paused. export const COMMANDS_PER_HOUR = 10; export const COMMANDS_PER_DAY = 40; @@ -43,20 +59,30 @@ export function parseReviewCommand( (line) => !line.trimStart().startsWith(">") && !/^( {4}|\t)/.test(line), ) .join("\n"); - // Only a mention that starts a line is a command, so "@gitdogbot review" - // quoted in a sentence does not run one. + // A command starts a line, or follows another review bot's command on it, + // as in "@coderabbitai review @gitdogbot review". "@gitdog" is shorthand + // for the production app only, so a development app never answers it. + const names = slug === "gitdogbot" ? `${slug}|gitdog` : slug; const match = new RegExp( - `^[ \\t]*@${slug}(?=\\s|[:,]|$)[:,]?[ \\t]*([^\\n]*)`, + `^[ \\t]*((?:@(?:${REVIEW_BOTS})(?:[ \\t]+[a-z-]+){0,2}[ \\t]+){0,3})@(?:${names})(?=\\s|[:,]|$)[:,]?[ \\t]*([^@\\n]*)`, "im", ).exec(text); if (!match) return null; - const words = match[1] + const chained = match[1].length > 0; + const words = match[2] .toLowerCase() .replace(/[^a-z\s]/g, " ") .trim() .split(/\s+/) .filter(Boolean); switch (true) { + // A chained mention without a command is a thank-you, not a request. + case chained && + !( + ["help", "review", "resolve"].includes(words[0] ?? "") || + (words[0] === "full" && words[1] === "review") + ): + return null; case words.length === 0 || words[0] === "help": return "help"; case words[0] === "full" && words[1] === "review": @@ -80,5 +106,121 @@ export function helpText(slug: string): string { `- \`${mention} resolve\`: resolve every Gitdog review thread`, `- \`${mention} help\`: show this list`, "Collaborators can run every command; the pull request author can run all but a full review.", + ...(slug === "gitdogbot" ? ["`@gitdog` works as shorthand."] : []), ].join("\n"); } + +type StatusAction = { + status: string; + errorMessage?: string; + reviewCancelled?: boolean; + reviewRequested?: boolean; + executionResult?: { + published?: boolean; + reason?: string; + reviewUrl?: string; + }; + proposedChanges?: { + source?: { headSha?: string }; + changedFiles?: number; + reviewedSince?: string; + }; +}; + +// A review cancelled because the pull request moved on, not a failure. +export function isCancelledReview(action: { + errorMessage?: string; + reviewCancelled?: boolean; +}): boolean { + return ( + action.reviewCancelled === true || + action.errorMessage?.includes("superseded") === true + ); +} + +// Failures safe to show on a public pull request, in the owner's words. The +// second value says whether asking again can help. +function failureReason(message: string | undefined): [string, boolean] { + switch (true) { + case !message: + return ["Gitdog could not complete the review.", true]; + case message!.startsWith("Not enough tokens remain") || + message!.startsWith("Token limit reached"): + return [ + "The token limit of the account that connected this repository has been reached.", + true, + ]; + // A user's own provider key is the owner's business, not the public's. + case message!.startsWith("Your "): + return [ + "The AI provider settings of the account that connected this repository need attention.", + true, + ]; + case message!.startsWith("GitHub publication outcome requires inspection"): + return [ + "Gitdog could not confirm that the review was posted. Check the reviews above before asking again.", + false, + ]; + case /split this PR/.test(message!): + return [ + "This pull request is too large to review completely. Split it into smaller pull requests.", + false, + ]; + default: + return ["Gitdog could not complete the review.", true]; + } +} + +/** + * The status comment for a review in its current state, or null when the + * state needs no comment (a successful review is the review itself, but an + * existing status comment is still updated to point at it). + */ +export function reviewStatusBody( + action: StatusAction, + slug: string, +): string | null { + const sha = action.proposedChanges?.source?.headSha ?? "this pull request"; + const files = action.proposedChanges?.changedFiles; + switch (action.status) { + case "pending": + return `**Reviewing** ${sha}${ + typeof files === "number" + ? ` · ${files} changed ${files === 1 ? "file" : "files"}` + : "" + }\n\nThe review will be posted here when it is done.`; + case "pending_approval": + return `**Review ready for approval** for ${sha}\n\nThe repository owner can post it from Gitdog.`; + case "approved": + return `**Posting the review** for ${sha}`; + case "rejected": + return `**Review not posted** for ${sha}\n\nThe repository owner chose not to post this review.`; + case "completed": + if (action.executionResult?.published) + return `**Reviewed** ${sha}${ + action.executionResult.reviewUrl + ? ` · [see the review](${action.executionResult.reviewUrl})` + : "" + }`; + if (action.executionResult?.reason === "no_new_changes") + return action.proposedChanges?.reviewedSince + ? `**No new changes to review** in ${sha} since ${action.proposedChanges.reviewedSince}\n\nNothing in the pull request diff changed, so no review was posted. Comment \`@${slug} full review\` to review the whole pull request again.` + : `**Nothing to review** in ${sha}\n\nThe pull request has no changes Gitdog can review, such as only deleted files.`; + return null; + case "failed": { + if (isCancelledReview(action)) + return `**Review cancelled** for ${sha}\n\nThe pull request changed or was closed before the review finished.${ + // A request is answered either way; an automatic review is not. + action.reviewRequested + ? ` Comment \`@${slug} review\` to review the latest commit.` + : "" + }`; + const [reason, retry] = failureReason(action.errorMessage); + return `**Review not completed** for ${sha}\n\n${reason}${ + retry ? ` Comment \`@${slug} review\` to try again.` : "" + }`; + } + default: + return null; + } +} diff --git a/convex/pullRequests/errors.ts b/convex/pullRequests/errors.ts index 6498ea9..cc19ec3 100644 --- a/convex/pullRequests/errors.ts +++ b/convex/pullRequests/errors.ts @@ -33,6 +33,22 @@ export function reviewFailureCause(error: unknown): unknown { return error instanceof ReviewRequestError ? error.cause : error; } +// Thrown when the pull request or its review moved on mid-run: the review +// was cancelled, not failed. +const LIFECYCLE_CHANGES = new Set([ + "Review source, settings or lifecycle changed", + "Pull request changed before review", + "Pull request changed while reading its diff", + "Pull request changed during review", + "Pull request changed after the review was generated", + "Pull request changed before publication", +]); + +export function isLifecycleChange(error: unknown): boolean { + const cause = reviewFailureCause(error); + return cause instanceof Error && LIFECYCLE_CHANGES.has(cause.message); +} + export class ReviewResponseFormatError extends Error { constructor() { super("Review response did not match the required JSON format"); diff --git a/convex/pullRequests/internalAction.ts b/convex/pullRequests/internalAction.ts index acfb4d2..694559f 100644 --- a/convex/pullRequests/internalAction.ts +++ b/convex/pullRequests/internalAction.ts @@ -1,6 +1,6 @@ import { internalAction } from "../_generated/server"; import { internal } from "../_generated/api"; -import type { Id } from "../_generated/dataModel"; +import type { Doc, Id } from "../_generated/dataModel"; import type { ActionCtx } from "../_generated/server"; import { v } from "convex/values"; import { @@ -37,6 +37,7 @@ import { reviewWithContext } from "./workflow"; import { REVIEW_QUOTA_FAILURES } from "./internalMutation"; import { isReviewGroupModelFailure, + isLifecycleChange, isReviewRefusal, ReviewQuotaError, reviewGenerationError, @@ -57,10 +58,16 @@ import { } from "./review"; import { addIssueComment, + updateIssueComment, GITHUB_API_VERSION, fetchGitHubAppJsonWithRetry, } from "../github/helpers"; -import { helpText } from "./commands"; +import { + COMMAND_PAUSE_MS, + helpText, + isCancelledReview, + reviewStatusBody, +} from "./commands"; import { getConfiguredGitHubAppSlug } from "../github/botIdentity"; type Source = { headSha: string; baseSha: string; title: string; body: string }; @@ -327,6 +334,19 @@ export const generate = internalAction({ }); return; } + // Show on the pull request that a review is running, once per review. + if ( + !previous && + !verificationOnly && + initial.action.statusCommentId === undefined && + // After a recent automatic failure, a retry on the next push stays + // quiet until it has something to show. + (initial.action.reviewRequested === true || + initial.ticket.pullRequestLimitNoticeAt === undefined || + initial.ticket.pullRequestLimitNoticeAt < + Date.now() - COMMAND_PAUSE_MS) + ) + await postReviewingStatus(ctx, access, initial, data); const fileKeys = new Set(previous?.fileKeys); if (batched) { // Keep the complete file inventory bounded even when paths are long. @@ -615,6 +635,7 @@ export const generate = internalAction({ await ctx.runMutation(internal.pullRequests.internalMutation.finish, { actionId: args.actionId, stage: "generation", + cancelled: isLifecycleChange(error), page: args.page, group: args.group, error: @@ -640,18 +661,6 @@ export const generate = internalAction({ : undefined), ), }); - // Out of tokens: tell the pull request instead of failing silently. - // The notice skips rechecks, which promise not to post. - if ( - (cause instanceof ReviewQuotaError && - cause.reason === "insufficient") || - (cause instanceof ChatBillingError && cause.reason === "limit_reached") - ) - await ctx.scheduler.runAfter( - 0, - internal.pullRequests.internalAction.noticeLimit, - { actionId: args.actionId }, - ); } }, }); @@ -1111,6 +1120,9 @@ export const publish = internalAction({ await ctx.runMutation(internal.pullRequests.internalMutation.finish, { actionId: args.actionId, stage: claimed ? "publication" : "preflight", + // A pull request that moved on before anything could be posted was + // cancelled. + cancelled: !mayHavePosted && isLifecycleChange(error), claimedAt: claimed || undefined, error: !claimed ? "Review is stale or GitHub access is unavailable; no review was published" @@ -1170,6 +1182,49 @@ export const dismissStaleApproval = internalAction({ }, }); +/** Best effort: the review runs whether or not GitHub takes the comment. */ +async function postReviewingStatus( + ctx: ActionCtx, + access: VerifiedRepositoryAccess, + state: { + action: Doc<"ticketActions">; + ticket: Doc<"tickets">; + }, + data: { changedFiles: number }, +) { + const slug = getConfiguredGitHubAppSlug(); + if (!slug) return; + const body = reviewStatusBody( + { + ...state.action, + proposedChanges: { + ...state.action.proposedChanges, + changedFiles: data.changedFiles, + }, + }, + slug, + ); + if (!body) return; + try { + const commentId = await addIssueComment( + access.owner, + access.repo, + state.ticket.githubNumber, + body, + access.token, + ); + await ctx.runMutation( + internal.pullRequests.internalMutation.recordStatusComment, + { actionId: state.action._id, commentId, reviewing: true }, + ); + } catch (error) { + console.warn( + "Review status comment could not be posted:", + error instanceof Error ? error.message : "unknown error", + ); + } +} + async function reply( access: VerifiedRepositoryAccess, number: number, @@ -1336,56 +1391,105 @@ export const runCommand = internalAction({ ).catch(() => undefined); throw error; }); + // A started review announces itself in its status comment. + if (result.command !== "resolve" && result.status === "started") return; return reply(access, args.number, commandReply(result, slug)); } } }, }); -/** Tell a pull request, at most once a day, that its review limit was hit. */ -export const noticeLimit = internalAction({ - args: { actionId: v.id("ticketActions") }, - handler: async (ctx, args): Promise => { - const target = await ctx.runMutation( - internal.pullRequests.internalMutation.claimLimitNotice, - args, - ); - const slug = getConfiguredGitHubAppSlug(); - if (!target || !slug) return; - const access = await resolveVerifiedRepositoryAccess( - ctx, - target.repositoryId, - ); - await reply( - access, - target.number, - `**Review limit reached**\n\nGitdog could not review${target.headSha ? ` ${target.headSha}` : " this pull request"} because the token limit of the account that connected this repository has been reached. Once more tokens are available, comment \`@${slug} review\` to try again.`, - ); - }, -}); - -/** Answer a requested review that found no pull request changes to review. */ -export const noticeNoChanges = internalAction({ +/** + * Bring a review's status comment up to date once its outcome is known: post + * one for a failure or a no-change follow-up that has none yet, and + * otherwise edit it in place. A running review is left alone; its comment + * already says so. + */ +export const settleStatusComment = internalAction({ args: { actionId: v.id("ticketActions") }, handler: async (ctx, args): Promise => { - const state = await ctx.runQuery( - internal.pullRequests.internalQuery.get, - args, - ); const slug = getConfiguredGitHubAppSlug(); - if (!state || !slug) return; - const source = sourceOf(state.action.proposedChanges?.source); - const since = state.action.proposedChanges?.reviewedSince; + if (!slug) return; + const read = () => + ctx.runQuery(internal.pullRequests.internalQuery.get, args); + // Whether this state changes the pull request at all, decided before + // any GitHub token is fetched. + const plan = (state: NonNullable>>) => { + const { action } = state; + if ( + action.reviewVerificationOnly === true || + action.status === "pending" || + action.status === "approved" + ) + return null; + const body = reviewStatusBody(action, slug); + if (!body) return null; + if (action.statusCommentId !== undefined) + return { body, commentId: action.statusCommentId }; + // Only something a reader is waiting on gets a new comment: a failure + // worth retrying or a requested no-change result, while reviews stay on. + const wanted = + action.status === "failed" + ? !isCancelledReview(action) + : action.executionResult?.reason === "no_new_changes" && + action.reviewRequested === true; + return wanted && + state.ticket.status === "open" && + state.repository.settings.pullRequestReviewsEnabled === true + ? { body, commentId: undefined } + : null; + }; + const first = await read(); + if (!first || !plan(first)) return; const access = await resolveVerifiedRepositoryAccess( ctx, - state.repository._id, + first.repository._id, ); - await reply( - access, + // Re-read right before writing, so a late settle never overwrites a + // newer outcome with an older one. + const state = await read(); + const next = state && plan(state); + if (!state || !next) return; + const { action } = state; + // An automatic review that failed counts toward the daily allowance + // whether it edits its comment or needs a new one. + const automaticFailure = + action.reviewRequested !== true && + action.status === "failed" && + !isCancelledReview(action); + if (next.commentId !== undefined) { + await updateIssueComment( + access.owner, + access.repo, + next.commentId, + next.body, + access.token, + ); + if (automaticFailure) + await ctx.runMutation( + internal.pullRequests.internalMutation.stampAutomaticNotice, + { ticketId: state.ticket._id }, + ); + return; + } + if ( + automaticFailure && + !(await ctx.runMutation( + internal.pullRequests.internalMutation.claimAutomaticNotice, + { ticketId: state.ticket._id }, + )) + ) + return; + const commentId = await addIssueComment( + access.owner, + access.repo, state.ticket.githubNumber, - `**No new changes to review** in ${source.headSha}${ - typeof since === "string" ? ` since ${since}` : "" - }\n\nNothing in the pull request diff changed, so no review was posted. Comment \`@${slug} full review\` to review the whole pull request again.`, + next.body, + access.token, + ); + await ctx.runMutation( + internal.pullRequests.internalMutation.recordStatusComment, + { actionId: args.actionId, commentId }, ); }, }); diff --git a/convex/pullRequests/internalMutation.ts b/convex/pullRequests/internalMutation.ts index 1920a0d..8233f44 100644 --- a/convex/pullRequests/internalMutation.ts +++ b/convex/pullRequests/internalMutation.ts @@ -166,6 +166,15 @@ async function observeDroppedPush(ctx: MutationCtx, ticketId: Id<"tickets">) { ); } +/** Bring a review's status comment on the pull request up to date. */ +async function settleStatus(ctx: MutationCtx, actionId: Id<"ticketActions">) { + await ctx.scheduler.runAfter( + 0, + internal.pullRequests.internalAction.settleStatusComment, + { actionId }, + ); +} + function hasAttemptedReview(action: Doc<"ticketActions">) { if (action.reviewGenerationToken !== undefined) return true; // Keep the slot available when a queued review was superseded before its worker started. @@ -260,13 +269,16 @@ export const queueVerification = internalMutation({ action.executionClaimedAt === undefined && (!matches(action) || (!args.open && action.reviewVerificationOnly !== true)) - ) + ) { await ctx.db.patch(action._id, { status: "failed", errorMessage: SUPERSEDED_REVIEW, ...ENDED_BATCH, completedAt: Date.now(), }); + if (action.statusCommentId !== undefined) + await settleStatus(ctx, action._id); + } } await ctx.db.patch(ticket._id, { pullRequestObservedAt: Math.max( @@ -445,7 +457,7 @@ async function observePullRequest( ticket.githubTitle !== source.title || ticket.githubBody !== source.body; // A requested review a push cancelled before it ran carries over to the - // new head, so "Review started" is still answered with a review. + // new head, so the command is still answered with a review. if ( !request && args.open && @@ -493,13 +505,16 @@ async function observePullRequest( (action.reviewVerificationOnly !== true || commitChanged) && ["pending", "pending_approval", "approved"].includes(action.status) && action.executionClaimedAt === undefined - ) + ) { await ctx.db.patch(action._id, { status: "failed", errorMessage: SUPERSEDED_REVIEW, ...ENDED_BATCH, completedAt: now, }); + if (action.statusCommentId !== undefined) + await settleStatus(ctx, action._id); + } } await ctx.db.patch(ticket._id, { pullRequestObservedAt: observedAt, @@ -908,12 +923,7 @@ export const propose = internalMutation({ reviewFailureDetails: `Stage · Token quota check (review batch ${page})\nReason · ${quota.message}`, completedAt: Date.now(), }); - if (quota.reason === "insufficient") - await ctx.scheduler.runAfter( - 0, - internal.pullRequests.internalAction.noticeLimit, - { actionId: action._id }, - ); + await settleStatus(ctx, action._id); return false; } } @@ -1055,16 +1065,9 @@ export const propose = internalMutation({ // A review that ended unpublished is done, so a dropped push can be // reviewed now. One waiting for approval keeps it: reviewing the newer // head would supersede the review before the owner sees it. - else if (unpublished) { - // A requested review answered "started", so it says it found nothing. - if (nothingNew && action.reviewRequested === true) - await ctx.scheduler.runAfter( - 0, - internal.pullRequests.internalAction.noticeNoChanges, - { actionId: action._id }, - ); - await observeDroppedPush(ctx, action.ticketId); - } + else if (unpublished) await observeDroppedPush(ctx, action.ticketId); + // The status comment says the review is ready, waiting or found nothing. + await settleStatus(ctx, action._id); return true; }, }); @@ -1115,6 +1118,7 @@ export const claim = internalMutation({ action.proposedChanges?.findings?.length !== 0 ) { await ctx.db.patch(action._id, { status: "pending_approval" }); + await settleStatus(ctx, action._id); return false; } const user = await ctx.db.get(repo.userId); @@ -1171,6 +1175,8 @@ export const finish = internalMutation({ inlineFindings: v.optional(v.boolean()), // Earlier Gitdog approvals GitHub refused to dismiss. keptApprovals: v.optional(v.array(v.number())), + // The pull request moved on mid-review; the review was cancelled. + cancelled: v.optional(v.boolean()), }, handler: async (ctx, args) => { const action = await ctx.db.get(args.actionId); @@ -1195,6 +1201,7 @@ export const finish = internalMutation({ return; await ctx.db.patch(action._id, { status: args.error ? "failed" : "completed", + ...(args.error && args.cancelled ? { reviewCancelled: true } : {}), ...ENDED_BATCH, errorMessage: args.error, reviewFailureDetails: args.error ? args.reviewFailureDetails : undefined, @@ -1213,6 +1220,7 @@ export const finish = internalMutation({ } : undefined, }); + await settleStatus(ctx, action._id); await observeDroppedPush(ctx, action.ticketId); }, }); @@ -1225,13 +1233,13 @@ export const expire = internalMutation({ }, handler: async (ctx, args) => { const action = await ctx.db.get(args.actionId); - if ( + const expired = action?.actionType === "review" && (args.claimedAt === undefined ? action.status === "pending" && isBatchCursor(action.reviewBatch, args) : action.status === "approved" && - action.executionClaimedAt === args.claimedAt) - ) + action.executionClaimedAt === args.claimedAt); + if (action && expired) await ctx.db.patch(action._id, { status: "failed", completedAt: Date.now(), @@ -1246,7 +1254,11 @@ export const expire = internalMutation({ ? "Review exceeded its processing deadline" : "GitHub publication outcome requires inspection; automatic retries are disabled", }); - if (action) await observeDroppedPush(ctx, action.ticketId); + if (action) { + // Only an expiry that ended the review changes what the comment says. + if (expired) await settleStatus(ctx, action._id); + await observeDroppedPush(ctx, action.ticketId); + } }, }); @@ -1300,12 +1312,7 @@ export const completeUnposted = internalMutation({ completedAt: Date.now(), executionResult: { published: false, reason: "no_new_changes" }, }); - if (action.reviewRequested === true) - await ctx.scheduler.runAfter( - 0, - internal.pullRequests.internalAction.noticeNoChanges, - { actionId: action._id }, - ); + await settleStatus(ctx, action._id); await observeDroppedPush(ctx, action.ticketId); }, }); @@ -1381,36 +1388,51 @@ export const recordCommand = internalMutation({ }, }); -/** Claim the once-a-day notice that this PR hit its review limit. */ -export const claimLimitNotice = internalMutation({ - args: { actionId: v.id("ticketActions") }, +/** + * Allow one new status comment a day for a pull request's automatic + * reviews, so a pull request that keeps failing is not flooded. + */ +export const claimAutomaticNotice = internalMutation({ + args: { ticketId: v.id("tickets") }, handler: async (ctx, args) => { - const action = await ctx.db.get(args.actionId); - const ticket = action ? await ctx.db.get(action.ticketId) : null; - const repo = ticket?.repositoryId - ? await ctx.db.get(ticket.repositoryId) - : null; - if ( - !ticket?.repositoryId || - ticket.ticketType !== "pull_request" || - ticket.status !== "open" || - ticket.deletedAt !== undefined || - action?.reviewVerificationOnly === true || - !repo?.isActive || - repo.settings.pullRequestReviewsEnabled !== true - ) - return null; + const ticket = await ctx.db.get(args.ticketId); const now = Date.now(); if ( - ticket.pullRequestLimitNoticeAt !== undefined && - ticket.pullRequestLimitNoticeAt > now - COMMAND_PAUSE_MS + !ticket || + (ticket.pullRequestLimitNoticeAt !== undefined && + ticket.pullRequestLimitNoticeAt > now - COMMAND_PAUSE_MS) ) - return null; + return false; await ctx.db.patch(ticket._id, { pullRequestLimitNoticeAt: now }); - return { - repositoryId: ticket.repositoryId, - number: ticket.githubNumber, - headSha: action?.proposedChanges?.source?.headSha as string | undefined, - }; + return true; + }, +}); + +/** Start the daily quiet window after an automatic review failed. */ +export const stampAutomaticNotice = internalMutation({ + args: { ticketId: v.id("tickets") }, + handler: async (ctx, args) => { + const ticket = await ctx.db.get(args.ticketId); + if (ticket) + await ctx.db.patch(ticket._id, { pullRequestLimitNoticeAt: Date.now() }); + }, +}); + +/** Remember the status comment posted for a review, once. */ +export const recordStatusComment = internalMutation({ + args: { + actionId: v.id("ticketActions"), + commentId: v.number(), + // A "Reviewing" comment, which a review that has since ended must update. + reviewing: v.optional(v.boolean()), + }, + handler: async (ctx, args) => { + const action = await ctx.db.get(args.actionId); + if (!action || action.statusCommentId !== undefined) return false; + await ctx.db.patch(action._id, { statusCommentId: args.commentId }); + // The review may have ended while its comment was being posted. + if (args.reviewing && action.status !== "pending") + await settleStatus(ctx, action._id); + return true; }, }); diff --git a/convex/schema.ts b/convex/schema.ts index 69529b9..0d1e26f 100644 --- a/convex/schema.ts +++ b/convex/schema.ts @@ -849,8 +849,8 @@ const applicationTables = { pullRequestDroppedAt: v.optional(v.number()), // The commit Gitdog last approved; a push past it withdraws the approval. pullRequestApprovedSha: v.optional(v.string()), - // When Gitdog last told this PR its review limit was reached, so the - // notice is posted at most once a day. + // When Gitdog last posted a status comment for a failed automatic + // review, so a pull request that keeps failing hears it once a day. pullRequestLimitNoticeAt: v.optional(v.number()), // GitHub reference @@ -961,6 +961,10 @@ const applicationTables = { reviewVerificationOnly: v.optional(v.boolean()), // Started by a "@gitdogbot review" comment or the CLI. reviewRequested: v.optional(v.boolean()), + // The pull request comment that shows this review's progress and outcome. + statusCommentId: v.optional(v.number()), + // The pull request moved on mid-review, so the review was cancelled. + reviewCancelled: v.optional(v.boolean()), reviewRecheckOfActionId: v.optional(v.id("ticketActions")), // The last finished review of this PR: a later push reviews only the // files changed since its head. diff --git a/convex/ticketActions/internalMutation.ts b/convex/ticketActions/internalMutation.ts index 11100ba..93b2377 100644 --- a/convex/ticketActions/internalMutation.ts +++ b/convex/ticketActions/internalMutation.ts @@ -1424,6 +1424,13 @@ export const rejectPendingApproval = internalMutation({ } await ctx.db.patch(args.actionId, { status: "rejected" }); + // A rejected pull request review says so in its status comment. + if (action.actionType === "review" && action.statusCommentId !== undefined) + await ctx.scheduler.runAfter( + 0, + internal.pullRequests.internalAction.settleStatusComment, + { actionId: args.actionId }, + ); }, }); diff --git a/docs/github-app-setup.md b/docs/github-app-setup.md index 55dcd69..92e5cd5 100644 --- a/docs/github-app-setup.md +++ b/docs/github-app-setup.md @@ -84,12 +84,11 @@ review. Later reviews run when the author or a collaborator comments `@ review` (changes since the last review) or `@ full review`; `@ resolve` resolves Gitdog's threads and `@ help` lists the commands; `gitdog reviews request|resolve` runs the same from the CLI. -A command is a comment line that starts with the mention. The pull request +A command is a comment line that starts with the mention, or follows another review bot's command on the same line; `@gitdog` works as shorthand in production. The pull request author can run every command except a full review, which needs a collaborator. These arrive through the **Issue comments** event. A commenter who sends more than 10 commands an hour or 40 a day to one -repository is paused there for 24 hours. When the account runs out of tokens, -Gitdog says so on the pull request at most once a day. +repository is paused there for 24 hours. When a review cannot finish, for example because the account ran out of tokens, its status comment says why; for automatic reviews Gitdog posts at most one such comment a day per pull request. ### 6. Installation Settings diff --git a/packages/cli/docs/REFERENCE.md b/packages/cli/docs/REFERENCE.md index 3c14c01..5c4f243 100644 --- a/packages/cli/docs/REFERENCE.md +++ b/packages/cli/docs/REFERENCE.md @@ -385,7 +385,8 @@ review. The `reviews` commands match the pull request comments `@gitdogbot review` (commits since the last review), `@gitdogbot full review` and `@gitdogbot resolve` (resolves Gitdog's threads); the CLI prints the result instead of replying on GitHub. On GitHub, commands go in the pull request -conversation, not in replies inside a review thread. `reviews request` asks before spending tokens +conversation, not in replies inside a review thread; `@gitdog` works as +shorthand for `@gitdogbot`. `reviews request` asks before spending tokens (skip with `--yes`) and prints the queued review's action id. `reviews recheck` reruns a failed review on the latest commit without posting. diff --git a/public/llms-full.txt b/public/llms-full.txt index b3b14ab..df9d226 100644 --- a/public/llms-full.txt +++ b/public/llms-full.txt @@ -49,12 +49,12 @@ Gitdog reviews a pull request once, when it is opened or marked ready for review | `@gitdogbot resolve` | `gitdog reviews resolve ` | Resolves every Gitdog review thread | | `@gitdogbot help` | `gitdog reviews --help` | Lists the commands | -A command is a comment line that starts with the mention. Commands go in the pull request conversation; replies inside a review thread are not read, and pull requests opened by bots such as Dependabot are not reviewed. Collaborators can run every command; the pull request author, who may work from a fork, can run all but a full review. A review that failed or ran out of tokens can be requested again. +A command is a comment line that starts with the mention, or follows another review bot's command on the same line. `@gitdog` works as shorthand. While a review runs, Gitdog keeps one status comment on the pull request up to date. Commands go in the pull request conversation; replies inside a review thread are not read, and pull requests opened by bots such as Dependabot are not reviewed. Collaborators can run every command; the pull request author, who may work from a fork, can run all but a full review. A review that failed or ran out of tokens can be requested again. In issues and discussions, mention `@gitdogbot` with a question to get a reply grounded in the repository. - A review that finds no issues in fully reviewed changes approves the pull request; a later push, including updating the branch, withdraws the approval until `@gitdogbot review` reviews the new commits. -- When the account runs out of tokens, Gitdog says so on the pull request at most once a day. +- When a review cannot finish, for example because the account ran out of tokens, its status comment says why; for automatic reviews Gitdog posts at most one such comment a day per pull request. - Someone who sends more than 10 commands an hour or 40 a day to one repository is paused there for 24 hours. ### Gitdog CLI diff --git a/public/llms.txt b/public/llms.txt index d5ba734..f73b202 100644 --- a/public/llms.txt +++ b/public/llms.txt @@ -25,12 +25,12 @@ Gitdog reviews a pull request once, when it is opened or marked ready for review | `@gitdogbot resolve` | `gitdog reviews resolve ` | Resolves every Gitdog review thread | | `@gitdogbot help` | `gitdog reviews --help` | Lists the commands | -A command is a comment line that starts with the mention. Commands go in the pull request conversation; replies inside a review thread are not read, and pull requests opened by bots such as Dependabot are not reviewed. Collaborators can run every command; the pull request author, who may work from a fork, can run all but a full review. A review that failed or ran out of tokens can be requested again. +A command is a comment line that starts with the mention, or follows another review bot's command on the same line. `@gitdog` works as shorthand. While a review runs, Gitdog keeps one status comment on the pull request up to date. Commands go in the pull request conversation; replies inside a review thread are not read, and pull requests opened by bots such as Dependabot are not reviewed. Collaborators can run every command; the pull request author, who may work from a fork, can run all but a full review. A review that failed or ran out of tokens can be requested again. In issues and discussions, mention `@gitdogbot` with a question to get a reply grounded in the repository. - A review that finds no issues in fully reviewed changes approves the pull request; a later push, including updating the branch, withdraws the approval until `@gitdogbot review` reviews the new commits. -- When the account runs out of tokens, Gitdog says so on the pull request at most once a day. +- When a review cannot finish, for example because the account ran out of tokens, its status comment says why; for automatic reviews Gitdog posts at most one such comment a day per pull request. - Someone who sends more than 10 commands an hour or 40 a day to one repository is paused there for 24 hours. ## Pricing diff --git a/test/convex/pullRequests/commands.test.ts b/test/convex/pullRequests/commands.test.ts index 93b181a..1ce43e3 100644 --- a/test/convex/pullRequests/commands.test.ts +++ b/test/convex/pullRequests/commands.test.ts @@ -22,6 +22,20 @@ describe("pull request commands", () => { ["@gitdogbot help", "help"], ["@gitdogbot", "help"], ["@gitdogbot summarize this", "unknown"], + ["@gitdog review", "review"], + ["@coderabbitai review @gitdogbot review", "review"], + ["@coderabbitai full review @gitdog full review", "full_review"], + ["@gitdogbot review @coderabbitai review", "review"], + ["@alice I think @gitdogbot review is broken", null], + ["@mai I think @gitdogbot review is broken", null], + ["@kai please @gitdogbot review", null], + ["@coderabbitai and @gitdogbot, thanks for the reviews", null], + ["@coderabbitai @gitdogbot thanks", null], + ["@renovate[bot] rebase @gitdogbot review", "review"], + ["@coderabbitai review @gitdogbot full", null], + ["@coderabbitai @gitdogbot full stop", null], + ["@cursor review @gitdogbot review", "review"], + ["@gitdoghouse review", null], ["no mention here", null], ["@gitdogbot-dev review", null], ["email@gitdogbot review", null], @@ -31,6 +45,8 @@ describe("pull request commands", () => { expect(parseReviewCommand(body, "gitdogbot")).toBe(expected); }); it("uses the configured app slug", () => { + // The shorthand belongs to the production app only. + expect(parseReviewCommand("@gitdog review", "gitdogbot-dev")).toBeNull(); expect(parseReviewCommand("@gitdogbot-dev review", "gitdogbot-dev")).toBe( "review", ); diff --git a/test/convex/pullRequests/lifecycle.test.ts b/test/convex/pullRequests/lifecycle.test.ts index 0bb89c5..b97b92d 100644 --- a/test/convex/pullRequests/lifecycle.test.ts +++ b/test/convex/pullRequests/lifecycle.test.ts @@ -42,6 +42,11 @@ const settings = { }; async function fixture() { const t = convexTest(schema, modules); + // Review status comments go through these, apart from the review request. + const statusComments = { + add: vi.spyOn(helpers, "addIssueComment").mockResolvedValue(901), + update: vi.spyOn(helpers, "updateIssueComment").mockResolvedValue(), + }; const ids = await t.run(async (ctx) => { const userId = await ctx.db.insert("users", {}); // The one-time trial is already used, so quota checks see only the @@ -117,7 +122,7 @@ async function fixture() { full, }) ).actionId; - return { t, ...ids, observe, request }; + return { t, ...ids, observe, request, statusComments }; } const findings = [ { @@ -429,23 +434,262 @@ describe("review commands", () => { }), ).toBe("unavailable"); }); - it.each([ - ["a recheck", { reviewVerificationOnly: true }, {}], - ["a closed pull request", {}, { status: "closed" as const }], - ])("posts no limit notice for %s", async (_case, action, ticket) => { - const f = await fixture(); - const actionId = (await f.observe())!; - await f.t.run(async (ctx) => { - const current = (await ctx.db.get(actionId))!; - await ctx.db.patch(actionId, action); - await ctx.db.patch(current.ticketId, ticket); + describe("status comment", () => { + const settle = (f: Awaited>, actionId: string) => + f.t.action(internal.pullRequests.internalAction.settleStatusComment, { + actionId: actionId as never, + }); + async function withAccess(f: Awaited>) { + const identity = + await import("../../../convex/github/repositoryIdentity"); + const repository = (await f.t.run((ctx) => ctx.db.get(f.repositoryId)))!; + vi.spyOn(identity, "resolveVerifiedRepositoryAccess").mockResolvedValue({ + repository, + installation: {} as never, + githubRepositoryId: 123, + githubRepo: "owner/repo", + owner: "owner", + repo: "repo", + token: "fixture-token", + }); + } + it("posts a failure with a reason safe for a public pull request", async () => { + const f = await fixture(); + await withAccess(f); + const actionId = (await f.observe())!; + await f.t.run((ctx) => + ctx.db.patch(actionId, { + status: "failed", + errorMessage: + "Your Anthropic API key was rejected. Replace or remove it in Gitdog settings.", + }), + ); + await settle(f, actionId); + const body = f.statusComments.add.mock.calls[0][3]; + expect(body).toContain("**Review not completed**"); + expect(body).toContain("AI provider settings"); + expect(body).not.toContain("Anthropic API key"); + expect( + (await f.t.run((ctx) => ctx.db.get(actionId)))?.statusCommentId, + ).toBe(901); }); - expect( + it("says when the token limit stopped a review", async () => { + const f = await fixture(); + await withAccess(f); + const actionId = (await f.observe())!; + await f.t.run((ctx) => + ctx.db.patch(actionId, { + status: "failed", + errorMessage: + "Not enough tokens remain for this pull request review. No review was published.", + }), + ); + await settle(f, actionId); + expect(f.statusComments.add.mock.calls[0][3]).toContain( + "token limit of the account", + ); + }); + it("edits the reviewing comment in place when the review ends", async () => { + const f = await fixture(); + await withAccess(f); + const actionId = (await f.observe())!; + await f.t.run((ctx) => + ctx.db.patch(actionId, { + statusCommentId: 55, + status: "completed", + executionResult: { + published: true, + reviewUrl: + "https://github.com/owner/repo/pull/12#pullrequestreview-1", + }, + }), + ); + await settle(f, actionId); + expect(f.statusComments.add).not.toHaveBeenCalled(); + expect(f.statusComments.update.mock.calls[0][2]).toBe(55); + expect(f.statusComments.update.mock.calls[0][3]).toContain( + "**Reviewed**", + ); + }); + it("posts a failure of an automatic review at most once a day", async () => { + const f = await fixture(); + await withAccess(f); + const fail = async (headSha: string) => { + const id = (await f.observe({ ...source, headSha }))!; + await f.t.run((ctx) => + ctx.db.patch(id, { + status: "failed", + errorMessage: "Review exceeded its processing deadline", + }), + ); + await settle(f, id); + }; + await fail("c".repeat(40)); + await fail("d".repeat(40)); + expect(f.statusComments.add).toHaveBeenCalledTimes(1); + vi.advanceTimersByTime(24 * 60 * 60_000 + 1); + await fail("e".repeat(40)); + expect(f.statusComments.add).toHaveBeenCalledTimes(2); + }); + it.each([ + [ + "a review that changed under it as cancelled", + "Source preflight: The pull request changed before verification finished.", + "**Review cancelled**", + "try again", + ], + [ + "an oversized pull request without a retry hint", + "Review covers at most 300 changed files; split this PR for complete coverage", + "too large to review completely. Split it", + "try again", + ], + ])("words %s", async (_case, errorMessage, expected, absent) => { + const f = await fixture(); + await withAccess(f); + const actionId = (await f.observe())!; + await f.t.run((ctx) => + ctx.db.patch(actionId, { + status: "failed", + errorMessage, + statusCommentId: 7, + ...(expected.includes("cancelled") ? { reviewCancelled: true } : {}), + }), + ); + await settle(f, actionId); + const body = f.statusComments.update.mock.calls[0][3]; + expect(body).toContain(expected); + expect(body).not.toContain(absent); + }); + it("says when the owner rejects a review", async () => { + const f = await fixture(); + await withAccess(f); + const actionId = (await f.observe())!; + await f.t.run((ctx) => + ctx.db.patch(actionId, { + status: "pending_approval", + statusCommentId: 7, + }), + ); await f.t.mutation( - internal.pullRequests.internalMutation.claimLimitNotice, + internal.ticketActions.internalMutation.rejectPendingApproval, { actionId }, - ), - ).toBeNull(); + ); + await drainScheduledFunctions(f.t); + expect(f.statusComments.update.mock.calls.at(-1)?.[3]).toContain( + "**Review not posted**", + ); + }); + it("records a review the pull request moved past as cancelled", async () => { + const f = await fixture(); + const actionId = (await f.observe())!; + await f.t.mutation(internal.pullRequests.internalMutation.finish, { + actionId, + stage: "generation", + error: "Source preflight: changed", + cancelled: true, + }); + expect(await f.t.run((ctx) => ctx.db.get(actionId))).toMatchObject({ + status: "failed", + reviewCancelled: true, + }); + }); + it("counts an edited automatic failure toward the daily allowance", async () => { + const f = await fixture(); + await withAccess(f); + const actionId = (await f.observe())!; + await f.t.run((ctx) => + ctx.db.patch(actionId, { + status: "failed", + errorMessage: + "Not enough tokens remain for this pull request review. No review was published.", + statusCommentId: 7, + }), + ); + await settle(f, actionId); + expect(f.statusComments.update).toHaveBeenCalledTimes(1); + expect( + (await f.t.run((ctx) => ctx.db.query("tickets").first())) + ?.pullRequestLimitNoticeAt, + ).toBeTypeOf("number"); + }); + it("leaves a running review's comment alone", async () => { + const f = await fixture(); + await withAccess(f); + const actionId = (await f.observe())!; + await f.t.run((ctx) => ctx.db.patch(actionId, { statusCommentId: 7 })); + await settle(f, actionId); + expect(f.statusComments.update).not.toHaveBeenCalled(); + }); + it("settles a review that ended before its comment was recorded", async () => { + const f = await fixture(); + const actionId = (await f.observe())!; + await f.t.run((ctx) => + ctx.db.patch(actionId, { + status: "failed", + errorMessage: + "Pull request changed or closed; this review is superseded", + }), + ); + await f.t.mutation( + internal.pullRequests.internalMutation.recordStatusComment, + { actionId, commentId: 9, reviewing: true }, + ); + const scheduled = await f.t.run((ctx) => + ctx.db.system.query("_scheduled_functions").collect(), + ); + expect( + scheduled.some((job) => job.name.includes("settleStatusComment")), + ).toBe(true); + }); + it("does not settle when an expiry finds the review already moved on", async () => { + const f = await fixture(); + const actionId = (await f.observe())!; + await f.t.run((ctx) => + ctx.db.patch(actionId, { + status: "completed", + executionResult: { published: true }, + }), + ); + const before = ( + await f.t.run((ctx) => + ctx.db.system.query("_scheduled_functions").collect(), + ) + ).length; + await f.t.mutation(internal.pullRequests.internalMutation.expire, { + actionId, + }); + const after = await f.t.run((ctx) => + ctx.db.system.query("_scheduled_functions").collect(), + ); + expect( + after + .slice(before) + .some((job) => job.name.includes("settleStatusComment")), + ).toBe(false); + }); + it("posts nothing for a recheck or a cancelled review without a comment", async () => { + const f = await fixture(); + await withAccess(f); + const actionId = (await f.observe())!; + await f.t.run((ctx) => + ctx.db.patch(actionId, { + status: "failed", + reviewVerificationOnly: true, + errorMessage: "GitHub 502", + }), + ); + await settle(f, actionId); + await f.t.run((ctx) => + ctx.db.patch(actionId, { + reviewVerificationOnly: undefined, + errorMessage: + "Pull request changed or closed; this review is superseded", + }), + ); + await settle(f, actionId); + expect(f.statusComments.add).not.toHaveBeenCalled(); + }); }); it("pauses a commenter who sends too many commands", async () => { const f = await fixture(); @@ -528,27 +772,14 @@ describe("review commands", () => { const r = await replies(f); await r.run("help"); expect(r.posted()).toContain("`@gitdogbot full review`"); + // A started review answers in its own status comment, not a reply. await r.run("review"); - expect(r.posted()).toBe(`**Review started** for ${next.headSha}`); + expect(r.comment).toHaveBeenCalledTimes(1); await r.run("review"); expect(r.posted()).toContain("**A review is already running.**"); await r.run("pause_now"); expect(r.posted()).toContain("@octo sent too many Gitdog commands"); - expect(r.comment).toHaveBeenCalledTimes(4); - }); - it("posts the limit notice when a review runs out of tokens", async () => { - const f = await fixture(); - const actionId = (await f.observe())!; - const r = await replies(f); - await f.t.action(internal.pullRequests.internalAction.noticeLimit, { - actionId, - }); - await f.t.action(internal.pullRequests.internalAction.noticeLimit, { - actionId, - }); - expect(r.comment).toHaveBeenCalledTimes(1); - expect(r.posted()).toContain("**Review limit reached**"); - expect(r.posted()).toContain("`@gitdogbot review`"); + expect(r.comment).toHaveBeenCalledTimes(3); }); it("runs the same review commands from the CLI without posting a reply", async () => { const f = await reviewed(); @@ -576,22 +807,6 @@ describe("review commands", () => { }), ).rejects.toThrow("not authorized"); }); - it("tells a pull request about its limit at most once a day", async () => { - const f = await fixture(); - const actionId = (await f.observe())!; - const claim = () => - f.t.mutation(internal.pullRequests.internalMutation.claimLimitNotice, { - actionId, - }); - expect(await claim()).toMatchObject({ - repositoryId: f.repositoryId, - number: 12, - headSha: source.headSha, - }); - expect(await claim()).toBeNull(); - vi.advanceTimersByTime(24 * 60 * 60_000 + 1); - expect(await claim()).not.toBeNull(); - }); }); describe("PR review durable lifecycle", () => { it("does not enqueue bot-authored pull requests through observation", async () => { @@ -1881,6 +2096,32 @@ describe("approval", () => { "GitHub did not accept the review; no review was published", ); }); + it("records a pull request closed after the claim as cancelled", async () => { + const f = await publicationFixture(); + await github([]); + const live = await f.live.getMockImplementation()!( + {} as never, + 12 as never, + ); + // The read before the claim still sees the reviewed head. + f.live.mockResolvedValueOnce(live as never).mockResolvedValue({ + ...live, + state: "closed", + } as never); + f.fetch.mockClear(); + await f.t.action(internal.pullRequests.internalAction.publish, { + actionId: f.actionId, + manual: true, + }); + expect(f.fetch).not.toHaveBeenCalled(); + expect( + (await f.t.run((ctx) => ctx.db.get(f.actionId)))?.executionClaimedAt, + ).toBeDefined(); + expect(await f.t.run((ctx) => ctx.db.get(f.actionId))).toMatchObject({ + status: "failed", + reviewCancelled: true, + }); + }); it("records an earlier approval GitHub refused to dismiss", async () => { const f = await publicationFixture(); await github([], { dismissFails: true }); @@ -2676,6 +2917,34 @@ describe("batched PR reviews", () => { }; } + it.each([ + [false, 0], + [true, 1], + ])( + "stays quiet within a day of an automatic failure unless requested (requested %s)", + async (requested, posts) => { + const f = await batchFixture(); + await f.t.run(async (ctx) => { + const action = (await ctx.db.get(f.actionId))!; + await ctx.db.patch(action.ticketId, { + pullRequestLimitNoticeAt: Date.now(), + }); + if (requested) + await ctx.db.patch(f.actionId, { reviewRequested: true }); + }); + await f.generate(); + expect(f.statusComments.add).toHaveBeenCalledTimes(posts); + }, + ); + it("shows a reviewing comment once, when the review starts", async () => { + const f = await batchFixture(); + await f.generate(); + expect(f.statusComments.add).toHaveBeenCalledTimes(1); + expect(f.statusComments.add.mock.calls[0][3]).toContain("**Reviewing**"); + expect((await f.getAction())?.statusCommentId).toBe(901); + await f.generate(2); + expect(f.statusComments.add).toHaveBeenCalledTimes(1); + }); it("combines findings from a 61-file PR into one review body after all batches finish", async () => { const f = await batchFixture(); await f.generate(); @@ -2891,7 +3160,7 @@ describe("batched PR reviews", () => { ctx.db.system.query("_scheduled_functions").collect(), ); expect( - scheduled.filter((job) => job.name.includes("noticeLimit")), + scheduled.filter((job) => job.name.includes("settleStatusComment")), ).toHaveLength(1); }); @@ -3236,9 +3505,9 @@ describe("batched PR reviews", () => { // No review is posted; the requested review says it found nothing new. const urls = f.fetch.mock.calls.map(([url]) => String(url)); expect(urls.some((url) => url.includes("/reviews"))).toBe(false); - expect( - urls.filter((url) => url.endsWith("/issues/12/comments")), - ).toHaveLength(1); + expect(f.statusComments.add.mock.calls.at(-1)?.[3]).toContain( + "**No new changes to review**", + ); expect(await f.t.run((ctx) => ctx.db.get(f.nextId))).toMatchObject({ status: "completed", executionResult: { published: false, reason: "no_new_changes" }, @@ -3251,9 +3520,9 @@ describe("batched PR reviews", () => { expect(f.batches).toEqual([]); const urls = f.fetch.mock.calls.map(([url]) => String(url)); expect(urls.some((url) => url.includes("/reviews"))).toBe(false); - expect( - JSON.parse(f.fetch.mock.calls.at(-1)![1]!.body as string).body, - ).toContain("**No new changes to review**"); + expect(f.statusComments.add.mock.calls.at(-1)?.[3]).toContain( + "**No new changes to review**", + ); expect(await f.t.run((ctx) => ctx.db.get(f.nextId))).toMatchObject({ status: "completed", executionResult: { published: false, reason: "no_new_changes" },