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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ approval until the new commits are reviewed.
## Pull request commands

Gitdog reviews a pull request once, when it is opened or marked ready for
review. After that, ask for more in a pull request comment or from the CLI:
review. When pull request reviews turn on, it also gives up to five open pull
requests from the last 30 days their first review, skipping drafts and bots.
After that, ask for more in a pull request comment or from the CLI:

| Pull request comment | CLI | What it does |
| ----------------------- | ------------------------------------------- | ------------------------------------------------ |
Expand Down
6 changes: 6 additions & 0 deletions convex/apiKeys/gateway.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
reviewOpenPullRequests,
turnsReviewsOn,
} from "../pullRequests/catchUp";
import { internalMutation } from "../_generated/server";
import type { MutationCtx } from "../_generated/server";
import type { Doc } from "../_generated/dataModel";
Expand Down Expand Up @@ -200,6 +204,8 @@ export const execute = internalMutation({
Object.assign(settings, { [key]: value });
}
await ctx.db.patch(repo._id, { settings, updatedAt: Date.now() });
if (repo.isActive && turnsReviewsOn(repo.settings, settings))
await reviewOpenPullRequests(ctx, repo);
Comment thread
hyochan marked this conversation as resolved.
Resolved
return repo._id;
}
case "repoIndexing/query:getIndexingStatus":
Expand Down
40 changes: 40 additions & 0 deletions convex/pullRequests/catchUp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { internal } from "../_generated/api";
import type { Doc } from "../_generated/dataModel";
import type { MutationCtx } from "../_generated/server";

// Turning reviews off and on again cannot start this more than once an hour.
const CATCH_UP_COOLDOWN_MS = 60 * 60_000;

/**
* Reviews start on pull request events, so pull requests opened before
* reviews were on never get one. Each path that turns reviews on for an
* active repository calls this; it schedules a few of them for review.
*/
export async function reviewOpenPullRequests(
ctx: MutationCtx,
repository: Doc<"repositories">,
): Promise<void> {
const now = Date.now();
if (
repository.pullRequestCatchUpAt !== undefined &&
now - repository.pullRequestCatchUpAt < CATCH_UP_COOLDOWN_MS
)
return;
await ctx.db.patch(repository._id, { pullRequestCatchUpAt: now });
await ctx.scheduler.runAfter(
0,
internal.pullRequests.internalAction.reviewOpenPullRequests,
{ repositoryId: repository._id },
);
}

