Skip to content

Commit 4054f5d

Browse files
committed
Use octokit's iterator for paginating repos
1 parent 7f1467c commit 4054f5d

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

apps/webapp/app/services/gitHub.server.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { App, type Octokit } from "octokit";
22
import { env } from "../env.server";
33
import { prisma } from "~/db.server";
4+
import { logger } from "./logger.server";
45

56
export const githubApp =
67
env.GITHUB_APP_ENABLED === "1"
@@ -56,28 +57,30 @@ export async function linkGitHubAppInstallation(
5657
}
5758

5859
async function fetchInstallationRepositories(octokit: Octokit, installationId: number) {
59-
const all = [];
60-
let page = 1;
61-
const perPage = 100;
62-
const maxPages = 3;
60+
const iterator = octokit.paginate.iterator(octokit.rest.apps.listReposAccessibleToInstallation, {
61+
installation_id: installationId,
62+
per_page: 100,
63+
});
6364

64-
while (page <= maxPages) {
65-
const { data: repoData } = await octokit.rest.apps.listReposAccessibleToInstallation({
66-
installation_id: installationId,
67-
per_page: perPage,
68-
page,
69-
});
65+
const allRepos = [];
66+
const maxPages = 3;
67+
let pageCount = 0;
7068

71-
all.push(...repoData.repositories);
69+
for await (const { data } of iterator) {
70+
pageCount++;
71+
allRepos.push(...data);
7272

73-
if (repoData.repositories.length < perPage) {
73+
if (maxPages && pageCount >= maxPages) {
74+
logger.warn("GitHub installation repository fetch truncated", {
75+
installationId,
76+
maxPages,
77+
totalReposFetched: allRepos.length,
78+
});
7479
break;
7580
}
76-
77-
page++;
7881
}
7982

80-
return all.map((repo) => ({
83+
return allRepos.map((repo) => ({
8184
githubId: repo.id,
8285
name: repo.name,
8386
fullName: repo.full_name,

0 commit comments

Comments
 (0)