From 44bd7cefa2484df89929f2c726dcb8f80b560e1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=20G=C3=B3mez?= Date: Tue, 3 Mar 2020 23:31:53 +0100 Subject: [PATCH] Use idiomatic bash --- .github/workflows/labeler.yml | 2 +- Dockerfile | 1 + entrypoint.sh | 78 ++++------------------------------- src/ensure.sh | 18 ++++++++ src/github.sh | 26 ++++++++++++ src/github_actions.sh | 5 +++ src/labeler.sh | 43 +++++++++++++++++++ src/main.sh | 19 +++++++++ src/misc.sh | 9 ++++ 9 files changed, 131 insertions(+), 70 deletions(-) create mode 100644 src/ensure.sh create mode 100644 src/github.sh create mode 100644 src/github_actions.sh create mode 100644 src/labeler.sh create mode 100644 src/main.sh create mode 100644 src/misc.sh diff --git a/.github/workflows/labeler.yml b/.github/workflows/labeler.yml index 017664e..d67f1ee 100644 --- a/.github/workflows/labeler.yml +++ b/.github/workflows/labeler.yml @@ -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' diff --git a/Dockerfile b/Dockerfile index 1ff6327..de93316 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] diff --git a/entrypoint.sh b/entrypoint.sh index 943aeb3..f1362f9 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -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 $? diff --git a/src/ensure.sh b/src/ensure.sh new file mode 100644 index 0000000..919fe62 --- /dev/null +++ b/src/ensure.sh @@ -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 +} diff --git a/src/github.sh b/src/github.sh new file mode 100644 index 0000000..c5bb099 --- /dev/null +++ b/src/github.sh @@ -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" +} diff --git a/src/github_actions.sh b/src/github_actions.sh new file mode 100644 index 0000000..ae8ce8a --- /dev/null +++ b/src/github_actions.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +github_actions::get_pr_number() { + jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH" +} diff --git a/src/labeler.sh b/src/labeler.sh new file mode 100644 index 0000000..5201a87 --- /dev/null +++ b/src/labeler.sh @@ -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" +} diff --git a/src/main.sh b/src/main.sh new file mode 100644 index 0000000..bce88f9 --- /dev/null +++ b/src/main.sh @@ -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 $? +} diff --git a/src/misc.sh b/src/misc.sh new file mode 100644 index 0000000..f48e525 --- /dev/null +++ b/src/misc.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +echoerr() { + echo "$@" 1>&2 +} + +log::message() { + echo "$@" +}