refactor: simplify database colums

This commit is contained in:
Mauricio Siu 2024-09-01 00:48:47 -06:00
parent 73efe0d0ed
commit 766b166bf2
10 changed files with 70 additions and 76 deletions

View File

@ -47,7 +47,7 @@ const BitbucketProviderSchema = z.object({
})
.required(),
branch: z.string().min(1, "Branch is required"),
bitbucketProviderId: z.string().min(1, "Bitbucket Provider is required"),
bitbucketId: z.string().min(1, "Bitbucket Provider is required"),
});
type BitbucketProvider = z.infer<typeof BitbucketProviderSchema>;
@ -71,14 +71,14 @@ export const SaveBitbucketProvider = ({ applicationId }: Props) => {
owner: "",
repo: "",
},
bitbucketProviderId: "",
bitbucketId: "",
branch: "",
},
resolver: zodResolver(BitbucketProviderSchema),
});
const repository = form.watch("repository");
const bitbucketProviderId = form.watch("bitbucketProviderId");
const bitbucketId = form.watch("bitbucketId");
const {
data: repositories,
@ -86,7 +86,7 @@ export const SaveBitbucketProvider = ({ applicationId }: Props) => {
error,
isError,
} = api.gitProvider.getBitbucketRepositories.useQuery({
bitbucketProviderId,
bitbucketId,
});
const {
@ -97,11 +97,10 @@ export const SaveBitbucketProvider = ({ applicationId }: Props) => {
{
owner: repository?.owner,
repo: repository?.repo,
bitbucketProviderId,
bitbucketId,
},
{
enabled:
!!repository?.owner && !!repository?.repo && !!bitbucketProviderId,
enabled: !!repository?.owner && !!repository?.repo && !!bitbucketId,
},
);
@ -114,7 +113,7 @@ export const SaveBitbucketProvider = ({ applicationId }: Props) => {
owner: data.bitbucketOwner || "",
},
buildPath: data.bitbucketBuildPath || "/",
bitbucketProviderId: data.bitbucketProviderId || "",
bitbucketId: data.bitbucketId || "",
});
}
}, [form.reset, data, form]);
@ -125,7 +124,7 @@ export const SaveBitbucketProvider = ({ applicationId }: Props) => {
bitbucketRepository: data.repository.repo,
bitbucketOwner: data.repository.owner,
bitbucketBuildPath: data.buildPath,
bitbucketProviderId: data.bitbucketProviderId,
bitbucketId: data.bitbucketId,
applicationId,
})
.then(async () => {
@ -150,7 +149,7 @@ export const SaveBitbucketProvider = ({ applicationId }: Props) => {
<div className="grid md:grid-cols-2 gap-4">
<FormField
control={form.control}
name="bitbucketProviderId"
name="bitbucketId"
render={({ field }) => (
<FormItem className="md:col-span-2 flex flex-col">
<FormLabel>Bitbucket Account</FormLabel>
@ -174,8 +173,8 @@ export const SaveBitbucketProvider = ({ applicationId }: Props) => {
<SelectContent>
{bitbucketProviders?.map((bitbucketProvider) => (
<SelectItem
key={bitbucketProvider.bitbucketProviderId}
value={bitbucketProvider.bitbucketProviderId}
key={bitbucketProvider.bitbucketId}
value={bitbucketProvider.bitbucketId}
>
{bitbucketProvider.gitProvider.name}
</SelectItem>

View File

@ -46,7 +46,7 @@ const GithubProviderSchema = z.object({
})
.required(),
branch: z.string().min(1, "Branch is required"),
githubProviderId: z.string().min(1, "Github Provider is required"),
githubId: z.string().min(1, "Github Provider is required"),
});
type GithubProvider = z.infer<typeof GithubProviderSchema>;
@ -69,18 +69,18 @@ export const SaveGithubProvider = ({ applicationId }: Props) => {
owner: "",
repo: "",
},
githubProviderId: "",
githubId: "",
branch: "",
},
resolver: zodResolver(GithubProviderSchema),
});
const repository = form.watch("repository");
const githubProviderId = form.watch("githubProviderId");
const githubId = form.watch("githubId");
const { data: repositories, isLoading: isLoadingRepositories } =
api.gitProvider.getRepositories.useQuery({
githubProviderId,
githubId,
});
const {
@ -91,10 +91,10 @@ export const SaveGithubProvider = ({ applicationId }: Props) => {
{
owner: repository?.owner,
repo: repository?.repo,
githubProviderId,
githubId,
},
{
enabled: !!repository?.owner && !!repository?.repo && !!githubProviderId,
enabled: !!repository?.owner && !!repository?.repo && !!githubId,
},
);
@ -107,7 +107,7 @@ export const SaveGithubProvider = ({ applicationId }: Props) => {
owner: data.owner || "",
},
buildPath: data.buildPath || "/",
githubProviderId: data.githubProviderId || "",
githubId: data.githubId || "",
});
}
}, [form.reset, data, form]);
@ -119,7 +119,7 @@ export const SaveGithubProvider = ({ applicationId }: Props) => {
applicationId,
owner: data.repository.owner,
buildPath: data.buildPath,
githubProviderId: data.githubProviderId,
githubId: data.githubId,
})
.then(async () => {
toast.success("Service Provided Saved");
@ -140,7 +140,7 @@ export const SaveGithubProvider = ({ applicationId }: Props) => {
<div className="grid md:grid-cols-2 gap-4">
<FormField
control={form.control}
name="githubProviderId"
name="githubId"
render={({ field }) => (
<FormItem className="md:col-span-2 flex flex-col">
<FormLabel>Github Account</FormLabel>
@ -164,8 +164,8 @@ export const SaveGithubProvider = ({ applicationId }: Props) => {
<SelectContent>
{githubProviders?.map((githubProvider) => (
<SelectItem
key={githubProvider.githubProviderId}
value={githubProvider.githubProviderId}
key={githubProvider.githubId}
value={githubProvider.githubId}
>
{githubProvider.gitProvider.name}
</SelectItem>

View File

@ -47,7 +47,7 @@ const GitlabProviderSchema = z.object({
})
.required(),
branch: z.string().min(1, "Branch is required"),
gitlabProviderId: z.string().min(1, "Gitlab Provider is required"),
gitlabId: z.string().min(1, "Gitlab Provider is required"),
});
type GitlabProvider = z.infer<typeof GitlabProviderSchema>;
@ -70,21 +70,21 @@ export const SaveGitlabProvider = ({ applicationId }: Props) => {
owner: "",
repo: "",
},
gitlabProviderId: "",
gitlabId: "",
branch: "",
},
resolver: zodResolver(GitlabProviderSchema),
});
const repository = form.watch("repository");
const gitlabProviderId = form.watch("gitlabProviderId");
const gitlabId = form.watch("gitlabId");
const {
data: repositories,
isLoading: isLoadingRepositories,
error,
} = api.gitProvider.getGitlabRepositories.useQuery({
gitlabProviderId,
gitlabId,
});
const {
@ -95,10 +95,10 @@ export const SaveGitlabProvider = ({ applicationId }: Props) => {
{
owner: repository?.owner,
repo: repository?.repo,
gitlabProviderId: gitlabProviderId,
gitlabId: gitlabId,
},
{
enabled: !!repository?.owner && !!repository?.repo && !!gitlabProviderId,
enabled: !!repository?.owner && !!repository?.repo && !!gitlabId,
},
);
@ -111,7 +111,7 @@ export const SaveGitlabProvider = ({ applicationId }: Props) => {
owner: data.gitlabOwner || "",
},
buildPath: data.gitlabBuildPath || "/",
gitlabProviderId: data.gitlabProviderId || "",
gitlabId: data.gitlabId || "",
});
}
}, [form.reset, data, form]);
@ -122,7 +122,7 @@ export const SaveGitlabProvider = ({ applicationId }: Props) => {
gitlabRepository: data.repository.repo,
gitlabOwner: data.repository.owner,
gitlabBuildPath: data.buildPath,
gitlabProviderId: data.gitlabProviderId,
gitlabId: data.gitlabId,
applicationId,
})
.then(async () => {
@ -145,7 +145,7 @@ export const SaveGitlabProvider = ({ applicationId }: Props) => {
<div className="grid md:grid-cols-2 gap-4">
<FormField
control={form.control}
name="gitlabProviderId"
name="gitlabId"
render={({ field }) => (
<FormItem className="md:col-span-2 flex flex-col">
<FormLabel>Gitlab Account</FormLabel>
@ -169,8 +169,8 @@ export const SaveGitlabProvider = ({ applicationId }: Props) => {
<SelectContent>
{gitlabProviders?.map((gitlabProvider) => (
<SelectItem
key={gitlabProvider.gitlabProviderId}
value={gitlabProvider.gitlabProviderId}
key={gitlabProvider.gitlabId}
value={gitlabProvider.gitlabId}
>
{gitlabProvider.gitProvider.name}
</SelectItem>

View File

@ -1,5 +1,5 @@
import { buttonVariants } from "@/components/ui/button";
import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card";
import { Card, CardContent } from "@/components/ui/card";
import { AddGitlabProvider } from "./add-gitlab-provider";
import {
BitbucketIcon,
@ -49,13 +49,12 @@ export const ShowGitProviders = () => {
const isGitlab = gitProvider.providerType === "gitlab";
const haveGithubRequirements =
gitProvider.providerType === "github" &&
gitProvider.githubProvider?.githubPrivateKey &&
gitProvider.githubProvider?.githubAppId &&
gitProvider.githubProvider?.githubInstallationId;
gitProvider.github?.githubPrivateKey &&
gitProvider.github?.githubAppId &&
gitProvider.github?.githubInstallationId;
const haveGitlabRequirements =
gitProvider.gitlabProvider?.accessToken &&
gitProvider.gitlabProvider?.refreshToken;
gitProvider.gitlab?.accessToken && gitProvider.gitlab?.refreshToken;
return (
<div
className="space-y-4"
@ -89,7 +88,7 @@ export const ShowGitProviders = () => {
{!haveGithubRequirements && isGithub && (
<div className="flex flex-col gap-1">
<Link
href={`${gitProvider?.githubProvider?.githubAppName}/installations/new?state=gh_setup:${gitProvider?.githubProvider.githubProviderId}`}
href={`${gitProvider?.github?.githubAppName}/installations/new?state=gh_setup:${gitProvider?.github.githubId}`}
className={buttonVariants({ className: "w-fit" })}
>
Install Github App
@ -100,7 +99,7 @@ export const ShowGitProviders = () => {
{haveGithubRequirements && isGithub && (
<div className="flex flex-col gap-1">
<Link
href={`${gitProvider?.githubProvider?.githubAppName}`}
href={`${gitProvider?.github?.githubAppName}`}
target="_blank"
className={buttonVariants({
className: "w-fit",
@ -116,8 +115,8 @@ export const ShowGitProviders = () => {
<div className="flex flex-col gap-1">
<Link
href={getGitlabUrl(
gitProvider.gitlabProvider?.applicationId || "",
gitProvider.gitlabProvider?.gitlabProviderId || "",
gitProvider.gitlab?.applicationId || "",
gitProvider.gitlab?.gitlabId || "",
)}
target="_blank"
className={buttonVariants({

View File

@ -209,7 +209,7 @@ export const applicationRouter = createTRPCRouter({
owner: input.owner,
buildPath: input.buildPath,
applicationStatus: "idle",
githubProviderId: input.githubProviderId,
githubId: input.githubId,
});
return true;
@ -224,7 +224,7 @@ export const applicationRouter = createTRPCRouter({
gitlabBuildPath: input.gitlabBuildPath,
sourceType: "gitlab",
applicationStatus: "idle",
gitlabProviderId: input.gitlabProviderId,
gitlabId: input.gitlabId,
});
return true;
@ -239,7 +239,7 @@ export const applicationRouter = createTRPCRouter({
bitbucketBuildPath: input.bitbucketBuildPath,
sourceType: "bitbucket",
applicationStatus: "idle",
bitbucketProviderId: input.bitbucketProviderId,
bitbucketId: input.bitbucketId,
});
return true;

View File

@ -129,7 +129,7 @@ export const gitProvider = createTRPCRouter({
getGitlabRepositories: protectedProcedure
.input(
z.object({
gitlabProviderId: z.string().optional(),
gitlabId: z.string().optional(),
}),
)
.query(async ({ input }) => {
@ -141,7 +141,7 @@ export const gitProvider = createTRPCRouter({
z.object({
owner: z.string(),
repo: z.string(),
gitlabProviderId: z.string().optional(),
gitlabId: z.string().optional(),
}),
)
.query(async ({ input }) => {
@ -150,7 +150,7 @@ export const gitProvider = createTRPCRouter({
getBitbucketRepositories: protectedProcedure
.input(
z.object({
bitbucketProviderId: z.string().optional(),
bitbucketId: z.string().optional(),
}),
)
.query(async ({ input }) => {
@ -161,7 +161,7 @@ export const gitProvider = createTRPCRouter({
z.object({
owner: z.string(),
repo: z.string(),
bitbucketProviderId: z.string().optional(),
bitbucketId: z.string().optional(),
}),
)
.query(async ({ input }) => {
@ -170,7 +170,7 @@ export const gitProvider = createTRPCRouter({
getRepositories: protectedProcedure
.input(
z.object({
githubProviderId: z.string().optional(),
githubId: z.string().optional(),
}),
)
.query(async ({ input }) => {

View File

@ -71,7 +71,7 @@ export const apiTraefikConfig = z.object({
export const apiGetBranches = z.object({
repo: z.string().min(1),
owner: z.string().min(1),
githubProviderId: z.string().optional(),
githubId: z.string().optional(),
});
export const apiModifyTraefikConfig = z.object({
path: z.string().min(1),

View File

@ -111,18 +111,16 @@ export const cloneRawBitbucketRepository = async (
};
interface GetBitbucketRepositories {
bitbucketProviderId?: string;
bitbucketId?: string;
}
export const getBitbucketRepositories = async (
input: GetBitbucketRepositories,
) => {
if (!input.bitbucketProviderId) {
if (!input.bitbucketId) {
return [];
}
const bitbucketProvider = await getBitbucketProvider(
input.bitbucketProviderId,
);
const bitbucketProvider = await getBitbucketProvider(input.bitbucketId);
const url = `https://api.bitbucket.org/2.0/repositories/${bitbucketProvider.bitbucketUsername}`;
@ -168,16 +166,14 @@ export const getBitbucketRepositories = async (
interface GetBitbucketBranches {
owner: string;
repo: string;
bitbucketProviderId?: string;
bitbucketId?: string;
}
export const getBitbucketBranches = async (input: GetBitbucketBranches) => {
if (!input.bitbucketProviderId) {
if (!input.bitbucketId) {
return [];
}
const bitbucketProvider = await getBitbucketProvider(
input.bitbucketProviderId,
);
const bitbucketProvider = await getBitbucketProvider(input.bitbucketId);
const { owner, repo } = input;
const url = `https://api.bitbucket.org/2.0/repositories/${owner}/${repo}/refs/branches`;

View File

@ -176,15 +176,15 @@ export const cloneRawGithubRepository = async (
};
interface GetGithubRepositories {
githubProviderId?: string;
githubId?: string;
}
export const getGithubRepositories = async (input: GetGithubRepositories) => {
if (!input.githubProviderId) {
if (!input.githubId) {
return [];
}
const githubProvider = await getGithubProvider(input.githubProviderId);
const githubProvider = await getGithubProvider(input.githubId);
const octokit = new Octokit({
authStrategy: createAppAuth,
@ -207,14 +207,14 @@ export const getGithubRepositories = async (input: GetGithubRepositories) => {
interface GetGithubBranches {
owner: string;
repo: string;
githubProviderId?: string;
githubId?: string;
}
export const getGithubBranches = async (input: GetGithubBranches) => {
if (!input.githubProviderId) {
if (!input.githubId) {
return [];
}
const githubProvider = await getGithubProvider(input.githubProviderId);
const githubProvider = await getGithubProvider(input.githubId);
const octokit = new Octokit({
authStrategy: createAppAuth,

View File

@ -152,17 +152,17 @@ export const cloneGitlabRepository = async (
};
interface GetGitlabRepositories {
gitlabProviderId?: string;
gitlabId?: string;
}
export const getGitlabRepositories = async (input: GetGitlabRepositories) => {
if (!input.gitlabProviderId) {
if (!input.gitlabId) {
return [];
}
await refreshGitlabToken(input.gitlabProviderId);
await refreshGitlabToken(input.gitlabId);
const gitlabProvider = await getGitlabProvider(input.gitlabProviderId);
const gitlabProvider = await getGitlabProvider(input.gitlabId);
const response = await fetch(
`https://gitlab.com/api/v4/projects?membership=true&owned=true&page=${0}&per_page=${100}`,
{
@ -192,15 +192,15 @@ export const getGitlabRepositories = async (input: GetGitlabRepositories) => {
interface GetGitlabBranches {
owner: string;
repo: string;
gitlabProviderId?: string;
gitlabId?: string;
}
export const getGitlabBranches = async (input: GetGitlabBranches) => {
if (!input.gitlabProviderId) {
if (!input.gitlabId) {
return [];
}
const gitlabProvider = await getGitlabProvider(input.gitlabProviderId);
const gitlabProvider = await getGitlabProvider(input.gitlabId);
const projectResponse = await fetch(
`https://gitlab.com/api/v4/projects?search=${input.repo}&owned=true&page=1&per_page=100`,