@@ -11,6 +11,10 @@ interface MatchConfig {
11
11
}
12
12
13
13
type StringOrMatchConfig = string | MatchConfig ;
14
+ type LabelsConfig = Map <
15
+ string ,
16
+ { stringOrMatch : StringOrMatchConfig [ ] ; color ?: string }
17
+ > ;
14
18
type ClientType = ReturnType < typeof github . getOctokit > ;
15
19
16
20
// GitHub Issues cannot have more than 100 labels
@@ -55,13 +59,15 @@ export async function run() {
55
59
continue ;
56
60
}
57
61
58
- const labelGlobs : Map < string , StringOrMatchConfig [ ] > =
59
- await getLabelGlobs ( client , configPath ) ;
62
+ const labelsConfig : LabelsConfig = await getLabelGlobs (
63
+ client ,
64
+ configPath
65
+ ) ;
60
66
61
67
const preexistingLabels = pullRequest . labels . map ( l => l . name ) ;
62
68
const allLabels : Set < string > = new Set < string > ( preexistingLabels ) ;
63
69
64
- for ( const [ label , globs ] of labelGlobs . entries ( ) ) {
70
+ for ( const [ label , { stringOrMatch : globs } ] of labelsConfig . entries ( ) ) {
65
71
core . debug ( `processing ${ label } ` ) ;
66
72
if ( checkGlobs ( changedFiles , globs , dot ) ) {
67
73
allLabels . add ( label ) ;
@@ -77,7 +83,12 @@ export async function run() {
77
83
let newLabels : string [ ] = [ ] ;
78
84
79
85
if ( ! isListEqual ( labelsToAdd , preexistingLabels ) ) {
80
- await setLabels ( client , prNumber , labelsToAdd ) ;
86
+ await setLabels (
87
+ client ,
88
+ prNumber ,
89
+ labelsToAdd ,
90
+ getLabelsColor ( labelsConfig )
91
+ ) ;
81
92
newLabels = labelsToAdd . filter ( l => ! preexistingLabels . includes ( l ) ) ;
82
93
}
83
94
@@ -164,7 +175,7 @@ async function getChangedFiles(
164
175
async function getLabelGlobs (
165
176
client : ClientType ,
166
177
configurationPath : string
167
- ) : Promise < Map < string , StringOrMatchConfig [ ] > > {
178
+ ) : Promise < LabelsConfig > {
168
179
let configurationContent : string ;
169
180
try {
170
181
if ( ! fs . existsSync ( configurationPath ) ) {
@@ -196,6 +207,16 @@ async function getLabelGlobs(
196
207
return getLabelGlobMapFromObject ( configObject ) ;
197
208
}
198
209
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
+
199
220
async function fetchContent (
200
221
client : ClientType ,
201
222
repoPath : string
@@ -210,15 +231,24 @@ async function fetchContent(
210
231
return Buffer . from ( response . data . content , response . data . encoding ) . toString ( ) ;
211
232
}
212
233
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 ( ) ;
217
239
for ( const label in configObject ) {
218
240
if ( typeof configObject [ label ] === 'string' ) {
219
- labelGlobs . set ( label , [ configObject [ label ] ] ) ;
241
+ labelGlobs . set ( label , { stringOrMatch : [ configObject [ label ] ] } ) ;
220
242
} 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
+ } ) ;
222
252
} else {
223
253
throw Error (
224
254
`found unexpected type for label ${ label } (should be string or array of globs)`
@@ -337,12 +367,26 @@ function isListEqual(listA: string[], listB: string[]): boolean {
337
367
async function setLabels (
338
368
client : ClientType ,
339
369
prNumber : number ,
340
- labels : string [ ]
370
+ labels : string [ ] ,
371
+ labelsColour : Map < string , string >
341
372
) {
373
+ // remove previous labels
342
374
await client . rest . issues . setLabels ( {
343
375
owner : github . context . repo . owner ,
344
376
repo : github . context . repo . repo ,
345
377
issue_number : prNumber ,
346
- labels : labels
378
+ labels
347
379
} ) ;
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
+ }
348
392
}
0 commit comments