Skip to content

Commit

Permalink
support multiple rules (#1)
Browse files Browse the repository at this point in the history
* multiple tests

* update

* logging

* more logging

* remove logging

* update README
  • Loading branch information
danielchabr authored Jun 12, 2020
1 parent 574e3d1 commit e2639a0
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 30 deletions.
29 changes: 23 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
# pr-has-one-of-labels
Github Action to check if a PR has at least one of the provided labels
# PR Labels Checker
Github Action to check if a PR's labels pass the specified rules

Example workflow file:
## Input
- `hasSome`: Comma separated list of labels, PR needs at least of them
- `hasAll`: Comma separated list of labels, PR needs all of them
- `hasNone`: Comma separated list of labels, PR must not have any of them
- `hasNotAll`: Comma separated list of labels, PR must not have all of them

## Output
- `passed`: boolean

## Example workflow file
```yml
name: QA Labels Check
name: Labels Check
on:
pull_request:
types: [opened, edited, labeled, unlabeled, synchronize]
jobs:
QA-check:
if: github.base_ref == 'develop'
if: github.base_ref == 'master'
runs-on: ubuntu-latest
steps:
- uses: danielchabr/pr-has-one-of-labels@master
id: checkLabel
with:
hasSome: QA:tested,QA:skipped
Do_not_merge-check:
if: github.base_ref == 'master'
runs-on: ubuntu-latest
steps:
- uses: danielchabr/pr-has-one-of-labels@master
id: checkLabel
with:
labels: QA:tested,QA:skipped
hasNone: do not merge,blocked
```
23 changes: 16 additions & 7 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
name: 'Does PR have the label?'
description: 'Checks whether the PR has at least one of the specified labels'
name: 'PR labels checker'
description: 'Checks PR labels for specified rules'
inputs:
labels:
description: 'Comma separated list of labels'
required: true
hasSome:
description: 'Comma separated list of labels, PR needs at least of them'
required: false
hasAll:
description: 'Comma separated list of labels, PR needs all of them'
required: false
hasNone:
description: 'Comma separated list of labels, PR must not have any of them'
required: false
hasNotAll:
description: 'Comma separated list of labels, PR must not have all of them'
required: false
outputs:
has-label:
description: 'Has some label?'
passed:
description: 'Have the provided labels passed all tests?'
runs:
using: 'node12'
main: 'index.js'
Expand Down
65 changes: 51 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,61 @@
const core = require('@actions/core')
const github = require('@actions/github')

const labelsInput = core.getInput('labels')
const hasSomeInput = core.getInput('hasSome')
const hasAllInput = core.getInput('hasAll')
const hasNoneInput = core.getInput('hasNone')
const hasNotAllInput = core.getInput('hasNotAll')

const labels = labelsInput.split(',')
const hasSomeLabels = hasSomeInput.split(',')
const hasAllLabels = hasAllInput.split(',')
const hasNoneLabels = hasNoneInput.split(',')
const hasNotAllLabels = hasNotAllInput.split(',')

const hasLabel = github.context.payload.pull_request.labels.some((item) =>
labels.includes(item.name)
const failMessages = []
const prLabels = github.context.payload.pull_request.labels.map(item => item.name)

const hasSomeResult = !hasSomeInput || hasSomeLabels.some((label) =>
prLabels.includes(label)
)

if (!hasLabel) {
core.setFailed(
`The PR needs to have one of the following labels to pass this check: ${labels.join(
', '
)}`
)
}
const hasAllResult = !hasAllInput || hasAllLabels.every((label) =>
prLabels.includes(label)
)

core.setOutput('hasLabel', hasLabel)
const hasNoneResult = !hasNoneInput || hasNoneLabels.every((label) =>
!prLabels.includes(label)
)

console.log(
`Does PR have one of '${labels.join(', ')}' labels?: ${hasLabel}`
const hasNotAllResult = !hasNotAllInput || hasNotAllLabels.some((label) =>
!prLabels.includes(label)
)

if (!hasSomeResult) {
failMessages.push(`The PR needs to have at least one of the following labels to pass this check: ${hasSomeLabels.join(
', '
)}`)
}

if (!hasAllResult) {
failMessages.push(`The PR needs to have all of the following labels to pass this check: ${hasAllLabels.join(
', '
)}`)
}

if (!hasNoneResult) {
failMessages.push(`The PR needs to have none of the following labels to pass this check: ${hasNoneLabels.join(
', '
)}`)
}

if (!hasNotAllResult) {
failMessages.push(`The PR needs to not have at least one of the following labels to pass this check: ${hasNotAllLabels.join(
', '
)}`)
}

if (failMessages.length) {
core.setFailed(failMessages.join('. '))
}

core.setOutput('passed', failMessages.length === 0)
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pr-has-one-of-labels",
"version": "1.0.0",
"description": "Github Action to check if a PR has at least one of the provided labels",
"name": "pr-labels-checker",
"version": "2.0.0",
"description": "Github Action to check if a PR's labels pass the specified rules",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down

0 comments on commit e2639a0

Please sign in to comment.