Use idiomatic bash

This commit is contained in:
Rafa Gómez 2020-03-03 23:31:53 +01:00
parent 2f1a11e798
commit 44bd7cefa2
9 changed files with 131 additions and 70 deletions

View file

@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
name: Label the PR size
steps:
- uses: codelytv/pr-size-labeler@v1
- uses: codelytv/pr-size-labeler@improvements
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
xs_max_size: '10'

View file

@ -3,5 +3,6 @@ FROM alpine:3.10
RUN apk add --no-cache bash curl jq bc
ADD entrypoint.sh /entrypoint.sh
ADD src /src
ENTRYPOINT ["/entrypoint.sh"]

View file

@ -1,76 +1,16 @@
#!/bin/bash
set -e
#!/usr/bin/env bash
set -euo pipefail
if [[ -z "$GITHUB_REPOSITORY" ]]; then
echo "The env variable GITHUB_REPOSITORY is required."
exit 1
PR_SIZE_LABELER_HOME="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
if [ "$PR_SIZE_LABELER_HOME" == "/" ]; then
PR_SIZE_LABELER_HOME=""
fi
if [[ -z "$GITHUB_EVENT_PATH" ]]; then
echo "The env variable GITHUB_EVENT_PATH is required."
exit 1
fi
export PR_SIZE_LABELER_HOME
GITHUB_TOKEN="$1"
source "$PR_SIZE_LABELER_HOME/src/main.sh"
xs_max_size="$2"
s_max_size="$3"
m_max_size="$4"
l_max_size="$5"
fail_if_xl="$6"
URI="https://api.github.com"
API_HEADER="Accept: application/vnd.github.v3+json"
AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}"
echo "GitHub event"
echo "$GITHUB_EVENT_PATH"
number=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")
autolabel() {
# https://developer.github.com/v3/pulls/#get-a-single-pull-request
# Example: https://api.github.com/repos/CodelyTV/java-ddd-example/pulls/7
body=$(curl -sSL -H "${AUTH_HEADER}" -H "${API_HEADER}" "${URI}/repos/${GITHUB_REPOSITORY}/pulls/${number}")
additions=$(echo "$body" | jq '.additions')
deletions=$(echo "$body" | jq '.deletions')
total_modifications=$(echo "$additions + $deletions" | bc)
label_to_add=$(label_for "$total_modifications")
echo "Labeling pull request with $label_to_add"
curl -sSL \
-H "${AUTH_HEADER}" \
-H "${API_HEADER}" \
-X POST \
-H "Content-Type: application/json" \
-d "{\"labels\":[\"${label_to_add}\"]}" \
"${URI}/repos/${GITHUB_REPOSITORY}/issues/${number}/labels"
if [ "$label_to_add" == "size/xl" ] && [ "$fail_if_xl" == "true" ]; then
echo "Pr is xl, please, short this!!"
exit 1
fi
}
label_for() {
if [ "$1" -lt "$xs_max_size" ]; then
label="size/xs"
elif [ "$1" -lt "$s_max_size" ]; then
label="size/s"
elif [ "$1" -lt "$m_max_size" ]; then
label="size/m"
elif [ "$1" -lt "$l_max_size" ]; then
label="size/l"
else
label="size/xl"
fi
echo "$label"
}
autolabel
main "$@"
exit $?

18
src/ensure.sh Normal file
View file

@ -0,0 +1,18 @@
#!/usr/bin/env bash
ensure::env_variable_exist() {
if [[ -z "${!1}" ]]; then
echoerr "The env variable $1 is required."
exit 1
fi
}
ensure::total_args() {
local -r received_args=$(echo "$# - 1" | bc)
local -r expected_args=$1
if ((received_args != expected_args)); then
echoerr "Illegal number of parameters, $expected_args expected but $received_args found"
exit 1
fi
}

26
src/github.sh Normal file
View file

@ -0,0 +1,26 @@
#!/usr/bin/env bash
GITHUB_API_URI="https://api.github.com"
GITHUB_API_HEADER="Accept: application/vnd.github.v3+json"
github::calculate_total_modifications() {
local -r body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URI/repos/$GITHUB_REPOSITORY/pulls/$1")
local -r additions=$(echo "$body" | jq '.additions')
local -r deletions=$(echo "$body" | jq '.deletions')
echo "$additions + $deletions" | bc
}
github::add_label_to_pr() {
local -r pr_number=$1
local -r label_to_add=$2
curl -sSL \
-H "Authorization: token $GITHUB_TOKEN" \
-H "$GITHUB_API_HEADER" \
-X POST \
-H "Content-Type: application/json" \
-d "{\"labels\":[\"$label_to_add\"]}" \
"$GITHUB_API_URI/repos/$GITHUB_REPOSITORY/issues/$pr_number/labels"
}

5
src/github_actions.sh Normal file
View file

@ -0,0 +1,5 @@
#!/usr/bin/env bash
github_actions::get_pr_number() {
jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH"
}

43
src/labeler.sh Normal file
View file

@ -0,0 +1,43 @@
#!/usr/bin/env bash
labeler::label() {
local -r fail_if_xl="$5"
local -r pr_number=$(github_actions::get_pr_number)
local -r total_modifications=$(github::calculate_total_modifications "$pr_number")
log::message "total modifications: $total_modifications"
local -r label_to_add=$(labeler::label_for "$total_modifications" "$@")
log::message "Labeling pull request with $label_to_add"
github::add_label_to_pr "$pr_number" "$label_to_add"
if [ "$label_to_add" == "size/xl" ] && [ "$fail_if_xl" == "true" ]; then
echoerr "Pr is xl, please, short this!!"
exit 1
fi
}
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"
if [ "$total_modifications" -lt "$xs_max_size" ]; then
label="size/xs"
elif [ "$total_modifications" -lt "$s_max_size" ]; then
label="size/s"
elif [ "$total_modifications" -lt "$m_max_size" ]; then
label="size/m"
elif [ "$total_modifications" -lt "$l_max_size" ]; then
label="size/l"
else
label="size/xl"
fi
echo "$label"
}

19
src/main.sh Normal file
View file

@ -0,0 +1,19 @@
#!/usr/bin/env bash
source "$PR_SIZE_LABELER_HOME/src/ensure.sh"
source "$PR_SIZE_LABELER_HOME/src/github.sh"
source "$PR_SIZE_LABELER_HOME/src/github_actions.sh"
source "$PR_SIZE_LABELER_HOME/src/labeler.sh"
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 6 "$@"
export GITHUB_TOKEN="$1"
labeler::label "$2" "$3" "$4" "$5" "$6"
exit $?
}

9
src/misc.sh Normal file
View file

@ -0,0 +1,9 @@
#!/usr/bin/env bash
echoerr() {
echo "$@" 1>&2
}
log::message() {
echo "$@"
}