@@ -23,7 +23,7 @@ interface GerritWebLink {
23
23
24
24
const logger = createLogger ( 'Gerrit' ) ;
25
25
26
- export const getGerritReposFromConfig = async ( config : GerritConfig , ctx : AppContext ) => {
26
+ export const getGerritReposFromConfig = async ( config : GerritConfig , ctx : AppContext ) : Promise < GitRepository [ ] > => {
27
27
28
28
const url = config . url . endsWith ( '/' ) ? config . url : `${ config . url } /` ;
29
29
const hostname = new URL ( config . url ) . hostname ;
@@ -35,6 +35,8 @@ export const getGerritReposFromConfig = async (config: GerritConfig, ctx: AppCon
35
35
// exclude "All-Projects" and "All-Users" projects
36
36
delete projects [ 'All-Projects' ] ;
37
37
delete projects [ 'All-Users' ] ;
38
+ delete projects [ 'All-Avatars' ]
39
+ delete projects [ 'All-Archived-Projects' ]
38
40
39
41
logger . debug ( `Fetched ${ Object . keys ( projects ) . length } projects in ${ durationMs } ms.` ) ;
40
42
@@ -90,20 +92,35 @@ export const getGerritReposFromConfig = async (config: GerritConfig, ctx: AppCon
90
92
} ;
91
93
92
94
const fetchAllProjects = async ( url : string ) : Promise < GerritProjects > => {
93
-
94
95
const projectsEndpoint = `${ url } projects/` ;
95
- logger . debug ( `Fetching projects from Gerrit at ${ projectsEndpoint } ...` ) ;
96
- const response = await fetch ( projectsEndpoint ) ;
96
+ let allProjects : GerritProjects = { } ;
97
+ let start = 0 ; // Start offset for pagination
98
+ let hasMoreProjects = true ;
97
99
98
- if ( ! response . ok ) {
99
- throw new Error ( `Failed to fetch projects from Gerrit: ${ response . statusText } ` ) ;
100
- }
100
+ while ( hasMoreProjects ) {
101
+ const endpointWithParams = `${ projectsEndpoint } ?S=${ start } ` ;
102
+ logger . debug ( `Fetching projects from Gerrit at ${ endpointWithParams } ` ) ;
103
+
104
+ const response = await fetch ( endpointWithParams ) ;
105
+ if ( ! response . ok ) {
106
+ throw new Error ( `Failed to fetch projects from Gerrit: ${ response . statusText } ` ) ;
107
+ }
101
108
102
- const text = await response . text ( ) ;
109
+ const text = await response . text ( ) ;
110
+ const jsonText = text . replace ( ")]}'\n" , '' ) ; // Remove XSSI protection prefix
111
+ const data : GerritProjects = JSON . parse ( jsonText ) ;
112
+
113
+ // Merge the current batch of projects with allProjects
114
+ Object . assign ( allProjects , data ) ;
115
+
116
+ // Check if there are more projects to fetch
117
+ hasMoreProjects = Object . values ( data ) . some (
118
+ ( project ) => ( project as any ) . _more_projects === true
119
+ ) ;
120
+
121
+ // Update the offset based on the number of projects in the current response
122
+ start += Object . keys ( data ) . length ;
123
+ }
103
124
104
- // Gerrit prepends ")] }'\n" to prevent XSSI attacks; remove it
105
- // https://gerrit-review.googlesource.com/Documentation/rest-api.html
106
- const jsonText = text . replace ( ")]}'\n" , '' ) ;
107
- const data = JSON . parse ( jsonText ) ;
108
- return data ;
125
+ return allProjects ;
109
126
} ;
0 commit comments