Skip to content

Commit bfc42c9

Browse files
rgomezcasasfavmd
andcommitted
feature: Add support for ignoring files (CodelyTV#35)
Co-authored-by: favmd <m.dietl@frontmark.de>
1 parent 6601a2d commit bfc42c9

File tree

6 files changed

+42
-8
lines changed

6 files changed

+42
-8
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,17 @@ jobs:
5252
Please make sure you are NOT addressing multiple issues with one PR.
5353
Note this PR might be rejected due to its size.’
5454
github_api_url: 'api.github.com'
55+
files_to_ignore: ''
5556
```
5657
5758
## 🎛️ Available parameters
5859
5960
- `*_label` (`xs_label`, `s_label`…): Adjust size label names
6061
- `*_max_size` (`xs_max_size`, `s_max_size`…): Adjust which amount of changes you consider appropriate for each size based on your project context
6162
- `fail_if_xl`: Set to `'true'` will report GitHub Workflow failure if the PR size is xl allowing to forbid PR merge
63+
- `message_if_xl`: Let the user(s) know that the PR exceeds the recommended size and what the consequences are
6264
- `github_api_url`: Override this parameter in order to use with your own GitHub Enterprise Server. Example: `'https://github.example.com/api/v3'`
65+
- `files_to_ignore`: Whitespace separated list of files to ignore when calculating the PR size. Example: `'package-lock.json Pipfile.lock'`
6366

6467
## 🤔 Basic concepts or assumptions
6568

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ inputs:
5555
description: 'URL to the API of your Github Server, only necessary for Github Enterprise customers'
5656
required: false
5757
default: 'https://api.github.com'
58+
files_to_ignore:
59+
description: 'Whitespace separated list of files to ignore when calculating the PR size (sum of changes)'
60+
required: false
61+
default: ''
5862
runs:
5963
using: 'docker'
6064
image: 'Dockerfile'
@@ -72,6 +76,7 @@ runs:
7276
- --xl_label=${{ inputs.xl_label }}
7377
- --fail_if_xl=${{ inputs.fail_if_xl }}
7478
- --message_if_xl="${{ inputs.message_if_xl }}"
79+
- --files_to_ignore=${{ inputs.files_to_ignore }}
7580
branding:
7681
icon: 'tag'
7782
color: 'green'

src/github.sh

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,31 @@
33
GITHUB_API_HEADER="Accept: application/vnd.github.v3+json"
44

55
github::calculate_total_modifications() {
6-
local -r body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls/$1")
6+
local -r pr_number="${1}"
7+
local -r files_to_ignore="${2}"
8+
9+
if [ -z "$files_to_ignore" ]; then
10+
local -r body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls/$pr_number")
11+
12+
local -r additions=$(echo "$body" | jq '.additions')
13+
local -r deletions=$(echo "$body" | jq '.deletions')
14+
15+
echo $((additions + deletions))
16+
else
17+
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")
718

8-
local -r additions=$(echo "$body" | jq '.additions')
9-
local -r deletions=$(echo "$body" | jq '.deletions')
19+
local changes=0
20+
for file in $(echo "$body" | jq -r '.[] | @base64'); do
21+
for file_to_ignore in $files_to_ignore; do
22+
if [ "$file_to_ignore" != "$(basename $(jq::base64 '.filename'))" ]; then
23+
total_changes=$(jq::base64 '.changes')
24+
((changes += total_changes))
25+
fi
26+
done
27+
done
1028

11-
echo $((additions + deletions))
29+
echo $changes
30+
fi
1231
}
1332

1433
github::add_label_to_pr() {

src/labeler.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ labeler::label() {
88
local -r xl_label="${9}"
99
local -r fail_if_xl="${10}"
1010
local -r message_if_xl="${11}"
11+
local -r files_to_ignore="${12}"
1112

1213
local -r pr_number=$(github_actions::get_pr_number)
13-
local -r total_modifications=$(github::calculate_total_modifications "$pr_number")
14+
local -r total_modifications=$(github::calculate_total_modifications "$pr_number" "$files_to_ignore")
1415

15-
log::message "Total modifications: $total_modifications"
16+
log::message "Total modifications (additions + deletions): $total_modifications"
17+
log::message "Ignoring files (if present): $files_to_ignore"
1618

1719
local -r label_to_add=$(labeler::label_for "$total_modifications" "$@")
1820

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>
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>
1313
main() {
1414
eval "$(/root/bin/docpars -h "$(grep "^##?" "$PR_SIZE_LABELER_HOME/src/main.sh" | cut -c 5-)" : "$@")"
1515

@@ -30,7 +30,8 @@ main() {
3030
"$l_max_size" \
3131
"$xl_label" \
3232
"$fail_if_xl" \
33-
"$message_if_xl"
33+
"$message_if_xl" \
34+
"$files_to_ignore"
3435

3536
exit $?
3637
}

src/misc.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ coll::map() {
2525
str::quote() {
2626
echo "\"$1\""
2727
}
28+
29+
jq::base64() {
30+
echo "$file" | base64 -d | jq -r "$1"
31+
}

0 commit comments

Comments
 (0)