Skip to content

Commit 8199cae

Browse files
authored
PR labels workflow (#3428)
We had a couple of problems with the previous workflow: * unlabel events contain the labels _prior_ to unlabelling. So if we added/removed a label at the same time, it seems like we only get one action triggered and it contains the wrong labels. * Since it read labels from the event, re-running it didn't do anything. * This workflow now hits the API, which means re-running is a valid way to fix any races. * This also means it sees the labels _after_ the unlabelling, rather than before.
1 parent 0a45e8a commit 8199cae

File tree

1 file changed

+53
-13
lines changed

1 file changed

+53
-13
lines changed

.github/workflows/labels.yml

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,61 @@ name: PR Labels
22

33
on:
44
pull_request:
5-
types: [ opened, reopened, synchronize, labeled, unlabeled ]
5+
types: [ opened, reopened, synchronize, labeled, unlabeled ] # Trigger on these PR activities
66

77
jobs:
8-
check_release_label:
9-
name: Missing changelog label
8+
check_changelog_label:
9+
name: Validate Changelog Label
1010
runs-on: ubuntu-latest
11+
permissions:
12+
pull-requests: read # Grant permission to read PR information
1113
steps:
12-
- if: >
13-
contains(github.event.pull_request.labels.*.name, 'chore') == false &&
14-
contains(github.event.pull_request.labels.*.name, 'bug') == false &&
15-
contains(github.event.pull_request.labels.*.name, 'feature') == false &&
16-
contains(github.event.pull_request.labels.*.name, 'fix') == false &&
17-
contains(github.event.pull_request.labels.*.name, 'performance') == false &&
18-
contains(github.event.pull_request.labels.*.name, 'break') == false &&
19-
contains(github.event.pull_request.labels.*.name, 'wire-break') == false
14+
- name: Get PR Labels from API
15+
id: get_labels_api
16+
uses: octokit/[email protected] # Use an action to make API requests
17+
with:
18+
route: GET /repos/{owner}/{repo}/pulls/{pull_number}/labels
19+
owner: ${{ github.repository_owner }}
20+
repo: ${{ github.event.repo.name }}
21+
pull_number: ${{ github.event.pull_request.number }}
22+
env:
23+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Automatically provided token
24+
25+
- name: Extract and Check Labels
2026
run: |
21-
echo "${{ toJson(github.event.pull_request.labels.*.name) }}"
22-
exit 1
27+
REQUIRED_LABELS=(
28+
"chore"
29+
"bug"
30+
"feature"
31+
"fix"
32+
"performance"
33+
"break"
34+
"wire-break"
35+
)
36+
REQUIRED_LABELS_JSON=$(jq -n '$ARGS.positional' --args "${REQUIRED_LABELS[@]}")
37+
echo "Required Labels: $REQUIRED_LABELS_JSON"
38+
39+
# Parse the response from the API call
40+
# The API returns an array of label objects, we need just the 'name' property
41+
API_RESPONSE="${{ steps.get_labels_api.outputs.data }}"
42+
echo "API Response: $API_RESPONSE"
43+
44+
# Extract only the label names into a JSON array
45+
CURRENT_PR_LABELS_JSON=$(echo "$API_RESPONSE" | jq '[.[] | .name]')
46+
echo "Current PR Labels from API: $CURRENT_PR_LABELS_JSON"
47+
48+
found_one=false
49+
for label in "${REQUIRED_LABELS[@]}"; do
50+
if echo "$CURRENT_PR_LABELS_JSON" | jq -e "contains([\"$label\"])"; then
51+
echo "Found required label: $label"
52+
found_one=true
53+
break
54+
fi
55+
done
56+
57+
if [ "$found_one" = false ]; then
58+
echo "::error file=.github/workflows/pr-labels.yml::Pull Request is missing a required changelog label. Please add one of: ${REQUIRED_LABELS[*]}."
59+
exit 1
60+
else
61+
echo "Pull Request has a required changelog label."
62+
fi

0 commit comments

Comments
 (0)