1
1
'use strict'
2
2
3
- const subSystemLabels = {
4
- 'c++' : / ^ s r c \/ /
3
+ // order of entries in this map *does* matter for the resolved labels
4
+ const subSystemLabelsMap = {
5
+ // don't want to label it a c++ update when we're "only" bumping the Node.js version
6
+ 'c++' : / ^ s r c \/ (? ! n o d e _ v e r s i o n \. h ) / ,
7
+ // libuv needs an explicit mapping, as the ordinary /deps/ mapping below would
8
+ // end up as libuv changes labeled with "uv" (which is a non-existing label)
9
+ 'libuv' : / ^ d e p s \/ u v \/ / ,
10
+ '$1' : / ^ d e p s \/ ( [ ^ / ] + ) /
5
11
}
6
12
7
- const rxTests = / ^ t e s t \/ /
13
+ const exclusiveLabelsMap = {
14
+ test : / ^ t e s t \/ / ,
15
+ doc : / ^ d o c \/ / ,
16
+ benchmark : / ^ b e n c h m a r k \/ /
17
+ }
8
18
9
19
function resolveLabels ( filepathsChanged ) {
10
- if ( isOnly ( rxTests , filepathsChanged ) ) {
11
- return [ 'test' ]
12
- }
20
+ const exclusiveLabels = matchExclusiveSubSystem ( filepathsChanged )
21
+
22
+ return ( exclusiveLabels . length > 0 )
23
+ ? exclusiveLabels
24
+ : matchAllSubSystem ( filepathsChanged )
25
+ }
13
26
14
- return matchBySubSystemRegex ( filepathsChanged )
27
+ function matchExclusiveSubSystem ( filepathsChanged ) {
28
+ const labels = matchSubSystemsByRegex ( exclusiveLabelsMap , filepathsChanged )
29
+ return labels . length === 1 ? labels : [ ]
15
30
}
16
31
17
- function isOnly ( regexToMatch , filepathsChanged ) {
18
- return filepathsChanged . every ( ( filepath ) => regexToMatch . test ( filepath ) )
32
+ function matchAllSubSystem ( filepathsChanged ) {
33
+ return matchSubSystemsByRegex ( subSystemLabelsMap , filepathsChanged )
19
34
}
20
35
21
- function matchBySubSystemRegex ( filepathsChanged ) {
36
+ function matchSubSystemsByRegex ( rxLabelsMap , filepathsChanged ) {
22
37
// by putting matched labels into a map, we avoid duplicate labels
23
38
const labelsMap = filepathsChanged . reduce ( ( map , filepath ) => {
24
- const mappedSubSystem = mappedSubSystemForFile ( filepath )
39
+ const mappedSubSystem = mappedSubSystemForFile ( rxLabelsMap , filepath )
25
40
26
41
if ( mappedSubSystem ) {
27
42
map [ mappedSubSystem ] = true
@@ -33,12 +48,31 @@ function matchBySubSystemRegex (filepathsChanged) {
33
48
return Object . keys ( labelsMap )
34
49
}
35
50
36
- function mappedSubSystemForFile ( filepath ) {
37
- return Object . keys ( subSystemLabels ) . find ( ( labelName ) => {
38
- const rxForLabel = subSystemLabels [ labelName ]
51
+ function mappedSubSystemForFile ( labelsMap , filepath ) {
52
+ return Object . keys ( labelsMap ) . map ( ( labelName ) => {
53
+ const rxForLabel = labelsMap [ labelName ]
54
+ const matches = rxForLabel . exec ( filepath )
55
+
56
+ // return undefined when subsystem regex didn't match,
57
+ // we'll filter out these values with the .filter() below
58
+ if ( matches === null ) {
59
+ return undefined
60
+ }
61
+
62
+ // label names starting with $ means we want to extract a matching
63
+ // group from the regex we've just matched against
64
+ if ( labelName . startsWith ( '$' ) ) {
65
+ const wantedMatchGroup = labelName . substr ( 1 )
66
+ return matches [ wantedMatchGroup ]
67
+ }
68
+
69
+ // use label name as is when label doesn't look like a regex matching group
70
+ return labelName
71
+ } ) . filter ( withoutUndefinedValues ) [ 0 ]
72
+ }
39
73
40
- return rxForLabel . test ( filepath ) ? labelName : undefined
41
- } )
74
+ function withoutUndefinedValues ( label ) {
75
+ return label !== undefined
42
76
}
43
77
44
78
exports . resolveLabels = resolveLabels
0 commit comments