Skip to content

Commit 5b02d25

Browse files
committed
Support ignoring file deletions
1 parent 13d17c4 commit 5b02d25

12 files changed

+66
-12
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ jobs:
7272
| `github_api_url` | No | 'https://api.github.com' | URL for the GitHub API, can be changed for GitHub Enterprise Servers. |
7373
| `files_to_ignore` | No | '' | Files to ignore during PR size calculation. Supports newline or whitespace delimited list. |
7474
| `ignore_line_deletions` | No | 'false' | Whether to ignore lines which are deleted when calculating the PR size. If set to 'true', deleted lines will be ignored. |
75+
| `ignore_file_deletions` | No | 'false' | Whether to ignore completely deleted files when calculating the PR size. If set to 'true', deleted files will be ignored. |
7576

7677
### Example for `files_to_ignore`:
7778
```yml
@@ -87,6 +88,7 @@ files_to_ignore: |
8788

8889
- PR Size Labeler considers any line addition, deletion, or modification as a change.
8990
- A PR will be labeled as 'xl' if it exceeds the amount of changes defined in `l_max_size`.
91+
- `ignore_file_deletions` is distinct from `ignore_line_deletions` in that it only ignores files which are deleted completely.
9092

9193
## ⚖️ License
9294

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ inputs:
6363
description: 'Whether to ignore lines which are deleted when calculating the PR size. If set to "true", deleted lines will be ignored.'
6464
required: false
6565
default: 'false'
66+
ignore_file_deletions:
67+
description: 'Whether to ignore files which are deleted when calculating the PR size. If set to "true", deleted files will be ignored.'
68+
required: false
69+
default: 'false'
6670
runs:
6771
using: 'docker'
6872
image: 'Dockerfile'

src/github.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ github::calculate_total_modifications() {
66
local -r pr_number="${1}"
77
local -r files_to_ignore="${2}"
88
local -r ignore_line_deletions="${3}"
9+
local -r ignore_file_deletions="${4}"
910

1011
local additions=0
1112
local deletions=0
1213

13-
if [ -z "$files_to_ignore" ]; then
14+
if [ -z "$files_to_ignore" ] && [ "$ignore_file_deletions" != "true" ]; then
1415
local -r body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls/$pr_number")
1516

1617
additions=$(echo "$body" | jq '.additions')
@@ -19,12 +20,18 @@ github::calculate_total_modifications() {
1920
((deletions += $(echo "$body" | jq '.deletions')))
2021
fi
2122
else
23+
# NOTE: this code is not resilient to changes w/ > 100 files as we're not paginating
2224
local -r body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls/$pr_number/files?per_page=100")
2325

2426
for file in $(echo "$body" | jq -r '.[] | @base64'); do
2527
filename=$(jq::base64 '.filename')
28+
status=$(jq::base64 '.status')
2629
ignore=false
2730

31+
if [ "$ignore_file_deletions" == "true" ] && [ "$status" == "removed" ]; then
32+
continue
33+
fi
34+
2835
for pattern in $files_to_ignore; do
2936
if [[ $filename == $pattern ]]; then
3037
ignore=true

src/labeler.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ labeler::label() {
1010
local -r message_if_xl="${11}"
1111
local -r files_to_ignore="${12}"
1212
local -r ignore_line_deletions="${13}"
13+
local -r ignore_file_deletions="${14}"
1314

1415
local -r pr_number=$(github_actions::get_pr_number)
15-
local -r total_modifications=$(github::calculate_total_modifications "$pr_number" "${files_to_ignore[*]}" "$ignore_line_deletions")
16+
local -r total_modifications=$(github::calculate_total_modifications "$pr_number" "${files_to_ignore[*]}" "$ignore_line_deletions" "$ignore_file_deletions")
1617

1718
log::message "Total modifications (additions + deletions): $total_modifications"
1819
log::message "Ignoring files (if present): $files_to_ignore"

src/main.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ source "$PR_SIZE_LABELER_HOME/src/misc.sh"
99
##? Adds a size label to a GitHub Pull Request
1010
##?
1111
##? Usage:
12-
##? main.sh --github_token=<token> --xs_label=<label> --xs_max_size=<size> --s_label=<label> --s_max_size=<size> --m_label=<label> --m_max_size=<size> --l_label=<label> --l_max_size=<size> --xl_label=<label> --fail_if_xl=<false> --message_if_xl=<message> --github_api_url=<url> --files_to_ignore=<files> --ignore_line_deletions=<false>
12+
##? main.sh --github_token=<token> --xs_label=<label> --xs_max_size=<size> --s_label=<label> --s_max_size=<size> --m_label=<label> --m_max_size=<size> --l_label=<label> --l_max_size=<size> --xl_label=<label> --fail_if_xl=<false> --message_if_xl=<message> --github_api_url=<url> --files_to_ignore=<files> --ignore_line_deletions=<false> --ignore_file_deletions=<false>
1313
main() {
1414
eval "$(/root/bin/docpars -h "$(grep "^##?" "$PR_SIZE_LABELER_HOME/src/main.sh" | cut -c 5-)" : "$@")"
1515

@@ -32,7 +32,8 @@ main() {
3232
"$fail_if_xl" \
3333
"$message_if_xl" \
3434
"$files_to_ignore" \
35-
"$ignore_line_deletions"
35+
"$ignore_line_deletions" \
36+
"$ignore_file_deletions"
3637

3738
exit $?
3839
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,93 @@
11
[
22
{
33
"filename": ".editorconfig",
4+
"status": "added",
45
"additions": 9,
56
"deletions": 0,
67
"changes": 9
78
},
89
{
910
"filename": ".github/workflows/ci.yml",
11+
"status": "modified",
1012
"additions": 17,
1113
"deletions": 0,
1214
"changes": 17
1315
},
1416
{
1517
"filename": "asdasdasd.lock",
18+
"status": "modified",
1619
"additions": 1188,
1720
"deletions": 0,
1821
"changes": 1188
1922
},
2023
{
2124
"filename": "entrypoint.sh",
25+
"status": "modified",
2226
"additions": 4,
2327
"deletions": 4,
2428
"changes": 8
2529
},
2630
{
2731
"filename": "src/ensure.sh",
32+
"status": "modified",
2833
"additions": 4,
2934
"deletions": 4,
3035
"changes": 8
3136
},
3237
{
3338
"filename": "src/github.sh",
39+
"status": "added",
3440
"additions": 89,
3541
"deletions": 76,
3642
"changes": 165
3743
},
3844
{
3945
"filename": "src/github_actions.sh",
46+
"status": "modified",
4047
"additions": 1,
4148
"deletions": 1,
4249
"changes": 2
4350
},
4451
{
4552
"filename": "src/inner.lock",
53+
"status": "modified",
4654
"additions": 1188,
4755
"deletions": 0,
4856
"changes": 1188
4957
},
5058
{
5159
"filename": "src/labeler.sh",
60+
"status": "modified",
5261
"additions": 48,
5362
"deletions": 48,
5463
"changes": 96
5564
},
5665
{
5766
"filename": "src/log.sh",
67+
"status": "modified",
5868
"additions": 17,
5969
"deletions": 0,
6070
"changes": 17
6171
},
6272
{
6373
"filename": "src/main.sh",
74+
"status": "modified",
6475
"additions": 34,
6576
"deletions": 23,
6677
"changes": 57
6778
},
6879
{
6980
"filename": "src/misc.sh",
81+
"status": "modified",
7082
"additions": 10,
7183
"deletions": 14,
7284
"changes": 24
85+
},
86+
{
87+
"filename": "deleted_file.txt",
88+
"status": "removed",
89+
"additions": 0,
90+
"deletions": 123,
91+
"changes": 123
7392
}
7493
]

tests/github_test.sh

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,54 @@ function set_up() {
88
pr_number=123
99
files_to_ignore=''
1010
ignore_line_deletions='false'
11+
ignore_file_deletions='false'
1112

1213
function test_should_count_changes() {
1314
mock curl cat ./tests/fixtures/pull_request_api
1415

15-
assert_match_snapshot "$(github::calculate_total_modifications "$pr_number" "${files_to_ignore[*]}" "$ignore_line_deletions")"
16+
assert_match_snapshot "$(github::calculate_total_modifications "$pr_number" "${files_to_ignore[*]}" "$ignore_line_deletions" "$ignore_file_deletions")"
1617
}
1718

18-
function test_should_count_changes_ignore_deletions() {
19+
function test_should_count_changes_ignore_line_deletions() {
1920
ignore_line_deletions='true'
2021

2122
mock curl cat ./tests/fixtures/pull_request_api
2223

23-
assert_match_snapshot "$(github::calculate_total_modifications "$pr_number" "${files_to_ignore[*]}" "$ignore_line_deletions")"
24+
assert_match_snapshot "$(github::calculate_total_modifications "$pr_number" "${files_to_ignore[*]}" "$ignore_line_deletions" "$ignore_file_deletions")"
2425
}
2526

26-
# NOTE: when `files_to_ignore` is set, we have to invoke the PR files API and iterate each file
27+
# NOTE: when `files_to_ignore` or `ignore_file_deletions` is set, we have to invoke the PR files API and iterate each file
2728
# one at at time. This is why the mock call is diffent in the subsequent test cases
29+
function test_should_count_changes_ignore_file_deletions() {
30+
ignore_file_deletions='true'
31+
32+
mock curl cat ./tests/fixtures/pull_request_files_api
33+
34+
assert_match_snapshot "$(github::calculate_total_modifications "$pr_number" "${files_to_ignore[*]}" "$ignore_line_deletions" "$ignore_file_deletions")"
35+
}
36+
2837
function test_should_ignore_files_with_glob() {
2938
files_to_ignore=("*.lock" ".editorconfig")
3039

3140
mock curl cat ./tests/fixtures/pull_request_files_api
3241

33-
assert_match_snapshot "$(github::calculate_total_modifications "$pr_number" "${files_to_ignore[*]}" "$ignore_line_deletions")"
42+
assert_match_snapshot "$(github::calculate_total_modifications "$pr_number" "${files_to_ignore[*]}" "$ignore_line_deletions" "$ignore_file_deletions")"
3443
}
3544

36-
function test_should_ignore_files_with_glob_ignore_deletions() {
45+
function test_should_ignore_files_with_glob_ignore_line_deletions() {
3746
files_to_ignore=("*.lock" ".editorconfig")
3847
ignore_line_deletions='true'
3948

4049
mock curl cat ./tests/fixtures/pull_request_files_api
4150

42-
assert_match_snapshot "$(github::calculate_total_modifications "$pr_number" "${files_to_ignore[*]}" "$ignore_line_deletions")"
51+
assert_match_snapshot "$(github::calculate_total_modifications "$pr_number" "${files_to_ignore[*]}" "$ignore_line_deletions" "$ignore_file_deletions")"
52+
}
53+
54+
function test_should_ignore_files_with_glob_ignore_file_deletions() {
55+
files_to_ignore=("*.lock" ".editorconfig")
56+
ignore_file_deletions='true'
57+
58+
mock curl cat ./tests/fixtures/pull_request_files_api
59+
60+
assert_match_snapshot "$(github::calculate_total_modifications "$pr_number" "${files_to_ignore[*]}" "$ignore_line_deletions" "$ignore_file_deletions")"
4361
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2779

tests/snapshots/github_test_sh.test_should_count_changes_ignore_deletions.snapshot renamed to tests/snapshots/github_test_sh.test_should_count_changes_ignore_line_deletions.snapshot

File renamed without changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
394
1+
517

0 commit comments

Comments
 (0)