feat: add new git providers

This commit is contained in:
Mauricio Siu
2024-09-01 00:23:45 -06:00
parent 249fe8c7fe
commit c89e558143
4 changed files with 81 additions and 108 deletions

View File

@@ -79,7 +79,7 @@ export const SaveGithubProvider = ({ applicationId }: Props) => {
const githubProviderId = form.watch("githubProviderId");
const { data: repositories, isLoading: isLoadingRepositories } =
api.admin.getRepositories.useQuery({
api.gitProvider.getRepositories.useQuery({
githubProviderId,
});
@@ -87,7 +87,7 @@ export const SaveGithubProvider = ({ applicationId }: Props) => {
data: branches,
fetchStatus,
status,
} = api.admin.getBranches.useQuery(
} = api.gitProvider.getBranches.useQuery(
{
owner: repository?.owner,
repo: repository?.repo,

View File

@@ -86,96 +86,9 @@ export const adminRouter = createTRPCRouter({
}
}),
cleanGithubApp: adminProcedure.mutation(async ({ ctx }) => {
try {
return await updateAdmin(ctx.user.authId, {
githubAppName: "",
githubClientId: "",
githubClientSecret: "",
githubInstallationId: "",
});
} catch (error) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error to delete this github app",
cause: error,
});
}
}),
// haveGithubConfigured: protectedProcedure.query(async () => {
// const adminResponse = await findAdmin();
getRepositories: protectedProcedure
.input(
z.object({
githubProviderId: z.string().optional(),
}),
)
.query(async ({ input }) => {
if (!input.githubProviderId) {
return [];
}
const githubProvider = await getGithubProvider(input.githubProviderId);
const octokit = new Octokit({
authStrategy: createAppAuth,
auth: {
appId: githubProvider.githubAppId,
privateKey: githubProvider.githubPrivateKey,
installationId: githubProvider.githubInstallationId,
},
});
const repositories = (await octokit.paginate(
octokit.rest.apps.listReposAccessibleToInstallation,
)) as unknown as Awaited<
ReturnType<typeof octokit.rest.apps.listReposAccessibleToInstallation>
>["data"]["repositories"];
return repositories;
}),
getBranches: protectedProcedure
.input(apiGetBranches)
.query(async ({ input }) => {
// const admin = await findAdmin();
// const completeRequirements = haveGithubRequirements(admin);
// if (!completeRequirements) {
// throw new TRPCError({
// code: "BAD_REQUEST",
// message: "Admin need to setup correctly github account",
// });
// }
if (!input.githubProviderId) {
return [];
}
const githubProvider = await getGithubProvider(input.githubProviderId);
const octokit = new Octokit({
authStrategy: createAppAuth,
auth: {
appId: githubProvider.githubAppId,
privateKey: githubProvider.githubPrivateKey,
installationId: githubProvider.githubInstallationId,
},
});
const branches = (await octokit.paginate(
octokit.rest.repos.listBranches,
{
owner: input.owner,
repo: input.repo,
},
)) as unknown as Awaited<
ReturnType<typeof octokit.rest.repos.listBranches>
>["data"];
return branches;
}),
haveGithubConfigured: protectedProcedure.query(async () => {
const adminResponse = await findAdmin();
return haveGithubRequirements(adminResponse);
}),
// return haveGithubRequirements(adminResponse);
// }),
});

View File

@@ -3,12 +3,14 @@ import { db } from "@/server/db";
import {
apiCreateBitbucketProvider,
apiCreateGitlabProvider,
apiGetBranches,
} from "@/server/db/schema";
import { TRPCError } from "@trpc/server";
import {
createBitbucketProvider,
createGitlabProvider,
getBitbucketProvider,
getGithubProvider,
getGitlabProvider,
haveGithubRequirements,
removeGithubProvider,
@@ -18,6 +20,8 @@ import {
haveGitlabRequirements,
refreshGitlabToken,
} from "@/server/utils/providers/gitlab";
import { Octokit } from "octokit";
import { createAppAuth } from "@octokit/auth-app";
export const gitProvider = createTRPCRouter({
getAll: protectedProcedure.query(async () => {
@@ -329,6 +333,76 @@ export const gitProvider = createTRPCRouter({
throw error;
}
}),
getRepositories: protectedProcedure
.input(
z.object({
githubProviderId: z.string().optional(),
}),
)
.query(async ({ input }) => {
if (!input.githubProviderId) {
return [];
}
const githubProvider = await getGithubProvider(input.githubProviderId);
const octokit = new Octokit({
authStrategy: createAppAuth,
auth: {
appId: githubProvider.githubAppId,
privateKey: githubProvider.githubPrivateKey,
installationId: githubProvider.githubInstallationId,
},
});
const repositories = (await octokit.paginate(
octokit.rest.apps.listReposAccessibleToInstallation,
)) as unknown as Awaited<
ReturnType<typeof octokit.rest.apps.listReposAccessibleToInstallation>
>["data"]["repositories"];
return repositories;
}),
getBranches: protectedProcedure
.input(apiGetBranches)
.query(async ({ input }) => {
// const admin = await findAdmin();
// const completeRequirements = haveGithubRequirements(admin);
// if (!completeRequirements) {
// throw new TRPCError({
// code: "BAD_REQUEST",
// message: "Admin need to setup correctly github account",
// });
// }
if (!input.githubProviderId) {
return [];
}
const githubProvider = await getGithubProvider(input.githubProviderId);
const octokit = new Octokit({
authStrategy: createAppAuth,
auth: {
appId: githubProvider.githubAppId,
privateKey: githubProvider.githubPrivateKey,
installationId: githubProvider.githubInstallationId,
},
});
const branches = (await octokit.paginate(
octokit.rest.repos.listBranches,
{
owner: input.owner,
repo: input.repo,
},
)) as unknown as Awaited<
ReturnType<typeof octokit.rest.repos.listBranches>
>["data"];
return branches;
}),
});
// 1725175543
// {

View File

@@ -1,5 +1,5 @@
import { relations } from "drizzle-orm";
import { boolean, integer, pgTable, text } from "drizzle-orm/pg-core";
import { boolean, pgTable, text } from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";
import { nanoid } from "nanoid";
import { z } from "zod";
@@ -13,17 +13,9 @@ export const admins = pgTable("admin", {
.notNull()
.primaryKey()
.$defaultFn(() => nanoid()),
githubAppId: integer("githubAppId"),
githubAppName: text("githubAppName"),
serverIp: text("serverIp"),
certificateType: certificateType("certificateType").notNull().default("none"),
host: text("host"),
githubClientId: text("githubClientId"),
githubClientSecret: text("githubClientSecret"),
githubInstallationId: text("githubInstallationId"),
githubPrivateKey: text("githubPrivateKey"),
githubWebhookSecret: text("githubWebhookSecret"),
letsEncryptEmail: text("letsEncryptEmail"),
sshPrivateKey: text("sshPrivateKey"),
enableDockerCleanup: boolean("enableDockerCleanup").notNull().default(false),
@@ -46,12 +38,6 @@ export const adminsRelations = relations(admins, ({ one, many }) => ({
const createSchema = createInsertSchema(admins, {
adminId: z.string(),
githubAppName: z.string().optional(),
githubClientId: z.string().optional(),
githubClientSecret: z.string().optional(),
githubInstallationId: z.string().optional(),
githubPrivateKey: z.string().optional(),
githubAppId: z.number().optional(),
enableDockerCleanup: z.boolean().optional(),
sshPrivateKey: z.string().optional(),
certificateType: z.enum(["letsencrypt", "none"]).default("none"),