(db: PrismaClient)
| 43 | } |
| 44 | |
| 45 | public async init(db: PrismaClient) { |
| 46 | this.db = db; |
| 47 | const config = await loadConfig(env.CONFIG_PATH); |
| 48 | if (!config.apps) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | const githubApps = config.apps.filter(app => app.type === 'github') as GitHubAppConfig[]; |
| 53 | logger.info(`Found ${githubApps.length} GitHub apps in config`); |
| 54 | |
| 55 | for (const app of githubApps) { |
| 56 | const deploymentHostname = app.deploymentHostname as string || GITHUB_DEFAULT_DEPLOYMENT_HOSTNAME; |
| 57 | const privateKey = await getTokenFromConfig(app.privateKey); |
| 58 | |
| 59 | const octokitApp = new App({ |
| 60 | appId: Number(app.id), |
| 61 | privateKey: privateKey, |
| 62 | }); |
| 63 | this.octokitApps.set(Number(app.id), octokitApp); |
| 64 | |
| 65 | const installations = await octokitApp.octokit.request("GET /app/installations"); |
| 66 | logger.info(`Found ${installations.data.length} GitHub App installations for ${deploymentHostname}/${app.id}:`); |
| 67 | |
| 68 | for (const installationData of installations.data) { |
| 69 | if (!installationData.account || !installationData.account.login || !installationData.account.type) { |
| 70 | logger.warn(`Skipping installation ${installationData.id}: missing account data (${installationData.account})`); |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | logger.info(`\tInstallation ID: ${installationData.id}, Account: ${installationData.account.login}, Type: ${installationData.account.type}`); |
| 75 | |
| 76 | const owner = installationData.account.login; |
| 77 | const accountType = installationData.account.type.toLowerCase() as 'organization' | 'user'; |
| 78 | const installation: Installation = { |
| 79 | id: installationData.id, |
| 80 | appId: Number(app.id), |
| 81 | account: { |
| 82 | login: owner, |
| 83 | type: accountType, |
| 84 | }, |
| 85 | }; |
| 86 | this.installationMap.set(this.generateMapKey(owner, deploymentHostname), installation); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | this.initialized = true; |
| 91 | } |
| 92 | |
| 93 | public async getInstallationToken(owner: string, deploymentHostname: string = GITHUB_DEFAULT_DEPLOYMENT_HOSTNAME): Promise<string> { |
| 94 | this.ensureInitialized(); |
no test coverage detected