@@ -32,14 +32,18 @@ import { logErrorIfNeeded } from '../../helpers';
3232import type { CustomGithubCredentialsProvider } from '../GithubAppManager' ;
3333import {
3434 AppInstallationRepositories ,
35+ AppInstallationRepositoriesResponse ,
3536 AuthenticatedUserRepositoryList ,
37+ AuthenticatedUserRepositoryResponse ,
3638 isGithubAppCredential ,
3739 type ExtendedGithubCredentials ,
3840 type GithubFetchError ,
3941} from '../types' ;
4042import { buildOcto } from './ghUtils' ;
4143import { validateAndBuildRepoData , ValidatedRepo } from './repoUtils' ;
4244
45+ const GITHUB_REST_API_MAX_PAGE_SIZE = 100 ;
46+
4347/**
4448 * Creates the GithubFetchError to be stored in the returned errors array of the returned GithubRepositoryResponse object
4549 */
@@ -372,11 +376,9 @@ export async function listAllRepositoriesForAuthenticatedUser(
372376 pageSize ?: number ;
373377 } ,
374378) : Promise < AuthenticatedUserRepositoryList > {
375- const GITHUB_REST_API_MAX_PAGE_SIZE = 100 ;
376- const PAGE_NUMBER_REGEX_MATCH_INDEX = 1 ;
377- const SECOND_PAGE_NUMBER = 2 ;
378-
379- const fetchListForAuthenticatedUser = async ( pageNumber : number ) => {
379+ const fetchListForAuthenticatedUser = async (
380+ pageNumber : number ,
381+ ) : Promise < AuthenticatedUserRepositoryResponse > => {
380382 /**
381383 * The listForAuthenticatedUser endpoint will grab all the repositories the github token has explicit access to.
382384 * These would include repositories they own, repositories where they are a collaborator,
@@ -390,37 +392,15 @@ export async function listAllRepositoriesForAuthenticatedUser(
390392 } ) ;
391393 } ;
392394
393- const firstPageResponse = await fetchListForAuthenticatedUser ( 1 ) ;
394-
395- const lastPageNumberString = firstPageResponse ?. headers ?. link
396- ?. split ( ',' )
397- ?. find ( s => s . includes ( 'rel="last"' ) )
398- ?. match ( / p a g e = ( \d + ) / ) ?. [ PAGE_NUMBER_REGEX_MATCH_INDEX ] ;
399-
400- if ( ! lastPageNumberString ) {
401- deps . logger . debug (
402- `Unable to extract page number from rel='last' link found in response headers from 'repos.listForAuthenticatedUser' GH endpoint => returning current page size` ,
403- ) ;
404- return firstPageResponse . data ;
405- }
406-
407- const lastPageNumber = Number . parseInt ( lastPageNumberString , 10 ) ;
408-
409- if ( lastPageNumber < SECOND_PAGE_NUMBER ) {
410- return firstPageResponse . data ;
411- }
412-
413- const pagePromises = [ ] ;
414- for ( let i = SECOND_PAGE_NUMBER ; i <= lastPageNumber ; i ++ ) {
415- pagePromises . push ( fetchListForAuthenticatedUser ( i ) ) ;
416- }
417- const pageResponses = await Promise . all ( pagePromises ) ;
418- pageResponses . unshift ( firstPageResponse ) ;
395+ const allPages = await getAllPages (
396+ deps ,
397+ 'repos.listForAuthenticatedUser' ,
398+ fetchListForAuthenticatedUser ,
399+ ) ;
419400
420- const allRepositories = pageResponses . flatMap (
421- pageResponse => pageResponse . data ,
401+ const allRepositories = allPages . flatMap (
402+ pageResponseData => pageResponseData ,
422403 ) ;
423- // .sort((a, b) => a.name.localeCompare(b.name));
424404
425405 return allRepositories ;
426406}
@@ -434,52 +414,27 @@ export async function listAllRepositoriesAccessibleToInstallation(
434414 pageSize ?: number ;
435415 } ,
436416) : Promise < AppInstallationRepositories > {
437- const GITHUB_REST_API_MAX_PAGE_SIZE = 100 ;
438- const PAGE_NUMBER_REGEX_MATCH_INDEX = 1 ;
439- const SECOND_PAGE_NUMBER = 2 ;
440-
441- const fetchListReposAccessibleToInstallation = async ( pageNumber : number ) => {
417+ const fetchListReposAccessibleToInstallation = async (
418+ pageNumber : number ,
419+ ) : Promise < AppInstallationRepositoriesResponse > => {
442420 return await octokit . rest . apps . listReposAccessibleToInstallation ( {
443421 page : pageNumber ,
444422 per_page : options ?. pageSize ?? GITHUB_REST_API_MAX_PAGE_SIZE ,
445423 } ) ;
446424 } ;
447425
448- const firstPageResponse = await fetchListReposAccessibleToInstallation ( 1 ) ;
449-
450- const lastPageNumberString = firstPageResponse ?. headers ?. link
451- ?. split ( ',' )
452- ?. find ( s => s . includes ( 'rel="last"' ) )
453- ?. match ( / p a g e = ( \d + ) / ) ?. [ PAGE_NUMBER_REGEX_MATCH_INDEX ] ;
454-
455- if ( ! lastPageNumberString ) {
456- deps . logger . debug (
457- `Unable to extract page number from rel='last' link found in response headers from 'apps.listReposAccessibleToInstallation' GH endpoint => returning current page size` ,
458- ) ;
459- return firstPageResponse . data ;
460- }
461-
462- const lastPageNumber = Number . parseInt ( lastPageNumberString , 10 ) ;
463-
464- if ( lastPageNumber < SECOND_PAGE_NUMBER ) {
465- return firstPageResponse . data ;
466- }
467-
468- const pagePromises = [ ] ;
469- for ( let i = SECOND_PAGE_NUMBER ; i <= lastPageNumber ; i ++ ) {
470- pagePromises . push ( fetchListReposAccessibleToInstallation ( i ) ) ;
471- }
472- const pageResponses = await Promise . all ( pagePromises ) ;
473- pageResponses . unshift ( firstPageResponse ) ;
426+ const allPages = await getAllPages (
427+ deps ,
428+ 'apps.listReposAccessibleToInstallation' ,
429+ fetchListReposAccessibleToInstallation ,
430+ ) ;
474431
475- const allRepositories = pageResponses . flatMap (
476- pageResponse => pageResponse . data . repositories ,
432+ const allRepositories = allPages . flatMap (
433+ pageResponseData => pageResponseData . repositories ,
477434 ) ;
478- // .sort((a, b) => a.name.localeCompare(b.name));
479435
480- const total_count =
481- pageResponses ?. [ 0 ] ?. data . total_count ?? allRepositories . length ;
482- const repository_selection = pageResponses ?. [ 0 ] ?. data ?. repository_selection ;
436+ const total_count = allPages ?. [ 0 ] ?. total_count ?? allRepositories . length ;
437+ const repository_selection = allPages ?. [ 0 ] ?. repository_selection ;
483438
484439 return {
485440 repositories : allRepositories ,
0 commit comments