diff --git a/apps/web/src/api/routes/integrations.ts b/apps/web/src/api/routes/integrations.ts index 1c402bb6..05d72291 100644 --- a/apps/web/src/api/routes/integrations.ts +++ b/apps/web/src/api/routes/integrations.ts @@ -1,7 +1,7 @@ import { zValidator } from "@hono/zod-validator"; import { authMiddleware } from "api/helpers"; import { Hono } from "hono"; -import { githubApp } from "server/common/github-app"; +import { getGithubApp } from "server/common/github-app"; import type { GithubIntegrationSettings } from "server/common/integrations"; import { prisma } from "server/db/client"; import { z } from "zod"; @@ -39,7 +39,9 @@ export function makeIntegrationsRoute() { searchParams.set("projectId", projectId); return c.redirect( - await githubApp.getInstallationUrl({ state: searchParams.toString() }) + await getGithubApp().getInstallationUrl({ + state: searchParams.toString(), + }) ); } ) diff --git a/apps/web/src/server/common/github-app.ts b/apps/web/src/server/common/github-app.ts index 915dc0c2..4942c528 100644 --- a/apps/web/src/server/common/github-app.ts +++ b/apps/web/src/server/common/github-app.ts @@ -9,17 +9,18 @@ if ( throw new Error("Missing required environment variables for GitHub App"); } -export const githubApp = new App({ - // biome-ignore lint/style/noNonNullAssertion: we check above - appId: env.GITHUB_APP_ID!, - // biome-ignore lint/style/noNonNullAssertion: we check above - privateKey: env.GITHUB_APP_PRIVATE_KEY!, -}); +export const getGithubApp = () => + new App({ + // biome-ignore lint/style/noNonNullAssertion: we check above + appId: env.GITHUB_APP_ID!, + // biome-ignore lint/style/noNonNullAssertion: we check above + privateKey: env.GITHUB_APP_PRIVATE_KEY!, + }); const PER_PAGE = 100; export const getAllRepositoriesForInstallation = memoize( async (installationId: number) => { - const gh = await githubApp.getInstallationOctokit(installationId); + const gh = await getGithubApp().getInstallationOctokit(installationId); let count = 0; const res = await gh.request("GET /installation/repositories", { installation_id: installationId, diff --git a/apps/web/src/server/trpc/router/flags.ts b/apps/web/src/server/trpc/router/flags.ts index 8b694c31..dce1c438 100644 --- a/apps/web/src/server/trpc/router/flags.ts +++ b/apps/web/src/server/trpc/router/flags.ts @@ -5,7 +5,7 @@ import { getUseFeatureFlagRegex } from "@tryabby/core"; import { env } from "env/server.mjs"; import OpenAI from "openai"; import { ConfigCache } from "server/common/config-cache"; -import { githubApp } from "server/common/github-app"; +import { getGithubApp } from "server/common/github-app"; import { githubIntegrationSettingsSchema } from "server/common/integrations"; import { AIFlagRemovalService } from "server/services/AiFlagRemovalService"; import { FlagService } from "server/services/FlagService"; @@ -354,7 +354,7 @@ export const flagRouter = router({ const parsedIntegration = githubIntegrationSettingsSchema.parse( integration.settings ); - const gh = await githubApp.getInstallationOctokit( + const gh = await getGithubApp().getInstallationOctokit( parsedIntegration.installationId ); const repositoryId = parsedIntegration.repositoryIds[0]; diff --git a/apps/web/src/server/trpc/router/project.ts b/apps/web/src/server/trpc/router/project.ts index 0573d3ac..f5d29ed1 100644 --- a/apps/web/src/server/trpc/router/project.ts +++ b/apps/web/src/server/trpc/router/project.ts @@ -16,7 +16,7 @@ import dayjs from "dayjs"; import { assertUserHasAcessToProject } from "server/common/auth"; import { getAllRepositoriesForInstallation, - githubApp, + getGithubApp, } from "server/common/github-app"; import { githubIntegrationSettingsSchema } from "server/common/integrations"; import { match } from "ts-pattern"; @@ -344,7 +344,7 @@ export const projectRouter = router({ const integration = await githubIntegrationSettingsSchema.parseAsync(i.settings); - const gh = await githubApp.getInstallationOctokit( + const gh = await getGithubApp().getInstallationOctokit( integration.installationId ); const [potentialRepositories, ...installedRepos] =