kaneo (minimalist kanban) fork to experiment adding a tangled integration
github.com/usekaneo/kaneo
1import { config } from "dotenv-mono";
2import { App } from "octokit";
3
4config();
5
6let githubAppInstance: App | null = null;
7
8export function getGithubApp(): App | null {
9 if (githubAppInstance) {
10 return githubAppInstance;
11 }
12
13 if (
14 !process.env.GITHUB_WEBHOOK_SECRET ||
15 !process.env.GITHUB_APP_ID ||
16 !process.env.GITHUB_PRIVATE_KEY
17 ) {
18 return null;
19 }
20
21 githubAppInstance = new App({
22 appId: process.env.GITHUB_APP_ID ?? "",
23 privateKey: process.env.GITHUB_PRIVATE_KEY ?? "",
24 webhooks: {
25 secret: process.env.GITHUB_WEBHOOK_SECRET ?? "",
26 },
27 });
28
29 return githubAppInstance;
30}
31
32export async function getInstallationOctokit(installationId: number) {
33 const app = getGithubApp();
34 if (!app) {
35 throw new Error("GitHub App not configured");
36 }
37 return app.getInstallationOctokit(installationId);
38}
39
40export async function getInstallationIdForRepo(
41 owner: string,
42 repo: string,
43): Promise<number> {
44 const app = getGithubApp();
45 if (!app) {
46 throw new Error("GitHub App not configured");
47 }
48
49 const { data: installation } =
50 await app.octokit.rest.apps.getRepoInstallation({
51 owner,
52 repo,
53 });
54
55 return installation.id;
56}