Skip to content

Commit 989fd51

Browse files
authored
#78: Add pr diff size checker in CI and pre-commit (#91)
1 parent 17b0297 commit 989fd51

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed
+49
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

.github/workflows/pr.yml

+16
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ env:
1818
RUFF_VERSION: "0.6.7"
1919

2020
jobs:
21+
check-pr-diff:
22+
runs-on: ubuntu-latest
23+
24+
steps:
25+
- name: Checkout repository
26+
uses: actions/checkout@v4
27+
28+
- name: Check PR diff size
29+
uses: ./.github/actions/large-pr-check
30+
with:
31+
target_branch: ${{ github.event.pull_request.base.ref }}
32+
max_lines_changed: 300
33+
2134
lint:
2235
runs-on: ubuntu-latest
2336
timeout-minutes: 10
@@ -37,6 +50,9 @@ jobs:
3750
- name: Run Ruff
3851
run: ruff check --output-format=github .
3952

53+
- name: Spell Check
54+
uses: crate-ci/typos@master
55+
4056
test:
4157
runs-on: ubuntu-latest
4258
strategy:

.pre-commit-config.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,10 @@ repos:
4141
stages: [pre-push]
4242
language: python
4343
types: [python]
44+
- id: check-pr-size
45+
name: check-pr-size
46+
entry: chmod +x scripts/large-pr-checker.sh && ./large-pr-checker.sh
47+
pass_filenames: false
48+
stages: [pre-push]
49+
language: system
50+
types: [python]

scripts/large-pr-checker.sh

+30
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)