refactor: cleanup git provider router

This commit is contained in:
Mauricio Siu 2024-09-01 19:47:21 -06:00
parent 32ebd9b3b9
commit d261fd4efe
11 changed files with 3734 additions and 89 deletions

View File

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

View File

@ -0,0 +1,74 @@
ALTER TABLE "bitbucket_provider" RENAME TO "bitbucket";--> statement-breakpoint
ALTER TABLE "github_provider" RENAME TO "github";--> statement-breakpoint
ALTER TABLE "gitlab_provider" RENAME TO "gitlab";--> statement-breakpoint
ALTER TABLE "application" DROP CONSTRAINT "application_githubId_github_provider_githubId_fk";
--> statement-breakpoint
ALTER TABLE "application" DROP CONSTRAINT "application_gitlabId_gitlab_provider_gitlabId_fk";
--> statement-breakpoint
ALTER TABLE "application" DROP CONSTRAINT "application_bitbucketId_bitbucket_provider_bitbucketId_fk";
--> statement-breakpoint
ALTER TABLE "compose" DROP CONSTRAINT "compose_githubId_github_provider_githubId_fk";
--> statement-breakpoint
ALTER TABLE "compose" DROP CONSTRAINT "compose_gitlabId_gitlab_provider_gitlabId_fk";
--> statement-breakpoint
ALTER TABLE "compose" DROP CONSTRAINT "compose_bitbucketId_bitbucket_provider_bitbucketId_fk";
--> statement-breakpoint
ALTER TABLE "bitbucket" DROP CONSTRAINT "bitbucket_provider_gitProviderId_git_provider_gitProviderId_fk";
--> statement-breakpoint
ALTER TABLE "github" DROP CONSTRAINT "github_provider_gitProviderId_git_provider_gitProviderId_fk";
--> statement-breakpoint
ALTER TABLE "gitlab" DROP CONSTRAINT "gitlab_provider_gitProviderId_git_provider_gitProviderId_fk";
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "application" ADD CONSTRAINT "application_githubId_github_githubId_fk" FOREIGN KEY ("githubId") REFERENCES "public"."github"("githubId") ON DELETE set null ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "application" ADD CONSTRAINT "application_gitlabId_gitlab_gitlabId_fk" FOREIGN KEY ("gitlabId") REFERENCES "public"."gitlab"("gitlabId") ON DELETE set null ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "application" ADD CONSTRAINT "application_bitbucketId_bitbucket_bitbucketId_fk" FOREIGN KEY ("bitbucketId") REFERENCES "public"."bitbucket"("bitbucketId") ON DELETE set null ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "compose" ADD CONSTRAINT "compose_githubId_github_githubId_fk" FOREIGN KEY ("githubId") REFERENCES "public"."github"("githubId") ON DELETE set null ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "compose" ADD CONSTRAINT "compose_gitlabId_gitlab_gitlabId_fk" FOREIGN KEY ("gitlabId") REFERENCES "public"."gitlab"("gitlabId") ON DELETE set null ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "compose" ADD CONSTRAINT "compose_bitbucketId_bitbucket_bitbucketId_fk" FOREIGN KEY ("bitbucketId") REFERENCES "public"."bitbucket"("bitbucketId") ON DELETE set null ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "bitbucket" ADD CONSTRAINT "bitbucket_gitProviderId_git_provider_gitProviderId_fk" FOREIGN KEY ("gitProviderId") REFERENCES "public"."git_provider"("gitProviderId") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "github" ADD CONSTRAINT "github_gitProviderId_git_provider_gitProviderId_fk" FOREIGN KEY ("gitProviderId") REFERENCES "public"."git_provider"("gitProviderId") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "gitlab" ADD CONSTRAINT "gitlab_gitProviderId_git_provider_gitProviderId_fk" FOREIGN KEY ("gitProviderId") REFERENCES "public"."git_provider"("gitProviderId") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;

File diff suppressed because it is too large Load Diff

View File

@ -386,6 +386,13 @@
"when": 1725238308102,
"tag": "0054_short_hellfire_club",
"breakpoints": true
},
{
"idx": 55,
"version": "6",
"when": 1725240886749,
"tag": "0055_nice_king_cobra",
"breakpoints": true
}
]
}

View File

