PR Issue Labeler #20
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: PR Issue Labeler | |
on: | |
pull_request: | |
types: [opened, edited, reopened] | |
workflow_dispatch: # Manually trigger the workflow for existing open PRs | |
jobs: | |
labeler: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Check PR description for issue number | |
id: check_issue | |
run: | | |
ISSUE_NUMBER=$(echo "${{ github.event.pull_request.body }}" | grep -oP '#\K\d+') | |
echo "issue_number=$ISSUE_NUMBER" >> $GITHUB_ENV | |
- name: Handle missing issue number | |
if: env.issue_number == '' | |
run: | | |
gh pr comment ${{ github.event.pull_request.number }} --body "Please include an issue number (e.g., #123) in the description." | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Fetch issue labels and apply to PR | |
if: env.issue_number != '' | |
run: | | |
# Get issue labels | |
LABELS=$(gh issue view ${{ env.issue_number }} --json labels --jq '.labels[].name') | |
# Apply labels to the PR | |
for LABEL in $LABELS; do | |
gh pr edit ${{ github.event.pull_request.number }} --add-label "$LABEL" | |
done | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |