Skip to content

Commit

Permalink
refactor: accept params via docpars (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgomezcasas committed Mar 20, 2022
1 parent 2b4112f commit 6601a2d
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 56 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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.
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 17 additions & 17 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
13 changes: 6 additions & 7 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 $?
10 changes: 0 additions & 10 deletions src/ensure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 6 additions & 7 deletions src/github.sh
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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")
Expand All @@ -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() {
Expand All @@ -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"
}
10 changes: 5 additions & 5 deletions src/labeler.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 20 additions & 3 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=<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>
main() {
eval "$(/root/bin/docpars -h "$(grep "^##?" "$PR_SIZE_LABELER_HOME/src/main.sh" | cut -c 5-)" : "$@")"

ensure::env_variable_exist "GITHUB_REPOSITORY"
ensure::env_variable_exist "GITHUB_EVENT_PATH"
ensure::total_args 13 "$@"

export GITHUB_TOKEN="${1}"
export GITHUB_TOKEN="$github_token"
export GITHUB_API_URL="$github_api_url"

labeler::label "${2}" "${3}" "${4}" "${5}" "${6}" "${7}" "${8}" "${9}" "${10}" "${11}" "${12}"
labeler::label \
"$xs_label" \
"$xs_max_size" \
"$s_label" \
"$s_max_size" \
"$m_label" \
"$m_max_size" \
"$l_label" \
"$l_max_size" \
"$xl_label" \
"$fail_if_xl" \
"$message_if_xl"

exit $?
}

0 comments on commit 6601a2d

Please sign in to comment.