From 7a75530b9ac6680156c6e51be6606bcfdd226910 Mon Sep 17 00:00:00 2001 From: Rahul Bhardwaj Date: Thu, 3 Oct 2024 23:36:00 +0530 Subject: [PATCH 1/2] Add GitHub Actions workflow to label pull requests based on size --- .github/workflows/pullreq_size.yml | 62 ++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 .github/workflows/pullreq_size.yml diff --git a/.github/workflows/pullreq_size.yml b/.github/workflows/pullreq_size.yml new file mode 100644 index 0000000..975c717 --- /dev/null +++ b/.github/workflows/pullreq_size.yml @@ -0,0 +1,62 @@ +name: 'Label PR Based on Size' + +on: + pull_request: + types: [opened, reopened, synchronize] + +jobs: + label-pr-size: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up jq (for processing JSON) + run: sudo apt-get install jq + + - name: Label PR based on size + run: | + XS_LIMIT=10 + SM_LIMIT=50 + MD_LIMIT=200 + LG_LIMIT=500 + LABEL_XS="size-xs" + LABEL_SM="size-sm" + LABEL_MD="size-md" + LABEL_LG="size-lg" + LABEL_XL="size-xl" + + GITHUB_TOKEN="${{ secrets.GITHUB_TOKEN }}" + GITHUB_REPOSITORY="${{ github.repository }}" + PR_NUMBER="${{ github.event.pull_request.number }}" + API_URI="https://api.github.com" + API_HEADER="Accept: application/vnd.github.v3+json" + AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}" + + # Function to calculate total lines added and deleted + additions=$(curl -sSL -H "$API_HEADER" -H "$AUTH_HEADER" \ + "${API_URI}/repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}" | jq '.additions') + deletions=$(curl -sSL -H "$API_HEADER" -H "$AUTH_HEADER" \ + "${API_URI}/repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}" | jq '.deletions') + total_modifications=$((additions + deletions)) + + # Determine the label based on total changes + if [ "$total_modifications" -lt "$XS_LIMIT" ]; then + label="$LABEL_XS" + elif [ "$total_modifications" -lt "$SM_LIMIT" ]; then + label="$LABEL_SM" + elif [ "$total_modifications" -lt "$MD_LIMIT" ]; then + label="$LABEL_MD" + elif [ "$total_modifications" -lt "$LG_LIMIT" ]; then + label="$LABEL_LG" + else + label="$LABEL_XL" + fi + + # Apply the label + curl -sSL -H "$API_HEADER" -H "$AUTH_HEADER" -X POST -d "{\"labels\":[\"$label\"]}" \ + "${API_URI}/repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/labels" + + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From ec8886b817ed4564714f9e16b21d831ad635767c Mon Sep 17 00:00:00 2001 From: Rahul Bhardwaj Date: Fri, 4 Oct 2024 19:31:48 +0530 Subject: [PATCH 2/2] Add GitHub Actions workflow to enable labeling of pull requests based on size --- .github/workflows/pullreq_size.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/pullreq_size.yml b/.github/workflows/pullreq_size.yml index 975c717..25bb62d 100644 --- a/.github/workflows/pullreq_size.yml +++ b/.github/workflows/pullreq_size.yml @@ -7,6 +7,10 @@ on: jobs: label-pr-size: runs-on: ubuntu-latest + permissions: + issues: write + pull-requests: write + steps: - name: Checkout code