65 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-02-10 15:47:44 +01:00
import { decrypt, encrypt } from '$lib/crypto';
import { prisma } from './common';
2022-04-06 19:56:47 +02:00
import type { GithubApp } from '@prisma/client';
2022-02-10 15:47:44 +01:00
2022-04-06 19:56:47 +02:00
// TODO: We should change installation_id to be camelCase
export async function addInstallation({
gitSourceId,
installation_id
}: {
gitSourceId: string;
installation_id: string;
}): Promise<GithubApp> {
2022-02-10 15:47:44 +01:00
const source = await prisma.gitSource.findUnique({
where: { id: gitSourceId },
include: { githubApp: true }
});
return await prisma.githubApp.update({
where: { id: source.githubAppId },
data: { installationId: Number(installation_id) }
});
}
2022-04-06 19:56:47 +02:00
export async function getUniqueGithubApp({
githubAppId
}: {
githubAppId: string;
}): Promise<GithubApp> {
const body = await prisma.githubApp.findUnique({ where: { id: githubAppId } });
2022-02-10 15:47:44 +01:00
if (body.privateKey) body.privateKey = decrypt(body.privateKey);
return body;
}
export async function createGithubApp({
id,
client_id,
slug,
client_secret,
pem,
webhook_secret,
state
2022-04-06 19:56:47 +02:00
}: {
id: number;
client_id: string;
slug: string;
client_secret: string;
pem: string;
webhook_secret: string;
state: string;
}): Promise<GithubApp> {
2022-02-10 15:47:44 +01:00
const encryptedClientSecret = encrypt(client_secret);
const encryptedWebhookSecret = encrypt(webhook_secret);
const encryptedPem = encrypt(pem);
return await prisma.githubApp.create({
data: {
appId: id,
name: slug,
clientId: client_id,
clientSecret: encryptedClientSecret,
webhookSecret: encryptedWebhookSecret,
privateKey: encryptedPem,
gitSource: { connect: { id: state } }
}
});
}