37 lines
1.2 KiB
TypeScript
37 lines
1.2 KiB
TypeScript
"use server";
|
|
|
|
import { getGithubAccount } from "./account";
|
|
import { generateGithubJWT } from "./jwt";
|
|
|
|
export async function getInstallationId(userId: string, username: string): Promise<string | null> {
|
|
const account = await getGithubAccount(userId);
|
|
if(!account) return null;
|
|
|
|
const installations = await fetch('https://api.github.com/users/' + username + '/installation', {
|
|
headers: {
|
|
Authorization: `Bearer ${await generateGithubJWT()}`,
|
|
Accept: "application/vnd.github+json",
|
|
"X-GitHub-Api-Version": "2022-11-28",
|
|
},
|
|
method: 'GET',
|
|
}).then(res => res.json());
|
|
|
|
return installations.id as string;
|
|
|
|
}
|
|
|
|
export async function getInstallationAccessToken(userId: string, username: string) {
|
|
const installationId = await getInstallationId(userId, username);
|
|
if(!installationId) return null;
|
|
|
|
const token = await fetch(`https://api.github.com/app/installations/${installationId}/access_tokens`, {
|
|
headers: {
|
|
Authorization: `Bearer ${await generateGithubJWT()}`,
|
|
Accept: "application/vnd.github+json",
|
|
"X-GitHub-Api-Version": "2022-11-28",
|
|
},
|
|
method: 'POST',
|
|
}).then(res => res.json());
|
|
|
|
return token.token as string;
|
|
} |