From 8f03cb857cfeb43d4c122afbe9bdd0671bfdfd52 Mon Sep 17 00:00:00 2001 From: Rick Staa Date: Sat, 17 Jun 2023 05:54:23 +0200 Subject: [PATCH] fix: fix 'bad array subscript' error (#62) * 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 --- .github/workflows/test.yml | 20 +++++++++++++++ README.md | 12 ++++++--- action.yml | 5 ++++ entrypoint.sh | 45 ++++++++++++++++++--------------- testdata/emptyfolder/.gitignore | 0 5 files changed, 58 insertions(+), 24 deletions(-) create mode 100644 testdata/emptyfolder/.gitignore diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7772552..48a7213 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/README.md b/README.md index 0a970b5..6fb81e0 100644 --- a/README.md +++ b/README.md @@ -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` @@ -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. diff --git a/action.yml b/action.yml index a783602..770f5ac 100644 --- a/action.yml +++ b/action.yml @@ -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." @@ -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: diff --git a/entrypoint.sh b/entrypoint.sh index 251ed02..3ae836b 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,7 +1,7 @@ #!/bin/bash # -set -eu # Increase bash strictness +set -eu # Increase bash strictness. set -o pipefail if [[ -n "${GITHUB_WORKSPACE}" ]]; then @@ -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 @@ -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 \ @@ -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 @@ -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}" \ @@ -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<>"$GITHUB_ENV" -echo "${black_check_file_paths[@]}" >>"$GITHUB_ENV" -echo "EOF" >>"$GITHUB_ENV" +echo "BLACK_CHECK_FILE_PATHS<> "$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 diff --git a/testdata/emptyfolder/.gitignore b/testdata/emptyfolder/.gitignore new file mode 100644 index 0000000..e69de29