From b42c0da422ea724df42b809fa464fcfd7b4d5b8b Mon Sep 17 00:00:00 2001 From: Srijan Samridh <166486637+SrijanCoinDCX@users.noreply.github.com> Date: Fri, 6 Dec 2024 16:02:58 +0530 Subject: [PATCH] Update PRLabeling.yml --- .github/workflows/PRLabeling.yml | 72 ++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/.github/workflows/PRLabeling.yml b/.github/workflows/PRLabeling.yml index 105499e..773bbdc 100644 --- a/.github/workflows/PRLabeling.yml +++ b/.github/workflows/PRLabeling.yml @@ -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: @@ -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