|
| 1 | +import { PrismaClient } from "@sourcebot/db"; |
| 2 | +import { CachedPermittedExternalAccounts, cachedPermittedExternalAccountsSchema, createLogger } from "@sourcebot/shared"; |
| 3 | + |
| 4 | +const logger = createLogger('permission-utils'); |
| 5 | + |
| 6 | +/** |
| 7 | + * Rebuilds the AccountToRepoPermission join table for a given account |
| 8 | + * based on the cached external account IDs stored in repos. |
| 9 | + * |
| 10 | + * This is useful when a new account is created and we want to grant |
| 11 | + * access to repos without waiting for a full permission sync. |
| 12 | + * |
| 13 | + * @param db - Prisma client instance |
| 14 | + * @param accountId - The internal account ID |
| 15 | + * @param provider - The OAuth provider (e.g., 'github', 'gitlab') |
| 16 | + * @param providerAccountId - The external account ID from the provider |
| 17 | + */ |
| 18 | +export async function rebuildPermissionsFromCache( |
| 19 | + db: PrismaClient, |
| 20 | + accountId: string, |
| 21 | + provider: string, |
| 22 | + providerAccountId: string |
| 23 | +): Promise<void> { |
| 24 | + logger.info(`Rebuilding permissions from cache for account ${accountId} (${provider}:${providerAccountId})`); |
| 25 | + |
| 26 | + // Find all repos that have this external account ID in their cached permissions |
| 27 | + const repos = await db.repo.findMany({ |
| 28 | + where: { |
| 29 | + cachedPermittedExternalAccounts: { |
| 30 | + not: null, |
| 31 | + }, |
| 32 | + }, |
| 33 | + select: { |
| 34 | + id: true, |
| 35 | + cachedPermittedExternalAccounts: true, |
| 36 | + }, |
| 37 | + }); |
| 38 | + |
| 39 | + // Filter repos that include this specific external account ID for this provider |
| 40 | + const reposWithAccess = repos.filter(repo => { |
| 41 | + try { |
| 42 | + const cached = cachedPermittedExternalAccountsSchema.parse( |
| 43 | + repo.cachedPermittedExternalAccounts |
| 44 | + ); |
| 45 | + |
| 46 | + const providerAccountIds = cached[provider as keyof CachedPermittedExternalAccounts]; |
| 47 | + return providerAccountIds?.includes(providerAccountId) ?? false; |
| 48 | + } catch (error) { |
| 49 | + logger.warn(`Failed to parse cachedPermittedExternalAccounts for repo ${repo.id}:`, error); |
| 50 | + return false; |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + if (reposWithAccess.length === 0) { |
| 55 | + logger.info(`No repos found with cached permissions for account ${accountId}`); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + // Create AccountToRepoPermission entries |
| 60 | + await db.accountToRepoPermission.createMany({ |
| 61 | + data: reposWithAccess.map(repo => ({ |
| 62 | + accountId, |
| 63 | + repoId: repo.id, |
| 64 | + })), |
| 65 | + skipDuplicates: true, |
| 66 | + }); |
| 67 | + |
| 68 | + logger.info(`Rebuilt permissions for ${reposWithAccess.length} repos for account ${accountId}`); |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Synchronizes permissions for all existing accounts based on cached external account IDs. |
| 73 | + * |
| 74 | + * This can be used as a migration script or maintenance task to ensure the join table |
| 75 | + * is in sync with the cached data. |
| 76 | + * |
| 77 | + * @param db - Prisma client instance |
| 78 | + */ |
| 79 | +export async function syncAllPermissionsFromCache(db: PrismaClient): Promise<void> { |
| 80 | + logger.info('Starting full permission sync from cache'); |
| 81 | + |
| 82 | + const accounts = await db.account.findMany({ |
| 83 | + select: { |
| 84 | + id: true, |
| 85 | + provider: true, |
| 86 | + providerAccountId: true, |
| 87 | + }, |
| 88 | + }); |
| 89 | + |
| 90 | + let totalUpdated = 0; |
| 91 | + |
| 92 | + for (const account of accounts) { |
| 93 | + try { |
| 94 | + await rebuildPermissionsFromCache( |
| 95 | + db, |
| 96 | + account.id, |
| 97 | + account.provider, |
| 98 | + account.providerAccountId |
| 99 | + ); |
| 100 | + totalUpdated++; |
| 101 | + } catch (error) { |
| 102 | + logger.error(`Failed to rebuild permissions for account ${account.id}:`, error); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + logger.info(`Completed full permission sync from cache. Updated ${totalUpdated}/${accounts.length} accounts`); |
| 107 | +} |
0 commit comments