Skip to content

Commit

Permalink
Add workflow to enforce labels on PRs (#14413)
Browse files Browse the repository at this point in the history
  • Loading branch information
desertaxle authored Jul 8, 2024
1 parent 778ae8d commit 6a1300b
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions .github/workflows/label-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,43 @@ name: Ensure PR Label

on:
pull_request:
types: [opened, edited, labeled, unlabeled]
types: [opened, edited, labeled, unlabeled, synchronize, reopened]

jobs:
ensure-label:
runs-on: ubuntu-latest

steps:
- name: Check for required labels
id: label-check
run: |
LABELS=$(jq -r '.pull_request.labels[].name' "$GITHUB_EVENT_PATH")
REQUIRED_LABELS=("docs" "integrations" "fix" "enhancement" "feature" "maintenance")
LABEL_FOUND=false
for label in "${REQUIRED_LABELS[@]}"; do
if echo "$LABELS" | grep -q "$label"; then
LABEL_FOUND=true
break
fi
done
if [ "$LABEL_FOUND" = false ]; then
echo "##[error]This pull request must have one of the following labels to help with sorting for release notes:"
echo " - docs"
echo " - maintenance"
echo " - deprecation"
echo " - integrations"
echo " - fix"
echo " - enhancement"
echo " - feature"
exit 1
fi
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: "pip"

- name: Install yq
run: |
pip install yq
- name: Fail the PR if no required label is found
if: steps.label-check.outputs.LABEL_FOUND == 'false'
run: exit 1
- name: Ensure a required label is present
id: check-label
run: |
found=false
for required_label in $(yq -r '(.changelog.categories[] | select(.title != "Uncategorized") | .labels[]), (.changelog.exclude.labels[])' .github/release.yml); do
for pr_label in $(jq -r '.pull_request.labels[].name' "$GITHUB_EVENT_PATH"); do
if [[ "$required_label" == "$pr_label" ]]; then
found=true
break 2
fi
done
done
echo "label_exists=$found" >> $GITHUB_OUTPUT
- name: Fail if no required labels are found
if: steps.check-label.outputs.label_exists == 'false'
run: |
echo "None of the required labels are applied to the PR."
exit 1

0 comments on commit 6a1300b

Please sign in to comment.