Skip to content

Commit f7aad30

Browse files
authored
Merge pull request #7 from nifadyev/feature/add-check-for-max-changed-loc
Add Github Action check for max changed lines of code in PR
2 parents b57760b + a91ed72 commit f7aad30

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: "Large PR checker"
2+
description: "Blocks PR if number of lines changed is excessive. Modified version of https://github.com/adolfosilva/gh-large-pr-check/blob/main/action.yml"
3+
4+
inputs:
5+
max_lines_changed:
6+
description: "Maximum number of lines changed allowed"
7+
required: true
8+
default: "500"
9+
target_branch:
10+
description: The branch to compare against
11+
required: true
12+
default: main
13+
outputs:
14+
total_lines_changed:
15+
description: "Total lines changed in this PR"
16+
value: ${{ steps.get_total_lines_changed.outputs.total_lines_changed }}
17+
18+
runs:
19+
using: "composite"
20+
steps:
21+
- id: fetch_target_branch
22+
run: |
23+
git fetch origin ${{ inputs.target_branch }}
24+
shell: bash
25+
- id: get_total_lines_changed
26+
run: |
27+
size=$(git diff --shortstat origin/${{ inputs.target_branch }} ':(exclude)*.lock' \
28+
| awk '{ print $4+$6 }' \
29+
| awk -F- '{print $NF}' \
30+
| bc)
31+
32+
echo ""
33+
echo "Total lines changed (note: *.lock files are excluded from this count): $size"
34+
echo "total_lines_changed=$size" >> $GITHUB_OUTPUT
35+
shell: bash
36+
- name: Comment PR
37+
if: ${{ fromJSON(steps.get_total_lines_changed.outputs.total_lines_changed) > fromJSON(inputs.max_lines_changed) }}
38+
uses: thollander/actions-comment-pull-request@v2
39+
with:
40+
comment_tag: pr_size
41+
mode: recreate
42+
message: |
43+
:boom: :boom: :boom:
44+
Total lines changed ${{ steps.get_total_lines_changed.outputs.total_lines_changed }} is greater than ${{ inputs.max_lines_changed }}.
45+
Please consider breaking this PR down.
46+
- id: fail
47+
if: ${{ fromJSON(steps.get_total_lines_changed.outputs.total_lines_changed) > fromJSON(inputs.max_lines_changed) }}
48+
run: exit 1
49+
shell: bash
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Validate & Lint & Test
2+
3+
on: [pull_request]
4+
5+
concurrency:
6+
group: ${{ github.workflow }}-${{ github.ref }}
7+
cancel-in-progress: true
8+
9+
jobs:
10+
check-pr-diff:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout repository
15+
uses: actions/checkout@v4
16+
17+
- name: Check PR diff size
18+
uses: ./.github/actions/large-pr-check
19+
with:
20+
target_branch: ${{ github.event.pull_request.base.ref }}
21+
max_lines_changed: 100

{{cookiecutter.project_name}}/Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,8 @@ lint:
7676

7777
safety:
7878
poetry run safety check --policy-file=.safety-policy.yml
79+
80+
check-changed-loc:
81+
chmod +x ./scripts/pr-max-diff-checker.sh
82+
# WARNING: Adopt arguments for project specific requirements
83+
./scripts/pr-max-diff-checker.sh 300 origin/master
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
# Script helps to block Pull Requests with exceeded number of changed lines of code.
4+
# Firstly, it compares HEAD with source branch
5+
# Then it counts changed lines of code, excluding specified files and respecting max lines of code
6+
7+
# Read first input arg or take default value - 500
8+
MAX_CHANGED_LOC="${1:-500}"
9+
10+
# Second argument (source branch) is required
11+
if [ -z "$2" ]; then
12+
echo "No remote source branch supplied"
13+
exit 1
14+
else
15+
source_branch=$2
16+
fi
17+
18+
# Parse such line `2 files changed, 18 insertions(+), 248 deletions(-)` and write to variable
19+
changed_loc=$(git diff --shortstat ${source_branch} ':(exclude)*.lock' \
20+
| awk '{ print $4+$6 }' \
21+
| awk -F- '{print $NF}' \
22+
| bc)
23+
24+
if [[ $changed_loc -le $MAX_CHANGED_LOC ]]; then
25+
echo "${changed_loc} lines of code changed. It is allowed to create Pull Request"
26+
exit 0
27+
else
28+
echo "${changed_loc} lines of code changed. It is more than allowed ${MAX_CHANGED_LOC}. Please divide changes into several branches"
29+
exit 1
30+
fi

0 commit comments

Comments
 (0)