import { extractNextPageFromLink } from "#/utils/extract-next-page-from-link"; import { openHands } from "./open-hands-axios"; /** * Retrieves repositories where OpenHands Github App has been installed * @param installationIndex Pagination cursor position for app installation IDs * @param installations Collection of all App installation IDs for OpenHands Github App * @returns A list of repositories */ export const retrieveGitHubAppRepositories = async ( installationIndex: number, installations: number[], page = 1, per_page = 30, ) => { const installationId = installations[installationIndex]; const response = await openHands.get( "/api/github/repositories", { params: { sort: "pushed", page, per_page, installation_id: installationId, }, }, ); const link = response.headers.link ?? ""; const nextPage = extractNextPageFromLink(link); let nextInstallation: number | null; if (nextPage) { nextInstallation = installationIndex; } else if (installationIndex + 1 < installations.length) { nextInstallation = installationIndex + 1; } else { nextInstallation = null; } return { data: response.data.repositories, nextPage, installationIndex: nextInstallation, }; }; /** * Given a PAT, retrieves the repositories of the user * @returns A list of repositories */ export const retrieveGitHubUserRepositories = async ( page = 1, per_page = 30, ) => { const response = await openHands.get( "/api/github/repositories", { params: { sort: "pushed", page, per_page, }, }, ); const link = response.headers.link ?? ""; const nextPage = extractNextPageFromLink(link); return { data: response.data, nextPage }; };