|
1 | 1 | import { App, type Octokit } from "octokit"; |
2 | 2 | import { env } from "../env.server"; |
3 | 3 | import { prisma } from "~/db.server"; |
| 4 | +import { logger } from "./logger.server"; |
4 | 5 |
|
5 | 6 | export const githubApp = |
6 | 7 | env.GITHUB_APP_ENABLED === "1" |
@@ -56,28 +57,30 @@ export async function linkGitHubAppInstallation( |
56 | 57 | } |
57 | 58 |
|
58 | 59 | 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 | + }); |
63 | 64 |
|
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; |
70 | 68 |
|
71 | | - all.push(...repoData.repositories); |
| 69 | + for await (const { data } of iterator) { |
| 70 | + pageCount++; |
| 71 | + allRepos.push(...data); |
72 | 72 |
|
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 | + }); |
74 | 79 | break; |
75 | 80 | } |
76 | | - |
77 | | - page++; |
78 | 81 | } |
79 | 82 |
|
80 | | - return all.map((repo) => ({ |
| 83 | + return allRepos.map((repo) => ({ |
81 | 84 | githubId: repo.id, |
82 | 85 | name: repo.name, |
83 | 86 | fullName: repo.full_name, |
|
0 commit comments