diff --git a/.github/workflows/labeler.yml b/.github/workflows/labeler.yml index 02cbb44..eeb00b9 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: actions/checkout@v1 + - uses: actions/checkout@v3 - uses: ./ with: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -17,7 +17,6 @@ jobs: l_max_size: '450' fail_if_xl: 'true' message_if_xl: > - 'This PR exceeds the recommended size of 1000 lines. + This PR exceeds the recommended size of 1000 lines. Please make sure you are NOT addressing multiple issues with one PR. - Note this PR might be rejected due to its size.’ - 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 + Note this PR might be rejected due to its size. diff --git a/Dockerfile b/Dockerfile index d8b04f0..db24522 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,10 @@ -FROM alpine:3.10 +FROM alpine:3.15 -RUN apk add --no-cache bash curl jq +RUN apk add --no-cache bash curl jq wget +RUN mkdir -p "$HOME/bin" && \ + cd "$HOME/bin" && \ + 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 ./ && \ + chmod +x docpars ADD entrypoint.sh /entrypoint.sh ADD src /src diff --git a/README.md b/README.md index bbfd6f8..c407a62 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ jobs: - `*_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'` +- `github_api_url`: Override this parameter in order to use with your own GitHub Enterprise Server. Example: `'https://github.example.com/api/v3'` ## 🤔 Basic concepts or assumptions diff --git a/action.yml b/action.yml index 2b74e0f..dab2049 100644 --- a/action.yml +++ b/action.yml @@ -48,30 +48,30 @@ inputs: description: 'Message to show if the PR size is xl' required: false default: > - 'This PR exceeds the recommended size of 1000 lines. + This PR exceeds the recommended size of 1000 lines. Please make sure you are NOT addressing multiple issues with one PR. - Note this PR might be rejected due to its size.’ + Note this PR might be rejected due to its size. github_api_url: - description: 'URI to the API of your Github Server, only necessary for Github Enterprise customers' + description: 'URL to the API of your Github Server, only necessary for Github Enterprise customers' required: false - default: 'api.github.com' + default: 'https://api.github.com' runs: using: 'docker' 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 }} + - --github_token=${{ inputs.GITHUB_TOKEN }} + - --github_api_url=${{ inputs.github_api_url }} + - --xs_label=${{ inputs.xs_label }} + - --xs_max_size=${{ inputs.xs_max_size }} + - --s_label=${{ inputs.s_label }} + - --s_max_size=${{ inputs.s_max_size }} + - --m_label=${{ inputs.m_label }} + - --m_max_size=${{ inputs.m_max_size }} + - --l_label=${{ inputs.l_label }} + - --l_max_size=${{ inputs.l_max_size }} + - --xl_label=${{ inputs.xl_label }} + - --fail_if_xl=${{ inputs.fail_if_xl }} + - --message_if_xl="${{ inputs.message_if_xl }}" branding: icon: 'tag' color: 'green' diff --git a/entrypoint.sh b/entrypoint.sh index 242a3a6..c597256 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,23 +2,22 @@ set -euo pipefail PR_SIZE_LABELER_HOME="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" -PR_SIZE_LABELER_API="api.github.com" if [ "$PR_SIZE_LABELER_HOME" == "/" ]; then PR_SIZE_LABELER_HOME="" fi -if [ ! -z "${13}" ]; then - PR_SIZE_LABELER_API="${13}" -fi - export PR_SIZE_LABELER_HOME -export PR_SIZE_LABELER_API bash --version source "$PR_SIZE_LABELER_HOME/src/main.sh" -main "$@" +for a in "${@}"; do + arg=$(echo "$a" | tr -d '\n'| sed "s/'//g"| sed "s/’//g") + sanitizedArgs+=("$arg") +done + +main "${sanitizedArgs[@]}" exit $? diff --git a/src/ensure.sh b/src/ensure.sh index 05f1247..fe4dd48 100644 --- a/src/ensure.sh +++ b/src/ensure.sh @@ -6,13 +6,3 @@ ensure::env_variable_exist() { exit 1 fi } - -ensure::total_args() { - local -r received_args=$(( $# - 1 )) - 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 index 74e8beb..79257da 100644 --- a/src/github.sh +++ b/src/github.sh @@ -1,10 +1,9 @@ #!/usr/bin/env bash -GITHUB_API_URI="https://$PR_SIZE_LABELER_API" 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 body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls/$1") local -r additions=$(echo "$body" | jq '.additions') local -r deletions=$(echo "$body" | jq '.deletions') @@ -21,7 +20,7 @@ github::add_label_to_pr() { 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 -r body=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls/$1") 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") @@ -34,7 +33,7 @@ github::add_label_to_pr() { -X PATCH \ -H "Content-Type: application/json" \ -d "{\"labels\":[$comma_separated_labels]}" \ - "$GITHUB_API_URI/repos/$GITHUB_REPOSITORY/issues/$pr_number" + "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$pr_number" >/dev/null } github::format_labels() { @@ -54,13 +53,13 @@ github::format_labels() { } github::comment() { - local -r comment=$1 + local -r comment="$1" curl -sSL \ -H "Authorization: token $GITHUB_TOKEN" \ -H "$GITHUB_API_HEADER" \ -X POST \ -H "Content-Type: application/json" \ - -d "{\"body\":\"$comment\"}" \ - "$GITHUB_API_URI/repos/$GITHUB_REPOSITORY/issues/$pr_number/comments" + -d "{\"body\":$comment}" \ + "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$pr_number/comments" } diff --git a/src/labeler.sh b/src/labeler.sh index cab116e..58142f3 100644 --- a/src/labeler.sh +++ b/src/labeler.sh @@ -33,15 +33,15 @@ labeler::label() { } labeler::label_for() { - local -r total_modifications="${1}" + local -r total_modifications=${1} local -r xs_label="${2}" - local -r xs_max_size="${3}" + local -r xs_max_size=${3} local -r s_label="${4}" - local -r s_max_size="${5}" + local -r s_max_size=${5} local -r m_label="${6}" - local -r m_max_size="${7}" + local -r m_max_size=${7} local -r l_label="${8}" - local -r l_max_size="${9}" + local -r l_max_size=${9} local -r xl_label="${10}" if [ "$total_modifications" -lt "$xs_max_size" ]; then diff --git a/src/main.sh b/src/main.sh index 874d56d..e61a777 100644 --- a/src/main.sh +++ b/src/main.sh @@ -6,14 +6,31 @@ 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" +##? Adds a size label to a GitHub Pull Request +##? +##? Usage: +##? main.sh --github_token= --xs_label=