diff --git a/README.md b/README.md index 0c7a670..19600dc 100644 --- a/README.md +++ b/README.md @@ -55,22 +55,23 @@ jobs: ## 🎛️ Arguments -| Name | Required | Default Value | Description | -|-------------------|----------|----------------------|-------------------------------------------------------------------------------------------------------------------------| -| `GITHUB_TOKEN` | Yes | Automatically supplied| GitHub token needed to interact with the repository. | -| `xs_label` | No | 'size/xs' | Label for very small-sized PRs. | -| `xs_max_size` | No | '10' | Maximum number of changes allowed for XS-sized PRs. | -| `s_label` | No | 'size/s' | Label for small-sized PRs. | -| `s_max_size` | No | '100' | Maximum number of changes allowed for S-sized PRs. | -| `m_label` | No | 'size/m' | Label for medium-sized PRs. | -| `m_max_size` | No | '500' | Maximum number of changes allowed for M-sized PRs. | -| `l_label` | No | 'size/l' | Label for large-sized PRs. | -| `l_max_size` | No | '1000' | Maximum number of changes allowed for L-sized PRs. | -| `xl_label` | No | 'size/xl' | Label for extra-large-sized PRs. | -| `fail_if_xl` | No | 'false' | Whether to fail the GitHub workflow if the PR size is 'XL' (blocks the merge). | -| `message_if_xl` | No | Custom message | Message to display when a PR exceeds the 'XL' size limit. | -| `github_api_url` | No | 'https://api.github.com' | URL for the GitHub API, can be changed for GitHub Enterprise Servers. | -| `files_to_ignore` | No | '' | Files to ignore during PR size calculation. Supports newline or whitespace delimited list. | +| Name | Required | Default Value | Description | +|-------------------------|----------|----------------------|-------------------------------------------------------------------------------------------------------------------------| +| `GITHUB_TOKEN` | Yes | Automatically supplied| GitHub token needed to interact with the repository. | +| `xs_label` | No | 'size/xs' | Label for very small-sized PRs. | +| `xs_max_size` | No | '10' | Maximum number of changes allowed for XS-sized PRs. | +| `s_label` | No | 'size/s' | Label for small-sized PRs. | +| `s_max_size` | No | '100' | Maximum number of changes allowed for S-sized PRs. | +| `m_label` | No | 'size/m' | Label for medium-sized PRs. | +| `m_max_size` | No | '500' | Maximum number of changes allowed for M-sized PRs. | +| `l_label` | No | 'size/l' | Label for large-sized PRs. | +| `l_max_size` | No | '1000' | Maximum number of changes allowed for L-sized PRs. | +| `xl_label` | No | 'size/xl' | Label for extra-large-sized PRs. | +| `fail_if_xl` | No | 'false' | Whether to fail the GitHub workflow if the PR size is 'XL' (blocks the merge). | +| `message_if_xl` | No | Custom message | Message to display when a PR exceeds the 'XL' size limit. | +| `github_api_url` | No | 'https://api.github.com' | URL for the GitHub API, can be changed for GitHub Enterprise Servers. | +| `files_to_ignore` | No | '' | Files to ignore during PR size calculation. Supports newline or whitespace delimited list. | +| `ignore_line_deletions` | No | 'false' | Whether to ignore lines which are deleted when calculating the PR size. If set to 'true', deleted lines will be ignored. | ### Example for `files_to_ignore`: ```yml diff --git a/action.yml b/action.yml index 06be2ce..2ce23a6 100644 --- a/action.yml +++ b/action.yml @@ -59,6 +59,10 @@ inputs: description: 'Whitespace separated list of files to ignore when calculating the PR size (sum of changes)' required: false default: '' + ignore_line_deletions: + description: 'Whether to ignore lines which are deleted when calculating the PR size. If set to "true", deleted lines will be ignored.' + required: false + default: 'false' runs: using: 'docker' image: 'Dockerfile' @@ -77,6 +81,7 @@ runs: - --fail_if_xl=${{ inputs.fail_if_xl }} - --message_if_xl="${{ inputs.message_if_xl }}" - --files_to_ignore=${{ inputs.files_to_ignore }} + - --ignore_line_deletions=${{ inputs.ignore_line_deletions }} branding: icon: 'tag' color: 'green' diff --git a/src/github.sh b/src/github.sh index 0934dc0..cc285c2 100644 --- a/src/github.sh +++ b/src/github.sh @@ -5,36 +5,44 @@ 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_line_deletions="${3}" + + local additions=0 + local deletions=0 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') + additions=$(echo "$body" | jq '.additions') - echo $((additions + deletions)) + if [ "$ignore_line_deletions" != "true" ]; then + ((deletions += $(echo "$body" | jq '.deletions'))) + fi 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 changes=0 - for file in $(echo "$body" | jq -r '.[] | @base64'); do - local ignore_file=0 - for file_to_ignore in $files_to_ignore; do - if [ -z "$file_to_ignore" ]; then - continue - fi - if [[ "$(jq::base64 '.filename')" == $file_to_ignore ]]; then - ignore_file=1 + filename=$(jq::base64 '.filename') + ignore=false + + for pattern in $files_to_ignore; do + if [[ $filename == $pattern ]]; then + ignore=true + break fi done - if [ $ignore_file -eq 0 ]; then - ((changes += $(jq::base64 '.changes'))) + + if [ "$ignore" = false ]; then + ((additions += $(jq::base64 '.additions'))) + + if [ "$ignore_line_deletions" != "true" ]; then + ((deletions += $(jq::base64 '.deletions'))) + fi fi done - - echo $changes fi + + echo $((additions + deletions)) } github::add_label_to_pr() { diff --git a/src/github_actions.sh b/src/github_actions.sh index ae8ce8a..ec4c5c3 100644 --- a/src/github_actions.sh +++ b/src/github_actions.sh @@ -1,5 +1,12 @@ #!/usr/bin/env bash github_actions::get_pr_number() { - jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH" + local -r pull_request_number=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH") + + if [[ "$pull_request_number" != "null" ]]; then + echo "$pull_request_number" + else + echo "Not a pull request event" + exit 1 + fi } diff --git a/src/labeler.sh b/src/labeler.sh index dce7cc5..eb8d952 100644 --- a/src/labeler.sh +++ b/src/labeler.sh @@ -9,9 +9,10 @@ labeler::label() { local -r fail_if_xl="${10}" local -r message_if_xl="${11}" local -r files_to_ignore="${12}" + local -r ignore_line_deletions="${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_line_deletions") log::message "Total modifications (additions + deletions): $total_modifications" log::message "Ignoring files (if present): $files_to_ignore" diff --git a/src/main.sh b/src/main.sh index cb26580..6f3ef55 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=