mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-21 11:28:47 +00:00
refactor(gitea): update repository and branch fetching to use pagination with /user/repos and /branches endpoints
This commit is contained in:
@@ -362,17 +362,22 @@ export const testGiteaConnection = async (input: { giteaId: string }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const baseUrl = provider.giteaUrl.replace(/\/+$/, "");
|
const baseUrl = provider.giteaUrl.replace(/\/+$/, "");
|
||||||
const limit = 30;
|
|
||||||
let allRepos = 0;
|
|
||||||
let nextUrl = `${baseUrl}/api/v1/repos/search?limit=${limit}`;
|
|
||||||
|
|
||||||
while (nextUrl) {
|
// Use /user/repos to get authenticated user's repositories with pagination
|
||||||
const response = await fetch(nextUrl, {
|
let allRepos = 0;
|
||||||
headers: {
|
let page = 1;
|
||||||
Accept: "application/json",
|
const limit = 50; // Max per page
|
||||||
Authorization: `token ${provider.accessToken}`,
|
|
||||||
|
while (true) {
|
||||||
|
const response = await fetch(
|
||||||
|
`${baseUrl}/api/v1/user/repos?page=${page}&limit=${limit}`,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Accept: "application/json",
|
||||||
|
Authorization: `token ${provider.accessToken}`,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
);
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@@ -381,22 +386,18 @@ export const testGiteaConnection = async (input: { giteaId: string }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const repos = await response.json();
|
const repos = await response.json();
|
||||||
allRepos += repos.data.length;
|
if (!Array.isArray(repos) || repos.length === 0) {
|
||||||
|
break; // No more repositories
|
||||||
const linkHeader = response.headers.get("link");
|
|
||||||
nextUrl = "";
|
|
||||||
|
|
||||||
if (linkHeader) {
|
|
||||||
const nextLink = linkHeader
|
|
||||||
.split(",")
|
|
||||||
.find((link) => link.includes('rel="next"'));
|
|
||||||
if (nextLink) {
|
|
||||||
const matches = nextLink.match(/<([^>]+)>/);
|
|
||||||
if (matches?.[1]) {
|
|
||||||
nextUrl = matches[1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
allRepos += repos.length;
|
||||||
|
|
||||||
|
// Check if there are more pages
|
||||||
|
if (repos.length < limit) {
|
||||||
|
break; // Last page (fewer results than limit)
|
||||||
|
}
|
||||||
|
|
||||||
|
page++;
|
||||||
}
|
}
|
||||||
|
|
||||||
await updateGitea(giteaId, {
|
await updateGitea(giteaId, {
|
||||||
@@ -418,17 +419,22 @@ export const getGiteaRepositories = async (giteaId?: string) => {
|
|||||||
const giteaProvider = await findGiteaById(giteaId);
|
const giteaProvider = await findGiteaById(giteaId);
|
||||||
|
|
||||||
const baseUrl = giteaProvider.giteaUrl.replace(/\/+$/, "");
|
const baseUrl = giteaProvider.giteaUrl.replace(/\/+$/, "");
|
||||||
const limit = 30;
|
|
||||||
let allRepositories: any[] = [];
|
|
||||||
let nextUrl = `${baseUrl}/api/v1/repos/search?limit=${limit}`;
|
|
||||||
|
|
||||||
while (nextUrl) {
|
// Use /user/repos to get authenticated user's repositories with pagination
|
||||||
const response = await fetch(nextUrl, {
|
let allRepositories: any[] = [];
|
||||||
headers: {
|
let page = 1;
|
||||||
Accept: "application/json",
|
const limit = 50; // Max per page
|
||||||
Authorization: `token ${giteaProvider.accessToken}`,
|
|
||||||
|
while (true) {
|
||||||
|
const response = await fetch(
|
||||||
|
`${baseUrl}/api/v1/user/repos?page=${page}&limit=${limit}`,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Accept: "application/json",
|
||||||
|
Authorization: `token ${giteaProvider.accessToken}`,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
);
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
@@ -437,23 +443,19 @@ export const getGiteaRepositories = async (giteaId?: string) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await response.json();
|
const repos = await response.json();
|
||||||
allRepositories = [...allRepositories, ...result.data];
|
if (!Array.isArray(repos) || repos.length === 0) {
|
||||||
|
break; // No more repositories
|
||||||
const linkHeader = response.headers.get("link");
|
|
||||||
nextUrl = "";
|
|
||||||
|
|
||||||
if (linkHeader) {
|
|
||||||
const nextLink = linkHeader
|
|
||||||
.split(",")
|
|
||||||
.find((link) => link.includes('rel="next"'));
|
|
||||||
if (nextLink) {
|
|
||||||
const matches = nextLink.match(/<([^>]+)>/);
|
|
||||||
if (matches?.[1]) {
|
|
||||||
nextUrl = matches[1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
allRepositories = [...allRepositories, ...repos];
|
||||||
|
|
||||||
|
// Check if there are more pages
|
||||||
|
if (repos.length < limit) {
|
||||||
|
break; // Last page (fewer results than limit)
|
||||||
|
}
|
||||||
|
|
||||||
|
page++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -482,25 +484,43 @@ export const getGiteaBranches = async (input: {
|
|||||||
const giteaProvider = await findGiteaById(input.giteaId);
|
const giteaProvider = await findGiteaById(input.giteaId);
|
||||||
|
|
||||||
const baseUrl = giteaProvider.giteaUrl.replace(/\/+$/, "");
|
const baseUrl = giteaProvider.giteaUrl.replace(/\/+$/, "");
|
||||||
const url = `${baseUrl}/api/v1/repos/${input.owner}/${input.repo}/branches`;
|
|
||||||
|
|
||||||
const response = await fetch(url, {
|
// Handle pagination for branches
|
||||||
headers: {
|
let allBranches: any[] = [];
|
||||||
Accept: "application/json",
|
let page = 1;
|
||||||
Authorization: `token ${giteaProvider.accessToken}`,
|
const limit = 50; // Max per page
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
while (true) {
|
||||||
throw new Error(`Failed to fetch branches: ${response.statusText}`);
|
const response = await fetch(
|
||||||
|
`${baseUrl}/api/v1/repos/${input.owner}/${input.repo}/branches?page=${page}&limit=${limit}`,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Accept: "application/json",
|
||||||
|
Authorization: `token ${giteaProvider.accessToken}`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Failed to fetch branches: ${response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const branches = await response.json();
|
||||||
|
if (!Array.isArray(branches) || branches.length === 0) {
|
||||||
|
break; // No more branches
|
||||||
|
}
|
||||||
|
|
||||||
|
allBranches = [...allBranches, ...branches];
|
||||||
|
|
||||||
|
// Check if there are more pages
|
||||||
|
if (branches.length < limit) {
|
||||||
|
break; // Last page (fewer results than limit)
|
||||||
|
}
|
||||||
|
|
||||||
|
page++;
|
||||||
}
|
}
|
||||||
|
|
||||||
const branches = await response.json();
|
return allBranches?.map((branch: any) => ({
|
||||||
|
|
||||||
if (!branches) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
return branches?.map((branch: any) => ({
|
|
||||||
id: branch.name,
|
id: branch.name,
|
||||||
name: branch.name,
|
name: branch.name,
|
||||||
commit: {
|
commit: {
|
||||||
|
|||||||
Reference in New Issue
Block a user