feat: support size label customization (#26)

* Support size label customization

Reference: https://github.com/CodelyTV/pr-size-labeler/issues/25

* Use curly braces with positional arguments

Prevents issues with $10 and higher.
This commit is contained in:
Brian Flad 2021-09-20 05:30:23 -04:00 committed by GitHub
parent 48a746c126
commit 2b4112ffb7
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 68 additions and 22 deletions

View file

@ -37,10 +37,15 @@ jobs:
- uses: codelytv/pr-size-labeler@v1 - uses: codelytv/pr-size-labeler@v1
with: with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
xs_label: 'size/xs'
xs_max_size: '10' xs_max_size: '10'
s_label: 'size/s'
s_max_size: '100' s_max_size: '100'
m_label: 'size/m'
m_max_size: '500' m_max_size: '500'
l_label: 'size/l'
l_max_size: '1000' l_max_size: '1000'
xl_label: 'size/xl'
fail_if_xl: 'false' fail_if_xl: 'false'
message_if_xl: > message_if_xl: >
'This PR exceeds the recommended size of 1000 lines. 'This PR exceeds the recommended size of 1000 lines.
@ -51,6 +56,7 @@ jobs:
## 🎛️ Available parameters ## 🎛️ 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 - `*_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 - `fail_if_xl`: Set to `'true'` will report GitHub Workflow failure if the PR size is xl allowing to forbid PR merge
- `github_api_url`: Override this parameter in order to use with your own GitHub Enterprise Server. Example: `'github.example.com/api/v3'` - `github_api_url`: Override this parameter in order to use with your own GitHub Enterprise Server. Example: `'github.example.com/api/v3'`

View file

@ -4,22 +4,42 @@ inputs:
GITHUB_TOKEN: GITHUB_TOKEN:
description: 'GitHub token' description: 'GitHub token'
required: true required: true
xs_label:
description: 'Label for xs PR'
required: false
default: 'size/xs'
xs_max_size: xs_max_size:
description: 'Max size for a PR to be considered xs' description: 'Max size for a PR to be considered xs'
required: false required: false
default: '10' default: '10'
s_label:
description: 'Label for s PR'
required: false
default: 'size/s'
s_max_size: s_max_size:
description: 'Max size for a PR to be considered s' description: 'Max size for a PR to be considered s'
required: false required: false
default: '100' default: '100'
m_label:
description: 'Label for m PR'
required: false
default: 'size/m'
m_max_size: m_max_size:
description: 'Max size for a PR to be considered m' description: 'Max size for a PR to be considered m'
required: false required: false
default: '500' default: '500'
l_label:
description: 'Label for l PR'
required: false
default: 'size/l'
l_max_size: l_max_size:
description: 'Max size for a PR to be considered l' description: 'Max size for a PR to be considered l'
required: false required: false
default: '1000' default: '1000'
xl_label:
description: 'Label for xl PR'
required: false
default: 'size/xl'
fail_if_xl: fail_if_xl:
description: 'Report GitHub Workflow failure if the PR size is xl allowing to forbid PR merge' description: 'Report GitHub Workflow failure if the PR size is xl allowing to forbid PR merge'
required: false required: false
@ -40,10 +60,15 @@ runs:
image: 'Dockerfile' image: 'Dockerfile'
args: args:
- ${{ inputs.GITHUB_TOKEN }} - ${{ inputs.GITHUB_TOKEN }}
- ${{ inputs.xs_label }}
- ${{ inputs.xs_max_size }} - ${{ inputs.xs_max_size }}
- ${{ inputs.s_label }}
- ${{ inputs.s_max_size }} - ${{ inputs.s_max_size }}
- ${{ inputs.m_label }}
- ${{ inputs.m_max_size }} - ${{ inputs.m_max_size }}
- ${{ inputs.l_label }}
- ${{ inputs.l_max_size }} - ${{ inputs.l_max_size }}
- ${{ inputs.xl_label }}
- ${{ inputs.fail_if_xl }} - ${{ inputs.fail_if_xl }}
- ${{ inputs.message_if_xl }} - ${{ inputs.message_if_xl }}
- ${{ inputs.github_api_url }} - ${{ inputs.github_api_url }}

View file

@ -8,8 +8,8 @@ if [ "$PR_SIZE_LABELER_HOME" == "/" ]; then
PR_SIZE_LABELER_HOME="" PR_SIZE_LABELER_HOME=""
fi fi
if [ ! -z "$8" ]; then if [ ! -z "${13}" ]; then
PR_SIZE_LABELER_API=$8 PR_SIZE_LABELER_API="${13}"
fi fi
export PR_SIZE_LABELER_HOME export PR_SIZE_LABELER_HOME

View file

@ -13,11 +13,16 @@ github::calculate_total_modifications() {
} }
github::add_label_to_pr() { github::add_label_to_pr() {
local -r pr_number=$1 local -r pr_number="${1}"
local -r label_to_add=$2 local -r label_to_add="${2}"
local -r xs_label="${3}"
local -r s_label="${4}"
local -r m_label="${5}"
local -r l_label="${6}"
local -r xl_label="${7}"
local -r body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URI/repos/$GITHUB_REPOSITORY/pulls/$1") local -r body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URI/repos/$GITHUB_REPOSITORY/pulls/$1")
local labels=$(echo "$body" | jq .labels | jq -r ".[] | .name" | grep -v "size/") local labels=$(echo "$body" | jq .labels | jq -r ".[] | .name" | grep -e "$xs_label" -e "$s_label" -e "$m_label" -e "$l_label" -e "$xl_label" -v)
labels=$(printf "%s\n%s" "$labels" "$label_to_add") labels=$(printf "%s\n%s" "$labels" "$label_to_add")
local -r comma_separated_labels=$(github::format_labels "$labels") local -r comma_separated_labels=$(github::format_labels "$labels")

View file

@ -1,8 +1,13 @@
#!/usr/bin/env bash #!/usr/bin/env bash
labeler::label() { labeler::label() {
local -r fail_if_xl="$5" local -r xs_label="${1}"
local -r message_if_xl="$6" local -r s_label="${3}"
local -r m_label="${5}"
local -r l_label="${7}"
local -r xl_label="${9}"
local -r fail_if_xl="${10}"
local -r message_if_xl="${11}"
local -r pr_number=$(github_actions::get_pr_number) 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")
@ -13,9 +18,9 @@ labeler::label() {
log::message "Labeling pull request with $label_to_add" log::message "Labeling pull request with $label_to_add"
github::add_label_to_pr "$pr_number" "$label_to_add" github::add_label_to_pr "$pr_number" "$label_to_add" "$xs_label" "$s_label" "$m_label" "$l_label" "$xl_label"
if [ "$label_to_add" == "size/xl" ]; then if [ "$label_to_add" == "$xl_label" ]; then
if [ -n "$message_if_xl" ]; then if [ -n "$message_if_xl" ]; then
github::comment "$message_if_xl" github::comment "$message_if_xl"
fi fi
@ -28,22 +33,27 @@ labeler::label() {
} }
labeler::label_for() { labeler::label_for() {
local -r total_modifications="$1" local -r total_modifications="${1}"
local -r xs_max_size="$2" local -r xs_label="${2}"
local -r s_max_size="$3" local -r xs_max_size="${3}"
local -r m_max_size="$4" local -r s_label="${4}"
local -r l_max_size="$5" local -r s_max_size="${5}"
local -r m_label="${6}"
local -r m_max_size="${7}"
local -r l_label="${8}"
local -r l_max_size="${9}"
local -r xl_label="${10}"
if [ "$total_modifications" -lt "$xs_max_size" ]; then if [ "$total_modifications" -lt "$xs_max_size" ]; then
label="size/xs" label="$xs_label"
elif [ "$total_modifications" -lt "$s_max_size" ]; then elif [ "$total_modifications" -lt "$s_max_size" ]; then
label="size/s" label="$s_label"
elif [ "$total_modifications" -lt "$m_max_size" ]; then elif [ "$total_modifications" -lt "$m_max_size" ]; then
label="size/m" label="$m_label"
elif [ "$total_modifications" -lt "$l_max_size" ]; then elif [ "$total_modifications" -lt "$l_max_size" ]; then
label="size/l" label="$l_label"
else else
label="size/xl" label="$xl_label"
fi fi
echo "$label" echo "$label"

View file

@ -9,11 +9,11 @@ source "$PR_SIZE_LABELER_HOME/src/misc.sh"
main() { main() {
ensure::env_variable_exist "GITHUB_REPOSITORY" ensure::env_variable_exist "GITHUB_REPOSITORY"
ensure::env_variable_exist "GITHUB_EVENT_PATH" ensure::env_variable_exist "GITHUB_EVENT_PATH"
ensure::total_args 8 "$@" ensure::total_args 13 "$@"
export GITHUB_TOKEN="$1" export GITHUB_TOKEN="${1}"
labeler::label "$2" "$3" "$4" "$5" "$6" "$7" labeler::label "${2}" "${3}" "${4}" "${5}" "${6}" "${7}" "${8}" "${9}" "${10}" "${11}" "${12}"
exit $? exit $?
} }