feat: store the repository picker list and update it by webhook

The anonymize page's "Your repositories" picker now pages and searches a
per-user list stored in MongoDB instead of calling GitHub on every page and
search. GitHub is read only when the list is empty, when the user presses
the new refresh button, or after an installation change marks it stale.

- One sync per list (Mongo lock with a renewed lease); full pagination in
  the background after the first page is returned; rate limits pause syncs.
- GitHub App installation, installation_repositories and repository
  webhooks update the stored lists idempotently. The App must subscribe to
  the Repository event (documented in docs/github-app-setup.md).
- Legacy OAuth lists change only on refresh.
- Picker header shows "Updated 3 min ago" and a refresh button.
- dev-mock: stored list, simulated refresh and webhook endpoint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Thomas DurieuxandClaude Opus 5.5 committed 2026-11-29 08:06:30 +00:00
1 parent 0640d741f1
commit c73e992b7c
22 files changed
+1026 -153

No files matched your search

+3
View File
@@ -1,5 +1,6 @@
import { installDatabaseTiming } from "../core/database-timing";
import InstallationModel from "../core/model/github-installation";
import { UserRepositoryModel, UserRepositorySyncModel } from "../core/model/user-repositories";
import { credentialCipher } from "../core/credentials";
import CredentialModel from "../core/model/credentials/credentials.model";
import mongoose, { ConnectOptions } from "mongoose";
@@ -37,6 +38,8 @@ export async function connect(overrides: Pick<ConnectOptions, "appName" | "maxPo
await mongoose.connect(getMongoUrl(), options);
await CredentialModel.createIndexes();
if (config.GITHUB_APP_ENABLED) await InstallationModel.createIndexes();
await UserRepositoryModel.createIndexes();
await UserRepositorySyncModel.createIndexes();
await initializeOwnerAccessAlerts();
isConnected = true;
+9
View File
@@ -16,6 +16,7 @@ import { getCredentialToken } from "../../core/credentials";
import { APP_PROVIDER, appError, appUserToken, AppInstallation, clearAppTokenCache, exchangeAppToken,
githubRequest, GitHubRepositoryInfo, reconcileAppGrant, reconcileInstallation, installationURL, saveAppGrant, selectRepositoryAccess, userInstallations } from "../../core/github-app";
import { explicitErrorStatus, getUser, handleError } from "./route-utils";
import { applyRepositoryWebhook, clearRepositoryList, markRepositoryListStale } from "../../core/user-repository-list";
import { isDisabledAccount, safeAuthReturnTo } from "./auth-utils";
type Flow = { state: string; expires: number; ownerId?: string; returnTo: string; repository?: string; install?: boolean };
@@ -156,6 +157,7 @@ router.get("/app/setup", enabled, async (req, res) => {
await resetOwnerAccessAlerts(user.id);
}
await UserModel.updateOne({ _id: user.id }, { $set: { repositories: [] } });
await markRepositoryListStale([user.id]);
res.redirect(flow.returnTo);
} catch (error) { handleError(error, res, req); }
});
@@ -284,6 +286,7 @@ export async function revokeGrant(ownerId: string, provider: "github" | "github-
await CredentialModel.deleteMany({ ownerId, provider });
if (provider === "github") await UserModel.updateOne({ _id: ownerId }, { $unset: { "accessTokens.github": "", "accessTokenDates.github": "" } });
await UserModel.updateOne({ _id: ownerId }, { $set: { repositories: [] } });
await clearRepositoryList(ownerId, provider === "github" ? "oauth" : "github-app");
}
router.post("/connections/disconnect-oauth", async (req, res) => {
try {
@@ -323,6 +326,12 @@ githubAppWebhook.post("/", express.raw({ type: "application/json", limit: "2mb"
phase = "reconcile_grant";
for (const user of users) await reconcileAppGrant(String(user._id));
}
if (["installation", "installation_repositories", "repository"].includes(String(event))) {
// Stored picker lists first: these writes are idempotent and need no
// GitHub call, so they apply even if the installation check below fails.
phase = "update_repository_lists";
await applyRepositoryWebhook(String(event), body);
}
if (["installation", "installation_repositories"].includes(String(event)) && Number.isSafeInteger(body.installation?.id) && String(body.installation.app_id) === config.GITHUB_APP_ID) {
const installation = body.installation as AppInstallation;
// Fail closed immediately. Reconcile from GitHub, never trust event order
+6 -7
View File
@@ -1,6 +1,4 @@
import { repositoryPickerPage } from "../../core/github-repository-picker";
import { appUserToken } from "../../core/github-app";
import { getCredentialToken } from "../../core/credentials";
import { pickerConnection, repositoryListPage } from "../../core/user-repository-list";
import AnonymousError from "../../core/AnonymousError";
import { BillingQuota, BillingDraft } from "../../core/billing/models";
import { cancelAccountBilling } from "../../core/billing/stripe";
@@ -383,11 +381,12 @@ router.get("/repository_picker", async (req, res) => {
res.status(400).json({ error: "Invalid repository search" });
return;
}
const token = connection === "github-app" ? await appUserToken(user.id)
: connection === "oauth" ? await getCredentialToken(user.id) : await user.getAccessToken();
if (!token) { res.status(401).json({ error: "GitHub connection required" }); return; }
// Served from the stored list; GitHub is only asked when it is empty,
// forced, or stale after an installation change.
const list = await pickerConnection(user.id, connection);
if (!list) { res.status(401).json({ error: "GitHub connection required" }); return; }
res.set("Cache-Control", "no-store");
res.json(await repositoryPickerPage(token, cursor, query));
res.json(await repositoryListPage(user.id, list, { cursor, query, force: req.query.force === "1" }));
} catch (error) { handleError(error, res, req); }
});