mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
Merge pull request #1910 from Dokploy/1894-gitlab-self-hosted-cannot-find-all-repository
refactor: streamline GitLab repository fetching by introducing valida…
This commit is contained in:
@@ -246,32 +246,16 @@ export const getGitlabRepositories = async (gitlabId?: string) => {
|
|||||||
|
|
||||||
const gitlabProvider = await findGitlabById(gitlabId);
|
const gitlabProvider = await findGitlabById(gitlabId);
|
||||||
|
|
||||||
const response = await fetch(
|
const allProjects = await validateGitlabProvider(gitlabProvider);
|
||||||
`${gitlabProvider.gitlabUrl}/api/v4/projects?membership=true&owned=true&page=${0}&per_page=${100}`,
|
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${gitlabProvider.accessToken}`,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!response.ok) {
|
const filteredRepos = allProjects.filter((repo: any) => {
|
||||||
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;
|
const { full_path, kind } = repo.namespace;
|
||||||
const groupName = gitlabProvider.groupName?.toLowerCase();
|
const groupName = gitlabProvider.groupName?.toLowerCase();
|
||||||
|
|
||||||
if (groupName) {
|
if (groupName) {
|
||||||
const isIncluded = groupName
|
const isIncluded = groupName
|
||||||
.split(",")
|
.split(",")
|
||||||
.some((name) => full_path.toLowerCase().includes(name));
|
.some((name) => full_path === name);
|
||||||
|
|
||||||
return isIncluded && kind === "group";
|
return isIncluded && kind === "group";
|
||||||
}
|
}
|
||||||
@@ -432,34 +416,60 @@ export const testGitlabConnection = async (
|
|||||||
|
|
||||||
const gitlabProvider = await findGitlabById(gitlabId);
|
const gitlabProvider = await findGitlabById(gitlabId);
|
||||||
|
|
||||||
const response = await fetch(
|
const repositories = await validateGitlabProvider(gitlabProvider);
|
||||||
`${gitlabProvider.gitlabUrl}/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 filteredRepos = repositories.filter((repo: any) => {
|
||||||
const { full_path, kind } = repo.namespace;
|
const { full_path, kind } = repo.namespace;
|
||||||
|
|
||||||
if (groupName) {
|
if (groupName) {
|
||||||
return groupName
|
return groupName.split(",").some((name) => full_path === name);
|
||||||
.split(",")
|
|
||||||
.some((name) => full_path.toLowerCase().includes(name));
|
|
||||||
}
|
}
|
||||||
return kind === "user";
|
return kind === "user";
|
||||||
});
|
});
|
||||||
|
|
||||||
return filteredRepos.length;
|
return filteredRepos.length;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const validateGitlabProvider = async (gitlabProvider: Gitlab) => {
|
||||||
|
try {
|
||||||
|
const allProjects = [];
|
||||||
|
let page = 1;
|
||||||
|
const perPage = 100; // GitLab's max per page is 100
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const response = await fetch(
|
||||||
|
`${gitlabProvider.gitlabUrl}/api/v4/projects?membership=true&owned=true&page=${page}&per_page=${perPage}`,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${gitlabProvider.accessToken}`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "BAD_REQUEST",
|
||||||
|
message: `Failed to fetch repositories: ${response.statusText}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const projects = await response.json();
|
||||||
|
|
||||||
|
if (projects.length === 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
allProjects.push(...projects);
|
||||||
|
page++;
|
||||||
|
|
||||||
|
const total = response.headers.get("x-total");
|
||||||
|
if (total && allProjects.length >= Number.parseInt(total)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return allProjects;
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user