@@ -11,6 +11,10 @@ interface MatchConfig {
1111}
1212
1313type StringOrMatchConfig = string | MatchConfig ;
14+ type LabelsConfig = Map <
15+ string ,
16+ { stringOrMatch : StringOrMatchConfig [ ] ; color ?: string }
17+ > ;
1418type ClientType = ReturnType < typeof github . getOctokit > ;
1519
1620// GitHub Issues cannot have more than 100 labels
@@ -55,13 +59,15 @@ export async function run() {
5559 continue ;
5660 }
5761
58- const labelGlobs : Map < string , StringOrMatchConfig [ ] > =
59- await getLabelGlobs ( client , configPath ) ;
62+ const labelsConfig : LabelsConfig = await getLabelGlobs (
63+ client ,
64+ configPath
65+ ) ;
6066
6167 const preexistingLabels = pullRequest . labels . map ( l => l . name ) ;
6268 const allLabels : Set < string > = new Set < string > ( preexistingLabels ) ;
6369
64- for ( const [ label , globs ] of labelGlobs . entries ( ) ) {
70+ for ( const [ label , { stringOrMatch : globs } ] of labelsConfig . entries ( ) ) {
6571 core . debug ( `processing ${ label } ` ) ;
6672 if ( checkGlobs ( changedFiles , globs , dot ) ) {
6773 allLabels . add ( label ) ;
@@ -77,7 +83,12 @@ export async function run() {
7783 let newLabels : string [ ] = [ ] ;
7884
7985 if ( ! isListEqual ( labelsToAdd , preexistingLabels ) ) {
80- await setLabels ( client , prNumber , labelsToAdd ) ;
86+ await setLabels (
87+ client ,
88+ prNumber ,
89+ labelsToAdd ,
90+ getLabelsColor ( labelsConfig )
91+ ) ;
8192 newLabels = labelsToAdd . filter ( l => ! preexistingLabels . includes ( l ) ) ;
8293 }
8394
@@ -164,7 +175,7 @@ async function getChangedFiles(
164175async function getLabelGlobs (
165176 client : ClientType ,
166177 configurationPath : string
167- ) : Promise < Map < string , StringOrMatchConfig [ ] > > {
178+ ) : Promise < LabelsConfig > {
168179 let configurationContent : string ;
169180 try {
170181 if ( ! fs . existsSync ( configurationPath ) ) {
@@ -196,6 +207,16 @@ async function getLabelGlobs(
196207 return getLabelGlobMapFromObject ( configObject ) ;
197208}
198209
210+ function getLabelsColor ( labelsConfig : LabelsConfig ) : Map < string , string > {
211+ const labelsColor : Map < string , string > = new Map ( ) ;
212+ for ( const [ label , { color} ] of labelsConfig . entries ( ) ) {
213+ if ( color ) {
214+ labelsColor . set ( label , color ) ;
215+ }
216+ }
217+ return labelsColor ;
218+ }
219+
199220async function fetchContent (
200221 client : ClientType ,
201222 repoPath : string
@@ -210,15 +231,24 @@ async function fetchContent(
210231 return Buffer . from ( response . data . content , response . data . encoding ) . toString ( ) ;
211232}
212233
213- function getLabelGlobMapFromObject (
214- configObject : any
215- ) : Map < string , StringOrMatchConfig [ ] > {
216- const labelGlobs : Map < string , StringOrMatchConfig [ ] > = new Map ( ) ;
234+ function getLabelGlobMapFromObject ( configObject : any ) : LabelsConfig {
235+ const labelGlobs : Map <
236+ string ,
237+ { stringOrMatch : StringOrMatchConfig [ ] ; color ?: string }
238+ > = new Map ( ) ;
217239 for ( const label in configObject ) {
218240 if ( typeof configObject [ label ] === 'string' ) {
219- labelGlobs . set ( label , [ configObject [ label ] ] ) ;
241+ labelGlobs . set ( label , { stringOrMatch : [ configObject [ label ] ] } ) ;
220242 } else if ( configObject [ label ] instanceof Array ) {
221- labelGlobs . set ( label , configObject [ label ] ) ;
243+ labelGlobs . set ( label , { stringOrMatch : configObject [ label ] } ) ;
244+ } else if (
245+ typeof configObject [ label ] === 'object' &&
246+ configObject [ label ] ?. pattern
247+ ) {
248+ labelGlobs . set ( label , {
249+ stringOrMatch : configObject [ label ] . pattern ,
250+ color : configObject [ label ] . color
251+ } ) ;
222252 } else {
223253 throw Error (
224254 `found unexpected type for label ${ label } (should be string or array of globs)`
@@ -337,12 +367,26 @@ function isListEqual(listA: string[], listB: string[]): boolean {
337367async function setLabels (
338368 client : ClientType ,
339369 prNumber : number ,
340- labels : string [ ]
370+ labels : string [ ] ,
371+ labelsColour : Map < string , string >
341372) {
373+ // remove previous labels
342374 await client . rest . issues . setLabels ( {
343375 owner : github . context . repo . owner ,
344376 repo : github . context . repo . repo ,
345377 issue_number : prNumber ,
346- labels : labels
378+ labels
347379 } ) ;
380+
381+ for ( const label of labels ) {
382+ const color = labelsColour . get ( label ) ;
383+ if ( color ) {
384+ await client . rest . issues . updateLabel ( {
385+ owner : github . context . repo . owner ,
386+ repo : github . context . repo . repo ,
387+ name : label ,
388+ color : color ?? '#EDEDED'
389+ } ) ;
390+ }
391+ }
348392}
0 commit comments