Skip to content

Commit

Permalink
sagemathgh-37262: Added automatic size labeler
Browse files Browse the repository at this point in the history
    
I have implemented the `automatic size labeler`, which now assigns
labels to pull requests based on the number of lines changed

**Minimal**
Typically involves very small changes, bug fixes, or updates that
require only a few lines of code, often less than 50.
sagemath#37208 sagemath#37146 sagemath#37043

**Small**
Involves more substantial changes than minimal, potentially adding new
features or making modifications to existing ones. The range is usually
between 50 to 100 lines of code.
sagemath#37152 sagemath#37132

**Moderate**
Represents a significant portion of the codebase being modified, such as
adding new features, refactoring, or making extensive changes to
existing functionalities. This might involve between 100 to 300 lines of
code.
sagemath#36919 sagemath#37112

**Large**
Involves substantial and complex changes across various parts of the
codebase. This could include major architectural changes, the
introduction of new modules, or a significant overhaul of existing
features, often exceeding 300 lines of code.
sagemath#37125 sagemath#36977 sagemath#36972

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->
<!-- If your change requires a documentation PR, please link it
appropriately -->
<!-- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
<!-- Feel free to remove irrelevant items. -->

- [x] The title is concise, informative, and self-explanatory.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.

Fixes: sagemath#37254
    
URL: sagemath#37262
Reported by: Aman Moon
Reviewer(s): Sebastian Oehms
  • Loading branch information
Release Manager committed Apr 20, 2024
2 parents db77586 + 4afc8fd commit 4800287
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/pr-labeler.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This action automatically labels Pull-Requests
# based on files edited and no of lines changed.
name: Size Labeler/Checker
on:
pull_request_target:
types:
- opened
- reopened
- synchronize
- ready_for_review
- review_requested
- edited
jobs:
label-changes:
if: vars.SMALL_THRESHOLD && vars.MODERATE_THRESHOLD && vars.LARGE_THRESHOLD && github.event.pull_request.draft == false
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}

- name: Add labels based on size
run: |
git fetch origin $BASE_SHA
chmod a+x .github/workflows/set_labels_by_changes.sh
.github/workflows/set_labels_by_changes.sh
env:
BASE_SHA: ${{ github.base_ref }}
PR_BASE_SHA: ${{ github.event.pull_request.base.sha }}
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPOSITORY: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number}}
SMALL_THRESHOLD: ${{ vars.SMALL_THRESHOLD }}
MODERATE_THRESHOLD: ${{ vars.MODERATE_THRESHOLD }}
LARGE_THRESHOLD: ${{ vars.LARGE_THRESHOLD }}
67 changes: 67 additions & 0 deletions .github/workflows/set_labels_by_changes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env bash

echo 'set_labels_by_changes.sh called with environment:'
echo "BASE SHA: $PR_BASE_SHA"
echo "HEAD SHA: $PR_HEAD_SHA"
echo "SMALL THRESHOLD $SMALL_THRESHOLD"
echo "MODERATE THERESHOLD: $MODERATE_THRESHOLD"
echo "LARGE THRESHOLD: $LARGE_THRESHOLD"

# get all the changes made and changed files
CHANGES=$(git diff --ignore-all-space $PR_BASE_SHA $PR_HEAD_SHA)

# ignore blank lines
CHANGES=$(echo "$CHANGES" | grep -vE '^[\+\-]\s*$')

# ignore non necessary lines from git diff
CHANGES=$(echo "$CHANGES" | grep -E '^[+\-]' | grep -vE '^\+\+\+|^\-\-\-')

# count total no of lines
CHANGES=$(echo "$CHANGES" | wc -l)

echo "CHANGES MADE: $CHANGES"

AUTH_HEADER="Authorization: Bearer $GITHUB_TOKEN"

MINIMAL="v: minimal"
SMALL="v: small"
MODERATE="v: moderate"
LARGE="v: large"

DELETE_LABELS=("$MINIMAL" "$SMALL" "$MODERATE" "$LARGE")

if [ "$CHANGES" -gt "$LARGE_THRESHOLD" ]; then
SIZE_LABEL="$LARGE"
elif [ "$CHANGES" -gt "$MODERATE_THRESHOLD" ]; then
SIZE_LABEL="$MODERATE"
elif [ "$CHANGES" -gt "$SMALL_THRESHOLD" ]; then
SIZE_LABEL="$SMALL"
else
SIZE_LABEL="$MINIMAL"
fi

DELETE_LABELS=("${DELETE_LABELS[@]//${SIZE_LABEL}/}")

# API for adding labels on the Pull Request
API_URL="https://api.github.com/repos/$REPOSITORY/issues/$PR_NUMBER/labels"

echo "Adding label: ${SIZE_LABEL[@]}"
for LABEL in "${SIZE_LABEL[@]}"; do
curl -X POST \
-H "$AUTH_HEADER" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-d "{\"labels\":[\"$LABEL\"]}" \
"$API_URL" >/dev/null
done

echo "Deleting Labels:"

for DELETE_LABEL in "${DELETE_LABELS[@]}"; do
ENCODED_LABEL=$(echo "$DELETE_LABEL" | sed 's/ /%20/g')
curl -X DELETE \
-H "Accept: application/vnd.github+json" \
-H "$AUTH_HEADER" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"$API_URL/$ENCODED_LABEL" >/dev/null
done

0 comments on commit 4800287

Please sign in to comment.