Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔥 Replaces args input argument with blacks_flags #24

Merged
merged 1 commit into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 4 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ inputs:
Exit code when black formatting errors are found [true, false]. Defaults to 'false'.
required: false
default: "false"
black_flags:
description: "Additional black flags."
required: false
default: ""
# Reviewdog related inputs
annotate:
description: "Annotate black changes using reviewdog. Defaults to 'true'."
Expand Down Expand Up @@ -65,17 +69,6 @@ inputs:
default: ""
```

### Docker input args

Besides the aforementioned input arguments you can also supply additional input arguments for the black formatter using the args keyword [run.args](https://docs.github.com/en/free-pro-team@latest/actions/creating-actions/metadata-syntax-for-github-actions#runsargs).

```yaml
runs:
using: 'docker'
image: 'Dockerfile'
args: ". --verbose"
```

## Outputs

```yml
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ inputs:
Exit code when black formatting errors are found [true, false]. Defaults to 'false'.
required: false
default: "false"
black_flags:
description: "Additional black flags."
required: false
default: ""
# Reviewdog related inputs
annotate:
description: "Annotate black changes using reviewdog. Defaults to 'true'."
Expand Down
37 changes: 25 additions & 12 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@ set -eu # Increase bash strictness
set -o pipefail

if [[ -n "${GITHUB_WORKSPACE}" ]]; then
cd "${GITHUB_WORKSPACE}" || exit
cd "${GITHUB_WORKSPACE}/${INPUT_WORKDIR}" || exit
fi

export REVIEWDOG_GITHUB_API_TOKEN="${INPUT_GITHUB_TOKEN}"

# If no arguments are given use current working directory
if [[ "$#" -eq 0 ]]; then
black_args="."
else
black_args="$*"
black_args=(".")
if [[ "$#" -eq 0 && "${INPUT_BLACK_FLAGS}" != "" ]]; then
black_args+=(${INPUT_BLACK_FLAGS})
rickstaa marked this conversation as resolved.
Show resolved Hide resolved
elif [[ "$#" -ne 0 && "${INPUT_BLACK_FLAGS}" != "" ]]; then
black_args+=($* ${INPUT_BLACK_FLAGS})
rickstaa marked this conversation as resolved.
Show resolved Hide resolved
rickstaa marked this conversation as resolved.
Show resolved Hide resolved
elif [[ "$#" -ne 0 && "${INPUT_BLACK_FLAGS}" == "" ]]; then
black_args+=($*)
rickstaa marked this conversation as resolved.
Show resolved Hide resolved
fi

# Run black with reviewdog
black_exit_val="0"
reviewdog_exit_val="0"
if [[ "${INPUT_ANNOTATE}" = 'true' ]]; then
if [[ "${INPUT_ANNOTATE,,}" = 'true' ]]; then
if [[ "${INPUT_REPORTER}" = 'github-pr-review' ]]; then
echo "[action-black] Checking python code with the black formatter and reviewdog..."
black_check_output="$(black --diff --quiet --check ${INPUT_WORKDIR}/${black_args})" ||
black_check_output=$(black --diff --quiet --check ${black_args[@]}) ||
rickstaa marked this conversation as resolved.
Show resolved Hide resolved
black_exit_val="$?"

# Intput black formatter output to reviewdog
Expand All @@ -34,8 +37,18 @@ if [[ "${INPUT_ANNOTATE}" = 'true' ]]; then
-fail-on-error="${INPUT_FAIL_ON_ERROR}" \
${INPUT_REVIEWDOG_FLAGS} || reviewdog_exit_val="$?"
else

# Remove '-q' and '--quiet' form the black arguments
# NOTE: Having these flags in the action prevents the action from working.
black_args_tmp=()
for item in ${black_args[@]}; do
rickstaa marked this conversation as resolved.
Show resolved Hide resolved
if [[ "${item}" != "-q" && "${item}" != "--quiet" ]]; then
black_args_tmp+=("${item}") #Quotes when working with strings
fi
done

echo "[action-black] Checking python code with the black formatter and reviewdog..."
black_check_output="$(black --check ${INPUT_WORKDIR}/${black_args} 2>&1)" ||
black_check_output=$(black --check ${black_args_tmp[@]} 2>&1) ||
rickstaa marked this conversation as resolved.
Show resolved Hide resolved
black_exit_val="$?"

# Intput black formatter output to reviewdog
Expand All @@ -49,7 +62,7 @@ if [[ "${INPUT_ANNOTATE}" = 'true' ]]; then
fi
else
echo "[action-black] Checking python code using the black formatter..."
black --check "${INPUT_WORKDIR}/${black_args}" 2>&1 || black_exit_val="$?"
black --check ${black_args[@]} 2>&1 || black_exit_val="$?"
rickstaa marked this conversation as resolved.
Show resolved Hide resolved
fi

# Check for black/reviewdog errors
Expand Down Expand Up @@ -86,9 +99,9 @@ fi
# Also format code if this is requested
# NOTE: Useful for writing back changes or creating a pull request.
black_format_exit_val="0"
if [[ "${INPUT_FORMAT}" = 'true' && "${black_error}" = 'true' ]]; then
if [[ "${INPUT_FORMAT,,}" = 'true' && "${black_error}" = 'true' ]]; then
echo "[action-black] Formatting python code using the black formatter..."
black "${INPUT_WORKDIR}/${black_args}" || black_format_exit_val="$?"
black ${black_args[@]} || black_format_exit_val="$?"
rickstaa marked this conversation as resolved.
Show resolved Hide resolved

# Check whether black formatting was succesfull
if [[ "${black_format_exit_val}" -eq "0" ]]; then
Expand All @@ -101,7 +114,7 @@ if [[ "${INPUT_FORMAT}" = 'true' && "${black_error}" = 'true' ]]; then
"black formatter (error code: ${black_format_exit_val})."
exit 1
fi
elif [[ "${INPUT_FORMAT}" = 'true' && "${black_error}" = 'false' ]]; then
elif [[ "${INPUT_FORMAT,,}" = 'true' && "${black_error}" = 'false' ]]; then
echo "[action-black] Formatting not needed."
echo "::set-output name=is_formatted::false"
else
Expand Down