refactor: optimize gitlab fetch repositories

This commit is contained in:
Mauricio Siu
2024-09-01 10:00:47 -06:00
parent 766b166bf2
commit 1be580807f
9 changed files with 3452 additions and 37 deletions

View File

@@ -44,6 +44,7 @@ const GitlabProviderSchema = z.object({
.object({ .object({
repo: z.string().min(1, "Repo is required"), repo: z.string().min(1, "Repo is required"),
owner: z.string().min(1, "Owner is required"), owner: z.string().min(1, "Owner is required"),
id: z.number().nullable(),
}) })
.required(), .required(),
branch: z.string().min(1, "Branch is required"), branch: z.string().min(1, "Branch is required"),
@@ -69,6 +70,7 @@ export const SaveGitlabProvider = ({ applicationId }: Props) => {
repository: { repository: {
owner: "", owner: "",
repo: "", repo: "",
id: null,
}, },
gitlabId: "", gitlabId: "",
branch: "", branch: "",
@@ -95,6 +97,7 @@ export const SaveGitlabProvider = ({ applicationId }: Props) => {
{ {
owner: repository?.owner, owner: repository?.owner,
repo: repository?.repo, repo: repository?.repo,
id: repository?.id,
gitlabId: gitlabId, gitlabId: gitlabId,
}, },
{ {
@@ -109,6 +112,7 @@ export const SaveGitlabProvider = ({ applicationId }: Props) => {
repository: { repository: {
repo: data.gitlabRepository || "", repo: data.gitlabRepository || "",
owner: data.gitlabOwner || "", owner: data.gitlabOwner || "",
id: data.gitlabProjectId,
}, },
buildPath: data.gitlabBuildPath || "/", buildPath: data.gitlabBuildPath || "/",
gitlabId: data.gitlabId || "", gitlabId: data.gitlabId || "",
@@ -124,6 +128,7 @@ export const SaveGitlabProvider = ({ applicationId }: Props) => {
gitlabBuildPath: data.buildPath, gitlabBuildPath: data.buildPath,
gitlabId: data.gitlabId, gitlabId: data.gitlabId,
applicationId, applicationId,
gitlabProjectId: data.repository.id,
}) })
.then(async () => { .then(async () => {
toast.success("Service Provided Saved"); toast.success("Service Provided Saved");
@@ -155,6 +160,7 @@ export const SaveGitlabProvider = ({ applicationId }: Props) => {
form.setValue("repository", { form.setValue("repository", {
owner: "", owner: "",
repo: "", repo: "",
id: null,
}); });
form.setValue("branch", ""); form.setValue("branch", "");
}} }}
@@ -234,6 +240,7 @@ export const SaveGitlabProvider = ({ applicationId }: Props) => {
form.setValue("repository", { form.setValue("repository", {
owner: repo.owner.username as string, owner: repo.owner.username as string,
repo: repo.name, repo: repo.name,
id: repo.id,
}); });
form.setValue("branch", ""); form.setValue("branch", "");
}} }}

View File

@@ -218,7 +218,7 @@ export const BitbucketIcon = ({ className }: Props) => {
y1="21.921%" y1="21.921%"
y2="75.234%" y2="75.234%"
> >
<stop offset="7%" stopColor="currentColor" stop-opacity=".4" /> <stop offset="7%" stopColor="currentColor" stopOpacity=".4" />
<stop offset="100%" stopColor="currentColor" /> <stop offset="100%" stopColor="currentColor" />
</linearGradient> </linearGradient>
</defs> </defs>

View File

@@ -0,0 +1 @@
ALTER TABLE "application" ADD COLUMN "gitlabProjectId" integer;

File diff suppressed because it is too large Load Diff

View File

@@ -337,6 +337,13 @@
"when": 1725172770448, "when": 1725172770448,
"tag": "0047_optimal_peter_quill", "tag": "0047_optimal_peter_quill",
"breakpoints": true "breakpoints": true
},
{
"idx": 48,
"version": "6",
"when": 1725206119154,
"tag": "0048_unknown_radioactive_man",
"breakpoints": true
} }
] ]
} }

View File

@@ -225,6 +225,7 @@ export const applicationRouter = createTRPCRouter({
sourceType: "gitlab", sourceType: "gitlab",
applicationStatus: "idle", applicationStatus: "idle",
gitlabId: input.gitlabId, gitlabId: input.gitlabId,
gitlabProjectId: input.gitlabProjectId,
}); });
return true; return true;

View File

