Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CI Workflow] PR auto label MGMT #2749

Closed
wants to merge 5 commits into from
Closed
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
56 changes: 56 additions & 0 deletions .github/workflows/pr-labels.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Manage labels based on PR body

on:
pull_request_target:
types: [opened, edited, reopened, synchronize]

jobs:
manage-labels:
runs-on: ubuntu-latest
steps:
- name: Analyze PR Body and manage labels
shell: bash
run: |
body=$(jq -r '.pull_request.body' "$GITHUB_EVENT_PATH")
labels_to_add=()
labels_to_remove=()
declare -A label_checks=(
["New feature"]="enhancement"
["Bug fix|Hotfix|Security patch"]="bug"
["Documentation update"]="documentation"
["Refactoring"]="refactor"
["UI/UX improvement"]="UI/UX"
)
for pattern in "${!label_checks[@]}"; do
label="${label_checks[$pattern]}"
if echo "$body" | grep -Eq "\- \[x\] ($pattern)"; then
labels_to_add+=("$label")
else
labels_to_remove+=("$label")
fi
done

echo "LABELS_TO_ADD=$(IFS=,; echo "${labels_to_add[*]}")" >> $GITHUB_ENV
echo "LABELS_TO_REMOVE=$(IFS=,; echo "${labels_to_remove[*]}")" >> $GITHUB_ENV

- name: Add labels if necessary
if: env.LABELS_TO_ADD != ''
run: |
IFS=',' read -ra labels <<< "${LABELS_TO_ADD}"
for label in "${labels[@]}"; 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: |
IFS=',' read -ra labels <<< "${LABELS_TO_REMOVE}"
for label in "${labels[@]}"; 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