feat: add test connection bitbucket and gitlab

This commit is contained in:
Mauricio Siu
2024-09-01 21:49:59 -06:00
parent d261fd4efe
commit 99f63597a8
32 changed files with 1120 additions and 596 deletions

View File

@@ -58,7 +58,7 @@ interface Props {
export const SaveBitbucketProvider = ({ applicationId }: Props) => { export const SaveBitbucketProvider = ({ applicationId }: Props) => {
const { data: bitbucketProviders } = const { data: bitbucketProviders } =
api.gitProvider.bitbucketProviders.useQuery(); api.bitbucket.bitbucketProviders.useQuery();
const { data, refetch } = api.application.one.useQuery({ applicationId }); const { data, refetch } = api.application.one.useQuery({ applicationId });
const { mutateAsync, isLoading: isSavingBitbucketProvider } = const { mutateAsync, isLoading: isSavingBitbucketProvider } =
@@ -85,15 +85,20 @@ export const SaveBitbucketProvider = ({ applicationId }: Props) => {
isLoading: isLoadingRepositories, isLoading: isLoadingRepositories,
error, error,
isError, isError,
} = api.gitProvider.getBitbucketRepositories.useQuery({ } = api.bitbucket.getBitbucketRepositories.useQuery(
bitbucketId, {
}); bitbucketId,
},
{
enabled: !!bitbucketId,
},
);
const { const {
data: branches, data: branches,
fetchStatus, fetchStatus,
status, status,
} = api.gitProvider.getBitbucketBranches.useQuery( } = api.bitbucket.getBitbucketBranches.useQuery(
{ {
owner: repository?.owner, owner: repository?.owner,
repo: repository?.repo, repo: repository?.repo,

View File

@@ -56,7 +56,7 @@ interface Props {
} }
export const SaveGithubProvider = ({ applicationId }: Props) => { export const SaveGithubProvider = ({ applicationId }: Props) => {
const { data: githubProviders } = api.gitProvider.githubProviders.useQuery(); const { data: githubProviders } = api.github.githubProviders.useQuery();
const { data, refetch } = api.application.one.useQuery({ applicationId }); const { data, refetch } = api.application.one.useQuery({ applicationId });
const { mutateAsync, isLoading: isSavingGithubProvider } = const { mutateAsync, isLoading: isSavingGithubProvider } =
@@ -79,15 +79,20 @@ export const SaveGithubProvider = ({ applicationId }: Props) => {
const githubId = form.watch("githubId"); const githubId = form.watch("githubId");
const { data: repositories, isLoading: isLoadingRepositories } = const { data: repositories, isLoading: isLoadingRepositories } =
api.gitProvider.getGithubRepositories.useQuery({ api.github.getGithubRepositories.useQuery(
githubId, {
}); githubId,
},
{
enabled: !!githubId,
},
);
const { const {
data: branches, data: branches,
fetchStatus, fetchStatus,
status, status,
} = api.gitProvider.getGithubBranches.useQuery( } = api.github.getGithubBranches.useQuery(
{ {
owner: repository?.owner, owner: repository?.owner,
repo: repository?.repo, repo: repository?.repo,

View File

@@ -59,7 +59,7 @@ interface Props {
} }
export const SaveGitlabProvider = ({ applicationId }: Props) => { export const SaveGitlabProvider = ({ applicationId }: Props) => {
const { data: gitlabProviders } = api.gitProvider.gitlabProviders.useQuery(); const { data: gitlabProviders } = api.gitlab.gitlabProviders.useQuery();
const { data, refetch } = api.application.one.useQuery({ applicationId }); const { data, refetch } = api.application.one.useQuery({ applicationId });
const { mutateAsync, isLoading: isSavingGitlabProvider } = const { mutateAsync, isLoading: isSavingGitlabProvider } =
@@ -87,15 +87,20 @@ export const SaveGitlabProvider = ({ applicationId }: Props) => {
data: repositories, data: repositories,
isLoading: isLoadingRepositories, isLoading: isLoadingRepositories,
error, error,
} = api.gitProvider.getGitlabRepositories.useQuery({ } = api.gitlab.getGitlabRepositories.useQuery(
gitlabId, {
}); gitlabId,
},
{
enabled: !!gitlabId,
},
);
const { const {
data: branches, data: branches,
fetchStatus, fetchStatus,
status, status,
} = api.gitProvider.getGitlabBranches.useQuery( } = api.gitlab.getGitlabBranches.useQuery(
{ {
owner: repository?.owner, owner: repository?.owner,
repo: repository?.repo, repo: repository?.repo,

View File

@@ -25,10 +25,10 @@ interface Props {
} }
export const ShowProviderForm = ({ applicationId }: Props) => { export const ShowProviderForm = ({ applicationId }: Props) => {
const { data: githubProviders } = api.gitProvider.githubProviders.useQuery(); const { data: githubProviders } = api.github.githubProviders.useQuery();
const { data: gitlabProviders } = api.gitProvider.gitlabProviders.useQuery(); const { data: gitlabProviders } = api.gitlab.gitlabProviders.useQuery();
const { data: bitbucketProviders } = const { data: bitbucketProviders } =
api.gitProvider.bitbucketProviders.useQuery(); api.bitbucket.bitbucketProviders.useQuery();
const { data: application } = api.application.one.useQuery({ applicationId }); const { data: application } = api.application.one.useQuery({ applicationId });
const [tab, setSab] = useState<TabState>(application?.sourceType || "github"); const [tab, setSab] = useState<TabState>(application?.sourceType || "github");

View File

@@ -58,7 +58,7 @@ interface Props {
export const SaveBitbucketProviderCompose = ({ composeId }: Props) => { export const SaveBitbucketProviderCompose = ({ composeId }: Props) => {
const { data: bitbucketProviders } = const { data: bitbucketProviders } =
api.gitProvider.bitbucketProviders.useQuery(); api.bitbucket.bitbucketProviders.useQuery();
const { data, refetch } = api.compose.one.useQuery({ composeId }); const { data, refetch } = api.compose.one.useQuery({ composeId });
const { mutateAsync, isLoading: isSavingBitbucketProvider } = const { mutateAsync, isLoading: isSavingBitbucketProvider } =
@@ -85,15 +85,20 @@ export const SaveBitbucketProviderCompose = ({ composeId }: Props) => {
isLoading: isLoadingRepositories, isLoading: isLoadingRepositories,
error, error,
isError, isError,
} = api.gitProvider.getBitbucketRepositories.useQuery({ } = api.bitbucket.getBitbucketRepositories.useQuery(
bitbucketId, {
}); bitbucketId,
},
{
enabled: !!bitbucketId,
},
);
const { const {
data: branches, data: branches,
fetchStatus, fetchStatus,
status, status,
} = api.gitProvider.getBitbucketBranches.useQuery( } = api.bitbucket.getBitbucketBranches.useQuery(
{ {
owner: repository?.owner, owner: repository?.owner,
repo: repository?.repo, repo: repository?.repo,

View File

@@ -56,7 +56,7 @@ interface Props {
} }
export const SaveGithubProviderCompose = ({ composeId }: Props) => { export const SaveGithubProviderCompose = ({ composeId }: Props) => {
const { data: githubProviders } = api.gitProvider.githubProviders.useQuery(); const { data: githubProviders } = api.github.githubProviders.useQuery();
const { data, refetch } = api.compose.one.useQuery({ composeId }); const { data, refetch } = api.compose.one.useQuery({ composeId });
const { mutateAsync, isLoading: isSavingGithubProvider } = const { mutateAsync, isLoading: isSavingGithubProvider } =
@@ -79,15 +79,20 @@ export const SaveGithubProviderCompose = ({ composeId }: Props) => {
const githubId = form.watch("githubId"); const githubId = form.watch("githubId");
const { data: repositories, isLoading: isLoadingRepositories } = const { data: repositories, isLoading: isLoadingRepositories } =
api.gitProvider.getRepositories.useQuery({ api.github.getGithubRepositories.useQuery(
githubId, {
}); githubId,
},
{
enabled: !!githubId,
},
);
const { const {
data: branches, data: branches,
fetchStatus, fetchStatus,
status, status,
} = api.gitProvider.getBranches.useQuery( } = api.github.getGithubBranches.useQuery(
{ {
owner: repository?.owner, owner: repository?.owner,
repo: repository?.repo, repo: repository?.repo,

View File

@@ -59,7 +59,7 @@ interface Props {
} }
export const SaveGitlabProviderCompose = ({ composeId }: Props) => { export const SaveGitlabProviderCompose = ({ composeId }: Props) => {
const { data: gitlabProviders } = api.gitProvider.gitlabProviders.useQuery(); const { data: gitlabProviders } = api.gitlab.gitlabProviders.useQuery();
const { data, refetch } = api.compose.one.useQuery({ composeId }); const { data, refetch } = api.compose.one.useQuery({ composeId });
const { mutateAsync, isLoading: isSavingGitlabProvider } = const { mutateAsync, isLoading: isSavingGitlabProvider } =
@@ -87,15 +87,20 @@ export const SaveGitlabProviderCompose = ({ composeId }: Props) => {
data: repositories, data: repositories,
isLoading: isLoadingRepositories, isLoading: isLoadingRepositories,
error, error,
} = api.gitProvider.getGitlabRepositories.useQuery({ } = api.gitlab.getGitlabRepositories.useQuery(
gitlabId, {
}); gitlabId,
},
{
enabled: !!gitlabId,
},
);
const { const {
data: branches, data: branches,
fetchStatus, fetchStatus,
status, status,
} = api.gitProvider.getGitlabBranches.useQuery( } = api.gitlab.getGitlabBranches.useQuery(
{ {
owner: repository?.owner, owner: repository?.owner,
repo: repository?.repo, repo: repository?.repo,

View File

@@ -23,10 +23,10 @@ interface Props {
} }
export const ShowProviderFormCompose = ({ composeId }: Props) => { export const ShowProviderFormCompose = ({ composeId }: Props) => {
const { data: githubProviders } = api.gitProvider.githubProviders.useQuery(); const { data: githubProviders } = api.github.githubProviders.useQuery();
const { data: gitlabProviders } = api.gitProvider.gitlabProviders.useQuery(); const { data: gitlabProviders } = api.gitlab.gitlabProviders.useQuery();
const { data: bitbucketProviders } = const { data: bitbucketProviders } =
api.gitProvider.bitbucketProviders.useQuery(); api.bitbucket.bitbucketProviders.useQuery();
const { data: compose } = api.compose.one.useQuery({ composeId }); const { data: compose } = api.compose.one.useQuery({ composeId });
const [tab, setSab] = useState<TabState>(compose?.sourceType || "github"); const [tab, setSab] = useState<TabState>(compose?.sourceType || "github");

View File

@@ -52,8 +52,7 @@ export const AddBitbucketProvider = () => {
const utils = api.useUtils(); const utils = api.useUtils();
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.bitbucket.create.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

@@ -1,8 +1,4 @@
import { import { BitbucketIcon } from "@/components/icons/data-tools-icons";
BitbucketIcon,
GithubIcon,
GitlabIcon,
} from "@/components/icons/data-tools-icons";
import { AlertBlock } from "@/components/shared/alert-block"; import { AlertBlock } from "@/components/shared/alert-block";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { CardContent } from "@/components/ui/card"; import { CardContent } from "@/components/ui/card";
@@ -23,11 +19,8 @@ import {
} from "@/components/ui/form"; } from "@/components/ui/form";
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { api } from "@/utils/api"; import { api } from "@/utils/api";
import { useUrl } from "@/utils/hooks/use-url";
import { zodResolver } from "@hookform/resolvers/zod"; import { zodResolver } from "@hookform/resolvers/zod";
import { ExternalLink } from "lucide-react"; import { Edit } from "lucide-react";
import Link from "next/link";
import { useRouter } from "next/router";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { useForm } from "react-hook-form"; import { useForm } from "react-hook-form";
import { toast } from "sonner"; import { toast } from "sonner";
@@ -40,73 +33,77 @@ const Schema = z.object({
username: z.string().min(1, { username: z.string().min(1, {
message: "Username is required", message: "Username is required",
}), }),
password: z.string().min(1, {
message: "App Password is required",
}),
workspaceName: z.string().optional(), workspaceName: z.string().optional(),
}); });
type Schema = z.infer<typeof Schema>; type Schema = z.infer<typeof Schema>;
export const AddBitbucketProvider = () => { interface Props {
// const {data} = api.gitProvider. bitbucketId: string;
}
export const EditBitbucketProvider = ({ bitbucketId }: Props) => {
const { data: bitbucket } = api.bitbucket.one.useQuery(
{
bitbucketId,
},
{
enabled: !!bitbucketId,
},
);
const utils = api.useUtils(); const utils = api.useUtils();
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
const url = useUrl(); const { mutateAsync, error, isError } = api.bitbucket.update.useMutation();
const { mutateAsync, error, isError } = const { mutateAsync: testConnection, isLoading } =
api.gitProvider.createBitbucket.useMutation(); api.bitbucket.testConnection.useMutation();
const { data: auth } = api.auth.get.useQuery();
const router = useRouter();
const form = useForm<Schema>({ const form = useForm<Schema>({
defaultValues: { defaultValues: {
username: "", username: "",
password: "",
workspaceName: "", workspaceName: "",
}, },
resolver: zodResolver(Schema), resolver: zodResolver(Schema),
}); });
const username = form.watch("username");
const workspaceName = form.watch("workspaceName");
useEffect(() => { useEffect(() => {
form.reset({ form.reset({
username: "", username: bitbucket?.bitbucketUsername || "",
password: "", workspaceName: bitbucket?.bitbucketWorkspaceName || "",
workspaceName: "", name: bitbucket?.gitProvider.name || "",
}); });
}, [form, isOpen]); }, [form, isOpen]);
const onSubmit = async (data: Schema) => { const onSubmit = async (data: Schema) => {
await mutateAsync({ await mutateAsync({
bitbucketId,
gitProviderId: bitbucket?.gitProviderId || "",
bitbucketUsername: data.username, bitbucketUsername: data.username,
appPassword: data.password,
bitbucketWorkspaceName: data.workspaceName || "", bitbucketWorkspaceName: data.workspaceName || "",
authId: auth?.id || "",
name: data.name || "", name: data.name || "",
}) })
.then(async () => { .then(async () => {
await utils.gitProvider.getAll.invalidate(); await utils.gitProvider.getAll.invalidate();
toast.success("Bitbucket configured successfully"); toast.success("Bitbucket updated successfully");
setIsOpen(false); setIsOpen(false);
}) })
.catch(() => { .catch(() => {
toast.error("Error configuring Bitbucket"); toast.error("Error to update Bitbucket");
}); });
}; };
return ( return (
<Dialog open={isOpen} onOpenChange={setIsOpen}> <Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogTrigger asChild> <DialogTrigger asChild>
<Button <Button variant="ghost">
variant="secondary" <Edit className="size-4" />
className="flex items-center space-x-1 bg-blue-700 text-white hover:bg-blue-600"
>
<BitbucketIcon />
<span>Bitbucket</span>
</Button> </Button>
</DialogTrigger> </DialogTrigger>
<DialogContent className="sm:max-w-2xl overflow-y-auto max-h-screen"> <DialogContent className="sm:max-w-2xl overflow-y-auto max-h-screen">
<DialogHeader> <DialogHeader>
<DialogTitle className="flex items-center gap-2"> <DialogTitle className="flex items-center gap-2">
Bitbucket Provider <BitbucketIcon className="size-5" /> Update Bitbucket Provider <BitbucketIcon className="size-5" />
</DialogTitle> </DialogTitle>
</DialogHeader> </DialogHeader>
@@ -119,37 +116,6 @@ export const AddBitbucketProvider = () => {
> >
<CardContent className="p-0"> <CardContent className="p-0">
<div className="flex flex-col gap-4"> <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 <FormField
control={form.control} control={form.control}
name="name" name="name"
@@ -183,24 +149,6 @@ export const AddBitbucketProvider = () => {
)} )}
/> />
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
<FormLabel>App Password</FormLabel>
<FormControl>
<Input
type="password"
placeholder="ATBBPDYUC94nR96Nj7Cqpp4pfwKk03573DD2"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField <FormField
control={form.control} control={form.control}
name="workspaceName" name="workspaceName"
@@ -218,9 +166,31 @@ export const AddBitbucketProvider = () => {
)} )}
/> />
<Button isLoading={form.formState.isSubmitting}> <div className="flex w-full justify-end gap-4 mt-4">
Configure Bitbucket <Button
</Button> type="button"
variant={"secondary"}
isLoading={isLoading}
onClick={async () => {
await testConnection({
bitbucketId,
bitbucketUsername: username,
workspaceName: workspaceName,
})
.then(async (message) => {
toast.info(`Message: ${message}`);
})
.catch((error) => {
toast.error(`Error: ${error.message}`);
});
}}
>
Test Connection
</Button>
<Button type="submit" isLoading={form.formState.isSubmitting}>
Update
</Button>
</div>
</div> </div>
</CardContent> </CardContent>
</form> </form>

View File

@@ -53,8 +53,7 @@ export const AddGitlabProvider = () => {
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
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.gitlab.create.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

@@ -0,0 +1,180 @@
import { 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 { Edit } from "lucide-react";
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",
}),
groupName: z.string().optional(),
});
type Schema = z.infer<typeof Schema>;
interface Props {
gitlabId: string;
}
export const EditGitlabProvider = ({ gitlabId }: Props) => {
const { data: gitlab } = api.gitlab.one.useQuery(
{
gitlabId,
},
{
enabled: !!gitlabId,
},
);
const utils = api.useUtils();
const [isOpen, setIsOpen] = useState(false);
const { mutateAsync, error, isError } = api.gitlab.update.useMutation();
const { mutateAsync: testConnection, isLoading } =
api.gitlab.testConnection.useMutation();
const form = useForm<Schema>({
defaultValues: {
groupName: "",
name: "",
},
resolver: zodResolver(Schema),
});
const groupName = form.watch("groupName");
useEffect(() => {
form.reset({
groupName: gitlab?.groupName || "",
name: gitlab?.gitProvider.name || "",
});
}, [form, isOpen]);
const onSubmit = async (data: Schema) => {
await mutateAsync({
gitlabId,
gitProviderId: gitlab?.gitProviderId || "",
groupName: data.groupName || "",
name: data.name || "",
})
.then(async () => {
await utils.gitProvider.getAll.invalidate();
toast.success("Gitlab updated successfully");
setIsOpen(false);
})
.catch(() => {
toast.error("Error to update Gitlab");
});
};
return (
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogTrigger asChild>
<Button variant="ghost">
<Edit className="size-4" />
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-2xl overflow-y-auto max-h-screen">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
Update GitLab Provider <GitlabIcon 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">
<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="groupName"
render={({ field }) => (
<FormItem>
<FormLabel>Group Name (Optional)</FormLabel>
<FormControl>
<Input
placeholder="For organization/group access"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<div className="flex w-full justify-end gap-4 mt-4">
<Button
type="button"
variant={"secondary"}
isLoading={isLoading}
onClick={async () => {
await testConnection({
gitlabId,
groupName: groupName || "",
})
.then(async (message) => {
toast.info(`Message: ${message}`);
})
.catch((error) => {
toast.error(`Error: ${error.message}`);
});
}}
>
Test Connection
</Button>
<Button type="submit" isLoading={form.formState.isSubmitting}>
Update
</Button>
</div>
</div>
</CardContent>
</form>
</Form>
</DialogContent>
</Dialog>
);
};

View File

@@ -12,6 +12,9 @@ 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";
import { useUrl } from "@/utils/hooks/use-url"; import { useUrl } from "@/utils/hooks/use-url";
import { EditBitbucketProvider } from "./bitbucket/edit-bitbucket-provider";
import { EditGitlabProvider } from "./gitlab/edit-gitlab-provider";
import { formatDate } from "date-fns";
export const ShowGitProviders = () => { export const ShowGitProviders = () => {
const { data } = api.gitProvider.getAll.useQuery(); const { data } = api.gitProvider.getAll.useQuery();
@@ -48,6 +51,7 @@ export const ShowGitProviders = () => {
{data?.map((gitProvider, index) => { {data?.map((gitProvider, index) => {
const isGithub = gitProvider.providerType === "github"; const isGithub = gitProvider.providerType === "github";
const isGitlab = gitProvider.providerType === "gitlab"; const isGitlab = gitProvider.providerType === "gitlab";
const isBitbucket = gitProvider.providerType === "bitbucket";
const haveGithubRequirements = const haveGithubRequirements =
gitProvider.providerType === "github" && gitProvider.providerType === "github" &&
gitProvider.github?.githubPrivateKey && gitProvider.github?.githubPrivateKey &&
@@ -61,7 +65,7 @@ export const ShowGitProviders = () => {
className="space-y-4" className="space-y-4"
key={`${gitProvider.gitProviderId}-${index}`} key={`${gitProvider.gitProviderId}-${index}`}
> >
<Card className="flex sm:flex-row max-sm:gap-2 flex-col justify-between items-center p-4 h-full"> <Card className="flex sm:flex-row max-sm:gap-2 flex-col justify-between items-center p-4 h-full bg-transparent">
<div className="flex items-center space-x-4 w-full"> <div className="flex items-center space-x-4 w-full">
{gitProvider.providerType === "github" && ( {gitProvider.providerType === "github" && (
<GithubIcon className="w-6 h-6" /> <GithubIcon className="w-6 h-6" />
@@ -72,7 +76,7 @@ export const ShowGitProviders = () => {
{gitProvider.providerType === "bitbucket" && ( {gitProvider.providerType === "bitbucket" && (
<BitbucketIcon className="w-6 h-6" /> <BitbucketIcon className="w-6 h-6" />
)} )}
<div> <div className="flex flex-col gap-1">
<p className="font-medium"> <p className="font-medium">
{gitProvider.providerType === "github" {gitProvider.providerType === "github"
? "GitHub" ? "GitHub"
@@ -83,6 +87,15 @@ export const ShowGitProviders = () => {
<p className="text-sm text-muted-foreground"> <p className="text-sm text-muted-foreground">
{gitProvider.name} {gitProvider.name}
</p> </p>
<span>
<p className="text-sm text-muted-foreground">
Created{" "}
{formatDate(
gitProvider.createdAt,
"yyyy-MM-dd hh:mm:ss a",
)}
</p>
</span>
</div> </div>
</div> </div>
<div className="flex sm:gap-4 sm:flex-row flex-col"> <div className="flex sm:gap-4 sm:flex-row flex-col">
@@ -92,7 +105,7 @@ export const ShowGitProviders = () => {
href={`${gitProvider?.github?.githubAppName}/installations/new?state=gh_setup:${gitProvider?.github.githubId}`} href={`${gitProvider?.github?.githubAppName}/installations/new?state=gh_setup:${gitProvider?.github.githubId}`}
className={buttonVariants({ className: "w-fit" })} className={buttonVariants({ className: "w-fit" })}
> >
Install Github App Install
</Link> </Link>
</div> </div>
)} )}
@@ -107,7 +120,7 @@ export const ShowGitProviders = () => {
variant: "secondary", variant: "secondary",
})} })}
> >
<span className="text-sm">Manage Github App</span> <span className="text-sm">Manage</span>
</Link> </Link>
</div> </div>
)} )}
@@ -125,15 +138,26 @@ export const ShowGitProviders = () => {
variant: "secondary", variant: "secondary",
})} })}
> >
<span className="text-sm">Install Gitlab App</span> <span className="text-sm">Install</span>
</Link> </Link>
</div> </div>
)} )}
<div className="flex flex-row gap-1">
<RemoveGitProvider {isBitbucket && (
gitProviderId={gitProvider.gitProviderId} <EditBitbucketProvider
gitProviderType={gitProvider.providerType} bitbucketId={gitProvider.bitbucket.bitbucketId}
/> />
)}
{isGitlab && haveGitlabRequirements && (
<EditGitlabProvider
gitlabId={gitProvider.gitlab.gitlabId}
/>
)}
<RemoveGitProvider
gitProviderId={gitProvider.gitProviderId}
gitProviderType={gitProvider.providerType}
/>
</div>
</div> </div>
</Card> </Card>
</div> </div>

View File

@@ -25,7 +25,10 @@ import { securityRouter } from "./routers/security";
import { settingsRouter } from "./routers/settings"; import { settingsRouter } from "./routers/settings";
import { sshRouter } from "./routers/ssh-key"; import { sshRouter } from "./routers/ssh-key";
import { userRouter } from "./routers/user"; import { userRouter } from "./routers/user";
import { gitProvider } from "./routers/git-provider"; import { gitProviderRouter } from "./routers/git-provider";
import { bitbucketRouter } from "./routers/bitbucket";
import { githubRouter } from "./routers/github";
import { gitlabRouter } from "./routers/gitlab";
/** /**
* This is the primary router for your server. * This is the primary router for your server.
@@ -59,7 +62,10 @@ export const appRouter = createTRPCRouter({
cluster: clusterRouter, cluster: clusterRouter,
notification: notificationRouter, notification: notificationRouter,
sshKey: sshRouter, sshKey: sshRouter,
gitProvider: gitProvider, gitProvider: gitProviderRouter,
bitbucket: bitbucketRouter,
gitlab: gitlabRouter,
github: githubRouter,
}); });
// export type definition of API // export type definition of API

View File

@@ -0,0 +1,82 @@
import { createTRPCRouter, protectedProcedure } from "@/server/api/trpc";
import {
apiBitbucketTestConnection,
apiCreateBitbucket,
apiFindBitbucketBranches,
apiFindOneBitbucket,
apiUpdateBitbucket,
} from "@/server/db/schema";
import { db } from "@/server/db";
import {
getBitbucketRepositories,
getBitbucketBranches,
testBitbucketConnection,
} from "@/server/utils/providers/bitbucket";
import { TRPCError } from "@trpc/server";
import {
createBitbucket,
findBitbucketById,
updateBitbucket,
} from "../services/bitbucket";
export const bitbucketRouter = createTRPCRouter({
create: protectedProcedure
.input(apiCreateBitbucket)
.mutation(async ({ input }) => {
try {
return await createBitbucket(input);
} catch (error) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error to create this bitbucket provider",
cause: error,
});
}
}),
one: protectedProcedure
.input(apiFindOneBitbucket)
.query(async ({ input }) => {
return await findBitbucketById(input.bitbucketId);
}),
bitbucketProviders: protectedProcedure.query(async () => {
const result = await db.query.bitbucket.findMany({
with: {
gitProvider: true,
},
columns: {
bitbucketId: true,
},
});
return result;
}),
getBitbucketRepositories: protectedProcedure
.input(apiFindOneBitbucket)
.query(async ({ input }) => {
return await getBitbucketRepositories(input.bitbucketId);
}),
getBitbucketBranches: protectedProcedure
.input(apiFindBitbucketBranches)
.query(async ({ input }) => {
return await getBitbucketBranches(input);
}),
testConnection: protectedProcedure
.input(apiBitbucketTestConnection)
.mutation(async ({ input }) => {
try {
const result = await testBitbucketConnection(input);
return `Found ${result} repositories`;
} catch (error) {
throw new TRPCError({
code: "BAD_REQUEST",
message: error instanceof Error ? error?.message : `Error: ${error}`,
});
}
}),
update: protectedProcedure
.input(apiUpdateBitbucket)
.mutation(async ({ input }) => {
return await updateBitbucket(input.bitbucketId, input);
}),
});

View File

@@ -1,42 +1,11 @@
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 { apiRemoveGitProvider, gitProvider } from "@/server/db/schema";
apiCreateBitbucket,
apiCreateGitlab,
apiFindBitbucketBranches,
apiFindGithubBranches,
apiFindGitlabBranches,
apiFindOneBitbucket,
apiFindOneGithub,
apiFindOneGitlab,
apiRemoveGitProvider,
} from "@/server/db/schema";
import { TRPCError } from "@trpc/server"; import { TRPCError } from "@trpc/server";
import { import { removeGitProvider } from "../services/git-provider";
createBitbucket, import { asc, desc } from "drizzle-orm";
createGitlab,
findBitbucketById,
findGithubById,
findGitlabById,
haveGithubRequirements,
removeGitProvider,
} from "../services/git-provider";
import { z } from "zod";
import {
getGitlabBranches,
getGitlabRepositories,
haveGitlabRequirements,
} from "@/server/utils/providers/gitlab";
import {
getBitbucketBranches,
getBitbucketRepositories,
} from "@/server/utils/providers/bitbucket";
import {
getGithubBranches,
getGithubRepositories,
} from "@/server/utils/providers/github";
export const gitProvider = createTRPCRouter({ export const gitProviderRouter = createTRPCRouter({
getAll: protectedProcedure.query(async () => { getAll: protectedProcedure.query(async () => {
return await db.query.gitProvider.findMany({ return await db.query.gitProvider.findMany({
with: { with: {
@@ -44,23 +13,9 @@ export const gitProvider = createTRPCRouter({
bitbucket: true, bitbucket: true,
github: true, github: true,
}, },
orderBy: desc(gitProvider.createdAt),
}); });
}), }),
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 remove: protectedProcedure
.input(apiRemoveGitProvider) .input(apiRemoveGitProvider)
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
@@ -73,112 +28,4 @@ export const gitProvider = createTRPCRouter({
}); });
} }
}), }),
createGitlab: protectedProcedure
.input(apiCreateGitlab)
.mutation(async ({ input }) => {
try {
return await createGitlab(input);
} catch (error) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error to create this gitlab provider",
cause: error,
});
}
}),
createBitbucket: protectedProcedure
.input(apiCreateBitbucket)
.mutation(async ({ input }) => {
try {
return await createBitbucket(input);
} catch (error) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error to create this bitbucket provider",
cause: error,
});
}
}),
githubProviders: protectedProcedure.query(async () => {
const result = await db.query.github.findMany({
with: {
gitProvider: true,
},
});
const filtered = result
.filter((provider) => haveGithubRequirements(provider))
.map((provider) => {
return {
githubId: provider.githubId,
gitProvider: {
...provider.gitProvider,
},
};
});
return filtered;
}),
gitlabProviders: protectedProcedure.query(async () => {
const result = await db.query.gitlab.findMany({
with: {
gitProvider: true,
},
});
const filtered = result
.filter((provider) => haveGitlabRequirements(provider))
.map((provider) => {
return {
gitlabId: provider.gitlabId,
gitProvider: {
...provider.gitProvider,
},
};
});
return filtered;
}),
bitbucketProviders: protectedProcedure.query(async () => {
const result = await db.query.bitbucket.findMany({
with: {
gitProvider: true,
},
columns: {
bitbucketId: true,
},
});
return result;
}),
getGitlabRepositories: protectedProcedure
.input(apiFindOneGitlab)
.query(async ({ input }) => {
return await getGitlabRepositories(input.gitlabId);
}),
getGitlabBranches: protectedProcedure
.input(apiFindGitlabBranches)
.query(async ({ input }) => {
return await getGitlabBranches(input);
}),
getBitbucketRepositories: protectedProcedure
.input(apiFindOneBitbucket)
.query(async ({ input }) => {
return await getBitbucketRepositories(input.bitbucketId);
}),
getBitbucketBranches: protectedProcedure
.input(apiFindBitbucketBranches)
.query(async ({ input }) => {
return await getBitbucketBranches(input);
}),
getGithubRepositories: protectedProcedure
.input(apiFindOneGithub)
.query(async ({ input }) => {
return await getGithubRepositories(input.githubId);
}),
getGithubBranches: protectedProcedure
.input(apiFindGithubBranches)
.query(async ({ input }) => {
return await getGithubBranches(input);
}),
}); });

View File

@@ -0,0 +1,49 @@
import { createTRPCRouter, protectedProcedure } from "@/server/api/trpc";
import { apiFindGithubBranches, apiFindOneGithub } from "@/server/db/schema";
import { db } from "@/server/db";
import { findGithubById, haveGithubRequirements } from "../services/github";
import {
getGithubRepositories,
getGithubBranches,
} from "@/server/utils/providers/github";
export const githubRouter = createTRPCRouter({
one: protectedProcedure.input(apiFindOneGithub).query(async ({ input }) => {
return await findGithubById(input.githubId);
}),
getGithubRepositories: protectedProcedure
.input(apiFindOneGithub)
.query(async ({ input }) => {
return await getGithubRepositories(input.githubId);
}),
getGithubBranches: protectedProcedure
.input(apiFindGithubBranches)
.query(async ({ input }) => {
return await getGithubBranches(input);
}),
githubProviders: protectedProcedure.query(async () => {
const result = await db.query.github.findMany({
with: {
gitProvider: true,
},
});
const filtered = result
.filter((provider) => haveGithubRequirements(provider))
.map((provider) => {
return {
githubId: provider.githubId,
gitProvider: {
...provider.gitProvider,
},
};
});
return filtered;
}),
testConnection: protectedProcedure
.input(apiFindOneGithub)
.query(async ({ input }) => {
return await findGithubById(input.githubId);
}),
});

View File

@@ -0,0 +1,86 @@
import { createTRPCRouter, protectedProcedure } from "@/server/api/trpc";
import {
apiCreateGitlab,
apiFindGitlabBranches,
apiFindOneGitlab,
apiGitlabTestConnection,
apiUpdateGitlab,
} from "@/server/db/schema";
import {
haveGitlabRequirements,
getGitlabRepositories,
getGitlabBranches,
testGitlabConnection,
} from "@/server/utils/providers/gitlab";
import { TRPCError } from "@trpc/server";
import { createGitlab, findGitlabById, updateGitlab } from "../services/gitlab";
import { db } from "@/server/db";
export const gitlabRouter = createTRPCRouter({
create: protectedProcedure
.input(apiCreateGitlab)
.mutation(async ({ input }) => {
try {
return await createGitlab(input);
} catch (error) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error to create this gitlab provider",
cause: error,
});
}
}),
one: protectedProcedure.input(apiFindOneGitlab).query(async ({ input }) => {
return await findGitlabById(input.gitlabId);
}),
gitlabProviders: protectedProcedure.query(async () => {
const result = await db.query.gitlab.findMany({
with: {
gitProvider: true,
},
});
const filtered = result
.filter((provider) => haveGitlabRequirements(provider))
.map((provider) => {
return {
gitlabId: provider.gitlabId,
gitProvider: {
...provider.gitProvider,
},
};
});
return filtered;
}),
getGitlabRepositories: protectedProcedure
.input(apiFindOneGitlab)
.query(async ({ input }) => {
return await getGitlabRepositories(input.gitlabId);
}),
getGitlabBranches: protectedProcedure
.input(apiFindGitlabBranches)
.query(async ({ input }) => {
return await getGitlabBranches(input);
}),
testConnection: protectedProcedure
.input(apiGitlabTestConnection)
.mutation(async ({ input }) => {
try {
const result = await testGitlabConnection(input);
return `Found ${result} repositories`;
} catch (error) {
throw new TRPCError({
code: "BAD_REQUEST",
message: error instanceof Error ? error?.message : `Error: ${error}`,
});
}
}),
update: protectedProcedure
.input(apiUpdateGitlab)
.mutation(async ({ input }) => {
return await updateGitlab(input.gitlabId, input);
}),
});

View File

@@ -0,0 +1,88 @@
import { db } from "@/server/db";
import {
type apiCreateBitbucket,
type apiUpdateBitbucket,
bitbucket,
gitProvider,
} from "@/server/db/schema";
import { TRPCError } from "@trpc/server";
import { eq } from "drizzle-orm";
export type Bitbucket = typeof bitbucket.$inferSelect;
export const createBitbucket = async (
input: typeof apiCreateBitbucket._type,
) => {
return await db.transaction(async (tx) => {
const newGitProvider = await tx
.insert(gitProvider)
.values({
providerType: "bitbucket",
authId: input.authId,
name: input.name,
})
.returning()
.then((response) => response[0]);
if (!newGitProvider) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error to create the git provider",
});
}
await tx
.insert(bitbucket)
.values({
...input,
gitProviderId: newGitProvider?.gitProviderId,
})
.returning()
.then((response) => response[0]);
});
};
export const findBitbucketById = async (bitbucketId: string) => {
const bitbucketProviderResult = await db.query.bitbucket.findFirst({
where: eq(bitbucket.bitbucketId, bitbucketId),
with: {
gitProvider: true,
},
});
if (!bitbucketProviderResult) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Bitbucket Provider not found",
});
}
return bitbucketProviderResult;
};
export const updateBitbucket = async (
bitbucketId: string,
input: typeof apiUpdateBitbucket._type,
) => {
return await db.transaction(async (tx) => {
const result = await tx
.update(bitbucket)
.set({
...input,
})
.where(eq(bitbucket.bitbucketId, bitbucketId))
.returning();
if (input.name) {
await tx
.update(gitProvider)
.set({
name: input.name,
})
.where(eq(gitProvider.gitProviderId, input.gitProviderId))
.returning();
}
return result[0];
});
};

View File

@@ -1,19 +1,8 @@
import { db } from "@/server/db"; import { db } from "@/server/db";
import { import { type apiCreateGithub, github, gitProvider } from "@/server/db/schema";
type apiCreateBitbucket,
type apiCreateGithub,
type apiCreateGitlab,
bitbucket,
github,
gitlab,
gitProvider,
} 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 Github = typeof github.$inferSelect;
export type Gitlab = typeof gitlab.$inferSelect;
export const createGithub = async (input: typeof apiCreateGithub._type) => { export const createGithub = async (input: typeof apiCreateGithub._type) => {
return await db.transaction(async (tx) => { return await db.transaction(async (tx) => {
const newGitProvider = await tx const newGitProvider = await tx
@@ -44,68 +33,6 @@ export const createGithub = async (input: typeof apiCreateGithub._type) => {
}); });
}; };
export const createGitlab = async (input: typeof apiCreateGitlab._type) => {
return await db.transaction(async (tx) => {
const newGitProvider = await tx
.insert(gitProvider)
.values({
providerType: "gitlab",
authId: input.authId,
name: input.name,
})
.returning()
.then((response) => response[0]);
if (!newGitProvider) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error to create the git provider",
});
}
await tx
.insert(gitlab)
.values({
...input,
gitProviderId: newGitProvider?.gitProviderId,
})
.returning()
.then((response) => response[0]);
});
};
export const createBitbucket = async (
input: typeof apiCreateBitbucket._type,
) => {
return await db.transaction(async (tx) => {
const newGitProvider = await tx
.insert(gitProvider)
.values({
providerType: "bitbucket",
authId: input.authId,
name: input.name,
})
.returning()
.then((response) => response[0]);
if (!newGitProvider) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error to create the git provider",
});
}
await tx
.insert(bitbucket)
.values({
...input,
gitProviderId: newGitProvider?.gitProviderId,
})
.returning()
.then((response) => response[0]);
});
};
export const removeGitProvider = async (gitProviderId: string) => { export const removeGitProvider = async (gitProviderId: string) => {
const result = await db const result = await db
.delete(gitProvider) .delete(gitProvider)
@@ -114,71 +41,3 @@ export const removeGitProvider = async (gitProviderId: string) => {
return result[0]; return result[0];
}; };
export const findGithubById = async (githubId: string) => {
const githubProviderResult = await db.query.github.findFirst({
where: eq(github.githubId, githubId),
});
if (!githubProviderResult) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Github Provider not found",
});
}
return githubProviderResult;
};
export const haveGithubRequirements = (github: Github) => {
return !!(
github?.githubAppId &&
github?.githubPrivateKey &&
github?.githubInstallationId
);
};
export const findGitlabById = async (gitlabId: string) => {
const gitlabProviderResult = await db.query.gitlab.findFirst({
where: eq(gitlab.gitlabId, gitlabId),
});
if (!gitlabProviderResult) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Gitlab Provider not found",
});
}
return gitlabProviderResult;
};
export const updateGitlab = async (
gitlabId: string,
input: Partial<Gitlab>,
) => {
const result = await db
.update(gitlab)
.set({
...input,
})
.where(eq(gitlab.gitlabId, gitlabId))
.returning();
return result[0];
};
export const findBitbucketById = async (bitbucketId: string) => {
const bitbucketProviderResult = await db.query.bitbucket.findFirst({
where: eq(bitbucket.bitbucketId, bitbucketId),
});
if (!bitbucketProviderResult) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Bitbucket Provider not found",
});
}
return bitbucketProviderResult;
};

View File

@@ -0,0 +1,58 @@
import { db } from "@/server/db";
import { type apiCreateGithub, github, gitProvider } from "@/server/db/schema";
import { TRPCError } from "@trpc/server";
import { eq } from "drizzle-orm";
export type Github = typeof github.$inferSelect;
export const createGithub = async (input: typeof apiCreateGithub._type) => {
return await db.transaction(async (tx) => {
const newGitProvider = await tx
.insert(gitProvider)
.values({
providerType: "github",
authId: input.authId,
name: input.name,
})
.returning()
.then((response) => response[0]);
if (!newGitProvider) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error to create the git provider",
});
}
return await tx
.insert(github)
.values({
...input,
gitProviderId: newGitProvider?.gitProviderId,
})
.returning()
.then((response) => response[0]);
});
};
export const findGithubById = async (githubId: string) => {
const githubProviderResult = await db.query.github.findFirst({
where: eq(github.githubId, githubId),
});
if (!githubProviderResult) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Github Provider not found",
});
}
return githubProviderResult;
};
export const haveGithubRequirements = (github: Github) => {
return !!(
github?.githubAppId &&
github?.githubPrivateKey &&
github?.githubInstallationId
);
};

View File

@@ -0,0 +1,104 @@
import { db } from "@/server/db";
import {
type apiCreateGitlab,
type apiUpdateGitlab,
type bitbucket,
type github,
gitlab,
gitProvider,
} from "@/server/db/schema";
import { TRPCError } from "@trpc/server";
import { eq } from "drizzle-orm";
export type Github = typeof github.$inferSelect;
export type Bitbucket = typeof bitbucket.$inferSelect;
export type Gitlab = typeof gitlab.$inferSelect;
export const createGitlab = async (input: typeof apiCreateGitlab._type) => {
return await db.transaction(async (tx) => {
const newGitProvider = await tx
.insert(gitProvider)
.values({
providerType: "gitlab",
authId: input.authId,
name: input.name,
})
.returning()
.then((response) => response[0]);
if (!newGitProvider) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error to create the git provider",
});
}
await tx
.insert(gitlab)
.values({
...input,
gitProviderId: newGitProvider?.gitProviderId,
})
.returning()
.then((response) => response[0]);
});
};
export const findGitlabById = async (gitlabId: string) => {
const gitlabProviderResult = await db.query.gitlab.findFirst({
where: eq(gitlab.gitlabId, gitlabId),
with: {
gitProvider: true,
},
});
if (!gitlabProviderResult) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Gitlab Provider not found",
});
}
return gitlabProviderResult;
};
export const updateGitlab = async (
gitlabId: string,
input: typeof apiUpdateGitlab._type,
) => {
return await db.transaction(async (tx) => {
const result = await tx
.update(gitlab)
.set({
...input,
})
.where(eq(gitlab.gitlabId, gitlabId))
.returning();
if (input.name) {
await tx
.update(gitProvider)
.set({
name: input.name,
})
.where(eq(gitProvider.gitProviderId, input.gitProviderId))
.returning();
}
return result[0];
});
};
export const updateGitlabComplete = async (
gitlabId: string,
input: Partial<Gitlab>,
) => {
return await db
.update(gitlab)
.set({
...input,
})
.where(eq(gitlab.gitlabId, gitlabId))
.returning()
.then((response) => response[0]);
};

View File

@@ -21,7 +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 { bitbucket, github, gitlab } from "./git-provider"; import { bitbucket, github, gitlab } from ".";
export const sourceType = pgEnum("sourceType", [ export const sourceType = pgEnum("sourceType", [
"docker", "docker",

View File

@@ -0,0 +1,64 @@
import { relations } from "drizzle-orm";
import { pgTable, text } from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";
import { nanoid } from "nanoid";
import { z } from "zod";
import { gitProvider } from "./git-provider";
export const bitbucket = pgTable("bitbucket", {
bitbucketId: text("bitbucketId")
.notNull()
.primaryKey()
.$defaultFn(() => nanoid()),
bitbucketUsername: text("bitbucketUsername"),
appPassword: text("appPassword"),
bitbucketWorkspaceName: text("bitbucketWorkspaceName"),
gitProviderId: text("gitProviderId")
.notNull()
.references(() => gitProvider.gitProviderId, { onDelete: "cascade" }),
});
export const bitbucketProviderRelations = relations(bitbucket, ({ one }) => ({
gitProvider: one(gitProvider, {
fields: [bitbucket.gitProviderId],
references: [gitProvider.gitProviderId],
}),
}));
const createSchema = createInsertSchema(bitbucket);
export const apiCreateBitbucket = createSchema.extend({
bitbucketUsername: z.string().optional(),
appPassword: z.string().optional(),
bitbucketWorkspaceName: z.string().optional(),
gitProviderId: z.string().optional(),
authId: z.string().min(1),
name: z.string().min(1),
});
export const apiFindOneBitbucket = createSchema
.extend({
bitbucketId: z.string().min(1),
})
.pick({ bitbucketId: true });
export const apiBitbucketTestConnection = createSchema
.extend({
bitbucketId: z.string().min(1),
bitbucketUsername: z.string().optional(),
workspaceName: z.string().optional(),
})
.pick({ bitbucketId: true, bitbucketUsername: true, workspaceName: true });
export const apiFindBitbucketBranches = z.object({
owner: z.string(),
repo: z.string(),
bitbucketId: z.string().optional(),
});
export const apiUpdateBitbucket = createSchema.extend({
bitbucketId: z.string().min(1),
name: z.string().min(1),
bitbucketUsername: z.string().optional(),
bitbucketWorkspaceName: z.string().optional(),
});

View File

@@ -10,7 +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 { bitbucket, github, gitlab } from "./git-provider"; import { bitbucket, github, gitlab } from ".";
export const sourceTypeCompose = pgEnum("sourceTypeCompose", [ export const sourceTypeCompose = pgEnum("sourceTypeCompose", [
"git", "git",

View File

@@ -1,9 +1,12 @@
import { relations } from "drizzle-orm"; import { relations } from "drizzle-orm";
import { pgTable, text, pgEnum, integer } from "drizzle-orm/pg-core"; import { pgTable, text, pgEnum } 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";
import { auth } from "./auth"; import { auth } from "./auth";
import { bitbucket } from "./bitbucket";
import { github } from "./github";
import { gitlab } from "./gitlab";
export const gitProviderType = pgEnum("gitProviderType", [ export const gitProviderType = pgEnum("gitProviderType", [
"github", "github",
@@ -45,74 +48,6 @@ export const gitProviderRelations = relations(gitProvider, ({ one, many }) => ({
}), }),
})); }));
export const github = pgTable("github", {
githubId: text("githubId")
.notNull()
.primaryKey()
.$defaultFn(() => nanoid()),
githubAppName: text("githubAppName"),
githubAppId: integer("githubAppId"),
githubClientId: text("githubClientId"),
githubClientSecret: text("githubClientSecret"),
githubInstallationId: text("githubInstallationId"),
githubPrivateKey: text("githubPrivateKey"),
githubWebhookSecret: text("githubWebhookSecret"),
gitProviderId: text("gitProviderId")
.notNull()
.references(() => gitProvider.gitProviderId, { onDelete: "cascade" }),
});
export const githubProviderRelations = relations(github, ({ one }) => ({
gitProvider: one(gitProvider, {
fields: [github.gitProviderId],
references: [gitProvider.gitProviderId],
}),
}));
export const gitlab = pgTable("gitlab", {
gitlabId: text("gitlabId")
.notNull()
.primaryKey()
.$defaultFn(() => nanoid()),
applicationId: text("application_id"),
redirectUri: text("redirect_uri"),
secret: text("secret"),
accessToken: text("access_token"),
refreshToken: text("refresh_token"),
groupName: text("group_name"),
expiresAt: integer("expires_at"),
gitProviderId: text("gitProviderId")
.notNull()
.references(() => gitProvider.gitProviderId, { onDelete: "cascade" }),
});
export const gitlabProviderRelations = relations(gitlab, ({ one }) => ({
gitProvider: one(gitProvider, {
fields: [gitlab.gitProviderId],
references: [gitProvider.gitProviderId],
}),
}));
export const bitbucket = pgTable("bitbucket", {
bitbucketId: text("bitbucketId")
.notNull()
.primaryKey()
.$defaultFn(() => nanoid()),
bitbucketUsername: text("bitbucketUsername"),
appPassword: text("appPassword"),
bitbucketWorkspaceName: text("bitbucketWorkspaceName"),
gitProviderId: text("gitProviderId")
.notNull()
.references(() => gitProvider.gitProviderId, { onDelete: "cascade" }),
});
export const bitbucketProviderRelations = relations(bitbucket, ({ one }) => ({
gitProvider: one(gitProvider, {
fields: [bitbucket.gitProviderId],
references: [gitProvider.gitProviderId],
}),
}));
const createSchema = createInsertSchema(gitProvider); const createSchema = createInsertSchema(gitProvider);
export const apiRemoveGitProvider = createSchema export const apiRemoveGitProvider = createSchema
@@ -120,77 +55,3 @@ export const apiRemoveGitProvider = createSchema
gitProviderId: z.string().min(1), gitProviderId: z.string().min(1),
}) })
.pick({ gitProviderId: true }); .pick({ gitProviderId: true });
export const apiCreateGithub = createSchema.extend({
githubAppName: z.string().optional(),
githubAppId: z.number().optional(),
githubClientId: z.string().optional(),
githubClientSecret: z.string().optional(),
githubInstallationId: z.string().optional(),
githubPrivateKey: z.string().optional(),
githubWebhookSecret: z.string().nullable(),
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(),
groupName: z.string().optional(),
gitProviderId: z.string().optional(),
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

@@ -0,0 +1,54 @@
import { relations } from "drizzle-orm";
import { pgTable, text, integer } from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";
import { nanoid } from "nanoid";
import { z } from "zod";
import { gitProvider } from "./git-provider";
export const github = pgTable("github", {
githubId: text("githubId")
.notNull()
.primaryKey()
.$defaultFn(() => nanoid()),
githubAppName: text("githubAppName"),
githubAppId: integer("githubAppId"),
githubClientId: text("githubClientId"),
githubClientSecret: text("githubClientSecret"),
githubInstallationId: text("githubInstallationId"),
githubPrivateKey: text("githubPrivateKey"),
githubWebhookSecret: text("githubWebhookSecret"),
gitProviderId: text("gitProviderId")
.notNull()
.references(() => gitProvider.gitProviderId, { onDelete: "cascade" }),
});
export const githubProviderRelations = relations(github, ({ one }) => ({
gitProvider: one(gitProvider, {
fields: [github.gitProviderId],
references: [gitProvider.gitProviderId],
}),
}));
const createSchema = createInsertSchema(github);
export const apiCreateGithub = createSchema.extend({
githubAppName: z.string().optional(),
githubAppId: z.number().optional(),
githubClientId: z.string().optional(),
githubClientSecret: z.string().optional(),
githubInstallationId: z.string().optional(),
githubPrivateKey: z.string().optional(),
githubWebhookSecret: z.string().nullable(),
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 });

View File

@@ -0,0 +1,70 @@
import { relations } from "drizzle-orm";
import { pgTable, text, integer } from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";
import { nanoid } from "nanoid";
import { z } from "zod";
import { gitProvider } from "./git-provider";
export const gitlab = pgTable("gitlab", {
gitlabId: text("gitlabId")
.notNull()
.primaryKey()
.$defaultFn(() => nanoid()),
applicationId: text("application_id"),
redirectUri: text("redirect_uri"),
secret: text("secret"),
accessToken: text("access_token"),
refreshToken: text("refresh_token"),
groupName: text("group_name"),
expiresAt: integer("expires_at"),
gitProviderId: text("gitProviderId")
.notNull()
.references(() => gitProvider.gitProviderId, { onDelete: "cascade" }),
});
export const gitlabProviderRelations = relations(gitlab, ({ one }) => ({
gitProvider: one(gitProvider, {
fields: [gitlab.gitProviderId],
references: [gitProvider.gitProviderId],
}),
}));
const createSchema = createInsertSchema(gitlab);
export const apiCreateGitlab = createSchema.extend({
applicationId: z.string().optional(),
secret: z.string().optional(),
groupName: z.string().optional(),
gitProviderId: z.string().optional(),
redirectUri: z.string().optional(),
authId: z.string().min(1),
name: z.string().min(1),
});
export const apiFindOneGitlab = createSchema
.extend({
gitlabId: z.string().min(1),
})
.pick({ gitlabId: true });
export const apiGitlabTestConnection = createSchema
.extend({
groupName: z.string().optional(),
})
.pick({ gitlabId: true, groupName: true });
export const apiFindGitlabBranches = z.object({
id: z.number().nullable(),
owner: z.string(),
repo: z.string(),
gitlabId: z.string().optional(),
});
export const apiUpdateGitlab = createSchema.extend({
applicationId: z.string().optional(),
secret: z.string().optional(),
groupName: z.string().optional(),
redirectUri: z.string().optional(),
name: z.string().min(1),
gitlabId: z.string().min(1),
});

View File

@@ -24,3 +24,6 @@ export * from "./registry";
export * from "./notification"; export * from "./notification";
export * from "./ssh-key"; export * from "./ssh-key";
export * from "./git-provider"; export * from "./git-provider";
export * from "./bitbucket";
export * from "./github";
export * from "./gitlab";

View File

@@ -5,9 +5,12 @@ 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 { findBitbucketById } from "@/server/api/services/git-provider";
import type { Compose } from "@/server/api/services/compose"; import type { Compose } from "@/server/api/services/compose";
import type { apiFindBitbucketBranches } from "@/server/db/schema"; import type {
apiBitbucketTestConnection,
apiFindBitbucketBranches,
} from "@/server/db/schema";
import { findBitbucketById } from "@/server/api/services/bitbucket";
export type ApplicationWithBitbucket = InferResultType< export type ApplicationWithBitbucket = InferResultType<
"applications", "applications",
@@ -210,3 +213,50 @@ export const getBitbucketBranches = async (
throw error; throw error;
} }
}; };
export const testBitbucketConnection = async (
input: typeof apiBitbucketTestConnection._type,
) => {
const bitbucketProvider = await findBitbucketById(input.bitbucketId);
if (!bitbucketProvider) {
throw new Error("Bitbucket provider not found");
}
const { bitbucketUsername, workspaceName } = input;
const username = workspaceName || bitbucketUsername;
const url = `https://api.bitbucket.org/2.0/repositories/${username}`;
try {
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: `Basic ${Buffer.from(`${bitbucketProvider.bitbucketUsername}:${bitbucketProvider.appPassword}`).toString("base64")}`,
},
});
if (!response.ok) {
throw new TRPCError({
code: "BAD_REQUEST",
message: `Failed to fetch repositories: ${response.statusText}`,
});
}
const data = await response.json();
const mappedData = data.values.map((repo: any) => {
return {
name: repo.name,
url: repo.links.html.href,
owner: {
username: repo.workspace.slug,
},
};
}) as [];
return mappedData.length;
} catch (error) {
throw error;
}
};

View File

@@ -7,12 +7,10 @@ import { Octokit } from "octokit";
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 {
findGithubById,
type Github,
} from "@/server/api/services/git-provider";
import type { Compose } from "@/server/api/services/compose"; import type { Compose } from "@/server/api/services/compose";
import type { apiFindGithubBranches } from "@/server/db/schema"; import type { apiFindGithubBranches } from "@/server/db/schema";
import { type Github, findGithubById } from "@/server/api/services/github";
export const authGithub = (githubProvider: Github) => { export const authGithub = (githubProvider: Github) => {
if (!haveGithubRequirements(githubProvider)) { if (!haveGithubRequirements(githubProvider)) {

View File

@@ -4,13 +4,14 @@ import { APPLICATIONS_PATH, COMPOSE_PATH } from "@/server/constants";
import { TRPCError } from "@trpc/server"; 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 {
findGitlabById,
type Gitlab,
updateGitlab,
} 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";
import {
findGitlabById,
updateGitlabComplete,
type Gitlab,
} from "@/server/api/services/gitlab";
import type { apiGitlabTestConnection } from "@/server/db/schema";
export const refreshGitlabToken = async (gitlabProviderId: string) => { export const refreshGitlabToken = async (gitlabProviderId: string) => {
const gitlabProvider = await findGitlabById(gitlabProviderId); const gitlabProvider = await findGitlabById(gitlabProviderId);
@@ -21,7 +22,6 @@ export const refreshGitlabToken = async (gitlabProviderId: string) => {
gitlabProvider.expiresAt && gitlabProvider.expiresAt &&
currentTime + safetyMargin < gitlabProvider.expiresAt currentTime + safetyMargin < gitlabProvider.expiresAt
) { ) {
console.log("Token still valid, no need to refresh");
return; return;
} }
@@ -48,7 +48,7 @@ export const refreshGitlabToken = async (gitlabProviderId: string) => {
console.log("Refreshed token"); console.log("Refreshed token");
await updateGitlab(gitlabProviderId, { await updateGitlabComplete(gitlabProviderId, {
accessToken: data.access_token, accessToken: data.access_token,
refreshToken: data.refresh_token, refreshToken: data.refresh_token,
expiresAt, expiresAt,
@@ -284,3 +284,46 @@ export const cloneRawGitlabRepository = async (entity: Compose) => {
throw error; throw error;
} }
}; };
export const testGitlabConnection = async (
input: typeof apiGitlabTestConnection._type,
) => {
const { gitlabId, groupName } = input;
if (!gitlabId) {
throw new Error("Gitlab provider not found");
}
await refreshGitlabToken(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}`,
{
headers: {
Authorization: `Bearer ${gitlabProvider.accessToken}`,
},
},
);
if (!response.ok) {
throw new TRPCError({
code: "BAD_REQUEST",
message: `Failed to fetch repositories: ${response.statusText}`,
});
}
const repositories = await response.json();
const filteredRepos = repositories.filter((repo: any) => {
const { full_path, kind } = repo.namespace;
if (groupName) {
return full_path.toLowerCase().includes(groupName) && kind === "group";
}
return kind === "user";
});
return filteredRepos.length;
};