/** Whether a settings change turns pull request reviews on. */
export function turnsReviewsOn(
before: Doc<"repositories">["settings"],
after: { pullRequestReviewsEnabled?: boolean },
): boolean {
return (
before.pullRequestReviewsEnabled !== true &&
after.pullRequestReviewsEnabled === true
);
}
4 changes: 4 additions & 0 deletions convex/pullRequests/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ const fixedFailureReasons = new Map([
"Verification introduced an unreviewed finding",
"The AI verification response was not a valid selection of the reviewed findings.",
],
[
"Open pull requests are invalid",
"GitHub returned an unexpected list of open pull requests.",
],
[
"Finding must anchor to an added line in the reviewed diff",
"A finding did not point to a line added by the pull request.",
Expand Down
14 changes: 14 additions & 0 deletions convex/pullRequests/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ export async function getPullRequest(
throw new Error("Pull request identity is invalid");
return pr;
}
/** Open pull requests, the most recently updated first. */
export async function listOpenPullRequests(
access: VerifiedRepositoryAccess,
): Promise<Array<PullRequest & { updated_at: string }>> {
const pulls = await readReviewJson<
Array<PullRequest & { updated_at: string }>
>(
access,
"/pulls?state=open&sort=updated&direction=desc&per_page=30",
"List open pull requests",
);
if (!Array.isArray(pulls)) throw new Error("Open pull requests are invalid");
return pulls;
}
// Every group is a full model review, so bound the total before any spend.
const MAX_REVIEW_FILES = 300;
export const BATCH_REVIEW_THRESHOLD = 60;
Expand Down
70 changes: 70 additions & 0 deletions convex/pullRequests/internalAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
BATCH_REVIEW_THRESHOLD,
changedSince,
getPullRequest,
listOpenPullRequests,
hasUnresolvedOwnThreads,
resolveOwnThreads,
isBotPullRequestAuthor,
Expand Down Expand Up @@ -208,6 +209,75 @@ export const observe = internalAction({
},
});

// Reviews start on pull request events, so pull requests opened before reviews
// were on never get one. When reviews turn on, a few recently updated ones that
// were never reviewed get their first review, spaced further apart than one
// review action can run, because an account runs one review at a time.
const CATCH_UP_PULL_REQUESTS = 5;
const CATCH_UP_WINDOW_MS = 30 * 24 * 60 * 60_000;
const CATCH_UP_SPACING_MS = 12 * 60_000;

export const reviewOpenPullRequests = internalAction({
args: { repositoryId: v.id("repositories") },
handler: async (ctx, args): Promise<void> => {
const repo = await ctx.runQuery(
internal.repositories.internalQuery.getById,
{ repositoryId: args.repositoryId },
);
if (
!repo ||
repo.isActive !== true ||
repo.settings.pullRequestReviewsEnabled !== true
)
return;
// Reviews the account cannot pay for would only post failure comments on
// old pull requests; the owner can still ask for one with a command.
const provider = await resolveGenerationProvider(ctx, {
userId: repo.userId,
}).catch(() => null);
if (!provider) return;
if (
!usesUserKey(provider) &&
!(await ctx.runQuery(
internal.pullRequests.internalQuery.hasReviewAllowance,
{ userId: repo.userId },
))
)
return;
const access = await resolveVerifiedRepositoryAccess(
ctx,
args.repositoryId,
);
const now = Date.now();
const candidates = (await listOpenPullRequests(access)).filter(
(pr) =>
pr !== null &&
typeof pr === "object" &&
Number.isSafeInteger(pr.number) &&
pr.draft !== true &&
typeof pr.user?.login === "string" &&
!isBotPullRequestAuthor(pr.user) &&
now - Date.parse(pr.updated_at) <= CATCH_UP_WINDOW_MS,
);
const reviewed = new Set(
await ctx.runQuery(internal.pullRequests.internalQuery.reviewedNumbers, {
repositoryId: args.repositoryId,
numbers: candidates.map((pr) => pr.number),
}),
);
const recent = candidates
.filter((pr) => !reviewed.has(pr.number))
.slice(0, CATCH_UP_PULL_REQUESTS);
// observe reviews a pull request only until it has one finished review.
for (const [index, pr] of recent.entries())
await ctx.scheduler.runAfter(
index * CATCH_UP_SPACING_MS,
internal.pullRequests.internalAction.observe,
{ repositoryId: args.repositoryId, number: pr.number },
);
},
});

const REVIEW_COVERAGE_PREVIEW = 100;
const MAX_FAILED_REVIEW_GROUPS = 2;
const MAX_REFUSED_REVIEW_GROUPS = 5;
Expand Down
32 changes: 32 additions & 0 deletions convex/pullRequests/internalQuery.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { internalQuery } from "../_generated/server";
import { checkAccountTokenAllowance } from "../subscriptions/internalQuery";
import { v } from "convex/values";
export const get = internalQuery({
args: { actionId: v.id("ticketActions") },
Expand Down Expand Up @@ -37,3 +38,34 @@ export const getTicket = internalQuery({
return ticket;
},
});

/** Which of these pull requests already have a review of any kind. */
export const reviewedNumbers = internalQuery({
args: { repositoryId: v.id("repositories"), numbers: v.array(v.number()) },
handler: async (ctx, args) => {
const reviewed: number[] = [];
for (const number of args.numbers.slice(0, 30)) {
const ticket = await ctx.db
.query("tickets")
.withIndex("by_github_number", (q) =>
q.eq("repositoryId", args.repositoryId).eq("githubNumber", number),
)
.first();
if (!ticket || ticket.ticketType !== "pull_request") continue;
const review = await ctx.db
.query("ticketActions")
.withIndex("by_ticket", (q) => q.eq("ticketId", ticket._id))
.filter((q) => q.eq(q.field("actionType"), "review"))
.first();
if (review) reviewed.push(number);
}
return reviewed;
},
});

/** Whether the account has tokens left for a review on Gitdog's key. */
export const hasReviewAllowance = internalQuery({
args: { userId: v.id("users") },
handler: async (ctx, args) =>
(await checkAccountTokenAllowance(ctx, args.userId)).allowed,
});
6 changes: 6 additions & 0 deletions convex/repositories/internalMutation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { reviewOpenPullRequests } from "../pullRequests/catchUp";
import { paginationOptsValidator } from "convex/server";
import { v } from "convex/values";
import { internal } from "../_generated/api";
Expand Down Expand Up @@ -475,6 +476,11 @@ async function restoreRepositoryFromLiveAccess(
indexIdentityVerifiedGithubRepo: undefined,
updatedAt: now,
});
if (
shouldReactivate &&
repository.settings.pullRequestReviewsEnabled === true
)
await reviewOpenPullRequests(ctx, repository);
return shouldReactivate;
}