@@ -139,6 +139,7 @@ export const gitProvider = createTRPCRouter({
getGitlabBranches: protectedProcedure getGitlabBranches: protectedProcedure
.input( .input(
z.object({ z.object({
id: z.number().nullable(),
owner: z.string(), owner: z.string(),
repo: z.string(), repo: z.string(),
gitlabId: z.string().optional(), gitlabId: z.string().optional(),

View File

@@ -133,6 +133,7 @@ export const applications = pgTable("application", {
buildPath: text("buildPath").default("/"), buildPath: text("buildPath").default("/"),
autoDeploy: boolean("autoDeploy").$defaultFn(() => true), autoDeploy: boolean("autoDeploy").$defaultFn(() => true),
// Gitlab // Gitlab
gitlabProjectId: integer("gitlabProjectId"),
gitlabRepository: text("gitlabRepository"), gitlabRepository: text("gitlabRepository"),
gitlabOwner: text("gitlabOwner"), gitlabOwner: text("gitlabOwner"),
gitlabBranch: text("gitlabBranch"), gitlabBranch: text("gitlabBranch"),
@@ -421,6 +422,7 @@ export const apiSaveGitlabProvider = createSchema
gitlabOwner: true, gitlabOwner: true,
gitlabRepository: true, gitlabRepository: true,
gitlabId: true, gitlabId: true,
gitlabProjectId: true,
}) })
.required(); .required();

View File

@@ -45,6 +45,8 @@ export const refreshGitlabToken = async (gitlabProviderId: string) => {
const expiresAt = Math.floor(Date.now() / 1000) + data.expires_in; const expiresAt = Math.floor(Date.now() / 1000) + data.expires_in;
console.log("Refreshed token");
await updateGitlabProvider(gitlabProviderId, { await updateGitlabProvider(gitlabProviderId, {
accessToken: data.access_token, accessToken: data.access_token,
refreshToken: data.refresh_token, refreshToken: data.refresh_token,
@@ -151,11 +153,9 @@ export const cloneGitlabRepository = async (
} }
}; };
interface GetGitlabRepositories { export const getGitlabRepositories = async (input: {
gitlabId?: string; gitlabId?: string;
} }) => {
export const getGitlabRepositories = async (input: GetGitlabRepositories) => {
if (!input.gitlabId) { if (!input.gitlabId) {
return []; return [];
} }
@@ -180,7 +180,9 @@ export const getGitlabRepositories = async (input: GetGitlabRepositories) => {
} }
const repositories = await response.json(); const repositories = await response.json();
return repositories as { return repositories as {
id: number;
name: string; name: string;
url: string; url: string;
owner: { owner: {
@@ -189,46 +191,20 @@ export const getGitlabRepositories = async (input: GetGitlabRepositories) => {
}[]; }[];
}; };
interface GetGitlabBranches { export const getGitlabBranches = async (input: {
id: number | null;
gitlabId?: string;
owner: string; owner: string;
repo: string; repo: string;
gitlabId?: string; }) => {
} if (!input.gitlabId || !input.id) {
export const getGitlabBranches = async (input: GetGitlabBranches) => {
if (!input.gitlabId) {
return []; return [];
} }
const gitlabProvider = await getGitlabProvider(input.gitlabId); const gitlabProvider = await getGitlabProvider(input.gitlabId);
const projectResponse = await fetch(
`https://gitlab.com/api/v4/projects?search=${input.repo}&owned=true&page=1&per_page=100`,
{
headers: {
Authorization: `Bearer ${gitlabProvider.accessToken}`,
},
},
);
if (!projectResponse.ok) {
throw new TRPCError({
code: "BAD_REQUEST",
message: `Failed to fetch repositories: ${projectResponse.statusText}`,
});
}
const projects = await projectResponse.json();
const project = projects.find(
(p) => p.namespace.path === input.owner && p.name === input.repo,
);
if (!project) {
throw new Error(`Project not found: ${input.owner}/${input.repo}`);
}
const branchesResponse = await fetch( const branchesResponse = await fetch(
`https://gitlab.com/api/v4/projects/${project.id}/repository/branches`, `https://gitlab.com/api/v4/projects/${input.id}/repository/branches`,
{ {
headers: { headers: {
Authorization: `Bearer ${gitlabProvider.accessToken}`, Authorization: `Bearer ${gitlabProvider.accessToken}`,
@@ -243,6 +219,7 @@ export const getGitlabBranches = async (input: GetGitlabBranches) => {
const branches = await branchesResponse.json(); const branches = await branchesResponse.json();
return branches as { return branches as {
id: string;
name: string; name: string;
commit: { commit: {
id: string; id: string;