add: github api sdk
This commit is contained in:
parent
957d49c976
commit
4ec999d3a7
|
@ -0,0 +1,13 @@
|
||||||
|
"use server";
|
||||||
|
|
||||||
|
import prisma from "@/lib/prisma";
|
||||||
|
import { Account } from "@prisma/client";
|
||||||
|
|
||||||
|
export async function getGithubAccount(userId: string): Promise<Account | null> {
|
||||||
|
return prisma.account.findFirst({
|
||||||
|
where: {
|
||||||
|
userId: userId,
|
||||||
|
provider: "github",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
"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;
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
"use server";
|
||||||
|
|
||||||
|
import { sign } from 'jsonwebtoken';
|
||||||
|
|
||||||
|
export async function generateGithubJWT() {
|
||||||
|
const jwt = sign({ }, process.env.GITHUB_APP_PRIVATE_KEY as string, { algorithm: 'RS256', issuer: process.env.GITHUB_APP_ID, expiresIn: 5*60})
|
||||||
|
return jwt;
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
"use server";
|
||||||
|
|
||||||
|
import { getInstallationAccessToken } from "./installation";
|
||||||
|
|
||||||
|
export type GithubRepository = {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
full_name: string;
|
||||||
|
owner: {
|
||||||
|
login: string;
|
||||||
|
id: number;
|
||||||
|
type: string;
|
||||||
|
};
|
||||||
|
private: boolean;
|
||||||
|
html_url: string;
|
||||||
|
description: string;
|
||||||
|
fork: boolean;
|
||||||
|
url: string;
|
||||||
|
created_at: string;
|
||||||
|
updated_at: string;
|
||||||
|
pushed_at: string;
|
||||||
|
homepage: string;
|
||||||
|
size: number;
|
||||||
|
stargazers_count: number;
|
||||||
|
watchers_count: number;
|
||||||
|
language: string;
|
||||||
|
forks_count: number;
|
||||||
|
open_issues_count: number;
|
||||||
|
default_branch: string;
|
||||||
|
permissions: {
|
||||||
|
admin: boolean;
|
||||||
|
push: boolean;
|
||||||
|
pull: boolean;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function getUserRepository(userId: string, username: string): Promise<GithubRepository[]> {
|
||||||
|
const repositories = await fetch('https://api.github.com/installation/repositories', {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${await getInstallationAccessToken(userId, username)}`,
|
||||||
|
Accept: "application/vnd.github+json",
|
||||||
|
"X-GitHub-Api-Version": "2022-11-28",
|
||||||
|
},
|
||||||
|
method: 'GET',
|
||||||
|
}).then(res => res.json());
|
||||||
|
|
||||||
|
return repositories.repositories as GithubRepository[];
|
||||||
|
}
|
Loading…
Reference in New Issue