11import fetch from 'cross-fetch' ;
2- import { GerritConfig } from "@sourcebot/schemas/v2 /index.type"
2+ import { GerritConnectionConfig } from "@sourcebot/schemas/v3 /index.type"
33import { createLogger } from './logger.js' ;
44import micromatch from "micromatch" ;
55import { measure , fetchWithRetry } from './utils.js' ;
@@ -12,16 +12,19 @@ interface GerritProjects {
1212 [ projectName : string ] : GerritProjectInfo ;
1313}
1414
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+
1518interface GerritProjectInfo {
1619 id : string ;
17- state ?: string ;
20+ state ?: GerritProjectState ;
1821 web_links ?: GerritWebLink [ ] ;
1922}
2023
2124interface GerritProject {
2225 name : string ;
2326 id : string ;
24- state ?: string ;
27+ state ?: GerritProjectState ;
2528 web_links ?: GerritWebLink [ ] ;
2629}
2730
@@ -32,7 +35,7 @@ interface GerritWebLink {
3235
3336const logger = createLogger ( 'Gerrit' ) ;
3437
35- export const getGerritReposFromConfig = async ( config : GerritConfig ) : Promise < GerritProject [ ] > => {
38+ export const getGerritReposFromConfig = async ( config : GerritConnectionConfig ) : Promise < GerritProject [ ] > => {
3639 const url = config . url . endsWith ( '/' ) ? config . url : `${ config . url } /` ;
3740 const hostname = new URL ( config . url ) . hostname ;
3841
@@ -57,24 +60,24 @@ export const getGerritReposFromConfig = async (config: GerritConfig): Promise<Ge
5760 throw e ;
5861 }
5962
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-
6463 // include repos by glob if specified in config
6564 if ( config . projects ) {
6665 projects = projects . filter ( ( project ) => {
6766 return micromatch . isMatch ( project . name , config . projects ! ) ;
6867 } ) ;
6968 }
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 ;
7478 } ) ;
75- }
7679
77- logger . debug ( `Fetched ${ Object . keys ( projects ) . length } projects in ${ durationMs } ms.` ) ;
80+ logger . debug ( `Fetched ${ projects . length } projects in ${ durationMs } ms.` ) ;
7881 return projects ;
7982} ;
8083
@@ -137,3 +140,51 @@ const fetchAllProjects = async (url: string): Promise<GerritProject[]> => {
137140
138141 return allProjects ;
139142} ;
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