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

View File

@@ -218,7 +218,7 @@ export const BitbucketIcon = ({ className }: Props) => {
y1="21.921%"
y2="75.234%"
>
<stop offset="7%" stopColor="currentColor" stop-opacity=".4" />
<stop offset="7%" stopColor="currentColor" stopOpacity=".4" />
<stop offset="100%" stopColor="currentColor" />
</linearGradient>
</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,
"tag": "0047_optimal_peter_quill",
"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",
applicationStatus: "idle",
gitlabId: input.gitlabId,
gitlabProjectId: input.gitlabProjectId,
});
return true;

View File

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

View File

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

View File

@@ -45,6 +45,8 @@ export const refreshGitlabToken = async (gitlabProviderId: string) => {
const expiresAt = Math.floor(Date.now() / 1000) + data.expires_in;
console.log("Refreshed token");
await updateGitlabProvider(gitlabProviderId, {
accessToken: data.access_token,
refreshToken: data.refresh_token,
@@ -151,11 +153,9 @@ export const cloneGitlabRepository = async (
}
};
interface GetGitlabRepositories {
export const getGitlabRepositories = async (input: {
gitlabId?: string;
}
export const getGitlabRepositories = async (input: GetGitlabRepositories) => {
}) => {
if (!input.gitlabId) {
return [];
}
@@ -180,7 +180,9 @@ export const getGitlabRepositories = async (input: GetGitlabRepositories) => {
}
const repositories = await response.json();
return repositories as {
id: number;
name: string;
url: string;
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;
repo: string;
gitlabId?: string;
}
export const getGitlabBranches = async (input: GetGitlabBranches) => {
if (!input.gitlabId) {
}) => {
if (!input.gitlabId || !input.id) {
return [];
}
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(
`https://gitlab.com/api/v4/projects/${project.id}/repository/branches`,
`https://gitlab.com/api/v4/projects/${input.id}/repository/branches`,
{
headers: {
Authorization: `Bearer ${gitlabProvider.accessToken}`,
@@ -243,6 +219,7 @@ export const getGitlabBranches = async (input: GetGitlabBranches) => {
const branches = await branchesResponse.json();
return branches as {
id: string;
name: string;
commit: {
id: string;