linutil/.github/workflows/pr-labels.yaml

57 lines
2.1 KiB
YAML
Raw Normal View History

2024-09-16 20:56:18 +01:00
name: Manage labels based on PR body
on:
2024-09-17 11:52:36 +01:00
pull_request_target:
2024-09-16 20:56:18 +01:00
types: [opened, edited, reopened, synchronize]
jobs:
manage-labels:
runs-on: ubuntu-latest
steps:
- name: Analyze PR Body and manage labels
shell: bash
2024-09-16 20:56:18 +01:00
run: |
body=$(jq -r '.pull_request.body' "$GITHUB_EVENT_PATH")
2024-09-16 20:56:18 +01:00
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")
2024-09-16 20:56:18 +01:00
else
labels_to_remove+=("$label")
2024-09-16 20:56:18 +01:00
fi
done
echo "LABELS_TO_ADD=$(IFS=,; echo "${labels_to_add[*]}")" >> $GITHUB_ENV
echo "LABELS_TO_REMOVE=$(IFS=,; echo "${labels_to_remove[*]}")" >> $GITHUB_ENV
2024-09-16 20:56:18 +01:00
- name: Add labels if necessary
if: env.LABELS_TO_ADD != ''
run: |
IFS=',' read -ra labels <<< "${LABELS_TO_ADD}"
for label in "${labels[@]}"; do
2024-09-16 20:56:18 +01:00
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
2024-09-16 20:56:18 +01:00
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
2024-09-17 11:52:36 +01:00
done