Skip to content

Commit

Permalink
fix: fix 'bad array subscript' error (#62)
Browse files Browse the repository at this point in the history
* fix: fix 'bad array subscript' error

This commit fixes the 'bad array subscript' error that is thrown when no
python files are found (see https://github.com/rickstaa/panda-gazebo/actions/runs/5269309929/jobs/9527260724).
It also simplifies the codebase.

* feat: add empty folder warning/variable and verbose option

This commit adds the EMPTY_FOLDER environment variable which can be used
to check if the folder that was specified was empty. It now also throws
a warning in the github action console when no files are found. Lastly,
the `verbose` input argument was added which allows people to print the
black output to the github action console. It also adds a test to see if
the empty folder check works.

* fix: fix verbose input bug

* fix: upload empty folder

* test: fix verbose test

* feat: remove EMPTY_FOLDER env variable

* fix: fix warning color

* fix: uncomment code

* fix: remove test code
  • Loading branch information
rickstaa authored Jun 17, 2023
1 parent b494c89 commit 8f03cb8
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 24 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@ on:
- master
pull_request:
jobs:
test-empty:
name: runner / black-format (empty)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ./
with:
github_token: ${{ secrets.github_token }}
reporter: github-check
level: info
workdir: ./testdata/emptyfolder
- name: Check if code is left untouched (not formatted)
run: |
changed_files=$(git diff --name-only -- testdata/emptyfolder/ | wc -l)
if [[ ${changed_files} -eq 0 ]]; then
echo "No changes detected!"
else
echo "Changes detected!"
exit 1
fi
test-check:
name: runner / black-format (github-check)
runs-on: ubuntu-latest
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ jobs:

**Optional**. Additional black input arguments. Defaults to `""`.

| :warning: | Because this action uses the black output to create the annotations, it does not work with the black `--quiet` flag. |
| --------- | -------------------------------------------------------------------------------------------------------------------- |
> **Warning**
> Because this action uses the black output to create the annotations, it does not work with the black `--quiet` flag.

#### `verbose`

**Optional**. Set to `true` to print the black output to the github action console. Defaults to `false`.

#### `tool_name`

Expand Down Expand Up @@ -80,7 +84,9 @@ Default is github-pr-check.

## Outputs

### `BLACK_CHECK_FILE_PATHS`
### Environment variables

#### `BLACK_CHECK_FILE_PATHS`

Contains all the files that would be changed by black.

Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ inputs:
description: "Additional black flags."
required: false
default: ""
verbose:
description: "Print black output."
required: false
default: "false"
# Reviewdog related inputs
github_token:
description: "GITHUB_TOKEN."
Expand Down Expand Up @@ -54,6 +58,7 @@ runs:
INPUT_FILTER_MODE: ${{ inputs.filter_mode }}
INPUT_FAIL_ON_ERROR: ${{ inputs.fail_on_error }}
INPUT_REVIEWDOG_FLAGS: ${{ inputs.reviewdog_flags }}
INPUT_VERBOSE: ${{ inputs.verbose }}

# Ref: https://haya14busa.github.io/github-action-brandings/
branding:
Expand Down
45 changes: 24 additions & 21 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
# <!--alex disable black-->

set -eu # Increase bash strictness
set -eu # Increase bash strictness.
set -o pipefail

if [[ -n "${GITHUB_WORKSPACE}" ]]; then
Expand All @@ -22,7 +22,7 @@ if [[ "$(which black)" == "" ]]; then
fi
echo "[action-black] Black version: $(black --version)"

# Run black with reviewdog
# Run black with reviewdog.
black_exit_val="0"
reviewdog_exit_val="0"
if [[ "${INPUT_REPORTER}" = 'github-pr-review' ]]; then
Expand All @@ -31,7 +31,7 @@ if [[ "${INPUT_REPORTER}" = 'github-pr-review' ]]; then
black_check_output="$(black --diff --quiet --check . ${INPUT_BLACK_ARGS})" ||
black_exit_val="$?"

# Input black formatter output to reviewdog
# Input black formatter output to reviewdog.
# shellcheck disable=SC2086
echo "${black_check_output}" | /tmp/reviewdog -f="diff" \
-f.diff.strip=0 \
Expand All @@ -43,7 +43,7 @@ if [[ "${INPUT_REPORTER}" = 'github-pr-review' ]]; then
${INPUT_REVIEWDOG_FLAGS} || reviewdog_exit_val="$?"

# Re-generate black output. Needed because the output of the '--diff' option can not
# be used to retrieve the files that black would change
# be used to retrieve the files that black would change.
# shellcheck disable=SC2086,SC2034
black_check_output="$(black --check . ${INPUT_BLACK_ARGS} 2>&1)" || true
else
Expand All @@ -52,7 +52,7 @@ else
black_check_output="$(black --check . ${INPUT_BLACK_ARGS} 2>&1)" ||
black_exit_val="$?"

# Input black formatter output to reviewdog
# Input black formatter output to reviewdog.
# shellcheck disable=SC2086
echo "${black_check_output}" | /tmp/reviewdog -f="black" \
-name="${INPUT_TOOL_NAME}" \
Expand All @@ -63,29 +63,32 @@ else
${INPUT_REVIEWDOG_FLAGS} || reviewdog_exit_val="$?"
fi

# Remove jupyter warning if present
black_check_output="${black_check_output//"Skipping .ipynb files as Jupyter dependencies are not installed."/}"
black_check_output="${black_check_output//"You can fix this by running \`\`pip install black[jupyter]\`\`"/}"
# Print warning if no python files were found.
if [[ "${black_check_output}" == *"No Python files are present to be formatted. Nothing to do 😴"* ]]; then
echo -e "\e[33m[action-black]: WARNING: No Python files are present to be formatted. Nothing to do 😴"
fi

# Print black output if verbose is true.
if [[ "${INPUT_VERBOSE}" = 'true' ]]; then
echo "[action-black] Black output:"
echo "${black_check_output}"
fi

# Output the checked file paths that would be formatted
# Retrieve formatted files from black output. Only add lines that start with 'would reformat'.
black_check_file_paths=()
while read -r line; do
if [ "$line" != "" ]; then
black_check_file_paths+=("$line")
if [[ "${line}" == *"would reformat"* ]]; then
black_check_file_paths+=("${line/"would reformat "/}")
fi
done <<<"${black_check_output//"would reformat "/}"

# remove last two lines of black output, since they are irrelevant
unset "black_check_file_paths[-1]"
unset "black_check_file_paths[-1]"
done <<< "$black_check_output"

# Append the array elements to BLACK_CHECK_FILE_PATHS in github env
# Append the array elements to BLACK_CHECK_FILE_PATHS in github env.
# shellcheck disable=SC2129
echo "BLACK_CHECK_FILE_PATHS<<EOF" >>"$GITHUB_ENV"
echo "${black_check_file_paths[@]}" >>"$GITHUB_ENV"
echo "EOF" >>"$GITHUB_ENV"
echo "BLACK_CHECK_FILE_PATHS<<EOF" >> "$GITHUB_ENV"
echo "${black_check_file_paths[@]}" >> "$GITHUB_ENV"
echo "EOF" >> "$GITHUB_ENV"

# Throw error if an error occurred and fail_on_error is true
# Throw error if an error occurred and fail_on_error is true.
if [[ "${INPUT_FAIL_ON_ERROR}" = 'true' && ("${black_exit_val}" -ne '0' ||
"${reviewdog_exit_val}" -eq "1") ]]; then
if [[ "${black_exit_val}" -eq "123" ]]; then
Expand Down
Empty file added testdata/emptyfolder/.gitignore
Empty file.

0 comments on commit 8f03cb8

Please sign in to comment.