Skip to content

Commit

Permalink
fix(web): github app integration
Browse files Browse the repository at this point in the history
  • Loading branch information
cstrnt committed Aug 26, 2024
1 parent 74d0171 commit b7c25bf
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
6 changes: 4 additions & 2 deletions apps/web/src/api/routes/integrations.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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(),
})
);
}
)
Expand Down
15 changes: 8 additions & 7 deletions apps/web/src/server/common/github-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/server/trpc/router/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/server/trpc/router/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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] =
Expand Down

0 comments on commit b7c25bf

Please sign in to comment.