fix(repo-list): block installations before picker writes, resync stale lists, clear on revoke

- A failed picker write no longer skips blocking a deleted or suspended
  installation: the list update now runs after it.
- Lists older than 12 hours resync in the background, covering new org
  installs, OAuth lists and missed or reordered deliveries.
- Revoking a grant clears the stored list first, and a revoked App
  authorization webhook clears it too.
- The name index includes repositoryId to match the picker sort.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Thomas DurieuxandClaude Opus 5.5 committed 2026-11-29 11:50:37 +00:00
1 parent c73e992b7c
commit f12cde1400
3 files changed
+20 -10

No files matched your search

+2 -1
View File
@@ -28,7 +28,8 @@ const schema = new Schema({
syncedAt: { type: Date, required: true },
});
schema.index({ ownerId: 1, connection: 1, repositoryId: 1 }, { unique: true });
schema.index({ ownerId: 1, connection: 1, nameKey: 1 });
// Matches the picker sort, so a page never sorts in memory.
schema.index({ ownerId: 1, connection: 1, nameKey: 1, repositoryId: 1 });
schema.index({ repositoryId: 1 });
schema.index({ installationId: 1 }, { partialFilterExpression: { installationId: { $exists: true } } });
export const UserRepositoryModel = model("UserRepository", schema);
+4 -1
View File
@@ -20,6 +20,8 @@ const FIRST_PAGE_WAIT_MS = 10_000;
const FORCE_MIN_INTERVAL_MS = 10_000;
/** An empty list that synced recently is a user with no repositories, not a missing list. */
const EMPTY_RECHECK_MS = 5 * 60_000;
/** Webhooks miss some changes (new org installs, OAuth lists); resync in the background after this. */
const STALE_MS = 12 * 60 * 60_000;
/** Default pause after a rate-limit response without a reset time. */
const RATE_LIMIT_PAUSE_MS = 60_000;
/** Bound a runaway pagination: 100,000 repositories. */
@@ -199,7 +201,8 @@ export async function repositoryListPage(ownerId: string, connection: Repository
const lastSync = state?.syncedAt?.getTime() || 0;
const wanted = !lastSync || !!state?.needsSyncAt
|| (opts.force === true && now - lastSync >= FORCE_MIN_INTERVAL_MS)
|| (!hasRows && now - lastSync >= EMPTY_RECHECK_MS);
|| (!hasRows && now - lastSync >= EMPTY_RECHECK_MS)
|| now - lastSync >= STALE_MS;
const paused = !!state?.retryAfter && state.retryAfter.getTime() > now;
if (wanted && !paused) {
const started = await startRepositorySync(ownerId, connection);
+14 -8
View File
@@ -277,6 +277,8 @@ export async function revokeGrant(ownerId: string, provider: "github" | "github-
const token = await getCredentialToken(ownerId, provider);
const clientId = provider === "github" ? config.CLIENT_ID : config.GITHUB_APP_CLIENT_ID;
const clientSecret = provider === "github" ? config.CLIENT_SECRET : config.GITHUB_APP_CLIENT_SECRET;
// Before the GitHub call: a failed revoke must not keep private repo names.
await clearRepositoryList(ownerId, provider === "github" ? "oauth" : "github-app");
if (token) {
const response = await fetch(`https://api.github.com/applications/${clientId}/grant`, { method: "DELETE",
headers: { Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString("base64")}`, "Content-Type": "application/json", Accept: "application/vnd.github+json" },
@@ -286,7 +288,6 @@ 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 {
@@ -324,13 +325,12 @@ githubAppWebhook.post("/", express.raw({ type: "application/json", limit: "2mb"
phase = "find_grant_owners";
const users = await UserModel.find({ "externalIDs.github": String(body.sender.id) }).select("_id").lean();
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);
for (const user of users) {
await reconcileAppGrant(String(user._id));
// Private repository names must not outlive the grant; a live grant
// simply resyncs on the next picker load.
await clearRepositoryList(String(user._id), "github-app");
}
}
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;
@@ -345,6 +345,12 @@ githubAppWebhook.post("/", express.raw({ type: "application/json", limit: "2mb"
phase = "reconcile_installation";
if (body.action !== "deleted") await reconcileInstallation(installation.id, revision);
}
if (["installation", "installation_repositories", "repository"].includes(String(event))) {
// After the installation block: a failed picker write must never leave a
// deleted or suspended installation usable. The lists also resync by age.
phase = "update_repository_lists";
await applyRepositoryWebhook(String(event), body);
}
return res.status(204).end();
} catch (error) {
const delivery = req.headers["x-github-delivery"];