From c89e558143ada3ed08b3c64f16e3b372bfae7c6c Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Sun, 1 Sep 2024 00:23:45 -0600 Subject: [PATCH] feat: add new git providers --- .../general/generic/save-github-provider.tsx | 4 +- apps/dokploy/server/api/routers/admin.ts | 95 +------------------ .../server/api/routers/git-provider.ts | 74 +++++++++++++++ apps/dokploy/server/db/schema/admin.ts | 16 +--- 4 files changed, 81 insertions(+), 108 deletions(-) diff --git a/apps/dokploy/components/dashboard/application/general/generic/save-github-provider.tsx b/apps/dokploy/components/dashboard/application/general/generic/save-github-provider.tsx index 2c43e02f..6eebad04 100644 --- a/apps/dokploy/components/dashboard/application/general/generic/save-github-provider.tsx +++ b/apps/dokploy/components/dashboard/application/general/generic/save-github-provider.tsx @@ -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, diff --git a/apps/dokploy/server/api/routers/admin.ts b/apps/dokploy/server/api/routers/admin.ts index 8a01b5d7..fbb22356 100644 --- a/apps/dokploy/server/api/routers/admin.ts +++ b/apps/dokploy/server/api/routers/admin.ts @@ -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 - >["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 - >["data"]; - - return branches; - }), - haveGithubConfigured: protectedProcedure.query(async () => { - const adminResponse = await findAdmin(); - - return haveGithubRequirements(adminResponse); - }), + // return haveGithubRequirements(adminResponse); + // }), }); diff --git a/apps/dokploy/server/api/routers/git-provider.ts b/apps/dokploy/server/api/routers/git-provider.ts index dbd96c4f..44e255eb 100644 --- a/apps/dokploy/server/api/routers/git-provider.ts +++ b/apps/dokploy/server/api/routers/git-provider.ts @@ -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 + >["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 + >["data"]; + + return branches; + }), }); // 1725175543 // { diff --git a/apps/dokploy/server/db/schema/admin.ts b/apps/dokploy/server/db/schema/admin.ts index 52164f36..6783ff43 100644 --- a/apps/dokploy/server/db/schema/admin.ts +++ b/apps/dokploy/server/db/schema/admin.ts @@ -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"),