Skip to content

Commit 6601a2d

Browse files
authored
refactor: accept params via docpars (CodelyTV#34)
1 parent 2b4112f commit 6601a2d

File tree

9 files changed

+64
-56
lines changed

9 files changed

+64
-56
lines changed

.github/workflows/labeler.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
runs-on: ubuntu-latest
88
name: Label the PR size
99
steps:
10-
- uses: actions/checkout@v1
10+
- uses: actions/checkout@v3
1111
- uses: ./
1212
with:
1313
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -17,7 +17,6 @@ jobs:
1717
l_max_size: '450'
1818
fail_if_xl: 'true'
1919
message_if_xl: >
20-
'This PR exceeds the recommended size of 1000 lines.
20+
This PR exceeds the recommended size of 1000 lines.
2121
Please make sure you are NOT addressing multiple issues with one PR.
22-
Note this PR might be rejected due to its size.’
23-
github_api_url: 'api.github.com' # It would be ideal to test this out pointing to a GitHub Enterprise server, but there are no testing environments available
22+
Note this PR might be rejected due to its size.

Dockerfile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
FROM alpine:3.10
1+
FROM alpine:3.15
22

3-
RUN apk add --no-cache bash curl jq
3+
RUN apk add --no-cache bash curl jq wget
4+
RUN mkdir -p "$HOME/bin" && \
5+
cd "$HOME/bin" && \
6+
wget https://github.com/denisidoro/docpars/releases/download/v0.2.0/docpars-v0.2.0-x86_64-unknown-linux-musl.tar.gz && tar xvfz docpars-v0.2.0-x86_64-unknown-linux-musl.tar.gz -C ./ && \
7+
chmod +x docpars
48

59
ADD entrypoint.sh /entrypoint.sh
610
ADD src /src

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ jobs:
5959
- `*_label` (`xs_label`, `s_label`…): Adjust size label names
6060
- `*_max_size` (`xs_max_size`, `s_max_size`…): Adjust which amount of changes you consider appropriate for each size based on your project context
6161
- `fail_if_xl`: Set to `'true'` will report GitHub Workflow failure if the PR size is xl allowing to forbid PR merge
62-
- `github_api_url`: Override this parameter in order to use with your own GitHub Enterprise Server. Example: `'github.example.com/api/v3'`
62+
- `github_api_url`: Override this parameter in order to use with your own GitHub Enterprise Server. Example: `'https://github.example.com/api/v3'`
6363

6464
## 🤔 Basic concepts or assumptions
6565

action.yml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,30 @@ inputs:
4848
description: 'Message to show if the PR size is xl'
4949
required: false
5050
default: >
51-
'This PR exceeds the recommended size of 1000 lines.
51+
This PR exceeds the recommended size of 1000 lines.
5252
Please make sure you are NOT addressing multiple issues with one PR.
53-
Note this PR might be rejected due to its size.
53+
Note this PR might be rejected due to its size.
5454
github_api_url:
55-
description: 'URI to the API of your Github Server, only necessary for Github Enterprise customers'
55+
description: 'URL to the API of your Github Server, only necessary for Github Enterprise customers'
5656
required: false
57-
default: 'api.github.com'
57+
default: 'https://api.github.com'
5858
runs:
5959
using: 'docker'
6060
image: 'Dockerfile'
6161
args:
62-
- ${{ inputs.GITHUB_TOKEN }}
63-
- ${{ inputs.xs_label }}
64-
- ${{ inputs.xs_max_size }}
65-
- ${{ inputs.s_label }}
66-
- ${{ inputs.s_max_size }}
67-
- ${{ inputs.m_label }}
68-
- ${{ inputs.m_max_size }}
69-
- ${{ inputs.l_label }}
70-
- ${{ inputs.l_max_size }}
71-
- ${{ inputs.xl_label }}
72-
- ${{ inputs.fail_if_xl }}
73-
- ${{ inputs.message_if_xl }}
74-
- ${{ inputs.github_api_url }}
62+
- --github_token=${{ inputs.GITHUB_TOKEN }}
63+
- --github_api_url=${{ inputs.github_api_url }}
64+
- --xs_label=${{ inputs.xs_label }}
65+
- --xs_max_size=${{ inputs.xs_max_size }}
66+
- --s_label=${{ inputs.s_label }}
67+
- --s_max_size=${{ inputs.s_max_size }}
68+
- --m_label=${{ inputs.m_label }}
69+
- --m_max_size=${{ inputs.m_max_size }}
70+
- --l_label=${{ inputs.l_label }}
71+
- --l_max_size=${{ inputs.l_max_size }}
72+
- --xl_label=${{ inputs.xl_label }}
73+
- --fail_if_xl=${{ inputs.fail_if_xl }}
74+
- --message_if_xl="${{ inputs.message_if_xl }}"
7575
branding:
7676
icon: 'tag'
7777
color: 'green'

entrypoint.sh

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@
22
set -euo pipefail
33

44
PR_SIZE_LABELER_HOME="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
5-
PR_SIZE_LABELER_API="api.github.com"
65

76
if [ "$PR_SIZE_LABELER_HOME" == "/" ]; then
87
PR_SIZE_LABELER_HOME=""
98
fi
109

11-
if [ ! -z "${13}" ]; then
12-
PR_SIZE_LABELER_API="${13}"
13-
fi
14-
1510
export PR_SIZE_LABELER_HOME
16-
export PR_SIZE_LABELER_API
1711

1812
bash --version
1913

2014
source "$PR_SIZE_LABELER_HOME/src/main.sh"
2115

22-
main "$@"
16+
for a in "${@}"; do
17+
arg=$(echo "$a" | tr -d '\n'| sed "s/'//g"| sed "s/’//g")
18+
sanitizedArgs+=("$arg")
19+
done
20+
21+
main "${sanitizedArgs[@]}"
2322

2423
exit $?

src/ensure.sh

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,3 @@ ensure::env_variable_exist() {
66
exit 1
77
fi
88
}
9-
10-
ensure::total_args() {
11-
local -r received_args=$(( $# - 1 ))
12-
local -r expected_args=$1
13-
14-
if ((received_args != expected_args)); then
15-
echoerr "Illegal number of parameters, $expected_args expected but $received_args found"
16-
exit 1
17-
fi
18-
}

src/github.sh

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#!/usr/bin/env bash
22

3-
GITHUB_API_URI="https://$PR_SIZE_LABELER_API"
43
GITHUB_API_HEADER="Accept: application/vnd.github.v3+json"
54

65
github::calculate_total_modifications() {
7-
local -r body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URI/repos/$GITHUB_REPOSITORY/pulls/$1")
6+
local -r body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls/$1")
87

98
local -r additions=$(echo "$body" | jq '.additions')
109
local -r deletions=$(echo "$body" | jq '.deletions')
@@ -21,7 +20,7 @@ github::add_label_to_pr() {
2120
local -r l_label="${6}"
2221
local -r xl_label="${7}"
2322

24-
local -r body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URI/repos/$GITHUB_REPOSITORY/pulls/$1")
23+
local -r body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls/$1")
2524
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)
2625
labels=$(printf "%s\n%s" "$labels" "$label_to_add")
2726
local -r comma_separated_labels=$(github::format_labels "$labels")
@@ -34,7 +33,7 @@ github::add_label_to_pr() {
3433
-X PATCH \
3534
-H "Content-Type: application/json" \
3635
-d "{\"labels\":[$comma_separated_labels]}" \
37-
"$GITHUB_API_URI/repos/$GITHUB_REPOSITORY/issues/$pr_number"
36+
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$pr_number" >/dev/null
3837
}
3938

4039
github::format_labels() {
@@ -54,13 +53,13 @@ github::format_labels() {
5453
}
5554

5655
github::comment() {
57-
local -r comment=$1
56+
local -r comment="$1"
5857

5958
curl -sSL \
6059
-H "Authorization: token $GITHUB_TOKEN" \
6160
-H "$GITHUB_API_HEADER" \
6261
-X POST \
6362
-H "Content-Type: application/json" \
64-
-d "{\"body\":\"$comment\"}" \
65-
"$GITHUB_API_URI/repos/$GITHUB_REPOSITORY/issues/$pr_number/comments"
63+
-d "{\"body\":$comment}" \
64+
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$pr_number/comments"
6665
}

src/labeler.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ labeler::label() {
3333
}
3434

3535
labeler::label_for() {
36-
local -r total_modifications="${1}"
36+
local -r total_modifications=${1}
3737
local -r xs_label="${2}"
38-
local -r xs_max_size="${3}"
38+
local -r xs_max_size=${3}
3939
local -r s_label="${4}"
40-
local -r s_max_size="${5}"
40+
local -r s_max_size=${5}
4141
local -r m_label="${6}"
42-
local -r m_max_size="${7}"
42+
local -r m_max_size=${7}
4343
local -r l_label="${8}"
44-
local -r l_max_size="${9}"
44+
local -r l_max_size=${9}
4545
local -r xl_label="${10}"
4646

4747
if [ "$total_modifications" -lt "$xs_max_size" ]; then

src/main.sh

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,31 @@ source "$PR_SIZE_LABELER_HOME/src/github_actions.sh"
66
source "$PR_SIZE_LABELER_HOME/src/labeler.sh"
77
source "$PR_SIZE_LABELER_HOME/src/misc.sh"
88

9+
##? Adds a size label to a GitHub Pull Request
10+
##?
11+
##? Usage:
12+
##? main.sh --github_token=<token> --xs_label=<label> --xs_max_size=<size> --s_label=<label> --s_max_size=<size> --m_label=<label> --m_max_size=<size> --l_label=<label> --l_max_size=<size> --xl_label=<label> --fail_if_xl=<false> --message_if_xl=<message> --github_api_url=<url>
913
main() {
14+
eval "$(/root/bin/docpars -h "$(grep "^##?" "$PR_SIZE_LABELER_HOME/src/main.sh" | cut -c 5-)" : "$@")"
15+
1016
ensure::env_variable_exist "GITHUB_REPOSITORY"
1117
ensure::env_variable_exist "GITHUB_EVENT_PATH"
12-
ensure::total_args 13 "$@"
1318

14-
export GITHUB_TOKEN="${1}"
19+
export GITHUB_TOKEN="$github_token"
20+
export GITHUB_API_URL="$github_api_url"
1521

16-
labeler::label "${2}" "${3}" "${4}" "${5}" "${6}" "${7}" "${8}" "${9}" "${10}" "${11}" "${12}"
22+
labeler::label \
23+
"$xs_label" \
24+
"$xs_max_size" \
25+
"$s_label" \
26+
"$s_max_size" \
27+
"$m_label" \
28+
"$m_max_size" \
29+
"$l_label" \
30+
"$l_max_size" \
31+
"$xl_label" \
32+
"$fail_if_xl" \
33+
"$message_if_xl"
1734

1835
exit $?
1936
}

0 commit comments

Comments
 (0)