Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation for ignore_deleted option #61

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ 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'
ignore_deleted: 'false'
files_to_ignore: ''
```

Expand All @@ -62,6 +63,7 @@ jobs:
- `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'`
- `ignore_deleted`: Set to `'true'` will ignore any deleted files when calculating the PR size.
- `files_to_ignore`: Whitespace or newline separated list of files to ignore when calculating the PR size, regex match is supported.
### files_to_ignore Example:
```yml
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'
ignore_deleted:
description: 'Ignore deleted files when calculating the PR size'
required: false
default: 'false'
files_to_ignore:
description: 'Whitespace separated list of files to ignore when calculating the PR size (sum of changes)'
required: false
Expand All @@ -76,6 +80,7 @@ runs:
- --xl_label=${{ inputs.xl_label }}
- --fail_if_xl=${{ inputs.fail_if_xl }}
- --message_if_xl="${{ inputs.message_if_xl }}"
- --ignore_deleted=${{ inputs.ignore_deleted }}
- --files_to_ignore=${{ inputs.files_to_ignore }}
branding:
icon: 'tag'
Expand Down
Empty file modified src/ensure.sh
100644 → 100755
Empty file.
7 changes: 6 additions & 1 deletion src/github.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ GITHUB_API_HEADER="Accept: application/vnd.github.v3+json"
github::calculate_total_modifications() {
local -r pr_number="${1}"
local -r files_to_ignore="${2}"
local -r ignore_deleted="${3}"

if [ -z "$files_to_ignore" ]; then
if [[ (-z "$files_to_ignore" && "$ignore_deleted" == 'false') ]]; 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')
Expand All @@ -20,6 +21,10 @@ github::calculate_total_modifications() {

for file in $(echo "$body" | jq -r '.[] | @base64'); do
local ignore_file=0
if [[ ( "$ignore_deleted" == 'true' && "$(jq::base64 '.status')" == 'removed') ]]; then
echo "Ignoring deleted file: $(jq::base64 '.filename')"
continue
fi
for file_to_ignore in $files_to_ignore; do
if [ -z "$file_to_ignore" ]; then
continue
Expand Down
Empty file modified src/github_actions.sh
100644 → 100755
Empty file.
6 changes: 4 additions & 2 deletions src/labeler.sh
100644 → 100755
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 ignore_deleted="${12}"
local -r files_to_ignore="${13}"

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

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

Expand Down
3 changes: 2 additions & 1 deletion src/main.sh
100644 → 100755
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> --files_to_ignore=<files>
##? 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> --ignore_deleted=<false> --files_to_ignore=<files>
main() {
eval "$(/root/bin/docpars -h "$(grep "^##?" "$PR_SIZE_LABELER_HOME/src/main.sh" | cut -c 5-)" : "$@")"

Expand All @@ -31,6 +31,7 @@ main() {
"$xl_label" \
"$fail_if_xl" \
"$message_if_xl" \
"$ignore_deleted" \
"$files_to_ignore"

exit $?
Expand Down
Empty file modified src/misc.sh
100644 → 100755
Empty file.