diff --git a/README.md b/README.md index c407a62..de2da36 100644 --- a/README.md +++ b/README.md @@ -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' + files_to_ignore: '' ``` ## 🎛️ Available parameters @@ -59,7 +60,9 @@ jobs: - `*_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 diff --git a/action.yml b/action.yml index dab2049..06be2ce 100644 --- a/action.yml +++ b/action.yml @@ -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' @@ -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' diff --git a/src/github.sh b/src/github.sh index 79257da..fc673f4 100644 --- a/src/github.sh +++ b/src/github.sh @@ -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() { diff --git a/src/labeler.sh b/src/labeler.sh index 58142f3..dce7cc5 100644 --- a/src/labeler.sh +++ b/src/labeler.sh @@ -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" "$@") diff --git a/src/main.sh b/src/main.sh index e61a777..cb26580 100644 --- a/src/main.sh +++ b/src/main.sh @@ -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= --xs_label=