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

View File

@@ -86,96 +86,9 @@ export const adminRouter = createTRPCRouter({
} }
}), }),
cleanGithubApp: adminProcedure.mutation(async ({ ctx }) => { // haveGithubConfigured: protectedProcedure.query(async () => {
try { // const adminResponse = await findAdmin();
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,
});
}
}),
getRepositories: protectedProcedure // return haveGithubRequirements(adminResponse);
.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);
}),
}); });

View File

@@ -3,12 +3,14 @@ import { db } from "@/server/db";
import { import {
apiCreateBitbucketProvider, apiCreateBitbucketProvider,
apiCreateGitlabProvider, apiCreateGitlabProvider,
apiGetBranches,
} from "@/server/db/schema"; } from "@/server/db/schema";
import { TRPCError } from "@trpc/server"; import { TRPCError } from "@trpc/server";
import { import {
createBitbucketProvider, createBitbucketProvider,
createGitlabProvider, createGitlabProvider,
getBitbucketProvider, getBitbucketProvider,
getGithubProvider,
getGitlabProvider, getGitlabProvider,
haveGithubRequirements, haveGithubRequirements,
removeGithubProvider, removeGithubProvider,
@@ -18,6 +20,8 @@ import {
haveGitlabRequirements, haveGitlabRequirements,
refreshGitlabToken, refreshGitlabToken,
} from "@/server/utils/providers/gitlab"; } from "@/server/utils/providers/gitlab";
import { Octokit } from "octokit";
import { createAppAuth } from "@octokit/auth-app";
export const gitProvider = createTRPCRouter({ export const gitProvider = createTRPCRouter({
getAll: protectedProcedure.query(async () => { getAll: protectedProcedure.query(async () => {
@@ -329,6 +333,76 @@ export const gitProvider = createTRPCRouter({
throw error; 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 // 1725175543
// { // {

View File

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