Skip to content

Commit cc6827c

Browse files
johnlkOnkarRuikarJavierCanerwyland
authored
Support ignoring file deletions (#76)
* Support ignoring file deletions Tweak action.yml * Tweak the readme description Co-authored-by: Onkar Ruikar <[email protected]> * tweak removed file check to include line_deletion var Co-authored-by: Onkar Ruikar <[email protected]> * docs: unify the "Basic concepts or assumptions" readme section into the most related arguments to make it easier to read Improve the existing contribution guide (#74) * Improve the existing contribution guide * reorder the contribution steps to push testing up Co-authored-by: Javier Ferrer González <[email protected]> * tweak contribution process opener copy Co-authored-by: Javier Ferrer González <[email protected]> --------- Co-authored-by: Javier Ferrer González <[email protected]> Put back accidental deletion --------- Co-authored-by: Onkar Ruikar <[email protected]> Co-authored-by: Javier Ferrer González <[email protected]> Co-authored-by: Rob W <[email protected]>
1 parent f8dce3e commit cc6827c

12 files changed

+87
-35
lines changed

README.md

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020

2121
## 🚀 Usage
2222

23-
Create a file named `labeler.yml` inside the `.github/workflows` directory and paste the following configuration:
23+
Create a file named `labeler.yml` inside the `.github/workflows` directory and paste the following configuration.
24+
25+
> [!NOTE]
26+
> Take into account that PR Size Labeler considers any line addition, deletion, or modification as a change by default, but you can configure it with [optional arguments](https://github.com/CodelyTV/pr-size-labeler?tab=readme-ov-file#%EF%B8%8F-arguments) such as `files_to_ignore`, `ignore_file_deletions`, or even `ignore_line_deletions`.
2427
2528
```yml
2629
name: labeler
@@ -55,23 +58,24 @@ jobs:
5558
5659
## 🎛️ Arguments
5760
58-
| Name | Required | Default Value | Description |
59-
|-------------------------|----------|----------------------|-------------------------------------------------------------------------------------------------------------------------|
60-
| `GITHUB_TOKEN` | Yes | Automatically supplied| GitHub token needed to interact with the repository. |
61-
| `xs_label` | No | 'size/xs' | Label for very small-sized PRs. |
62-
| `xs_max_size` | No | '10' | Maximum number of changes allowed for XS-sized PRs. |
63-
| `s_label` | No | 'size/s' | Label for small-sized PRs. |
64-
| `s_max_size` | No | '100' | Maximum number of changes allowed for S-sized PRs. |
65-
| `m_label` | No | 'size/m' | Label for medium-sized PRs. |
66-
| `m_max_size` | No | '500' | Maximum number of changes allowed for M-sized PRs. |
67-
| `l_label` | No | 'size/l' | Label for large-sized PRs. |
68-
| `l_max_size` | No | '1000' | Maximum number of changes allowed for L-sized PRs. |
69-
| `xl_label` | No | 'size/xl' | Label for extra-large-sized PRs. |
70-
| `fail_if_xl` | No | 'false' | Whether to fail the GitHub workflow if the PR size is 'XL' (blocks the merge). |
71-
| `message_if_xl` | No | Custom message | Message to display when a PR exceeds the 'XL' size limit. |
72-
| `github_api_url` | No | 'https://api.github.com' | URL for the GitHub API, can be changed for GitHub Enterprise Servers. |
73-
| `files_to_ignore` | No | '' | Files to ignore during PR size calculation. Supports newline or whitespace delimited list. |
74-
| `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. |
61+
| Name | Required | Default Value | Description |
62+
|-------------------------|----------|----------------------|---------------------------------------------------------------------------------------------------------------------------|
63+
| `GITHUB_TOKEN` | Yes | Automatically supplied| GitHub token needed to interact with the repository. |
64+
| `xs_label` | No | 'size/xs' | Label for very small-sized PRs. |
65+
| `xs_max_size` | No | '10' | Maximum number of changes allowed for XS-sized PRs. |
66+
| `s_label` | No | 'size/s' | Label for small-sized PRs. |
67+
| `s_max_size` | No | '100' | Maximum number of changes allowed for S-sized PRs. |
68+
| `m_label` | No | 'size/m' | Label for medium-sized PRs. |
69+
| `m_max_size` | No | '500' | Maximum number of changes allowed for M-sized PRs. |
70+
| `l_label` | No | 'size/l' | Label for large-sized PRs. |
71+
| `l_max_size` | No | '1000' | Maximum number of changes allowed for L-sized PRs. |
72+
| `xl_label` | No | 'size/xl' | Label for extra-large-sized PRs. A PR will be labeled as 'xl' if it exceeds the amount of changes defined in `l_max_size` |
73+
| `fail_if_xl` | No | 'false' | Whether to fail the GitHub workflow if the PR size is 'XL' (blocks the merge). |
74+
| `message_if_xl` | No | Custom message | Message to display when a PR exceeds the 'XL' size limit. |
75+
| `github_api_url` | No | 'https://api.github.com' | URL for the GitHub API, can be changed for GitHub Enterprise Servers. |
76+
| `files_to_ignore` | No | '' | Files to ignore during PR size calculation. Supports newline or whitespace delimited list. |
77+
| `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. |
78+
| `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. Distinct from `ignore_line_deletions` in that it only ignores files which are deleted completely. If `ignore_line_deletions` is used then using `ignore_file_deletions` is redundant. |
7579

7680
### Example for `files_to_ignore`:
7781
```yml
@@ -83,11 +87,6 @@ files_to_ignore: |
8387
"docs/*"
8488
```
8589

86-
## 🤔 Basic concepts or assumptions
87-
88-
- PR Size Labeler considers any line addition, deletion, or modification as a change.
89-
- A PR will be labeled as 'xl' if it exceeds the amount of changes defined in `l_max_size`.
90-
9190
## Contributing
9291
If you would like to help improve the project, please read the [contribution guidelines](https://github.com/CodelyTV/pr-size-labeler/blob/main/.github/CONTRIBUTIONS.md).
9392

action.yml

Lines changed: 5 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'
@@ -82,6 +86,7 @@ runs:
8286
- --message_if_xl="${{ inputs.message_if_xl }}"
8387
- --files_to_ignore=${{ inputs.files_to_ignore }}
8488
- --ignore_line_deletions=${{ inputs.ignore_line_deletions }}
89+
- --ignore_file_deletions=${{ inputs.ignore_file_deletions }}
8590
branding:
8691
icon: 'tag'
8792
color: 'green'

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" || "$ignore_line_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)