Skip to content

Commit 7341f0f

Browse files
authored
Support validate-pr-label for merge queue (#8)
1 parent f7d9176 commit 7341f0f

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,19 @@ accepted labels are `breaking-change`, `bug`, `chore`, `ci`, `dependencies`,
7676
jobs:
7777
validate-pr-label:
7878
runs-on: ubuntu-latest
79+
permissions:
80+
issues: read
81+
pull-requests: read
7982
steps:
8083
- uses: js-soft/github-actions/validate-pr-label@main
8184
```
8285

8386
Inputs:
8487

8588
- `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`.
89+
- `github-token`: GitHub token used to read pull request labels when the action runs for a `merge_group` event. Defaults to `${{ github.token }}`.
90+
91+
When using this action as a required check for a branch with a merge queue, add
92+
`merge_group` to the workflow triggers. The action reads labels from the
93+
`pull_request` event payload for regular pull request runs and from the GitHub
94+
API for merge queue runs.

validate-pr-label/action.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ inputs:
66
description: Comma-separated list of labels that are accepted for pull requests.
77
required: false
88
default: breaking-change, bug, chore, ci, dependencies, documentation, enhancement, refactoring, test
9+
github-token:
10+
description: GitHub token used to read pull request labels when the action runs for a merge_group event.
11+
required: false
912

1013
runs:
1114
using: composite
@@ -14,6 +17,10 @@ runs:
1417
shell: bash
1518
env:
1619
VALID_LABELS: ${{ inputs.valid-labels }}
20+
GH_TOKEN: ${{ inputs.github-token || github.token }}
21+
ACTION_EVENT_NAME: ${{ github.event_name }}
22+
ACTION_REF_NAME: ${{ github.ref_name }}
23+
ACTION_REPOSITORY: ${{ github.repository }}
1724
EVENT_LABELS: ${{ join(github.event.pull_request.labels.*.name, ',') }}
1825
run: |
1926
set -euo pipefail
@@ -25,8 +32,30 @@ runs:
2532
printf '%s' "$value"
2633
}
2734
35+
get_labels_from_merge_group() {
36+
local queue_ref="$1"
37+
38+
if [[ ! "$queue_ref" =~ pr-([0-9]+)- ]]; then
39+
echo "::error::Could not determine the pull request number from merge queue ref '$queue_ref'."
40+
exit 1
41+
fi
42+
43+
local pr_number="${BASH_REMATCH[1]}"
44+
45+
if [ -z "$(trim "${GH_TOKEN:-}")" ]; then
46+
echo "::error::A GitHub token is required to validate pull request labels for merge_group events."
47+
exit 1
48+
fi
49+
50+
gh api "repos/$ACTION_REPOSITORY/issues/$pr_number" --jq '.labels[].name' | paste -sd, -
51+
}
52+
2853
labels="$EVENT_LABELS"
2954
55+
if [ "$ACTION_EVENT_NAME" = "merge_group" ]; then
56+
labels="$(get_labels_from_merge_group "$ACTION_REF_NAME")"
57+
fi
58+
3059
if [ -z "$(trim "$labels")" ]; then
3160
echo "::error::The pull request must have one of these labels: $VALID_LABELS"
3261
exit 1

0 commit comments

Comments
 (0)