@ -3,14 +3,23 @@ import { db } from "@/server/db";
import {
apiCreateBitbucket,
apiCreateGitlab,
apiGetBranches,
apiFindBitbucketBranches,
apiFindGithubBranches,
apiFindGitlabBranches,
apiFindOneBitbucket,
apiFindOneGithub,
apiFindOneGitlab,
apiRemoveGitProvider,
} from "@/server/db/schema";
import { TRPCError } from "@trpc/server";
import {
createBitbucket,
createGitlab,
findBitbucketById,
findGithubById,
findGitlabById,
haveGithubRequirements,
removeGithub,
removeGitProvider,
} from "../services/git-provider";
import { z } from "zod";
import {
@ -37,11 +46,26 @@ export const gitProvider = createTRPCRouter({
},
});
}),
oneGithub: protectedProcedure
.input(apiFindOneGithub)
.query(async ({ input }) => {
return await findGithubById(input.githubId);
}),
oneGitlab: protectedProcedure
.input(apiFindOneGitlab)
.query(async ({ input }) => {
return await findGitlabById(input.gitlabId);
}),
oneBitbucket: protectedProcedure
.input(apiFindOneBitbucket)
.query(async ({ input }) => {
return await findBitbucketById(input.bitbucketId);
}),
remove: protectedProcedure
.input(z.object({ gitProviderId: z.string() }))
.input(apiRemoveGitProvider)
.mutation(async ({ input }) => {
try {
return await removeGithub(input.gitProviderId);
return await removeGitProvider(input.gitProviderId);
} catch (error) {
throw new TRPCError({
code: "BAD_REQUEST",
@ -127,64 +151,34 @@ export const gitProvider = createTRPCRouter({
}),
getGitlabRepositories: protectedProcedure
.input(
z.object({
gitlabId: z.string().optional(),
}),
)
.input(apiFindOneGitlab)
.query(async ({ input }) => {
return await getGitlabRepositories(input);
return await getGitlabRepositories(input.gitlabId);
}),
getGitlabBranches: protectedProcedure
.input(
z.object({
id: z.number().nullable(),
owner: z.string(),
repo: z.string(),
gitlabId: z.string().optional(),
}),
)
.input(apiFindGitlabBranches)
.query(async ({ input }) => {
return await getGitlabBranches(input);
}),
getBitbucketRepositories: protectedProcedure
.input(
z.object({
bitbucketId: z.string().optional(),
}),
)
.input(apiFindOneBitbucket)
.query(async ({ input }) => {
return await getBitbucketRepositories(input);
return await getBitbucketRepositories(input.bitbucketId);
}),
getBitbucketBranches: protectedProcedure
.input(
z.object({
owner: z.string(),
repo: z.string(),
bitbucketId: z.string().optional(),
}),
)
.input(apiFindBitbucketBranches)
.query(async ({ input }) => {
return await getBitbucketBranches(input);
}),
getRepositories: protectedProcedure
.input(
z.object({
githubId: z.string().optional(),
}),
)
getGithubRepositories: protectedProcedure
.input(apiFindOneGithub)
.query(async ({ input }) => {
return await getGithubRepositories(input);
return await getGithubRepositories(input.githubId);
}),
getBranches: protectedProcedure
.input(apiGetBranches)
getGithubBranches: protectedProcedure
.input(apiFindGithubBranches)
.query(async ({ input }) => {
return await getGithubBranches(input);
}),
// getGithub: protectedProcedure
// .input(apiGetGithub)
// .query(async ({ input }) => {
// return await findGithub(input);
// }),
});

View File

@ -106,7 +106,7 @@ export const createBitbucket = async (
});
};
export const removeGithub = async (gitProviderId: string) => {
export const removeGitProvider = async (gitProviderId: string) => {
const result = await db
.delete(gitProvider)
.where(eq(gitProvider.gitProviderId, gitProviderId))

View File

@ -68,11 +68,6 @@ export const apiTraefikConfig = z.object({
traefikConfig: z.string().min(1),
});
export const apiGetBranches = z.object({
repo: z.string().min(1),
owner: z.string().min(1),
githubId: z.string().optional(),
});
export const apiModifyTraefikConfig = z.object({
path: z.string().min(1),
traefikConfig: z.string().min(1),

View File

@ -115,6 +115,12 @@ export const bitbucketProviderRelations = relations(bitbucket, ({ one }) => ({
const createSchema = createInsertSchema(gitProvider);
export const apiRemoveGitProvider = createSchema
.extend({
gitProviderId: z.string().min(1),
})
.pick({ gitProviderId: true });
export const apiCreateGithub = createSchema.extend({
githubAppName: z.string().optional(),
githubAppId: z.number().optional(),
@ -126,6 +132,18 @@ export const apiCreateGithub = createSchema.extend({
gitProviderId: z.string().optional(),
});
export const apiFindGithubBranches = z.object({
repo: z.string().min(1),
owner: z.string().min(1),
githubId: z.string().optional(),
});
export const apiFindOneGithub = createSchema
.extend({
githubId: z.string().min(1),
})
.pick({ githubId: true });
export const apiCreateGitlab = createSchema.extend({
applicationId: z.string().optional(),
secret: z.string().optional(),
@ -134,9 +152,45 @@ export const apiCreateGitlab = createSchema.extend({
redirectUri: z.string().optional(),
});
export const apiFindOneGitlab = createSchema
.extend({
gitlabId: z.string().min(1),
})
.pick({ gitlabId: true });
export const apiFindGitlabBranches = z.object({
id: z.number().nullable(),
owner: z.string(),
repo: z.string(),
gitlabId: z.string().optional(),
});
export const apiCreateBitbucket = createSchema.extend({
bitbucketUsername: z.string().optional(),
appPassword: z.string().optional(),
bitbucketWorkspaceName: z.string().optional(),
gitProviderId: z.string().optional(),
});
export const apiFindOneBitbucket = createSchema
.extend({
bitbucketId: z.string().min(1),
})
.pick({ bitbucketId: true });
export const apiFindBitbucketBranches = z.object({
owner: z.string(),
repo: z.string(),
bitbucketId: z.string().optional(),
});
export const apiUpdateBitbucket = createSchema.extend({
bitbucketUsername: z.string().optional(),
bitbucketWorkspaceName: z.string().optional(),
});
export const apiUpdateGitlab = createSchema.extend({
applicationId: z.string().optional(),
secret: z.string().optional(),
groupName: z.string().optional(),
redirectUri: z.string().optional(),
});

View File

@ -7,6 +7,7 @@ import { spawnAsync } from "../process/spawnAsync";
import type { InferResultType } from "@/server/types/with";
import { findBitbucketById } from "@/server/api/services/git-provider";
import type { Compose } from "@/server/api/services/compose";
import type { apiFindBitbucketBranches } from "@/server/db/schema";
export type ApplicationWithBitbucket = InferResultType<
"applications",
@ -113,17 +114,11 @@ export const cloneRawBitbucketRepository = async (entity: Compose) => {
}
};
interface GetBitbucketRepositories {
bitbucketId?: string;
}
export const getBitbucketRepositories = async (
input: GetBitbucketRepositories,
) => {
if (!input.bitbucketId) {
export const getBitbucketRepositories = async (bitbucketId?: string) => {
if (!bitbucketId) {
return [];
}
const bitbucketProvider = await findBitbucketById(input.bitbucketId);
const bitbucketProvider = await findBitbucketById(bitbucketId);
const username =
bitbucketProvider.bitbucketWorkspaceName ||
@ -169,13 +164,9 @@ export const getBitbucketRepositories = async (
}
};
interface GetBitbucketBranches {
owner: string;
repo: string;
bitbucketId?: string;
}
export const getBitbucketBranches = async (input: GetBitbucketBranches) => {
export const getBitbucketBranches = async (
input: typeof apiFindBitbucketBranches._type,
) => {
if (!input.bitbucketId) {
return [];
}

View File

@ -12,6 +12,7 @@ import {
type Github,
} from "@/server/api/services/git-provider";
import type { Compose } from "@/server/api/services/compose";
import type { apiFindGithubBranches } from "@/server/db/schema";
export const authGithub = (githubProvider: Github) => {
if (!haveGithubRequirements(githubProvider)) {
@ -176,16 +177,12 @@ export const cloneRawGithubRepository = async (entity: Compose) => {
}
};
interface GetGithubRepositories {
githubId?: string;
}
export const getGithubRepositories = async (input: GetGithubRepositories) => {
if (!input.githubId) {
export const getGithubRepositories = async (githubId?: string) => {
if (!githubId) {
return [];
}
const githubProvider = await findGithubById(input.githubId);
const githubProvider = await findGithubById(githubId);
const octokit = new Octokit({
authStrategy: createAppAuth,
@ -205,13 +202,9 @@ export const getGithubRepositories = async (input: GetGithubRepositories) => {
return repositories;
};
interface GetGithubBranches {
owner: string;
repo: string;
githubId?: string;
}
export const getGithubBranches = async (input: GetGithubBranches) => {
export const getGithubBranches = async (
input: typeof apiFindGithubBranches._type,
) => {
if (!input.githubId) {
return [];
}

View File

@ -150,16 +150,14 @@ export const cloneGitlabRepository = async (
}
};
export const getGitlabRepositories = async (input: {
gitlabId?: string;
}) => {
if (!input.gitlabId) {
export const getGitlabRepositories = async (gitlabId?: string) => {
if (!gitlabId) {
return [];
}
await refreshGitlabToken(input.gitlabId);
await refreshGitlabToken(gitlabId);
const gitlabProvider = await findGitlabById(input.gitlabId);
const gitlabProvider = await findGitlabById(gitlabId);
const response = await fetch(
`https://gitlab.com/api/v4/projects?membership=true&owned=true&page=${0}&per_page=${100}`,