Refactor Gitea repository fetching to handle pagination

- Updated the `testGiteaConnection` and `getGiteaRepositories` functions to implement pagination when fetching repositories from the Gitea API, ensuring all repositories are retrieved.
- Enhanced error handling for API responses and improved the structure of the returned repository data.
- Removed deprecated code related to direct repository fetching, streamlining the overall logic.
This commit is contained in:
Mauricio Siu 2025-05-04 21:03:31 -06:00
parent 7e365e1947
commit 44af0ec975

View File

@ -362,9 +362,12 @@ export const testGiteaConnection = async (input: { giteaId: string }) => {
}
const baseUrl = provider.giteaUrl.replace(/\/+$/, "");
const url = `${baseUrl}/api/v1/user/repos`;
const limit = 30;
let allRepos = 0;
let nextUrl = `${baseUrl}/api/v1/repos/search?limit=${limit}`;
const response = await fetch(url, {
while (nextUrl) {
const response = await fetch(nextUrl, {
headers: {
Accept: "application/json",
Authorization: `token ${provider.accessToken}`,
@ -378,11 +381,29 @@ export const testGiteaConnection = async (input: { giteaId: string }) => {
}
const repos = await response.json();
allRepos += repos.data.length;
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];
}
}
}
}
await updateGitea(giteaId, {
lastAuthenticatedAt: Math.floor(Date.now() / 1000),
});
return repos.length;
return allRepos;
} catch (error) {
throw error;
}
@ -394,13 +415,15 @@ export const getGiteaRepositories = async (giteaId?: string) => {
}
await refreshGiteaToken(giteaId);
const giteaProvider = await findGiteaById(giteaId);
const baseUrl = giteaProvider.giteaUrl.replace(/\/+$/, "");
const url = `${baseUrl}/api/v1/user/repos`;
const limit = 30;
let allRepositories: any[] = [];
let nextUrl = `${baseUrl}/api/v1/repos/search?limit=${limit}`;
const response = await fetch(url, {
while (nextUrl) {
const response = await fetch(nextUrl, {
headers: {
Accept: "application/json",
Authorization: `token ${giteaProvider.accessToken}`,
@ -414,18 +437,35 @@ export const getGiteaRepositories = async (giteaId?: string) => {
});
}
const repositories = await response.json();
const result = await response.json();
allRepositories = [...allRepositories, ...result.data];
const mappedRepositories = repositories.map((repo: any) => ({
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];
}
}
}
}
return (
allRepositories?.map((repo: any) => ({
id: repo.id,
name: repo.name,
url: repo.full_name,
owner: {
username: repo.owner.login,
},
}));
return mappedRepositories;
})) || []
);
};
export const getGiteaBranches = async (input: {
@ -457,7 +497,10 @@ export const getGiteaBranches = async (input: {
const branches = await response.json();
return branches.map((branch: any) => ({
if (!branches) {
return [];
}
return branches?.map((branch: any) => ({
id: branch.name,
name: branch.name,
commit: {