Skip to content

Commit

Permalink
feat(web): add github app uninstallation
Browse files Browse the repository at this point in the history
  • Loading branch information
cstrnt committed Aug 29, 2024
1 parent d29e448 commit b4bdd8e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@monaco-editor/react": "^4.5.1",
"@next-auth/prisma-adapter": "1.0.5",
"@next/mdx": "14.0.4",
"@octokit/webhooks": "^13.3.0",
"@prisma/client": "5.19.0",
"@radix-ui/react-avatar": "^1.0.3",
"@radix-ui/react-dialog": "^1.0.5",
Expand Down
40 changes: 39 additions & 1 deletion apps/web/src/api/routes/integrations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { zValidator } from "@hono/zod-validator";
import { Webhooks } from "@octokit/webhooks";
import { authMiddleware } from "api/helpers";
import { env } from "env/server.mjs";
import { Hono } from "hono";
Expand Down Expand Up @@ -91,7 +92,44 @@ export function makeIntegrationsRoute() {
} satisfies GithubIntegrationSettings,
},
});
return c.redirect(`/projects/${project.id}/settings`);
return c.redirect(
`${env.NEXTAUTH_URL}/projects/${project.id}/settings`
);
}
)
.post(
"/github/webhook",

async (c) => {
if (!env.GITHUB_APP_WEBHOOK_SECRET) {
return c.json({ message: "Webhook secret not set" }, { status: 500 });
}
const webhooks = new Webhooks({
secret: env.GITHUB_APP_WEBHOOK_SECRET,
});

webhooks.on("installation.deleted", async ({ payload }) => {
const integration = await prisma.integration.findFirst({
where: {
settings: {
path: "$.installationId",
equals: payload.installation?.id,
},
},
});

if (!integration) return;
await prisma.integration.delete({ where: { id: integration.id } });
});

await webhooks.verifyAndReceive({
id: c.req.header("X-GitHub-Delivery") as string,
// biome-ignore lint/suspicious/noExplicitAny: we don't care about the type here
name: c.req.header("X-GitHub-Event") as any,
signature: c.req.header("X-Hub-Signature-256") as string,
payload: await c.req.text(),
});
return c.json({ message: "ok" });
}
);
}
2 changes: 1 addition & 1 deletion apps/web/src/components/settings/Integrations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function Integrations({ projectId }: { projectId: string | undefined }) {
{selectedRepository.name}
</span>
) : (
"Select framework..."
"Select repository..."
)}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/env/schema.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const serverSchema = z.object({
.transform((s) =>
s ? Buffer.from(s, "base64").toString("ascii") : undefined
),
GITHUB_APP_WEBHOOK_SECRET: z.string().optional(),
OPENAI_API_KEY: z.string().optional(),
});

Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b4bdd8e

Please sign in to comment.