Skip to content

Commit f4fe33f

Browse files
authored
Add pull request label validation action (#1)
1 parent 080062c commit f4fe33f

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,21 @@ Classic PAT scopes:
3838
Inputs:
3939

4040
- `branch`: branch to inspect and release from. Defaults to `main`.
41+
42+
## validate-pr-label
43+
44+
Validates that a pull request has at least one accepted label. By default, the
45+
accepted labels are `breaking-change`, `bug`, `chore`, `ci`, `dependencies`,
46+
`documentation`, `enhancement`, `refactoring`, and `test`.
47+
48+
```yaml
49+
jobs:
50+
validate-pr-label:
51+
runs-on: ubuntu-latest
52+
steps:
53+
- uses: js-soft/github-actions/validate-pr-label@main
54+
```
55+
56+
Inputs:
57+
58+
- `valid-labels`: comma-separated list of labels that are accepted for pull requests. Defaults to `breaking-change, bug, chore, ci, dependencies, documentation, enhancement, refactoring, test`.

validate-pr-label/action.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Validate pull request label
2+
description: Validates that a pull request has at least one accepted label.
3+
4+
inputs:
5+
valid-labels:
6+
description: Comma-separated list of labels that are accepted for pull requests.
7+
required: false
8+
default: breaking-change, bug, chore, ci, dependencies, documentation, enhancement, refactoring, test
9+
10+
runs:
11+
using: composite
12+
steps:
13+
- name: Validate labels
14+
shell: bash
15+
env:
16+
VALID_LABELS: ${{ inputs.valid-labels }}
17+
EVENT_LABELS: ${{ join(github.event.pull_request.labels.*.name, ',') }}
18+
run: |
19+
set -euo pipefail
20+
21+
trim() {
22+
local value="$1"
23+
value="${value#"${value%%[![:space:]]*}"}"
24+
value="${value%"${value##*[![:space:]]}"}"
25+
printf '%s' "$value"
26+
}
27+
28+
labels="$EVENT_LABELS"
29+
30+
if [ -z "$(trim "$labels")" ]; then
31+
echo "::error::The pull request must have one of these labels: $VALID_LABELS"
32+
exit 1
33+
fi
34+
35+
IFS=',' read -ra valid_labels <<< "$VALID_LABELS"
36+
IFS=',' read -ra pr_labels <<< "$labels"
37+
38+
for pr_label in "${pr_labels[@]}"; do
39+
pr_label="$(trim "$pr_label")"
40+
41+
for valid_label in "${valid_labels[@]}"; do
42+
valid_label="$(trim "$valid_label")"
43+
44+
if [ "$pr_label" = "$valid_label" ]; then
45+
echo "Pull request label '$pr_label' is accepted."
46+
exit 0
47+
fi
48+
done
49+
done
50+
51+
echo "::error::The pull request must have one of these labels: $VALID_LABELS"
52+
echo "Current labels: $labels"
53+
exit 1

0 commit comments

Comments
 (0)