11'use strict'
22
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 \/ ( [ ^ / ] + ) /
511}
612
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+ }
818
919function 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+ }
1326
14- return matchBySubSystemRegex ( filepathsChanged )
27+ function matchExclusiveSubSystem ( filepathsChanged ) {
28+ const labels = matchSubSystemsByRegex ( exclusiveLabelsMap , filepathsChanged )
29+ return labels . length === 1 ? labels : [ ]
1530}
1631
17- function isOnly ( regexToMatch , filepathsChanged ) {
18- return filepathsChanged . every ( ( filepath ) => regexToMatch . test ( filepath ) )
32+ function matchAllSubSystem ( filepathsChanged ) {
33+ return matchSubSystemsByRegex ( subSystemLabelsMap , filepathsChanged )
1934}
2035
21- function matchBySubSystemRegex ( filepathsChanged ) {
36+ function matchSubSystemsByRegex ( rxLabelsMap , filepathsChanged ) {
2237 // by putting matched labels into a map, we avoid duplicate labels
2338 const labelsMap = filepathsChanged . reduce ( ( map , filepath ) => {
24- const mappedSubSystem = mappedSubSystemForFile ( filepath )
39+ const mappedSubSystem = mappedSubSystemForFile ( rxLabelsMap , filepath )
2540
2641 if ( mappedSubSystem ) {
2742 map [ mappedSubSystem ] = true
@@ -33,12 +48,31 @@ function matchBySubSystemRegex (filepathsChanged) {
3348 return Object . keys ( labelsMap )
3449}
3550
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+ }
3973
40- return rxForLabel . test ( filepath ) ? labelName : undefined
41- } )
74+ function withoutUndefinedValues ( label ) {
75+ return label !== undefined
4276}
4377
4478exports . resolveLabels = resolveLabels
0 commit comments