Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,19 @@ a pull request comment, or from the CLI:
| `@gitdogbot resolve` | `gitdog reviews resolve <ticket-id>` | 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
Expand Down
28 changes: 28 additions & 0 deletions convex/github/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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,
Expand Down
150 changes: 146 additions & 4 deletions convex/pullRequests/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
hyochan marked this conversation as resolved.
Resolved
// 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;
Expand Down Expand Up @@ -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.
Comment thread
hyochan marked this conversation as resolved.
Resolved
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.
Comment thread
hyochan marked this conversation as resolved.
Resolved
Comment thread
hyochan marked this conversation as resolved.
Resolved
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":
Expand All @@ -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.`;
Comment thread
hyochan marked this conversation as resolved.
Resolved
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;
}
}
16 changes: 16 additions & 0 deletions convex/pullRequests/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
hyochan marked this conversation as resolved.
Resolved
// 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");
Expand Down
Loading
Loading