refactor: simplify naming schema

This commit is contained in:
Mauricio Siu 2024-09-01 19:34:25 -06:00
parent 879311c332
commit 32ebd9b3b9
20 changed files with 393 additions and 211 deletions

View File

@ -12,6 +12,7 @@ const baseApp: ApplicationNested = {
branch: null, branch: null,
buildArgs: null, buildArgs: null,
buildPath: "/", buildPath: "/",
gitlabPathNamespace: "",
buildType: "nixpacks", buildType: "nixpacks",
bitbucketBranch: "", bitbucketBranch: "",
bitbucketBuildPath: "", bitbucketBuildPath: "",

View File

@ -53,7 +53,7 @@ export const AddBitbucketProvider = () => {
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
const url = useUrl(); const url = useUrl();
const { mutateAsync, error, isError } = const { mutateAsync, error, isError } =
api.gitProvider.createBitbucketProvider.useMutation(); api.gitProvider.createBitbucket.useMutation();
const { data: auth } = api.auth.get.useQuery(); const { data: auth } = api.auth.get.useQuery();
const router = useRouter(); const router = useRouter();
const form = useForm<Schema>({ const form = useForm<Schema>({

View File

@ -0,0 +1,231 @@
import {
BitbucketIcon,
GithubIcon,
GitlabIcon,
} from "@/components/icons/data-tools-icons";
import { AlertBlock } from "@/components/shared/alert-block";
import { Button } from "@/components/ui/button";
import { CardContent } from "@/components/ui/card";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { api } from "@/utils/api";
import { useUrl } from "@/utils/hooks/use-url";
import { zodResolver } from "@hookform/resolvers/zod";
import { ExternalLink } from "lucide-react";
import Link from "next/link";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
import { z } from "zod";
const Schema = z.object({
name: z.string().min(1, {
message: "Name is required",
}),
username: z.string().min(1, {
message: "Username is required",
}),
password: z.string().min(1, {
message: "App Password is required",
}),
workspaceName: z.string().optional(),
});
type Schema = z.infer<typeof Schema>;
export const AddBitbucketProvider = () => {
// const {data} = api.gitProvider.
const utils = api.useUtils();
const [isOpen, setIsOpen] = useState(false);
const url = useUrl();
const { mutateAsync, error, isError } =
api.gitProvider.createBitbucket.useMutation();
const { data: auth } = api.auth.get.useQuery();
const router = useRouter();
const form = useForm<Schema>({
defaultValues: {
username: "",
password: "",
workspaceName: "",
},
resolver: zodResolver(Schema),
});
useEffect(() => {
form.reset({
username: "",
password: "",
workspaceName: "",
});
}, [form, isOpen]);
const onSubmit = async (data: Schema) => {
await mutateAsync({
bitbucketUsername: data.username,
appPassword: data.password,
bitbucketWorkspaceName: data.workspaceName || "",
authId: auth?.id || "",
name: data.name || "",
})
.then(async () => {
await utils.gitProvider.getAll.invalidate();
toast.success("Bitbucket configured successfully");
setIsOpen(false);
})
.catch(() => {
toast.error("Error configuring Bitbucket");
});
};
return (
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogTrigger asChild>
<Button
variant="secondary"
className="flex items-center space-x-1 bg-blue-700 text-white hover:bg-blue-600"
>
<BitbucketIcon />
<span>Bitbucket</span>
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-2xl overflow-y-auto max-h-screen">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
Bitbucket Provider <BitbucketIcon className="size-5" />
</DialogTitle>
</DialogHeader>
{isError && <AlertBlock type="error">{error?.message}</AlertBlock>}
<Form {...form}>
<form
id="hook-form-add-bitbucket"
onSubmit={form.handleSubmit(onSubmit)}
className="grid w-full gap-1"
>
<CardContent className="p-0">
<div className="flex flex-col gap-4">
<p className="text-muted-foreground text-sm">
To integrate your Bitbucket account, you need to create a new
App Password in your Bitbucket settings. Follow these steps:
</p>
<ol className="list-decimal list-inside text-sm text-muted-foreground">
<li className="flex flex-row gap-2 items-center">
Create new App Password{" "}
<Link
href="https://bitbucket.org/account/settings/app-passwords/new"
target="_blank"
>
<ExternalLink className="w-fit text-primary size-4" />
</Link>
</li>
<li>
When creating the App Password, ensure you grant the
following permissions:
<ul className="list-disc list-inside ml-4">
<li>Account: Read</li>
<li>Workspace membership: Read</li>
<li>Projects: Read</li>
<li>Repositories: Read</li>
<li>Pull requests: Read</li>
<li>Webhooks: Read and write</li>
</ul>
</li>
<li>
After creating, you'll receive an App Password. Copy it and
paste it below along with your Bitbucket username.
</li>
</ol>
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Name</FormLabel>
<FormControl>
<Input
placeholder="Random Name eg(my-personal-account)"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="username"
render={({ field }) => (
<FormItem>
<FormLabel>Bitbucket Username</FormLabel>
<FormControl>
<Input
placeholder="Your Bitbucket username"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
<FormLabel>App Password</FormLabel>
<FormControl>
<Input
type="password"
placeholder="ATBBPDYUC94nR96Nj7Cqpp4pfwKk03573DD2"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="workspaceName"
render={({ field }) => (
<FormItem>
<FormLabel>Workspace Name (Optional)</FormLabel>
<FormControl>
<Input
placeholder="For organization accounts"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button isLoading={form.formState.isSubmitting}>
Configure Bitbucket
</Button>
</div>
</CardContent>
</form>
</Form>
</DialogContent>
</Dialog>
);
};

View File

@ -54,7 +54,7 @@ export const AddGitlabProvider = () => {
const url = useUrl(); const url = useUrl();
const { data: auth } = api.auth.get.useQuery(); const { data: auth } = api.auth.get.useQuery();
const { mutateAsync, error, isError } = const { mutateAsync, error, isError } =
api.gitProvider.createGitlabProvider.useMutation(); api.gitProvider.createGitlab.useMutation();
const webhookUrl = `${url}/api/providers/gitlab/callback`; const webhookUrl = `${url}/api/providers/gitlab/callback`;
const form = useForm<Schema>({ const form = useForm<Schema>({

View File

@ -1,13 +1,13 @@
import { buttonVariants } from "@/components/ui/button"; import { buttonVariants } from "@/components/ui/button";
import { Card, CardContent } from "@/components/ui/card"; import { Card, CardContent } from "@/components/ui/card";
import { AddGitlabProvider } from "./add-gitlab-provider"; import { AddGitlabProvider } from "./gitlab/add-gitlab-provider";
import { import {
BitbucketIcon, BitbucketIcon,
GithubIcon, GithubIcon,
GitlabIcon, GitlabIcon,
} from "@/components/icons/data-tools-icons"; } from "@/components/icons/data-tools-icons";
import { AddGithubProvider } from "./add-github-provider"; import { AddGithubProvider } from "./github/add-github-provider";
import { AddBitbucketProvider } from "./add-bitbucket-provider"; import { AddBitbucketProvider } from "./bitbucket/add-bitbucket-provider";
import { api } from "@/utils/api"; import { api } from "@/utils/api";
import Link from "next/link"; import Link from "next/link";
import { RemoveGitProvider } from "./remove-git-provider"; import { RemoveGitProvider } from "./remove-git-provider";

View File

@ -1,6 +1,6 @@
import { findAdmin } from "@/server/api/services/admin"; import { findAdmin } from "@/server/api/services/admin";
import { db } from "@/server/db"; import { db } from "@/server/db";
import { applications, compose, githubProvider } from "@/server/db/schema"; import { applications, compose } from "@/server/db/schema";
import type { DeploymentJob } from "@/server/queues/deployments-queue"; import type { DeploymentJob } from "@/server/queues/deployments-queue";
import { myQueue } from "@/server/queues/queueSetup"; import { myQueue } from "@/server/queues/queueSetup";
import { Webhooks } from "@octokit/webhooks"; import { Webhooks } from "@octokit/webhooks";
@ -22,13 +22,13 @@ export default async function handler(
const signature = req.headers["x-hub-signature-256"]; const signature = req.headers["x-hub-signature-256"];
const github = req.body; const github = req.body;
if(!github?.installation.id) { if (!github?.installation.id) {
res.status(400).json({ message: "Github Installation not found" }); res.status(400).json({ message: "Github Installation not found" });
return; return;
} }
const githubResult = await db.query.githubProvider.findFirst({ const githubResult = await db.query.github.findFirst({
where: eq(githubProvider.githubInstallationId, github.installation.id), where: eq(github.githubInstallationId, github.installation.id),
}); });
if (!githubResult) { if (!githubResult) {

View File

@ -1,6 +1,6 @@
import { createGithubProvider } from "@/server/api/services/git-provider"; import { createGithub } from "@/server/api/services/git-provider";
import { db } from "@/server/db"; import { db } from "@/server/db";
import { githubProvider } from "@/server/db/schema"; import { github } from "@/server/db/schema";
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
import type { NextApiRequest, NextApiResponse } from "next"; import type { NextApiRequest, NextApiResponse } from "next";
import { Octokit } from "octokit"; import { Octokit } from "octokit";
@ -34,7 +34,7 @@ export default async function handler(
}, },
); );
await createGithubProvider({ await createGithub({
name: data.name, name: data.name,
githubAppName: data.html_url, githubAppName: data.html_url,
githubAppId: data.id, githubAppId: data.id,
@ -46,11 +46,11 @@ export default async function handler(
}); });
} else if (action === "gh_setup") { } else if (action === "gh_setup") {
await db await db
.update(githubProvider) .update(github)
.set({ .set({
githubInstallationId: installation_id, githubInstallationId: installation_id,
}) })
.where(eq(githubProvider.githubId, value as string)) .where(eq(github.githubId, value as string))
.returning(); .returning();
} }

View File

@ -1,6 +1,6 @@
import { import {
getGitlabProvider, findGitlabById,
updateGitlabProvider, updateGitlab,
} from "@/server/api/services/git-provider"; } from "@/server/api/services/git-provider";
import type { NextApiRequest, NextApiResponse } from "next"; import type { NextApiRequest, NextApiResponse } from "next";
@ -14,7 +14,7 @@ export default async function handler(
return res.status(400).json({ error: "Missing or invalid code" }); return res.status(400).json({ error: "Missing or invalid code" });
} }
const gitlab = await getGitlabProvider(gitlabId as string); const gitlab = await findGitlabById(gitlabId as string);
const response = await fetch("https://gitlab.com/oauth/token", { const response = await fetch("https://gitlab.com/oauth/token", {
method: "POST", method: "POST",
@ -37,7 +37,7 @@ export default async function handler(
} }
const expiresAt = Math.floor(Date.now() / 1000) + result.expires_in; const expiresAt = Math.floor(Date.now() / 1000) + result.expires_in;
await updateGitlabProvider(gitlab.gitlabId, { await updateGitlab(gitlab.gitlabId, {
accessToken: result.access_token, accessToken: result.access_token,
refreshToken: result.refresh_token, refreshToken: result.refresh_token,
expiresAt, expiresAt,

View File

@ -1,16 +1,16 @@
import { createTRPCRouter, protectedProcedure } from "@/server/api/trpc"; import { createTRPCRouter, protectedProcedure } from "@/server/api/trpc";
import { db } from "@/server/db"; import { db } from "@/server/db";
import { import {
apiCreateBitbucketProvider, apiCreateBitbucket,
apiCreateGitlabProvider, apiCreateGitlab,
apiGetBranches, apiGetBranches,
} from "@/server/db/schema"; } from "@/server/db/schema";
import { TRPCError } from "@trpc/server"; import { TRPCError } from "@trpc/server";
import { import {
createBitbucketProvider, createBitbucket,
createGitlabProvider, createGitlab,
haveGithubRequirements, haveGithubRequirements,
removeGithubProvider, removeGithub,
} from "../services/git-provider"; } from "../services/git-provider";
import { z } from "zod"; import { z } from "zod";
import { import {
@ -41,7 +41,7 @@ export const gitProvider = createTRPCRouter({
.input(z.object({ gitProviderId: z.string() })) .input(z.object({ gitProviderId: z.string() }))
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
try { try {
return await removeGithubProvider(input.gitProviderId); return await removeGithub(input.gitProviderId);
} catch (error) { } catch (error) {
throw new TRPCError({ throw new TRPCError({
code: "BAD_REQUEST", code: "BAD_REQUEST",
@ -49,11 +49,11 @@ export const gitProvider = createTRPCRouter({
}); });
} }
}), }),
createGitlabProvider: protectedProcedure createGitlab: protectedProcedure
.input(apiCreateGitlabProvider) .input(apiCreateGitlab)
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
try { try {
return await createGitlabProvider(input); return await createGitlab(input);
} catch (error) { } catch (error) {
throw new TRPCError({ throw new TRPCError({
code: "BAD_REQUEST", code: "BAD_REQUEST",
@ -62,11 +62,11 @@ export const gitProvider = createTRPCRouter({
}); });
} }
}), }),
createBitbucketProvider: protectedProcedure createBitbucket: protectedProcedure
.input(apiCreateBitbucketProvider) .input(apiCreateBitbucket)
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
try { try {
return await createBitbucketProvider(input); return await createBitbucket(input);
} catch (error) { } catch (error) {
throw new TRPCError({ throw new TRPCError({
code: "BAD_REQUEST", code: "BAD_REQUEST",
@ -76,7 +76,7 @@ export const gitProvider = createTRPCRouter({
} }
}), }),
githubProviders: protectedProcedure.query(async () => { githubProviders: protectedProcedure.query(async () => {
const result = await db.query.githubProvider.findMany({ const result = await db.query.github.findMany({
with: { with: {
gitProvider: true, gitProvider: true,
}, },
@ -96,7 +96,7 @@ export const gitProvider = createTRPCRouter({
return filtered; return filtered;
}), }),
gitlabProviders: protectedProcedure.query(async () => { gitlabProviders: protectedProcedure.query(async () => {
const result = await db.query.gitlabProvider.findMany({ const result = await db.query.gitlab.findMany({
with: { with: {
gitProvider: true, gitProvider: true,
}, },
@ -115,7 +115,7 @@ export const gitProvider = createTRPCRouter({
return filtered; return filtered;
}), }),
bitbucketProviders: protectedProcedure.query(async () => { bitbucketProviders: protectedProcedure.query(async () => {
const result = await db.query.bitbucketProvider.findMany({ const result = await db.query.bitbucket.findMany({
with: { with: {
gitProvider: true, gitProvider: true,
}, },
@ -182,21 +182,9 @@ export const gitProvider = createTRPCRouter({
.query(async ({ input }) => { .query(async ({ input }) => {
return await getGithubBranches(input); return await getGithubBranches(input);
}), }),
// getGithub: protectedProcedure
// .input(apiGetGithub)
// .query(async ({ input }) => {
// return await findGithub(input);
// }),
}); });
// 1725175543
// {
// access_token: '11d422887d8fac712191ee9b09dfdb043a705938cd67a4a39f36b4bc65b3106d',
// token_type: 'Bearer',
// expires_in: 7200,
// refresh_token: '3806d8022d32886c19d91eb9d1cea9328b864387f39c5d0469d08c48e18b674e',
// scope: 'api read_user read_repository',
// created_at: 1725167656
// }
// {
// access_token: 'd256b52b10bf72ebf2784f8c0528e48a04a7d249c28695b6cc105b47b09c7336',
// token_type: 'Bearer',
// expires_in: 7200,
// refresh_token: '265eb87d0bbef410e0c30a2c239c4fa3698943219a439fb43cf2f8227d1fcaf2',
// scope: 'api read_user read_repository',
// created_at: 1725167803
// }

View File

@ -83,9 +83,9 @@ export const findApplicationById = async (applicationId: string) => {
security: true, security: true,
ports: true, ports: true,
registry: true, registry: true,
gitlabProvider: true, gitlab: true,
githubProvider: true, github: true,
bitbucketProvider: true, bitbucket: true,
}, },
}); });
if (!application) { if (!application) {

View File

@ -93,9 +93,9 @@ export const findComposeById = async (composeId: string) => {
deployments: true, deployments: true,
mounts: true, mounts: true,
domains: true, domains: true,
githubProvider: true, github: true,
gitlabProvider: true, gitlab: true,
bitbucketProvider: true, bitbucket: true,
}, },
}); });
if (!result) { if (!result) {

View File

@ -1,22 +1,20 @@
import { db } from "@/server/db"; import { db } from "@/server/db";
import { import {
type apiCreateBitbucketProvider, type apiCreateBitbucket,
type apiCreateGithubProvider, type apiCreateGithub,
type apiCreateGitlabProvider, type apiCreateGitlab,
bitbucketProvider, bitbucket,
githubProvider, github,
gitlabProvider, gitlab,
gitProvider, gitProvider,
} from "@/server/db/schema"; } from "@/server/db/schema";
import { TRPCError } from "@trpc/server"; import { TRPCError } from "@trpc/server";
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
export type GithubProvider = typeof githubProvider.$inferSelect; export type Github = typeof github.$inferSelect;
export type GitlabProvider = typeof gitlabProvider.$inferSelect; export type Gitlab = typeof gitlab.$inferSelect;
export const createGithubProvider = async ( export const createGithub = async (input: typeof apiCreateGithub._type) => {
input: typeof apiCreateGithubProvider._type,
) => {
return await db.transaction(async (tx) => { return await db.transaction(async (tx) => {
const newGitProvider = await tx const newGitProvider = await tx
.insert(gitProvider) .insert(gitProvider)
@ -36,7 +34,7 @@ export const createGithubProvider = async (
} }
return await tx return await tx
.insert(githubProvider) .insert(github)
.values({ .values({
...input, ...input,
gitProviderId: newGitProvider?.gitProviderId, gitProviderId: newGitProvider?.gitProviderId,
@ -46,9 +44,7 @@ export const createGithubProvider = async (
}); });
}; };
export const createGitlabProvider = async ( export const createGitlab = async (input: typeof apiCreateGitlab._type) => {
input: typeof apiCreateGitlabProvider._type,
) => {
return await db.transaction(async (tx) => { return await db.transaction(async (tx) => {
const newGitProvider = await tx const newGitProvider = await tx
.insert(gitProvider) .insert(gitProvider)
@ -68,7 +64,7 @@ export const createGitlabProvider = async (
} }
await tx await tx
.insert(gitlabProvider) .insert(gitlab)
.values({ .values({
...input, ...input,
gitProviderId: newGitProvider?.gitProviderId, gitProviderId: newGitProvider?.gitProviderId,
@ -78,8 +74,8 @@ export const createGitlabProvider = async (
}); });
}; };
export const createBitbucketProvider = async ( export const createBitbucket = async (
input: typeof apiCreateBitbucketProvider._type, input: typeof apiCreateBitbucket._type,
) => { ) => {
return await db.transaction(async (tx) => { return await db.transaction(async (tx) => {
const newGitProvider = await tx const newGitProvider = await tx
@ -100,7 +96,7 @@ export const createBitbucketProvider = async (
} }
await tx await tx
.insert(bitbucketProvider) .insert(bitbucket)
.values({ .values({
...input, ...input,
gitProviderId: newGitProvider?.gitProviderId, gitProviderId: newGitProvider?.gitProviderId,
@ -110,7 +106,7 @@ export const createBitbucketProvider = async (
}); });
}; };
export const removeGithubProvider = async (gitProviderId: string) => { export const removeGithub = async (gitProviderId: string) => {
const result = await db const result = await db
.delete(gitProvider) .delete(gitProvider)
.where(eq(gitProvider.gitProviderId, gitProviderId)) .where(eq(gitProvider.gitProviderId, gitProviderId))
@ -119,9 +115,9 @@ export const removeGithubProvider = async (gitProviderId: string) => {
return result[0]; return result[0];
}; };
export const getGithubProvider = async (githubId: string) => { export const findGithubById = async (githubId: string) => {
const githubProviderResult = await db.query.githubProvider.findFirst({ const githubProviderResult = await db.query.github.findFirst({
where: eq(githubProvider.githubId, githubId), where: eq(github.githubId, githubId),
}); });
if (!githubProviderResult) { if (!githubProviderResult) {
@ -134,17 +130,17 @@ export const getGithubProvider = async (githubId: string) => {
return githubProviderResult; return githubProviderResult;
}; };
export const haveGithubRequirements = (githubProvider: GithubProvider) => { export const haveGithubRequirements = (github: Github) => {
return !!( return !!(
githubProvider?.githubAppId && github?.githubAppId &&
githubProvider?.githubPrivateKey && github?.githubPrivateKey &&
githubProvider?.githubInstallationId github?.githubInstallationId
); );
}; };
export const getGitlabProvider = async (gitlabId: string) => { export const findGitlabById = async (gitlabId: string) => {
const gitlabProviderResult = await db.query.gitlabProvider.findFirst({ const gitlabProviderResult = await db.query.gitlab.findFirst({
where: eq(gitlabProvider.gitlabId, gitlabId), where: eq(gitlab.gitlabId, gitlabId),
}); });
if (!gitlabProviderResult) { if (!gitlabProviderResult) {
@ -157,24 +153,24 @@ export const getGitlabProvider = async (gitlabId: string) => {
return gitlabProviderResult; return gitlabProviderResult;
}; };
export const updateGitlabProvider = async ( export const updateGitlab = async (
gitlabId: string, gitlabId: string,
input: Partial<GitlabProvider>, input: Partial<Gitlab>,
) => { ) => {
const result = await db const result = await db
.update(gitlabProvider) .update(gitlab)
.set({ .set({
...input, ...input,
}) })
.where(eq(gitlabProvider.gitlabId, gitlabId)) .where(eq(gitlab.gitlabId, gitlabId))
.returning(); .returning();
return result[0]; return result[0];
}; };
export const getBitbucketProvider = async (bitbucketId: string) => { export const findBitbucketById = async (bitbucketId: string) => {
const bitbucketProviderResult = await db.query.bitbucketProvider.findFirst({ const bitbucketProviderResult = await db.query.bitbucket.findFirst({
where: eq(bitbucketProvider.bitbucketId, bitbucketId), where: eq(bitbucket.bitbucketId, bitbucketId),
}); });
if (!bitbucketProviderResult) { if (!bitbucketProviderResult) {

View File

@ -21,11 +21,7 @@ import { security } from "./security";
import { applicationStatus } from "./shared"; import { applicationStatus } from "./shared";
import { sshKeys } from "./ssh-key"; import { sshKeys } from "./ssh-key";
import { generateAppName } from "./utils"; import { generateAppName } from "./utils";
import { import { bitbucket, github, gitlab } from "./git-provider";
bitbucketProvider,
githubProvider,
gitlabProvider,
} from "./git-provider";
export const sourceType = pgEnum("sourceType", [ export const sourceType = pgEnum("sourceType", [
"docker", "docker",
@ -187,18 +183,15 @@ export const applications = pgTable("application", {
projectId: text("projectId") projectId: text("projectId")
.notNull() .notNull()
.references(() => projects.projectId, { onDelete: "cascade" }), .references(() => projects.projectId, { onDelete: "cascade" }),
githubId: text("githubId").references(() => githubProvider.githubId, { githubId: text("githubId").references(() => github.githubId, {
onDelete: "set null", onDelete: "set null",
}), }),
gitlabId: text("gitlabId").references(() => gitlabProvider.gitlabId, { gitlabId: text("gitlabId").references(() => gitlab.gitlabId, {
onDelete: "set null",
}),
bitbucketId: text("bitbucketId").references(() => bitbucket.bitbucketId, {
onDelete: "set null", onDelete: "set null",
}), }),
bitbucketId: text("bitbucketId").references(
() => bitbucketProvider.bitbucketId,
{
onDelete: "set null",
},
),
}); });
export const applicationsRelations = relations( export const applicationsRelations = relations(
@ -222,17 +215,17 @@ export const applicationsRelations = relations(
fields: [applications.registryId], fields: [applications.registryId],
references: [registry.registryId], references: [registry.registryId],
}), }),
githubProvider: one(githubProvider, { github: one(github, {
fields: [applications.githubId], fields: [applications.githubId],
references: [githubProvider.githubId], references: [github.githubId],
}), }),
gitlabProvider: one(gitlabProvider, { gitlab: one(gitlab, {
fields: [applications.gitlabId], fields: [applications.gitlabId],
references: [gitlabProvider.gitlabId], references: [gitlab.gitlabId],
}), }),
bitbucketProvider: one(bitbucketProvider, { bitbucket: one(bitbucket, {
fields: [applications.bitbucketId], fields: [applications.bitbucketId],
references: [bitbucketProvider.bitbucketId], references: [bitbucket.bitbucketId],
}), }),
}), }),
); );

View File

@ -10,11 +10,7 @@ import { mounts } from "./mount";
import { projects } from "./project"; import { projects } from "./project";
import { applicationStatus } from "./shared"; import { applicationStatus } from "./shared";
import { generateAppName } from "./utils"; import { generateAppName } from "./utils";
import { import { bitbucket, github, gitlab } from "./git-provider";
bitbucketProvider,
githubProvider,
gitlabProvider,
} from "./git-provider";
export const sourceTypeCompose = pgEnum("sourceTypeCompose", [ export const sourceTypeCompose = pgEnum("sourceTypeCompose", [
"git", "git",
@ -76,18 +72,15 @@ export const compose = pgTable("compose", {
.notNull() .notNull()
.$defaultFn(() => new Date().toISOString()), .$defaultFn(() => new Date().toISOString()),
githubId: text("githubId").references(() => githubProvider.githubId, { githubId: text("githubId").references(() => github.githubId, {
onDelete: "set null", onDelete: "set null",
}), }),
gitlabId: text("gitlabId").references(() => gitlabProvider.gitlabId, { gitlabId: text("gitlabId").references(() => gitlab.gitlabId, {
onDelete: "set null",
}),
bitbucketId: text("bitbucketId").references(() => bitbucket.bitbucketId, {
onDelete: "set null", onDelete: "set null",
}), }),
bitbucketId: text("bitbucketId").references(
() => bitbucketProvider.bitbucketId,
{
onDelete: "set null",
},
),
}); });
export const composeRelations = relations(compose, ({ one, many }) => ({ export const composeRelations = relations(compose, ({ one, many }) => ({
@ -102,17 +95,17 @@ export const composeRelations = relations(compose, ({ one, many }) => ({
references: [sshKeys.sshKeyId], references: [sshKeys.sshKeyId],
}), }),
domains: many(domains), domains: many(domains),
githubProvider: one(githubProvider, { github: one(github, {
fields: [compose.githubId], fields: [compose.githubId],
references: [githubProvider.githubId], references: [github.githubId],
}), }),
gitlabProvider: one(gitlabProvider, { gitlab: one(gitlab, {
fields: [compose.gitlabId], fields: [compose.gitlabId],
references: [gitlabProvider.gitlabId], references: [gitlab.gitlabId],
}), }),
bitbucketProvider: one(bitbucketProvider, { bitbucket: one(bitbucket, {
fields: [compose.bitbucketId], fields: [compose.bitbucketId],
references: [bitbucketProvider.bitbucketId], references: [bitbucket.bitbucketId],
}), }),
})); }));

View File

@ -27,17 +27,17 @@ export const gitProvider = pgTable("git_provider", {
}); });
export const gitProviderRelations = relations(gitProvider, ({ one, many }) => ({ export const gitProviderRelations = relations(gitProvider, ({ one, many }) => ({
github: one(githubProvider, { github: one(github, {
fields: [gitProvider.gitProviderId], fields: [gitProvider.gitProviderId],
references: [githubProvider.gitProviderId], references: [github.gitProviderId],
}), }),
gitlab: one(gitlabProvider, { gitlab: one(gitlab, {
fields: [gitProvider.gitProviderId], fields: [gitProvider.gitProviderId],
references: [gitlabProvider.gitProviderId], references: [gitlab.gitProviderId],
}), }),
bitbucket: one(bitbucketProvider, { bitbucket: one(bitbucket, {
fields: [gitProvider.gitProviderId], fields: [gitProvider.gitProviderId],
references: [bitbucketProvider.gitProviderId], references: [bitbucket.gitProviderId],
}), }),
auth: one(auth, { auth: one(auth, {
fields: [gitProvider.authId], fields: [gitProvider.authId],
@ -45,7 +45,7 @@ export const gitProviderRelations = relations(gitProvider, ({ one, many }) => ({
}), }),
})); }));
export const githubProvider = pgTable("github_provider", { export const github = pgTable("github", {
githubId: text("githubId") githubId: text("githubId")
.notNull() .notNull()
.primaryKey() .primaryKey()
@ -62,17 +62,14 @@ export const githubProvider = pgTable("github_provider", {
.references(() => gitProvider.gitProviderId, { onDelete: "cascade" }), .references(() => gitProvider.gitProviderId, { onDelete: "cascade" }),
}); });
export const githubProviderRelations = relations( export const githubProviderRelations = relations(github, ({ one }) => ({
githubProvider, gitProvider: one(gitProvider, {
({ one, }) => ({ fields: [github.gitProviderId],
gitProvider: one(gitProvider, { references: [gitProvider.gitProviderId],
fields: [githubProvider.gitProviderId],
references: [gitProvider.gitProviderId],
}),
}), }),
); }));
export const gitlabProvider = pgTable("gitlab_provider", { export const gitlab = pgTable("gitlab", {
gitlabId: text("gitlabId") gitlabId: text("gitlabId")
.notNull() .notNull()
.primaryKey() .primaryKey()
@ -89,17 +86,14 @@ export const gitlabProvider = pgTable("gitlab_provider", {
.references(() => gitProvider.gitProviderId, { onDelete: "cascade" }), .references(() => gitProvider.gitProviderId, { onDelete: "cascade" }),
}); });
export const gitlabProviderRelations = relations( export const gitlabProviderRelations = relations(gitlab, ({ one }) => ({
gitlabProvider, gitProvider: one(gitProvider, {
({ one}) => ({ fields: [gitlab.gitProviderId],
gitProvider: one(gitProvider, { references: [gitProvider.gitProviderId],
fields: [gitlabProvider.gitProviderId],
references: [gitProvider.gitProviderId],
}),
}), }),
); }));
export const bitbucketProvider = pgTable("bitbucket_provider", { export const bitbucket = pgTable("bitbucket", {
bitbucketId: text("bitbucketId") bitbucketId: text("bitbucketId")
.notNull() .notNull()
.primaryKey() .primaryKey()
@ -112,19 +106,16 @@ export const bitbucketProvider = pgTable("bitbucket_provider", {
.references(() => gitProvider.gitProviderId, { onDelete: "cascade" }), .references(() => gitProvider.gitProviderId, { onDelete: "cascade" }),
}); });
export const bitbucketProviderRelations = relations( export const bitbucketProviderRelations = relations(bitbucket, ({ one }) => ({
bitbucketProvider, gitProvider: one(gitProvider, {
({ one }) => ({ fields: [bitbucket.gitProviderId],
gitProvider: one(gitProvider, { references: [gitProvider.gitProviderId],
fields: [bitbucketProvider.gitProviderId],
references: [gitProvider.gitProviderId],
}),
}), }),
); }));
const createSchema = createInsertSchema(gitProvider); const createSchema = createInsertSchema(gitProvider);
export const apiCreateGithubProvider = createSchema.extend({ export const apiCreateGithub = createSchema.extend({
githubAppName: z.string().optional(), githubAppName: z.string().optional(),
githubAppId: z.number().optional(), githubAppId: z.number().optional(),
githubClientId: z.string().optional(), githubClientId: z.string().optional(),
@ -135,7 +126,7 @@ export const apiCreateGithubProvider = createSchema.extend({
gitProviderId: z.string().optional(), gitProviderId: z.string().optional(),
}); });
export const apiCreateGitlabProvider = createSchema.extend({ export const apiCreateGitlab = createSchema.extend({
applicationId: z.string().optional(), applicationId: z.string().optional(),
secret: z.string().optional(), secret: z.string().optional(),
groupName: z.string().optional(), groupName: z.string().optional(),
@ -143,7 +134,7 @@ export const apiCreateGitlabProvider = createSchema.extend({
redirectUri: z.string().optional(), redirectUri: z.string().optional(),
}); });
export const apiCreateBitbucketProvider = createSchema.extend({ export const apiCreateBitbucket = createSchema.extend({
bitbucketUsername: z.string().optional(), bitbucketUsername: z.string().optional(),
appPassword: z.string().optional(), appPassword: z.string().optional(),
bitbucketWorkspaceName: z.string().optional(), bitbucketWorkspaceName: z.string().optional(),

View File

@ -5,17 +5,17 @@ import { TRPCError } from "@trpc/server";
import { recreateDirectory } from "../filesystem/directory"; import { recreateDirectory } from "../filesystem/directory";
import { spawnAsync } from "../process/spawnAsync"; import { spawnAsync } from "../process/spawnAsync";
import type { InferResultType } from "@/server/types/with"; import type { InferResultType } from "@/server/types/with";
import { getBitbucketProvider } from "@/server/api/services/git-provider"; import { findBitbucketById } from "@/server/api/services/git-provider";
import type { Compose } from "@/server/api/services/compose"; import type { Compose } from "@/server/api/services/compose";
export type ApplicationWithBitbucket = InferResultType< export type ApplicationWithBitbucket = InferResultType<
"applications", "applications",
{ bitbucketProvider: true } { bitbucket: true }
>; >;
export type ComposeWithBitbucket = InferResultType< export type ComposeWithBitbucket = InferResultType<
"compose", "compose",
{ bitbucketProvider: true } { bitbucket: true }
>; >;
export const cloneBitbucketRepository = async ( export const cloneBitbucketRepository = async (
@ -30,7 +30,7 @@ export const cloneBitbucketRepository = async (
bitbucketOwner, bitbucketOwner,
bitbucketBranch, bitbucketBranch,
bitbucketId, bitbucketId,
bitbucketProvider, bitbucket,
} = entity; } = entity;
if (!bitbucketId) { if (!bitbucketId) {
@ -44,7 +44,7 @@ export const cloneBitbucketRepository = async (
const outputPath = join(basePath, appName, "code"); const outputPath = join(basePath, appName, "code");
await recreateDirectory(outputPath); await recreateDirectory(outputPath);
const repoclone = `bitbucket.org/${bitbucketOwner}/${bitbucketRepository}.git`; const repoclone = `bitbucket.org/${bitbucketOwner}/${bitbucketRepository}.git`;
const cloneUrl = `https://${bitbucketProvider?.bitbucketUsername}:${bitbucketProvider?.appPassword}@${repoclone}`; const cloneUrl = `https://${bitbucket?.bitbucketUsername}:${bitbucket?.appPassword}@${repoclone}`;
try { try {
writeStream.write(`\nCloning Repo ${repoclone} to ${outputPath}: ✅\n`); writeStream.write(`\nCloning Repo ${repoclone} to ${outputPath}: ✅\n`);
await spawnAsync( await spawnAsync(
@ -90,7 +90,7 @@ export const cloneRawBitbucketRepository = async (entity: Compose) => {
}); });
} }
const bitbucketProvider = await getBitbucketProvider(bitbucketId); const bitbucketProvider = await findBitbucketById(bitbucketId);
const basePath = COMPOSE_PATH; const basePath = COMPOSE_PATH;
const outputPath = join(basePath, appName, "code"); const outputPath = join(basePath, appName, "code");
await recreateDirectory(outputPath); await recreateDirectory(outputPath);
@ -123,7 +123,7 @@ export const getBitbucketRepositories = async (
if (!input.bitbucketId) { if (!input.bitbucketId) {
return []; return [];
} }
const bitbucketProvider = await getBitbucketProvider(input.bitbucketId); const bitbucketProvider = await findBitbucketById(input.bitbucketId);
const username = const username =
bitbucketProvider.bitbucketWorkspaceName || bitbucketProvider.bitbucketWorkspaceName ||
@ -179,7 +179,7 @@ export const getBitbucketBranches = async (input: GetBitbucketBranches) => {
if (!input.bitbucketId) { if (!input.bitbucketId) {
return []; return [];
} }
const bitbucketProvider = await getBitbucketProvider(input.bitbucketId); const bitbucketProvider = await findBitbucketById(input.bitbucketId);
const { owner, repo } = input; const { owner, repo } = input;
const url = `https://api.bitbucket.org/2.0/repositories/${owner}/${repo}/refs/branches`; const url = `https://api.bitbucket.org/2.0/repositories/${owner}/${repo}/refs/branches`;

View File

@ -8,12 +8,12 @@ import { recreateDirectory } from "../filesystem/directory";
import { spawnAsync } from "../process/spawnAsync"; import { spawnAsync } from "../process/spawnAsync";
import type { InferResultType } from "@/server/types/with"; import type { InferResultType } from "@/server/types/with";
import { import {
getGithubProvider, findGithubById,
type GithubProvider, type Github,
} from "@/server/api/services/git-provider"; } from "@/server/api/services/git-provider";
import type { Compose } from "@/server/api/services/compose"; import type { Compose } from "@/server/api/services/compose";
export const authGithub = (githubProvider: GithubProvider) => { export const authGithub = (githubProvider: Github) => {
if (!haveGithubRequirements(githubProvider)) { if (!haveGithubRequirements(githubProvider)) {
throw new TRPCError({ throw new TRPCError({
code: "NOT_FOUND", code: "NOT_FOUND",
@ -45,7 +45,7 @@ export const getGithubToken = async (
return installation.token; return installation.token;
}; };
export const haveGithubRequirements = (githubProvider: GithubProvider) => { export const haveGithubRequirements = (githubProvider: Github) => {
return !!( return !!(
githubProvider?.githubAppId && githubProvider?.githubAppId &&
githubProvider?.githubPrivateKey && githubProvider?.githubPrivateKey &&
@ -70,13 +70,10 @@ const getErrorCloneRequirements = (entity: {
export type ApplicationWithGithub = InferResultType< export type ApplicationWithGithub = InferResultType<
"applications", "applications",
{ githubProvider: true } { github: true }
>; >;
export type ComposeWithGithub = InferResultType< export type ComposeWithGithub = InferResultType<"compose", { github: true }>;
"compose",
{ githubProvider: true }
>;
export const cloneGithubRepository = async ( export const cloneGithubRepository = async (
entity: ApplicationWithGithub | ComposeWithGithub, entity: ApplicationWithGithub | ComposeWithGithub,
logPath: string, logPath: string,
@ -108,7 +105,7 @@ export const cloneGithubRepository = async (
}); });
} }
const githubProvider = await getGithubProvider(githubId); const githubProvider = await findGithubById(githubId);
const basePath = isCompose ? COMPOSE_PATH : APPLICATIONS_PATH; const basePath = isCompose ? COMPOSE_PATH : APPLICATIONS_PATH;
const outputPath = join(basePath, appName, "code"); const outputPath = join(basePath, appName, "code");
const octokit = authGithub(githubProvider); const octokit = authGithub(githubProvider);
@ -155,7 +152,7 @@ export const cloneRawGithubRepository = async (entity: Compose) => {
message: "GitHub Provider not found", message: "GitHub Provider not found",
}); });
} }
const githubProvider = await getGithubProvider(githubId); const githubProvider = await findGithubById(githubId);
const basePath = COMPOSE_PATH; const basePath = COMPOSE_PATH;
const outputPath = join(basePath, appName, "code"); const outputPath = join(basePath, appName, "code");
const octokit = authGithub(githubProvider); const octokit = authGithub(githubProvider);
@ -188,7 +185,7 @@ export const getGithubRepositories = async (input: GetGithubRepositories) => {
return []; return [];
} }
const githubProvider = await getGithubProvider(input.githubId); const githubProvider = await findGithubById(input.githubId);
const octokit = new Octokit({ const octokit = new Octokit({
authStrategy: createAppAuth, authStrategy: createAppAuth,
@ -218,7 +215,7 @@ export const getGithubBranches = async (input: GetGithubBranches) => {
if (!input.githubId) { if (!input.githubId) {
return []; return [];
} }
const githubProvider = await getGithubProvider(input.githubId); const githubProvider = await findGithubById(input.githubId);
const octokit = new Octokit({ const octokit = new Octokit({
authStrategy: createAppAuth, authStrategy: createAppAuth,

View File

@ -5,15 +5,15 @@ import { TRPCError } from "@trpc/server";
import { recreateDirectory } from "../filesystem/directory"; import { recreateDirectory } from "../filesystem/directory";
import { spawnAsync } from "../process/spawnAsync"; import { spawnAsync } from "../process/spawnAsync";
import { import {
getGitlabProvider, findGitlabById,
type GitlabProvider, type Gitlab,
updateGitlabProvider, updateGitlab,
} from "@/server/api/services/git-provider"; } from "@/server/api/services/git-provider";
import type { InferResultType } from "@/server/types/with"; import type { InferResultType } from "@/server/types/with";
import type { Compose } from "@/server/api/services/compose"; import type { Compose } from "@/server/api/services/compose";
export const refreshGitlabToken = async (gitlabProviderId: string) => { export const refreshGitlabToken = async (gitlabProviderId: string) => {
const gitlabProvider = await getGitlabProvider(gitlabProviderId); const gitlabProvider = await findGitlabById(gitlabProviderId);
const currentTime = Math.floor(Date.now() / 1000); const currentTime = Math.floor(Date.now() / 1000);
const safetyMargin = 60; const safetyMargin = 60;
@ -48,7 +48,7 @@ export const refreshGitlabToken = async (gitlabProviderId: string) => {
console.log("Refreshed token"); console.log("Refreshed token");
await updateGitlabProvider(gitlabProviderId, { await updateGitlab(gitlabProviderId, {
accessToken: data.access_token, accessToken: data.access_token,
refreshToken: data.refresh_token, refreshToken: data.refresh_token,
expiresAt, expiresAt,
@ -56,7 +56,7 @@ export const refreshGitlabToken = async (gitlabProviderId: string) => {
return data; return data;
}; };
export const haveGitlabRequirements = (gitlabProvider: GitlabProvider) => { export const haveGitlabRequirements = (gitlabProvider: Gitlab) => {
return !!(gitlabProvider?.accessToken && gitlabProvider?.refreshToken); return !!(gitlabProvider?.accessToken && gitlabProvider?.refreshToken);
}; };
@ -77,13 +77,10 @@ const getErrorCloneRequirements = (entity: {
export type ApplicationWithGitlab = InferResultType< export type ApplicationWithGitlab = InferResultType<
"applications", "applications",
{ gitlabProvider: true } { gitlab: true }
>; >;
export type ComposeWithGitlab = InferResultType< export type ComposeWithGitlab = InferResultType<"compose", { gitlab: true }>;
"compose",
{ gitlabProvider: true }
>;
export const cloneGitlabRepository = async ( export const cloneGitlabRepository = async (
entity: ApplicationWithGitlab | ComposeWithGitlab, entity: ApplicationWithGitlab | ComposeWithGitlab,
@ -91,13 +88,8 @@ export const cloneGitlabRepository = async (
isCompose = false, isCompose = false,
) => { ) => {
const writeStream = createWriteStream(logPath, { flags: "a" }); const writeStream = createWriteStream(logPath, { flags: "a" });
const { const { appName, gitlabBranch, gitlabId, gitlab, gitlabPathNamespace } =
appName, entity;
gitlabBranch,
gitlabId,
gitlabProvider,
gitlabPathNamespace,
} = entity;
if (!gitlabId) { if (!gitlabId) {
throw new TRPCError({ throw new TRPCError({
@ -127,7 +119,7 @@ export const cloneGitlabRepository = async (
const outputPath = join(basePath, appName, "code"); const outputPath = join(basePath, appName, "code");
await recreateDirectory(outputPath); await recreateDirectory(outputPath);
const repoclone = `gitlab.com/${gitlabPathNamespace}.git`; const repoclone = `gitlab.com/${gitlabPathNamespace}.git`;
const cloneUrl = `https://oauth2:${gitlabProvider?.accessToken}@${repoclone}`; const cloneUrl = `https://oauth2:${gitlab?.accessToken}@${repoclone}`;
try { try {
writeStream.write(`\nClonning Repo ${repoclone} to ${outputPath}: ✅\n`); writeStream.write(`\nClonning Repo ${repoclone} to ${outputPath}: ✅\n`);
@ -167,7 +159,7 @@ export const getGitlabRepositories = async (input: {
await refreshGitlabToken(input.gitlabId); await refreshGitlabToken(input.gitlabId);
const gitlabProvider = await getGitlabProvider(input.gitlabId); const gitlabProvider = await findGitlabById(input.gitlabId);
const response = await fetch( const response = await fetch(
`https://gitlab.com/api/v4/projects?membership=true&owned=true&page=${0}&per_page=${100}`, `https://gitlab.com/api/v4/projects?membership=true&owned=true&page=${0}&per_page=${100}`,
@ -227,7 +219,7 @@ export const getGitlabBranches = async (input: {
return []; return [];
} }
const gitlabProvider = await getGitlabProvider(input.gitlabId); const gitlabProvider = await findGitlabById(input.gitlabId);
const branchesResponse = await fetch( const branchesResponse = await fetch(
`https://gitlab.com/api/v4/projects/${input.id}/repository/branches`, `https://gitlab.com/api/v4/projects/${input.id}/repository/branches`,
@ -270,7 +262,7 @@ export const cloneRawGitlabRepository = async (entity: Compose) => {
}); });
} }
const gitlabProvider = await getGitlabProvider(gitlabId); const gitlabProvider = await findGitlabById(gitlabId);
await refreshGitlabToken(gitlabId); await refreshGitlabToken(gitlabId);
const basePath = COMPOSE_PATH; const basePath = COMPOSE_PATH;

File diff suppressed because one or more lines are too long