Skip to content

Commit

Permalink
Update PRLabeling.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
SrijanCoinDCX authored Dec 6, 2024
1 parent 92f818d commit b42c0da
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions .github/workflows/PRLabeling.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: Label PR Based on Changes
on:
pull_request:
types: [opened, synchronize, reopened]
workflow_dispatch: # Allows you to manually trigger the action on existing PRs

jobs:
label_pr:
Expand Down Expand Up @@ -89,3 +90,74 @@ jobs:
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels \
-d '["${{ env.LABEL }}"]'
label_existing_prs:
runs-on: ubuntu-latest
needs: label_pr
if: github.event_name == 'workflow_dispatch' # Only trigger when manually run

steps:
- name: Fetch all open PRs
run: |
PRS=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/pulls?state=open")
# Loop through each open PR to apply the labels
for PR in $(echo "$PRS" | jq -r '.[].number'); do
echo "Processing PR #$PR"
# Fetch changed files for each PR
CHANGED_FILES=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/pulls/$PR/files")
CHANGED_LINES=0
for FILE in $(echo "$CHANGED_FILES" | jq -r '.[].additions'); do
CHANGED_LINES=$((CHANGED_LINES + FILE))
done
for FILE in $(echo "$CHANGED_FILES" | jq -r '.[].deletions'); do
CHANGED_LINES=$((CHANGED_LINES + FILE))
done
# Set the label based on the number of changed lines
if [ "$CHANGED_LINES" -lt 50 ]; then
LABEL="small"
COLOR="0E8A16" # Green
elif [ "$CHANGED_LINES" -le 200 ]; then
LABEL="medium"
COLOR="FBCA04" # Yellow
else
LABEL="large"
COLOR="D93F0B" # Red
fi
# Get the current labels on PR
CURRENT_LABELS=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/issues/$PR/labels")
OLD_LABEL=$(echo "$CURRENT_LABELS" | jq -r '.[] | select(.name=="small" or .name=="medium" or .name=="large") | .name')
# Remove old label if it exists and is different
if [ "$OLD_LABEL" != "" ] && [ "$OLD_LABEL" != "$LABEL" ]; then
curl -X DELETE \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/issues/$PR/labels/$OLD_LABEL"
fi
# Add the new label with the corresponding color
curl -X POST \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/${{ github.repository }}/labels \
-d '{
"name": "'${LABEL}'",
"color": "'${COLOR}'"
}'
# Apply the new label to the PR
curl -X POST \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/${{ github.repository }}/issues/$PR/labels \
-d '["'${LABEL}'"]'
done

0 comments on commit b42c0da

Please sign in to comment.