|
| 1 | +import { READ_DB_CONFIG, getDbConnection } from '@crowd/data-access-layer/src/database' |
| 2 | +import { pgpQx } from '@crowd/data-access-layer/src/queryExecutor' |
| 3 | +import { getServiceLogger } from '@crowd/logging' |
| 4 | +import { |
| 5 | + NangoIntegration, |
| 6 | + createNangoGithubTokenConnection, |
| 7 | + getNangoConnections, |
| 8 | + initNangoCloudClient, |
| 9 | +} from '@crowd/nango' |
| 10 | + |
| 11 | +const log = getServiceLogger() |
| 12 | + |
| 13 | +// Required environment variables |
| 14 | +const REQUIRED_ENV_VARS = [ |
| 15 | + 'NANGO_CLOUD_SECRET_KEY', |
| 16 | + 'NANGO_CLOUD_INTEGRATIONS', |
| 17 | + 'CROWD_GITHUB_APP_ID', |
| 18 | + 'CROWD_GITHUB_CLIENT_ID', |
| 19 | +] |
| 20 | + |
| 21 | +function validateEnvVars(): void { |
| 22 | + const missing: string[] = [] |
| 23 | + for (const envVar of REQUIRED_ENV_VARS) { |
| 24 | + if (!process.env[envVar]) { |
| 25 | + missing.push(envVar) |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + if (missing.length > 0) { |
| 30 | + log.error(`Missing required environment variables: ${missing.join(', ')}`) |
| 31 | + process.exit(1) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +const processArguments = process.argv.slice(2) |
| 36 | + |
| 37 | +const executeMode = processArguments.includes('--execute') |
| 38 | +const dryRunMode = !executeMode |
| 39 | + |
| 40 | +if (dryRunMode) { |
| 41 | + log.info('Running in DRY-RUN mode. Use --execute flag to actually create connections.') |
| 42 | +} |
| 43 | + |
| 44 | +setImmediate(async () => { |
| 45 | + validateEnvVars() |
| 46 | + |
| 47 | + const appId = process.env.CROWD_GITHUB_APP_ID |
| 48 | + const clientId = process.env.CROWD_GITHUB_CLIENT_ID |
| 49 | + |
| 50 | + const db = await getDbConnection(READ_DB_CONFIG()) |
| 51 | + const qx = pgpQx(db) |
| 52 | + |
| 53 | + await initNangoCloudClient() |
| 54 | + |
| 55 | + // Get all github-token-* connections from Nango |
| 56 | + const allConnections = await getNangoConnections() |
| 57 | + const tokenConnections = allConnections.filter( |
| 58 | + (c) => |
| 59 | + c.provider_config_key === NangoIntegration.GITHUB && |
| 60 | + c.connection_id.toLowerCase().startsWith('github-token-'), |
| 61 | + ) |
| 62 | + |
| 63 | + log.info(`Found ${tokenConnections.length} github-token-* connections in Nango`) |
| 64 | + |
| 65 | + // Extract installation IDs from connection names (e.g., github-token-52165842 -> 52165842) |
| 66 | + const nangoInstallationIds = tokenConnections |
| 67 | + .map((c) => { |
| 68 | + const match = c.connection_id.match(/^github-token-(\d+)$/i) |
| 69 | + return match ? match[1] : null |
| 70 | + }) |
| 71 | + .filter((id): id is string => id !== null) |
| 72 | + |
| 73 | + log.info(`Extracted ${nangoInstallationIds.length} installation IDs from Nango connections`) |
| 74 | + |
| 75 | + // Find all GitHub integrations in the database that are NOT in Nango |
| 76 | + const missingInstallations = await qx.select( |
| 77 | + ` |
| 78 | + SELECT |
| 79 | + id, |
| 80 | + "integrationIdentifier", |
| 81 | + "tenantId", |
| 82 | + "segmentId", |
| 83 | + status, |
| 84 | + "createdAt" |
| 85 | + FROM integrations |
| 86 | + WHERE platform = 'github' |
| 87 | + AND "deletedAt" IS NULL |
| 88 | + AND "integrationIdentifier" IS NOT NULL |
| 89 | + ${nangoInstallationIds.length > 0 ? `AND "integrationIdentifier" NOT IN ($(nangoInstallationIds:csv))` : ''} |
| 90 | + ORDER BY "createdAt" DESC |
| 91 | + `, |
| 92 | + { nangoInstallationIds }, |
| 93 | + ) |
| 94 | + |
| 95 | + log.info( |
| 96 | + `Found ${missingInstallations.length} GitHub installations in database that are NOT in Nango`, |
| 97 | + ) |
| 98 | + |
| 99 | + // Report existing github-token-* connections |
| 100 | + log.info('='.repeat(80)) |
| 101 | + log.info('EXISTING GITHUB TOKEN CONNECTIONS IN NANGO') |
| 102 | + log.info('='.repeat(80)) |
| 103 | + log.info(`Total: ${tokenConnections.length}`) |
| 104 | + |
| 105 | + log.info('') |
| 106 | + log.info('='.repeat(80)) |
| 107 | + log.info('MISSING GITHUB INSTALLATIONS (in DB but not in Nango)') |
| 108 | + log.info('='.repeat(80)) |
| 109 | + |
| 110 | + if (missingInstallations.length === 0) { |
| 111 | + log.info('All GitHub installations in the database are present in Nango.') |
| 112 | + log.info('='.repeat(80)) |
| 113 | + process.exit(0) |
| 114 | + } |
| 115 | + |
| 116 | + log.info(`Total missing: ${missingInstallations.length}`) |
| 117 | + log.info('') |
| 118 | + |
| 119 | + if (dryRunMode) { |
| 120 | + log.info('DRY-RUN: The following connections would be created:') |
| 121 | + for (const installation of missingInstallations) { |
| 122 | + log.info( |
| 123 | + { |
| 124 | + connectionId: `github-token-${installation.integrationIdentifier}`, |
| 125 | + installationId: installation.integrationIdentifier, |
| 126 | + integrationId: installation.id, |
| 127 | + tenantId: installation.tenantId, |
| 128 | + segmentId: installation.segmentId, |
| 129 | + status: installation.status, |
| 130 | + }, |
| 131 | + `Would create: github-token-${installation.integrationIdentifier}`, |
| 132 | + ) |
| 133 | + } |
| 134 | + log.info('') |
| 135 | + log.info('='.repeat(80)) |
| 136 | + log.info('To actually create these connections, run with --execute flag') |
| 137 | + log.info('='.repeat(80)) |
| 138 | + } else { |
| 139 | + log.info('EXECUTE MODE: Creating missing connections...') |
| 140 | + log.info('') |
| 141 | + |
| 142 | + let successCount = 0 |
| 143 | + let errorCount = 0 |
| 144 | + let lastLoggedPercent = -1 |
| 145 | + |
| 146 | + for (let i = 0; i < missingInstallations.length; i++) { |
| 147 | + const installation = missingInstallations[i] |
| 148 | + const installationId = installation.integrationIdentifier |
| 149 | + |
| 150 | + const percent = Math.floor(((i + 1) / missingInstallations.length) * 100) |
| 151 | + if (percent % 5 === 0 && percent !== lastLoggedPercent) { |
| 152 | + lastLoggedPercent = percent |
| 153 | + log.info(`Progress: ${i + 1}/${missingInstallations.length} (${percent}%)`) |
| 154 | + } |
| 155 | + |
| 156 | + try { |
| 157 | + log.info( |
| 158 | + { |
| 159 | + installationId, |
| 160 | + integrationId: installation.id, |
| 161 | + tenantId: installation.tenantId, |
| 162 | + }, |
| 163 | + `Creating connection: github-token-${installationId}`, |
| 164 | + ) |
| 165 | + |
| 166 | + const connectionId = await createNangoGithubTokenConnection(installationId, appId, clientId) |
| 167 | + |
| 168 | + log.info( |
| 169 | + { connectionId, installationId }, |
| 170 | + `Successfully created connection: ${connectionId}`, |
| 171 | + ) |
| 172 | + successCount++ |
| 173 | + } catch (err) { |
| 174 | + log.error( |
| 175 | + { |
| 176 | + installationId, |
| 177 | + integrationId: installation.id, |
| 178 | + err, |
| 179 | + }, |
| 180 | + `Failed to create connection for installation ${installationId}`, |
| 181 | + ) |
| 182 | + errorCount++ |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + log.info('') |
| 187 | + log.info('='.repeat(80)) |
| 188 | + log.info('SUMMARY') |
| 189 | + log.info('='.repeat(80)) |
| 190 | + log.info(`Successfully created: ${successCount}`) |
| 191 | + log.info(`Failed: ${errorCount}`) |
| 192 | + log.info(`Total attempted: ${missingInstallations.length}`) |
| 193 | + log.info('='.repeat(80)) |
| 194 | + } |
| 195 | + |
| 196 | + process.exit(0) |
| 197 | +}) |
0 commit comments