[CI Workflow] PR auto label MGMT #2
Workflow file for this run
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: Manage labels based on PR body | |
on: | |
pull_request: | |
types: [opened, edited, reopened, synchronize] | |
jobs: | |
manage-labels: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Analyze PR Body and manage labels | |
run: | | |
body="${{ github.event.pull_request.body }}" | |
# Initialize the labels to be added and removed | |
labels_to_add=() | |
labels_to_remove=() | |
# Check for enhancement | |
if echo "$body" | grep -q '\- \[x\] New feature'; then | |
labels_to_add+=("enhancement") | |
else | |
labels_to_remove+=("enhancement") | |
fi | |
# Check for bug-related labels (Bug fix, Hotfix, Security patch) | |
if echo "$body" | grep -q '\- \[x\] Bug fix\|\- \[x\] Hotfix\|\- \[x\] Security patch'; then | |
labels_to_add+=("bug") | |
else | |
labels_to_remove+=("bug") | |
fi | |
# Check for documentation update | |
if echo "$body" | grep -q '\- \[x\] Documentation update'; then | |
labels_to_add+=("documentation") | |
else | |
labels_to_remove+=("documentation") | |
fi | |
# Check for refactoring | |
if echo "$body" | grep -q '\- \[x\] Refactoring'; then | |
labels_to_add+=("refactor") | |
else | |
labels_to_remove+=("refactor") | |
fi | |
# Check for UI/UX improvement | |
if echo "$body" | grep -q '\- \[x\] UI/UX improvement'; then | |
labels_to_add+=("UI/UX") | |
else | |
labels_to_remove+=("UI/UX") | |
fi | |
# Export the labels to add and remove as environment variables (formatted as space-separated lists) | |
echo "LABELS_TO_ADD=${labels_to_add[*]}" >> $GITHUB_ENV | |
echo "LABELS_TO_REMOVE=${labels_to_remove[*]}" >> $GITHUB_ENV | |
- name: Add labels if necessary | |
if: env.LABELS_TO_ADD != '' | |
run: | | |
for label in ${{ env.LABELS_TO_ADD }}; do | |
curl -s -X POST \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
-d "{\"labels\": [\"$label\"]}" \ | |
https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels | |
done | |
- name: Remove labels if necessary | |
if: env.LABELS_TO_REMOVE != '' | |
run: | | |
for label in ${{ env.LABELS_TO_REMOVE }}; do | |
curl -s -X DELETE \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels/$label | |
done |