Skip to content

Commit 2b4112f

Browse files
authored
feat: support size label customization (#26)
* Support size label customization Reference: #25 * Use curly braces with positional arguments Prevents issues with $10 and higher.
1 parent 48a746c commit 2b4112f

File tree

6 files changed

+68
-22
lines changed

6 files changed

+68
-22
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ jobs:
3737
- uses: codelytv/pr-size-labeler@v1
3838
with:
3939
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
40+
xs_label: 'size/xs'
4041
xs_max_size: '10'
42+
s_label: 'size/s'
4143
s_max_size: '100'
44+
m_label: 'size/m'
4245
m_max_size: '500'
46+
l_label: 'size/l'
4347
l_max_size: '1000'
48+
xl_label: 'size/xl'
4449
fail_if_xl: 'false'
4550
message_if_xl: >
4651
'This PR exceeds the recommended size of 1000 lines.
@@ -51,6 +56,7 @@ jobs:
5156
5257
## 🎛️ Available parameters
5358
59+
- `*_label` (`xs_label`, `s_label`…): Adjust size label names
5460
- `*_max_size` (`xs_max_size`, `s_max_size`…): Adjust which amount of changes you consider appropriate for each size based on your project context
5561
- `fail_if_xl`: Set to `'true'` will report GitHub Workflow failure if the PR size is xl allowing to forbid PR merge
5662
- `github_api_url`: Override this parameter in order to use with your own GitHub Enterprise Server. Example: `'github.example.com/api/v3'`

action.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,42 @@ inputs:
44
GITHUB_TOKEN:
55
description: 'GitHub token'
66
required: true
7+
xs_label:
8+
description: 'Label for xs PR'
9+
required: false
10+
default: 'size/xs'
711
xs_max_size:
812
description: 'Max size for a PR to be considered xs'
913
required: false
1014
default: '10'
15+
s_label:
16+
description: 'Label for s PR'
17+
required: false
18+
default: 'size/s'
1119
s_max_size:
1220
description: 'Max size for a PR to be considered s'
1321
required: false
1422
default: '100'
23+
m_label:
24+
description: 'Label for m PR'
25+
required: false
26+
default: 'size/m'
1527
m_max_size:
1628
description: 'Max size for a PR to be considered m'
1729
required: false
1830
default: '500'
31+
l_label:
32+
description: 'Label for l PR'
33+
required: false
34+
default: 'size/l'
1935
l_max_size:
2036
description: 'Max size for a PR to be considered l'
2137
required: false
2238
default: '1000'
39+
xl_label:
40+
description: 'Label for xl PR'
41+
required: false
42+
default: 'size/xl'
2343
fail_if_xl:
2444
description: 'Report GitHub Workflow failure if the PR size is xl allowing to forbid PR merge'
2545
required: false
@@ -40,10 +60,15 @@ runs:
4060
image: 'Dockerfile'
4161
args:
4262
- ${{ inputs.GITHUB_TOKEN }}
63+
- ${{ inputs.xs_label }}
4364
- ${{ inputs.xs_max_size }}
65+
- ${{ inputs.s_label }}
4466
- ${{ inputs.s_max_size }}
67+
- ${{ inputs.m_label }}
4568
- ${{ inputs.m_max_size }}
69+
- ${{ inputs.l_label }}
4670
- ${{ inputs.l_max_size }}
71+
- ${{ inputs.xl_label }}
4772
- ${{ inputs.fail_if_xl }}
4873
- ${{ inputs.message_if_xl }}
4974
- ${{ inputs.github_api_url }}

entrypoint.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ if [ "$PR_SIZE_LABELER_HOME" == "/" ]; then
88
PR_SIZE_LABELER_HOME=""
99
fi
1010

11-
if [ ! -z "$8" ]; then
12-
PR_SIZE_LABELER_API=$8
11+
if [ ! -z "${13}" ]; then
12+
PR_SIZE_LABELER_API="${13}"
1313
fi
1414

1515
export PR_SIZE_LABELER_HOME

src/github.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ github::calculate_total_modifications() {
1313
}
1414

1515
github::add_label_to_pr() {
16-
local -r pr_number=$1
17-
local -r label_to_add=$2
16+
local -r pr_number="${1}"
17+
local -r label_to_add="${2}"
18+
local -r xs_label="${3}"
19+
local -r s_label="${4}"
20+
local -r m_label="${5}"
21+
local -r l_label="${6}"
22+
local -r xl_label="${7}"
1823

1924
local -r body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URI/repos/$GITHUB_REPOSITORY/pulls/$1")
20-
local labels=$(echo "$body" | jq .labels | jq -r ".[] | .name" | grep -v "size/")
25+
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)
2126
labels=$(printf "%s\n%s" "$labels" "$label_to_add")
2227
local -r comma_separated_labels=$(github::format_labels "$labels")
2328

src/labeler.sh

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
#!/usr/bin/env bash
22

33
labeler::label() {
4-
local -r fail_if_xl="$5"
5-
local -r message_if_xl="$6"
4+
local -r xs_label="${1}"
5+
local -r s_label="${3}"
6+
local -r m_label="${5}"
7+
local -r l_label="${7}"
8+
local -r xl_label="${9}"
9+
local -r fail_if_xl="${10}"
10+
local -r message_if_xl="${11}"
611

712
local -r pr_number=$(github_actions::get_pr_number)
813
local -r total_modifications=$(github::calculate_total_modifications "$pr_number")
@@ -13,9 +18,9 @@ labeler::label() {
1318

1419
log::message "Labeling pull request with $label_to_add"
1520

16-
github::add_label_to_pr "$pr_number" "$label_to_add"
21+
github::add_label_to_pr "$pr_number" "$label_to_add" "$xs_label" "$s_label" "$m_label" "$l_label" "$xl_label"
1722

18-
if [ "$label_to_add" == "size/xl" ]; then
23+
if [ "$label_to_add" == "$xl_label" ]; then
1924
if [ -n "$message_if_xl" ]; then
2025
github::comment "$message_if_xl"
2126
fi
@@ -28,22 +33,27 @@ labeler::label() {
2833
}
2934

3035
labeler::label_for() {
31-
local -r total_modifications="$1"
32-
local -r xs_max_size="$2"
33-
local -r s_max_size="$3"
34-
local -r m_max_size="$4"
35-
local -r l_max_size="$5"
36+
local -r total_modifications="${1}"
37+
local -r xs_label="${2}"
38+
local -r xs_max_size="${3}"
39+
local -r s_label="${4}"
40+
local -r s_max_size="${5}"
41+
local -r m_label="${6}"
42+
local -r m_max_size="${7}"
43+
local -r l_label="${8}"
44+
local -r l_max_size="${9}"
45+
local -r xl_label="${10}"
3646

3747
if [ "$total_modifications" -lt "$xs_max_size" ]; then
38-
label="size/xs"
48+
label="$xs_label"
3949
elif [ "$total_modifications" -lt "$s_max_size" ]; then
40-
label="size/s"
50+
label="$s_label"
4151
elif [ "$total_modifications" -lt "$m_max_size" ]; then
42-
label="size/m"
52+
label="$m_label"
4353
elif [ "$total_modifications" -lt "$l_max_size" ]; then
44-
label="size/l"
54+
label="$l_label"
4555
else
46-
label="size/xl"
56+
label="$xl_label"
4757
fi
4858

4959
echo "$label"

src/main.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ source "$PR_SIZE_LABELER_HOME/src/misc.sh"
99
main() {
1010
ensure::env_variable_exist "GITHUB_REPOSITORY"
1111
ensure::env_variable_exist "GITHUB_EVENT_PATH"
12-
ensure::total_args 8 "$@"
12+
ensure::total_args 13 "$@"
1313

14-
export GITHUB_TOKEN="$1"
14+
export GITHUB_TOKEN="${1}"
1515

16-
labeler::label "$2" "$3" "$4" "$5" "$6" "$7"
16+
labeler::label "${2}" "${3}" "${4}" "${5}" "${6}" "${7}" "${8}" "${9}" "${10}" "${11}" "${12}"
1717

1818
exit $?
1919
}

0 commit comments

Comments
 (0)