diff --git a/README.md b/README.md index ef326aa..f0dc293 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,10 @@ ## 🚀 Usage -Create a file named `labeler.yml` inside the `.github/workflows` directory and paste the following configuration: +Create a file named `labeler.yml` inside the `.github/workflows` directory and paste the following configuration. + +> [!NOTE] +> Take into account that PR Size Labeler considers any line addition, deletion, or modification as a change by default, but you can configure it with [optional arguments](https://github.com/CodelyTV/pr-size-labeler?tab=readme-ov-file#%EF%B8%8F-arguments) such as `files_to_ignore`, `ignore_file_deletions`, or even `ignore_line_deletions`. ```yml name: labeler @@ -55,23 +58,24 @@ 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. | -| `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. | +| 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. A PR will be labeled as 'xl' if it exceeds the amount of changes defined in `l_max_size` | +| `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. | +| `ignore_file_deletions` | No | 'false' | Whether to ignore completely deleted files when calculating the PR size. If set to 'true', deleted files will be ignored. Distinct from `ignore_line_deletions` in that it only ignores files which are deleted completely. If `ignore_line_deletions` is used then using `ignore_file_deletions` is redundant. | ### Example for `files_to_ignore`: ```yml @@ -83,11 +87,6 @@ files_to_ignore: | "docs/*" ``` -## 🤔 Basic concepts or assumptions - -- PR Size Labeler considers any line addition, deletion, or modification as a change. -- A PR will be labeled as 'xl' if it exceeds the amount of changes defined in `l_max_size`. - ## Contributing If you would like to help improve the project, please read the [contribution guidelines](https://github.com/CodelyTV/pr-size-labeler/blob/main/.github/CONTRIBUTIONS.md). diff --git a/action.yml b/action.yml index fa20f92..1536dd2 100644 --- a/action.yml +++ b/action.yml @@ -63,6 +63,10 @@ inputs: 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' + ignore_file_deletions: + description: 'Whether to ignore files which are deleted when calculating the PR size. If set to "true", deleted files will be ignored.' + required: false + default: 'false' runs: using: 'docker' image: 'Dockerfile' @@ -82,6 +86,7 @@ runs: - --message_if_xl="${{ inputs.message_if_xl }}" - --files_to_ignore=${{ inputs.files_to_ignore }} - --ignore_line_deletions=${{ inputs.ignore_line_deletions }} + - --ignore_file_deletions=${{ inputs.ignore_file_deletions }} branding: icon: 'tag' color: 'green' diff --git a/src/github.sh b/src/github.sh index 06c9e48..f575a0d 100644 --- a/src/github.sh +++ b/src/github.sh @@ -6,11 +6,12 @@ github::calculate_total_modifications() { local -r pr_number="${1}" local -r files_to_ignore="${2}" local -r ignore_line_deletions="${3}" + local -r ignore_file_deletions="${4}" local additions=0 local deletions=0 - if [ -z "$files_to_ignore" ]; then + if [ -z "$files_to_ignore" ] && [ "$ignore_file_deletions" != "true" ]; then local -r body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls/$pr_number") additions=$(echo "$body" | jq '.additions') @@ -19,12 +20,18 @@ github::calculate_total_modifications() { ((deletions += $(echo "$body" | jq '.deletions'))) fi else + # NOTE: this code is not resilient to changes w/ > 100 files as we're not paginating 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") for file in $(echo "$body" | jq -r '.[] | @base64'); do filename=$(jq::base64 '.filename') + status=$(jq::base64 '.status') ignore=false + if [[ ( "$ignore_file_deletions" == "true" || "$ignore_line_deletions" == "true" ) && "$status" == "removed" ]]; then + continue + fi + for pattern in $files_to_ignore; do if [[ $filename == $pattern ]]; then ignore=true diff --git a/src/labeler.sh b/src/labeler.sh index eb8d952..b85996e 100644 --- a/src/labeler.sh +++ b/src/labeler.sh @@ -10,9 +10,10 @@ labeler::label() { local -r message_if_xl="${11}" local -r files_to_ignore="${12}" local -r ignore_line_deletions="${13}" + local -r ignore_file_deletions="${14}" local -r pr_number=$(github_actions::get_pr_number) - local -r total_modifications=$(github::calculate_total_modifications "$pr_number" "${files_to_ignore[*]}" "$ignore_line_deletions") + local -r total_modifications=$(github::calculate_total_modifications "$pr_number" "${files_to_ignore[*]}" "$ignore_line_deletions" "$ignore_file_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 6f3ef55..62e92be 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=