refactor: accept params via docpars (#34)

This commit is contained in:
Rafa Gómez 2022-03-20 10:53:21 +01:00 committed by GitHub
parent 2b4112ffb7
commit 6601a2d04f
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 64 additions and 56 deletions

View file

@ -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.

View file

@ -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

View file

@ -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

View file

@ -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'

View file

@ -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 $?

View file

@ -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
}

View file

@ -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"
}

View file

@ -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

View file

@ -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 $?
}