1
1
import fetch from 'cross-fetch' ;
2
- import { GerritConfig } from "@sourcebot/schemas/v2 /index.type"
2
+ import { GerritConnectionConfig } from "@sourcebot/schemas/v3 /index.type"
3
3
import { createLogger } from './logger.js' ;
4
4
import micromatch from "micromatch" ;
5
5
import { measure , fetchWithRetry } from './utils.js' ;
@@ -12,16 +12,19 @@ interface GerritProjects {
12
12
[ projectName : string ] : GerritProjectInfo ;
13
13
}
14
14
15
+ // https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#:~:text=date%20upon%20submit.-,state,-optional
16
+ type GerritProjectState = 'ACTIVE' | 'READ_ONLY' | 'HIDDEN' ;
17
+
15
18
interface GerritProjectInfo {
16
19
id : string ;
17
- state ?: string ;
20
+ state ?: GerritProjectState ;
18
21
web_links ?: GerritWebLink [ ] ;
19
22
}
20
23
21
24
interface GerritProject {
22
25
name : string ;
23
26
id : string ;
24
- state ?: string ;
27
+ state ?: GerritProjectState ;
25
28
web_links ?: GerritWebLink [ ] ;
26
29
}
27
30
@@ -32,7 +35,7 @@ interface GerritWebLink {
32
35
33
36
const logger = createLogger ( 'Gerrit' ) ;
34
37
35
- export const getGerritReposFromConfig = async ( config : GerritConfig ) : Promise < GerritProject [ ] > => {
38
+ export const getGerritReposFromConfig = async ( config : GerritConnectionConfig ) : Promise < GerritProject [ ] > => {
36
39
const url = config . url . endsWith ( '/' ) ? config . url : `${ config . url } /` ;
37
40
const hostname = new URL ( config . url ) . hostname ;
38
41
@@ -57,24 +60,24 @@ export const getGerritReposFromConfig = async (config: GerritConfig): Promise<Ge
57
60
throw e ;
58
61
}
59
62
60
- // exclude "All-Projects" and "All-Users" projects
61
- const excludedProjects = [ 'All-Projects' , 'All-Users' , 'All-Avatars' , 'All-Archived-Projects' ] ;
62
- projects = projects . filter ( project => ! excludedProjects . includes ( project . name ) ) ;
63
-
64
63
// include repos by glob if specified in config
65
64
if ( config . projects ) {
66
65
projects = projects . filter ( ( project ) => {
67
66
return micromatch . isMatch ( project . name , config . projects ! ) ;
68
67
} ) ;
69
68
}
70
-
71
- if ( config . exclude && config . exclude . projects ) {
72
- projects = projects . filter ( ( project ) => {
73
- return ! micromatch . isMatch ( project . name , config . exclude ! . projects ! ) ;
69
+
70
+ projects = projects
71
+ . filter ( ( project ) => {
72
+ const isExcluded = shouldExcludeProject ( {
73
+ project,
74
+ exclude : config . exclude ,
75
+ } ) ;
76
+
77
+ return ! isExcluded ;
74
78
} ) ;
75
- }
76
79
77
- logger . debug ( `Fetched ${ Object . keys ( projects ) . length } projects in ${ durationMs } ms.` ) ;
80
+ logger . debug ( `Fetched ${ projects . length } projects in ${ durationMs } ms.` ) ;
78
81
return projects ;
79
82
} ;
80
83
@@ -137,3 +140,51 @@ const fetchAllProjects = async (url: string): Promise<GerritProject[]> => {
137
140
138
141
return allProjects ;
139
142
} ;
143
+
144
+ const shouldExcludeProject = ( {
145
+ project,
146
+ exclude,
147
+ } : {
148
+ project : GerritProject ,
149
+ exclude ?: GerritConnectionConfig [ 'exclude' ] ,
150
+ } ) => {
151
+ let reason = '' ;
152
+
153
+ const shouldExclude = ( ( ) => {
154
+ if ( [
155
+ 'All-Projects' ,
156
+ 'All-Users' ,
157
+ 'All-Avatars' ,
158
+ 'All-Archived-Projects'
159
+ ] . includes ( project . name ) ) {
160
+ reason = `Project is a special project.` ;
161
+ return true ;
162
+ }
163
+
164
+ if ( ! ! exclude ?. readOnly && project . state === 'READ_ONLY' ) {
165
+ reason = `\`exclude.readOnly\` is true` ;
166
+ return true ;
167
+ }
168
+
169
+ if ( ! ! exclude ?. hidden && project . state === 'HIDDEN' ) {
170
+ reason = `\`exclude.hidden\` is true` ;
171
+ return true ;
172
+ }
173
+
174
+ if ( exclude ?. projects ) {
175
+ if ( micromatch . isMatch ( project . name , exclude . projects ) ) {
176
+ reason = `\`exclude.projects\` contains ${ project . name } ` ;
177
+ return true ;
178
+ }
179
+ }
180
+
181
+ return false ;
182
+ } ) ( ) ;
183
+
184
+ if ( shouldExclude ) {
185
+ logger . debug ( `Excluding project ${ project . name } . Reason: ${ reason } ` ) ;
186
+ return true ;
187
+ }
188
+
189
+ return false ;
190
+ }
0 commit comments