Skip to content

Commit 8e7d171

Browse files
committed
Support ignore deletions with "ignore_file_deletions" param
Test files_to_ignore code path Tweak the variable type Correct bad logic in the file ignore block Debug More debugging More debugging Fingers crossed Another try Last try Lets see if this one does it Testing again More tests More testing Last test Wait, I might be onto something Put some changes back Fixes Whoops Walk back yml test
1 parent 1cc5389 commit 8e7d171

File tree

6 files changed

+49
-21
lines changed

6 files changed

+49
-21
lines changed

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ inputs:
5959
description: 'Whitespace separated list of files to ignore when calculating the PR size (sum of changes)'
6060
required: false
6161
default: ''
62+
ignore_file_deletions:
63+
description: 'Ignore lines that are deleted, thereby only considering additions.'
64+
required: false
65+
default: 'false'
6266
runs:
6367
using: 'docker'
6468
image: 'Dockerfile'
@@ -77,6 +81,7 @@ runs:
7781
- --fail_if_xl=${{ inputs.fail_if_xl }}
7882
- --message_if_xl="${{ inputs.message_if_xl }}"
7983
- --files_to_ignore=${{ inputs.files_to_ignore }}
84+
- --ignore_file_deletions=${{ inputs.ignore_file_deletions }}
8085
branding:
8186
icon: 'tag'
8287
color: 'green'

src/github.sh

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,49 @@ GITHUB_API_HEADER="Accept: application/vnd.github.v3+json"
55
github::calculate_total_modifications() {
66
local -r pr_number="${1}"
77
local -r files_to_ignore="${2}"
8+
local -r ignore_file_deletions="${3}"
9+
10+
local additions=0
11+
local deletions=0
812

913
if [ -z "$files_to_ignore" ]; then
1014
local -r body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls/$pr_number")
1115

12-
local -r additions=$(echo "$body" | jq '.additions')
13-
local -r deletions=$(echo "$body" | jq '.deletions')
16+
additions=$(echo "$body" | jq '.additions')
1417

15-
echo $((additions + deletions))
18+
if [ "$ignore_file_deletions" != "true" ]; then
19+
((deletions += $(echo "$body" | jq '.deletions')))
20+
fi
1621
else
1722
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")
1823

19-
local changes=0
20-
2124
for file in $(echo "$body" | jq -r '.[] | @base64'); do
22-
local ignore_file=0
23-
for file_to_ignore in $files_to_ignore; do
24-
if [ -z "$file_to_ignore" ]; then
25-
continue
26-
fi
27-
if [[ "$(jq::base64 '.filename')" == $file_to_ignore ]]; then
28-
ignore_file=1
25+
_jq() {
26+
echo ${file} | base64 -d | jq -r ${1}
27+
}
28+
29+
filename=$(_jq '.filename')
30+
ignore=false
31+
32+
for pattern in $files_to_ignore; do
33+
34+
if [[ $filename == $pattern ]]; then
35+
ignore=true
36+
break
2937
fi
3038
done
31-
if [ $ignore_file -eq 0 ]; then
32-
((changes += $(jq::base64 '.changes')))
39+
40+
if [ "$ignore" = false ]; then
41+
((additions += $(_jq '.additions')))
42+
43+
if [ "$ignore_file_deletions" != "true" ]; then
44+
((deletions += $(_jq '.deletions')))
45+
fi
3346
fi
3447
done
35-
36-
echo $changes
3748
fi
49+
50+
echo $((additions + deletions))
3851
}
3952

4053
github::add_label_to_pr() {

src/github_actions.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#!/usr/bin/env bash
22

33
github_actions::get_pr_number() {
4-
jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH"
4+
local -r pull_request_number=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")
5+
6+
if [[ "$pull_request_number" != "null" ]]; then
7+
echo "$pull_request_number"
8+
else
9+
echo "Not a pull request event"
10+
exit 1
11+
fi
512
}

src/labeler.sh

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

1314
local -r pr_number=$(github_actions::get_pr_number)
14-
local -r total_modifications=$(github::calculate_total_modifications "$pr_number" "$files_to_ignore")
15+
local -r total_modifications=$(github::calculate_total_modifications "$pr_number" "${files_to_ignore[*]}" "$ignore_file_deletions")
1516

1617
log::message "Total modifications (additions + deletions): $total_modifications"
1718
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>
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_file_deletions=<false>
1313
main() {
1414
eval "$(/root/bin/docpars -h "$(grep "^##?" "$PR_SIZE_LABELER_HOME/src/main.sh" | cut -c 5-)" : "$@")"
1515

@@ -31,7 +31,8 @@ main() {
3131
"$xl_label" \
3232
"$fail_if_xl" \
3333
"$message_if_xl" \
34-
"$files_to_ignore"
34+
"$files_to_ignore" \
35+
"$ignore_file_deletions"
3536

3637
exit $?
3738
}

tests/github_test.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ function set_up() {
88
function test_should_ignore_files_with_glob() {
99
local -r pr_number=123
1010
local -r files_to_ignore=("*.lock" ".editorconfig")
11+
1112
mock curl cat ./tests/fixtures/test_should_ignore_files_with_regex_response
1213

13-
assert_match_snapshot "$(github::calculate_total_modifications "$pr_number" "${files_to_ignore[*]}")"
14+
assert_match_snapshot "$(github::calculate_total_modifications "$pr_number" "${files_to_ignore[*]}" "false")"
1415
}

0 commit comments

Comments
 (0)