Skip to content

Commit e2639a0

Browse files
authored
support multiple rules (#1)
* multiple tests * update * logging * more logging * remove logging * update README
1 parent 574e3d1 commit e2639a0

File tree

4 files changed

+93
-30
lines changed

4 files changed

+93
-30
lines changed

README.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
1-
# pr-has-one-of-labels
2-
Github Action to check if a PR has at least one of the provided labels
1+
# PR Labels Checker
2+
Github Action to check if a PR's labels pass the specified rules
33

4-
Example workflow file:
4+
## Input
5+
- `hasSome`: Comma separated list of labels, PR needs at least of them
6+
- `hasAll`: Comma separated list of labels, PR needs all of them
7+
- `hasNone`: Comma separated list of labels, PR must not have any of them
8+
- `hasNotAll`: Comma separated list of labels, PR must not have all of them
9+
10+
## Output
11+
- `passed`: boolean
12+
13+
## Example workflow file
514
```yml
6-
name: QA Labels Check
15+
name: Labels Check
716
on:
817
pull_request:
918
types: [opened, edited, labeled, unlabeled, synchronize]
1019
jobs:
1120
QA-check:
12-
if: github.base_ref == 'develop'
21+
if: github.base_ref == 'master'
22+
runs-on: ubuntu-latest
23+
steps:
24+
- uses: danielchabr/pr-has-one-of-labels@master
25+
id: checkLabel
26+
with:
27+
hasSome: QA:tested,QA:skipped
28+
Do_not_merge-check:
29+
if: github.base_ref == 'master'
1330
runs-on: ubuntu-latest
1431
steps:
1532
- uses: danielchabr/pr-has-one-of-labels@master
1633
id: checkLabel
1734
with:
18-
labels: QA:tested,QA:skipped
35+
hasNone: do not merge,blocked
1936
```

action.yml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
name: 'Does PR have the label?'
2-
description: 'Checks whether the PR has at least one of the specified labels'
1+
name: 'PR labels checker'
2+
description: 'Checks PR labels for specified rules'
33
inputs:
4-
labels:
5-
description: 'Comma separated list of labels'
6-
required: true
4+
hasSome:
5+
description: 'Comma separated list of labels, PR needs at least of them'
6+
required: false
7+
hasAll:
8+
description: 'Comma separated list of labels, PR needs all of them'
9+
required: false
10+
hasNone:
11+
description: 'Comma separated list of labels, PR must not have any of them'
12+
required: false
13+
hasNotAll:
14+
description: 'Comma separated list of labels, PR must not have all of them'
15+
required: false
716
outputs:
8-
has-label:
9-
description: 'Has some label?'
17+
passed:
18+
description: 'Have the provided labels passed all tests?'
1019
runs:
1120
using: 'node12'
1221
main: 'index.js'

index.js

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,61 @@
11
const core = require('@actions/core')
22
const github = require('@actions/github')
33

4-
const labelsInput = core.getInput('labels')
4+
const hasSomeInput = core.getInput('hasSome')
5+
const hasAllInput = core.getInput('hasAll')
6+
const hasNoneInput = core.getInput('hasNone')
7+
const hasNotAllInput = core.getInput('hasNotAll')
58

6-
const labels = labelsInput.split(',')
9+
const hasSomeLabels = hasSomeInput.split(',')
10+
const hasAllLabels = hasAllInput.split(',')
11+
const hasNoneLabels = hasNoneInput.split(',')
12+
const hasNotAllLabels = hasNotAllInput.split(',')
713

8-
const hasLabel = github.context.payload.pull_request.labels.some((item) =>
9-
labels.includes(item.name)
14+
const failMessages = []
15+
const prLabels = github.context.payload.pull_request.labels.map(item => item.name)
16+
17+
const hasSomeResult = !hasSomeInput || hasSomeLabels.some((label) =>
18+
prLabels.includes(label)
1019
)
1120

12-
if (!hasLabel) {
13-
core.setFailed(
14-
`The PR needs to have one of the following labels to pass this check: ${labels.join(
15-
', '
16-
)}`
17-
)
18-
}
21+
const hasAllResult = !hasAllInput || hasAllLabels.every((label) =>
22+
prLabels.includes(label)
23+
)
1924

20-
core.setOutput('hasLabel', hasLabel)
25+
const hasNoneResult = !hasNoneInput || hasNoneLabels.every((label) =>
26+
!prLabels.includes(label)
27+
)
2128

22-
console.log(
23-
`Does PR have one of '${labels.join(', ')}' labels?: ${hasLabel}`
29+
const hasNotAllResult = !hasNotAllInput || hasNotAllLabels.some((label) =>
30+
!prLabels.includes(label)
2431
)
32+
33+
if (!hasSomeResult) {
34+
failMessages.push(`The PR needs to have at least one of the following labels to pass this check: ${hasSomeLabels.join(
35+
', '
36+
)}`)
37+
}
38+
39+
if (!hasAllResult) {
40+
failMessages.push(`The PR needs to have all of the following labels to pass this check: ${hasAllLabels.join(
41+
', '
42+
)}`)
43+
}
44+
45+
if (!hasNoneResult) {
46+
failMessages.push(`The PR needs to have none of the following labels to pass this check: ${hasNoneLabels.join(
47+
', '
48+
)}`)
49+
}
50+
51+
if (!hasNotAllResult) {
52+
failMessages.push(`The PR needs to not have at least one of the following labels to pass this check: ${hasNotAllLabels.join(
53+
', '
54+
)}`)
55+
}
56+
57+
if (failMessages.length) {
58+
core.setFailed(failMessages.join('. '))
59+
}
60+
61+
core.setOutput('passed', failMessages.length === 0)

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "pr-has-one-of-labels",
3-
"version": "1.0.0",
4-
"description": "Github Action to check if a PR has at least one of the provided labels",
2+
"name": "pr-labels-checker",
3+
"version": "2.0.0",
4+
"description": "Github Action to check if a PR's labels pass the specified rules",
55
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"

0 commit comments

Comments
 (0)