-
Notifications
You must be signed in to change notification settings - Fork 3
79 lines (63 loc) · 2.48 KB
/
check-pr-labels.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
name: PR needs one allowed label
on:
pull_request:
types: [opened, synchronize, labeled, unlabeled]
jobs:
check-pr-labels:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install dependencies
run: |
sudo apt-get install -y jq
pip install yq # Tool to parse YAML files
- name: Read allowed labels from .github/release.yml
id: read_labels
run: |
# Will read allowed PR labels from .github/release.yml file.
allowed_labels=$(yq '.changelog.categories[].labels[] | select(. != "*")' .github/release.yml)
allowed_labels=$(echo "$allowed_labels" | tr -d '"')
if [ -z "$allowed_labels" ]; then
echo "Error: No allowed labels found in .github/release.yml."
exit 1
fi
echo "Allowed labels from release.yml:"
echo "$allowed_labels"
echo "---"
# Convert to an array of strings
allowed_labels=$(echo "$allowed_labels" | tr '\n' ';')
# set the allowed labels in the GITHUB_OUTPUT
echo "ALLOWED_LABELS=${allowed_labels}" >> "$GITHUB_OUTPUT"
- name: Check PR Labels
id: check_labels
env:
ALLOWED_LABELS: ${{ steps.read_labels.outputs.ALLOWED_LABELS }}
run: |
# Will check that PR has exactly one of the allowed labels.
if [ -z "$ALLOWED_LABELS" ]; then
echo "Error: No allowed labels found from .github/release.yml. Configuration issue."
exit 1
fi
pr_labels=$(jq -r '.pull_request.labels[].name' $GITHUB_EVENT_PATH)
if [ -z "$pr_labels" ]; then
echo "Error: PR has no labels. Please add one of the allowed labels: $ALLOWED_LABELS"
exit 1
fi
echo "Labels on PR:"
echo "$pr_labels"
echo "---"
# Convert the string an to an array of strings
IFS=';' read -r -a allowed_labels_list <<< "$ALLOWED_LABELS"
count=0
for label in "${allowed_labels_list[@]}"; do
echo "Checking allowed label \"$label\" in PR labels."
if echo "$pr_labels" | grep -q "^$label$"; then
count=$((count + 1))
fi
done
echo "Allowed label count: $count"
if [ "$count" -ne 1 ]; then
echo "Error: PR must have exactly one of the allowed labels: $ALLOWED_LABELS"
exit 1
fi