Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,19 @@ accepted labels are `breaking-change`, `bug`, `chore`, `ci`, `dependencies`,
jobs:
validate-pr-label:
runs-on: ubuntu-latest
permissions:
issues: read
pull-requests: read
steps:
- uses: js-soft/github-actions/validate-pr-label@main
```

Inputs:

- `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`.
- `github-token`: GitHub token used to read pull request labels when the action runs for a `merge_group` event. Defaults to `${{ github.token }}`.

When using this action as a required check for a branch with a merge queue, add
`merge_group` to the workflow triggers. The action reads labels from the
`pull_request` event payload for regular pull request runs and from the GitHub
API for merge queue runs.
29 changes: 29 additions & 0 deletions validate-pr-label/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ inputs:
description: Comma-separated list of labels that are accepted for pull requests.
required: false
default: breaking-change, bug, chore, ci, dependencies, documentation, enhancement, refactoring, test
github-token:
description: GitHub token used to read pull request labels when the action runs for a merge_group event.
required: false

runs:
using: composite
Expand All @@ -14,6 +17,10 @@ runs:
shell: bash
env:
VALID_LABELS: ${{ inputs.valid-labels }}
GH_TOKEN: ${{ inputs.github-token || github.token }}
ACTION_EVENT_NAME: ${{ github.event_name }}
ACTION_REF_NAME: ${{ github.ref_name }}
ACTION_REPOSITORY: ${{ github.repository }}
EVENT_LABELS: ${{ join(github.event.pull_request.labels.*.name, ',') }}
run: |
set -euo pipefail
Expand All @@ -25,8 +32,30 @@ runs:
printf '%s' "$value"
}

get_labels_from_merge_group() {
local queue_ref="$1"

if [[ ! "$queue_ref" =~ pr-([0-9]+)- ]]; then
echo "::error::Could not determine the pull request number from merge queue ref '$queue_ref'."
exit 1
fi

local pr_number="${BASH_REMATCH[1]}"

if [ -z "$(trim "${GH_TOKEN:-}")" ]; then
echo "::error::A GitHub token is required to validate pull request labels for merge_group events."
exit 1
fi

gh api "repos/$ACTION_REPOSITORY/issues/$pr_number" --jq '.labels[].name' | paste -sd, -
}

labels="$EVENT_LABELS"

if [ "$ACTION_EVENT_NAME" = "merge_group" ]; then
labels="$(get_labels_from_merge_group "$ACTION_REF_NAME")"
fi

if [ -z "$(trim "$labels")" ]; then
echo "::error::The pull request must have one of these labels: $VALID_LABELS"
exit 1
Expand Down