Skip to content

Commit c6c86cc

Browse files
committed
updated tests and documentation
1 parent 82e9f5a commit c6c86cc

File tree

5 files changed

+99
-11
lines changed

5 files changed

+99
-11
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,25 @@ This action locates one of a given list of labels in the labels active on the wo
44

55
## Inputs
66

7+
While `allowed` and `allowed_multiple` are both optional, at least one of either is required.
8+
79
### `allowed`
810

9-
**Required** The labels to look for. Separate via commas or newlines (using a block string).
11+
**Optional** The labels to look for to match exactly one of. Separate via commas or newlines (using a block string).
12+
13+
### `allowed_multiple`
14+
15+
**Optional** The labels to look for to match many of. Separate via commas or newlines (using a block string).
16+
17+
### `default_match`
18+
19+
**Optional** A value to return if no matching labels are found. If this value is not specified and no matching labels are found, this action will exit with a failing code.
1020

1121
## Outputs
1222

1323
### `match`
1424

15-
The one label from the `allowed` list that was located. The action will fail if no labels matched or more than one was found.
25+
The one label from the `allowed` or `allowed_multiple` list that was located. If no `default_match` is specified, the action will fail if no labels matched or more than one was found.
1626

1727
## Example usage
1828

dist/index.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,23 @@ function parseAllowed(allowed) {
1313
)
1414
}
1515

16-
function findMatching(labelNames, allowedLabels, isMultipleAllowed, defaultValue) {
16+
function findMatching(
17+
labelNames,
18+
allowedLabels,
19+
isMultipleAllowed,
20+
defaultMatch
21+
) {
1722
const allowedLabelsSet = new Set(allowedLabels)
1823
const matchingLabels = labelNames.filter((labelName) =>
1924
allowedLabelsSet.has(labelName)
2025
)
21-
if ( matchingLabels.length === 0 && defaultValue !== undefined) {
22-
return [defaultValue]
26+
if (
27+
matchingLabels.length === 0 &&
28+
(defaultMatch !== undefined || defaultMatch === '')
29+
) {
30+
return [defaultMatch]
2331
}
32+
2433
if (
2534
isMultipleAllowed
2635
? matchingLabels.length === 0
@@ -25656,7 +25665,12 @@ function run() {
2565625665
const defaultMatch = core.getInput('default_match')
2565725666
let matchingLabel
2565825667
if (allowedLabels.length > 0) {
25659-
matchingLabel = match.findMatching(labelNames, allowedLabels, false, defaultMatch)
25668+
matchingLabel = match.findMatching(
25669+
labelNames,
25670+
allowedLabels,
25671+
false,
25672+
defaultMatch
25673+
)
2566025674
} else if (allowedMultipleLabels.length > 0) {
2566125675
matchingLabel = match.findMatching(
2566225676
labelNames,

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ function run() {
1414
const defaultMatch = core.getInput('default_match')
1515
let matchingLabel
1616
if (allowedLabels.length > 0) {
17-
matchingLabel = match.findMatching(labelNames, allowedLabels, false, defaultMatch)
17+
matchingLabel = match.findMatching(
18+
labelNames,
19+
allowedLabels,
20+
false,
21+
defaultMatch
22+
)
1823
} else if (allowedMultipleLabels.length > 0) {
1924
matchingLabel = match.findMatching(
2025
labelNames,

match.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,23 @@ function parseAllowed(allowed) {
77
)
88
}
99

10-
function findMatching(labelNames, allowedLabels, isMultipleAllowed, defaultValue) {
10+
function findMatching(
11+
labelNames,
12+
allowedLabels,
13+
isMultipleAllowed,
14+
defaultMatch
15+
) {
1116
const allowedLabelsSet = new Set(allowedLabels)
1217
const matchingLabels = labelNames.filter((labelName) =>
1318
allowedLabelsSet.has(labelName)
1419
)
15-
if ( matchingLabels.length === 0 && defaultValue !== undefined) {
16-
return [defaultValue]
20+
if (
21+
matchingLabels.length === 0 &&
22+
(defaultMatch !== undefined || defaultMatch === '')
23+
) {
24+
return [defaultMatch]
1725
}
26+
1827
if (
1928
isMultipleAllowed
2029
? matchingLabels.length === 0

match.test.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,54 @@ test('match returns default for no labels', (t) => {
6868
'default'
6969
)
7070
t.deepEqual(matchedLabel, ['default'])
71-
})
71+
})
72+
73+
test('match returns default for no matching labels', (t) => {
74+
const matchedLabel = match.findMatching(
75+
['not matching'],
76+
['major', 'minor', 'patch'],
77+
false,
78+
'default'
79+
)
80+
t.deepEqual(matchedLabel, ['default'])
81+
})
82+
83+
test('match returns default for no matching multiple labels', (t) => {
84+
const matchedLabel = match.findMatching(
85+
['not matching', 'still not'],
86+
['major', 'minor', 'patch'],
87+
true,
88+
'default'
89+
)
90+
t.deepEqual(matchedLabel, ['default'])
91+
})
92+
93+
test('match does not return default for matching labels', (t) => {
94+
const matchedLabel = match.findMatching(
95+
['not matching', 'minor'],
96+
['major', 'minor', 'patch'],
97+
false,
98+
'default'
99+
)
100+
t.deepEqual(matchedLabel, ['minor'])
101+
})
102+
103+
test('match does not return default for matchin multiple labels', (t) => {
104+
const matchedLabel = match.findMatching(
105+
['major', 'minor'],
106+
['major', 'minor', 'patch'],
107+
true,
108+
'default'
109+
)
110+
t.deepEqual(matchedLabel, ['major', 'minor'])
111+
})
112+
113+
test('match does not return default for matchin multiple labels but not all', (t) => {
114+
const matchedLabel = match.findMatching(
115+
['patch', 'not matching', 'minor'],
116+
['major', 'minor', 'patch'],
117+
true,
118+
'default'
119+
)
120+
t.deepEqual(matchedLabel, ['patch', 'minor'])
121+
})

0 commit comments

Comments
 (0)