Expand Down
13 changes: 12 additions & 1 deletion convex/repositories/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import {
} from "../github/installationAccess";
import { getCanonicalGitHubRepositoryFullNameKey } from "../github/repositoryIdentity";
import { pauseRepositoryForAccessRemoval } from "./accessLifecycle";
import {
reviewOpenPullRequests,
turnsReviewsOn,
} from "../pullRequests/catchUp";
import {
getRepositoryNameQuarantineFullName,
readCanonicalRepositoryNameMatches,
Expand Down Expand Up @@ -152,6 +156,8 @@ async function reactivateInstallationReconnect(
internal.repoIndexing.action.indexRepository,
{ repositoryId: repository._id, expectedIndexVersion: indexVersion },
);
if (repository.settings.pullRequestReviewsEnabled === true)
await reviewOpenPullRequests(ctx, repository);
}
return repository._id;
}
Expand Down Expand Up @@ -392,10 +398,13 @@ export const updateSettings = mutation({
throw new Error("repository disconnect cleanup is still in progress");
}

const settings = { ...repo.settings, ...args.settings };
await ctx.db.patch(args.repositoryId, {
settings: { ...repo.settings, ...args.settings },
settings,
updatedAt: Date.now(),
});
if (repo.isActive && turnsReviewsOn(repo.settings, settings))
await reviewOpenPullRequests(ctx, repo);

return args.repositoryId;
},
Expand Down Expand Up @@ -547,6 +556,8 @@ export const toggleActive = mutation({
expectedIndexVersion: activationVersion,
},
);
if (repo.settings.pullRequestReviewsEnabled === true)
await reviewOpenPullRequests(ctx, repo);
}

return args.repositoryId;
Expand Down
3 changes: 3 additions & 0 deletions convex/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,9 @@ const applicationTables = {
),

// Issue Assistant settings (simplified)
// When open pull requests were last scheduled for review because
// reviews turned on.
pullRequestCatchUpAt: v.optional(v.number()),
settings: v.object({
// Issue Assistant features
issueAssistantEnabled: v.boolean(), // Master toggle
Expand Down
8 changes: 7 additions & 1 deletion docs/github-app-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ dropped in verification only when the verifier quotes source that refutes it.
found there; `bun run test:e2e:recall` replays the reviewer on them.

Gitdog reviews a pull request once, when it is opened or marked ready for
review. Later reviews run when the author or a collaborator comments
review. When pull request reviews are turned on (web, CLI or project key), or a
repository with them on is reactivated, reconnected or regains GitHub access,
up to five open pull requests updated in the last 30 days that were never
reviewed get their first review, 12 minutes apart. Drafts and bot pull requests
are skipped, it runs at most once an hour per repository, and nothing is
scheduled when the account has no tokens and no usable key of its own. Later
reviews run when the author or a collaborator comments
`@<app-slug> review` (changes since the last review) or `@<app-slug> full
review`; `@<app-slug> resolve` resolves Gitdog's threads and `@<app-slug> help`
lists the commands; `gitdog reviews request|resolve` runs the same from the CLI.
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/docs/REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ npx gitdog@latest reviews recheck ACTION_ID
```

Gitdog reviews a pull request once, when it is opened or marked ready for
review. The `reviews` commands match the pull request comments
review, and turning `pullRequestReviewsEnabled` on also gives up to five open
pull requests from the last 30 days their first review. The `reviews` commands match the pull request comments
`@gitdogai review` (commits since the last review), `@gitdogai full review`
and `@gitdogai resolve` (resolves Gitdog's threads); the CLI prints the result
instead of replying on GitHub. On GitHub, commands go in the pull request
Expand Down
2 changes: 1 addition & 1 deletion public/llms-full.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ When a pull request is opened or marked ready for review, Gitdog:

## Commands

Gitdog reviews a pull request once, when it is opened or marked ready for review. After that, the pull request author or a repository collaborator asks for more:
Gitdog reviews a pull request once, when it is opened or marked ready for review. When pull request reviews turn on, or a repository with them on is reactivated, reconnected or regains GitHub access, Gitdog also gives up to five open pull requests from the last 30 days their first review, skipping drafts and bots. After that, the pull request author or a repository collaborator asks for more:

| Pull request comment | CLI | What it does |
| --- | --- | --- |
Expand Down
2 changes: 1 addition & 1 deletion public/llms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Gitdog is an AI automation platform for GitHub that helps developers automate re

## Commands

Gitdog reviews a pull request once, when it is opened or marked ready for review. After that, the pull request author or a repository collaborator asks for more:
Gitdog reviews a pull request once, when it is opened or marked ready for review. When pull request reviews turn on, or a repository with them on is reactivated, reconnected or regains GitHub access, Gitdog also gives up to five open pull requests from the last 30 days their first review, skipping drafts and bots. After that, the pull request author or a repository collaborator asks for more:

| Pull request comment | CLI | What it does |
| --- | --- | --- |
Expand Down
Loading
Loading