Skip to content

Commit

Permalink
feat: support size label customization (#26)
Browse files Browse the repository at this point in the history
* Support size label customization

Reference: #25

* Use curly braces with positional arguments

Prevents issues with $10 and higher.
  • Loading branch information
bflad authored Sep 20, 2021
1 parent 48a746c commit 2b4112f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 22 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ jobs:
- uses: codelytv/pr-size-labeler@v1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
xs_label: 'size/xs'
xs_max_size: '10'
s_label: 'size/s'
s_max_size: '100'
m_label: 'size/m'
m_max_size: '500'
l_label: 'size/l'
l_max_size: '1000'
xl_label: 'size/xl'
fail_if_xl: 'false'
message_if_xl: >
'This PR exceeds the recommended size of 1000 lines.
Expand All @@ -51,6 +56,7 @@ jobs:
## 🎛️ 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
- `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'`
Expand Down
25 changes: 25 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,42 @@ inputs:
GITHUB_TOKEN:
description: 'GitHub token'
required: true
xs_label:
description: 'Label for xs PR'
required: false
default: 'size/xs'
xs_max_size:
description: 'Max size for a PR to be considered xs'
required: false
default: '10'
s_label:
description: 'Label for s PR'
required: false
default: 'size/s'
s_max_size:
description: 'Max size for a PR to be considered s'
required: false
default: '100'
m_label:
description: 'Label for m PR'
required: false
default: 'size/m'
m_max_size:
description: 'Max size for a PR to be considered m'
required: false
default: '500'
l_label:
description: 'Label for l PR'
required: false
default: 'size/l'
l_max_size:
description: 'Max size for a PR to be considered l'
required: false
default: '1000'
xl_label:
description: 'Label for xl PR'
required: false
default: 'size/xl'
fail_if_xl:
description: 'Report GitHub Workflow failure if the PR size is xl allowing to forbid PR merge'
required: false
Expand All @@ -40,10 +60,15 @@ runs:
image: 'Dockerfile'
args:
- ${{ inputs.GITHUB_TOKEN }}
- ${{ inputs.xs_label }}
- ${{ inputs.xs_max_size }}
- ${{ inputs.s_label }}
- ${{ inputs.s_max_size }}
- ${{ inputs.m_label }}
- ${{ inputs.m_max_size }}
- ${{ inputs.l_label }}
- ${{ inputs.l_max_size }}
- ${{ inputs.xl_label }}
- ${{ inputs.fail_if_xl }}
- ${{ inputs.message_if_xl }}
- ${{ inputs.github_api_url }}
Expand Down
4 changes: 2 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ if [ "$PR_SIZE_LABELER_HOME" == "/" ]; then
PR_SIZE_LABELER_HOME=""
fi

if [ ! -z "$8" ]; then
PR_SIZE_LABELER_API=$8
if [ ! -z "${13}" ]; then
PR_SIZE_LABELER_API="${13}"
fi

export PR_SIZE_LABELER_HOME
Expand Down
11 changes: 8 additions & 3 deletions src/github.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ github::calculate_total_modifications() {
}

github::add_label_to_pr() {
local -r pr_number=$1
local -r label_to_add=$2
local -r pr_number="${1}"
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 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")
local -r comma_separated_labels=$(github::format_labels "$labels")

Expand Down
38 changes: 24 additions & 14 deletions src/labeler.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#!/usr/bin/env bash

labeler::label() {
local -r fail_if_xl="$5"
local -r message_if_xl="$6"
local -r xs_label="${1}"
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 total_modifications=$(github::calculate_total_modifications "$pr_number")
Expand All @@ -13,9 +18,9 @@ labeler::label() {

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
github::comment "$message_if_xl"
fi
Expand All @@ -28,22 +33,27 @@ labeler::label() {
}

labeler::label_for() {
local -r total_modifications="$1"
local -r xs_max_size="$2"
local -r s_max_size="$3"
local -r m_max_size="$4"
local -r l_max_size="$5"
local -r total_modifications="${1}"
local -r xs_label="${2}"
local -r xs_max_size="${3}"
local -r s_label="${4}"
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
label="size/xs"
label="$xs_label"
elif [ "$total_modifications" -lt "$s_max_size" ]; then
label="size/s"
label="$s_label"
elif [ "$total_modifications" -lt "$m_max_size" ]; then
label="size/m"
label="$m_label"
elif [ "$total_modifications" -lt "$l_max_size" ]; then
label="size/l"
label="$l_label"
else
label="size/xl"
label="$xl_label"
fi

echo "$label"
Expand Down
6 changes: 3 additions & 3 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ source "$PR_SIZE_LABELER_HOME/src/misc.sh"
main() {
ensure::env_variable_exist "GITHUB_REPOSITORY"
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 $?
}

0 comments on commit 2b4112f

Please sign in to comment.