Skip to content

Commit

Permalink
feature: Add support for ignoring files (#35)
Browse files Browse the repository at this point in the history
Co-authored-by: favmd <m.dietl@frontmark.de>
  • Loading branch information
rgomezcasas and favmd committed Mar 20, 2022
1 parent 6601a2d commit bfc42c9
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,17 @@ jobs:
Please make sure you are NOT addressing multiple issues with one PR.
Note this PR might be rejected due to its size.’
github_api_url: 'api.github.com'
files_to_ignore: ''
```

## 🎛️ Available parameters

- `*_label` (`xs_label`, `s_label`…): Adjust size label names
- `*_max_size` (`xs_max_size`, `s_max_size`…): Adjust which amount of changes you consider appropriate for each size based on your project context
- `fail_if_xl`: Set to `'true'` will report GitHub Workflow failure if the PR size is xl allowing to forbid PR merge
- `message_if_xl`: Let the user(s) know that the PR exceeds the recommended size and what the consequences are
- `github_api_url`: Override this parameter in order to use with your own GitHub Enterprise Server. Example: `'https://github.example.com/api/v3'`
- `files_to_ignore`: Whitespace separated list of files to ignore when calculating the PR size. Example: `'package-lock.json Pipfile.lock'`

## 🤔 Basic concepts or assumptions

Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ inputs:
description: 'URL to the API of your Github Server, only necessary for Github Enterprise customers'
required: false
default: 'https://api.github.com'
files_to_ignore:
description: 'Whitespace separated list of files to ignore when calculating the PR size (sum of changes)'
required: false
default: ''
runs:
using: 'docker'
image: 'Dockerfile'
Expand All @@ -72,6 +76,7 @@ runs:
- --xl_label=${{ inputs.xl_label }}
- --fail_if_xl=${{ inputs.fail_if_xl }}
- --message_if_xl="${{ inputs.message_if_xl }}"
- --files_to_ignore=${{ inputs.files_to_ignore }}
branding:
icon: 'tag'
color: 'green'
27 changes: 23 additions & 4 deletions src/github.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,31 @@
GITHUB_API_HEADER="Accept: application/vnd.github.v3+json"

github::calculate_total_modifications() {
local -r body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls/$1")
local -r pr_number="${1}"
local -r files_to_ignore="${2}"

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

local -r additions=$(echo "$body" | jq '.additions')
local -r deletions=$(echo "$body" | jq '.deletions')

echo $((additions + deletions))
else
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")

local -r additions=$(echo "$body" | jq '.additions')
local -r deletions=$(echo "$body" | jq '.deletions')
local changes=0
for file in $(echo "$body" | jq -r '.[] | @base64'); do
for file_to_ignore in $files_to_ignore; do
if [ "$file_to_ignore" != "$(basename $(jq::base64 '.filename'))" ]; then
total_changes=$(jq::base64 '.changes')
((changes += total_changes))
fi
done
done

echo $((additions + deletions))
echo $changes
fi
}

github::add_label_to_pr() {
Expand Down
6 changes: 4 additions & 2 deletions src/labeler.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ labeler::label() {
local -r xl_label="${9}"
local -r fail_if_xl="${10}"
local -r message_if_xl="${11}"
local -r files_to_ignore="${12}"

local -r pr_number=$(github_actions::get_pr_number)
local -r total_modifications=$(github::calculate_total_modifications "$pr_number")
local -r total_modifications=$(github::calculate_total_modifications "$pr_number" "$files_to_ignore")

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

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

Expand Down
5 changes: 3 additions & 2 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ source "$PR_SIZE_LABELER_HOME/src/misc.sh"
##? Adds a size label to a GitHub Pull Request
##?
##? Usage:
##? 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>
##? 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>
main() {
eval "$(/root/bin/docpars -h "$(grep "^##?" "$PR_SIZE_LABELER_HOME/src/main.sh" | cut -c 5-)" : "$@")"

Expand All @@ -30,7 +30,8 @@ main() {
"$l_max_size" \
"$xl_label" \
"$fail_if_xl" \
"$message_if_xl"
"$message_if_xl" \
"$files_to_ignore"

exit $?
}
4 changes: 4 additions & 0 deletions src/misc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ coll::map() {
str::quote() {
echo "\"$1\""
}

jq::base64() {
echo "$file" | base64 -d | jq -r "$1"
}

0 comments on commit bfc42c9

Please sign in to comment.