Skip to content

Commit

Permalink
🐛 Fixes a small bug in the error catch behavoir (#23)
Browse files Browse the repository at this point in the history
This commit makes the error catch behaviour more reliable.
  • Loading branch information
rickstaa authored Dec 30, 2020
1 parent 3ebde42 commit 8252fd2
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 83 deletions.
66 changes: 33 additions & 33 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

> 28 December 2020
- :loud_sound: Fixes stdout log syntax error [`e0022cd`](https://github.com/reviewdog/action-black/commit/e0022cd62cd2c823111535545f6a8f27c2135f16)
* :loud_sound: Fixes stdout log syntax error [`e0022cd`](https://github.com/reviewdog/action-black/commit/e0022cd62cd2c823111535545f6a8f27c2135f16)

#### [v1.3.2](https://github.com/reviewdog/action-black/compare/v1.3.1...v1.3.2)

> 28 December 2020
- :memo: Updates changelog [`a075d2a`](https://github.com/reviewdog/action-black/commit/a075d2a718c33d40b3dd3d38687a5059cef24a9e)
- :twisted_rightwards_arrows: Merge branch 'master' of github.com:reviewdog/action-black [`a87842e`](https://github.com/reviewdog/action-black/commit/a87842e921aca34830b744e197f9e3bd440d8b80)
- :twisted_rightwards_arrows: Merge branch 'fixes_error_catch_behavoir' [`f133a93`](https://github.com/reviewdog/action-black/commit/f133a93598433abd142bb21d8c994351da2930df)
* :memo: Updates changelog [`a075d2a`](https://github.com/reviewdog/action-black/commit/a075d2a718c33d40b3dd3d38687a5059cef24a9e)
* :twisted_rightwards_arrows: Merge branch 'master' of github.com:reviewdog/action-black [`a87842e`](https://github.com/reviewdog/action-black/commit/a87842e921aca34830b744e197f9e3bd440d8b80)
* :twisted_rightwards_arrows: Merge branch 'fixes_error_catch_behavoir' [`f133a93`](https://github.com/reviewdog/action-black/commit/f133a93598433abd142bb21d8c994351da2930df)

#### [v1.3.1](https://github.com/reviewdog/action-black/compare/v1.3...v1.3.1)

Expand All @@ -26,24 +26,24 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

> 28 December 2020
- :bug: Fixes bug in the is_formatted output argument [`#22`](https://github.com/reviewdog/action-black/pull/22)
- :memo: Updates changelog [`a075d2a`](https://github.com/reviewdog/action-black/commit/a075d2a718c33d40b3dd3d38687a5059cef24a9e)
- :twisted_rightwards_arrows: Merge branch 'master' of github.com:reviewdog/action-black [`a87842e`](https://github.com/reviewdog/action-black/commit/a87842e921aca34830b744e197f9e3bd440d8b80)
- :twisted_rightwards_arrows: Merge branch 'fixes_error_catch_behavoir' [`f133a93`](https://github.com/reviewdog/action-black/commit/f133a93598433abd142bb21d8c994351da2930df)
* :bug: Fixes bug in the is_formatted output argument [`#22`](https://github.com/reviewdog/action-black/pull/22)
* :memo: Updates changelog [`a075d2a`](https://github.com/reviewdog/action-black/commit/a075d2a718c33d40b3dd3d38687a5059cef24a9e)
* :twisted_rightwards_arrows: Merge branch 'master' of github.com:reviewdog/action-black [`a87842e`](https://github.com/reviewdog/action-black/commit/a87842e921aca34830b744e197f9e3bd440d8b80)
* :twisted_rightwards_arrows: Merge branch 'fixes_error_catch_behavoir' [`f133a93`](https://github.com/reviewdog/action-black/commit/f133a93598433abd142bb21d8c994351da2930df)

#### [v1.3.0](https://github.com/reviewdog/action-black/compare/v1.2.2...v1.3.0)

> 28 December 2020
- :sparkles: Adds is_formatted output argument [`#21`](https://github.com/reviewdog/action-black/pull/21)
- :green_heart: Fixes release gh-action [`16f69ed`](https://github.com/reviewdog/action-black/commit/16f69ed9c674938748f426c4f0a0a052dee5c250)
* :sparkles: Adds is_formatted output argument [`#21`](https://github.com/reviewdog/action-black/pull/21)
* :green_heart: Fixes release gh-action [`16f69ed`](https://github.com/reviewdog/action-black/commit/16f69ed9c674938748f426c4f0a0a052dee5c250)

#### [v1.2.2](https://github.com/reviewdog/action-black/compare/v1.2.1...v1.2.2)

> 28 December 2020
- :memo: Adds changelog [`8b9c42d`](https://github.com/reviewdog/action-black/commit/8b9c42dee4fdcfdbb5a398ac1c39dba1c75e9424)
- :memo: Updates README.md [`8d123cb`](https://github.com/reviewdog/action-black/commit/8d123cbcbc57aa500c283cf8f3ad46d10bf793cd)
* :memo: Adds changelog [`8b9c42d`](https://github.com/reviewdog/action-black/commit/8b9c42dee4fdcfdbb5a398ac1c39dba1c75e9424)
* :memo: Updates README.md [`8d123cb`](https://github.com/reviewdog/action-black/commit/8d123cbcbc57aa500c283cf8f3ad46d10bf793cd)

#### [v1.2.1](https://github.com/reviewdog/action-black/compare/v1.2...v1.2.1)

Expand All @@ -53,23 +53,23 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

> 28 December 2020
- :memo: Adds changelog [`8b9c42d`](https://github.com/reviewdog/action-black/commit/8b9c42dee4fdcfdbb5a398ac1c39dba1c75e9424)
- :art: :bug: Fixes a small bug while catching the exit code [`ac37b4f`](https://github.com/reviewdog/action-black/commit/ac37b4f4b861666e736df9943171a85e853e0ccb)
- :art: :fire: Removes redundant test [`f042d95`](https://github.com/reviewdog/action-black/commit/f042d9566d475dd75a1af0400a63affe4cdae2da)
* :memo: Adds changelog [`8b9c42d`](https://github.com/reviewdog/action-black/commit/8b9c42dee4fdcfdbb5a398ac1c39dba1c75e9424)
* :art: :bug: Fixes a small bug while catching the exit code [`ac37b4f`](https://github.com/reviewdog/action-black/commit/ac37b4f4b861666e736df9943171a85e853e0ccb)
* :art: :fire: Removes redundant test [`f042d95`](https://github.com/reviewdog/action-black/commit/f042d9566d475dd75a1af0400a63affe4cdae2da)

#### [v1.2.0](https://github.com/reviewdog/action-black/compare/v1.1.2...v1.2.0)

> 28 December 2020
- :twisted_rightwards_arrows: Merge pull request #19 from reviewdog/add-auto-format-option [`7854a02`](https://github.com/reviewdog/action-black/commit/7854a0223bd5288b5f0b5c6fa34170bde21d7615)
- :sparkles: Adds ability to also format the code [`2d3fcd1`](https://github.com/reviewdog/action-black/commit/2d3fcd14c4eccf82ef584084d0a6d4f02869dd05)
- :bug: Fixes docker input arguments bug [`7a8c881`](https://github.com/reviewdog/action-black/commit/7a8c881dae3ee394d7b2df183bf24136a039a5b5)
* :twisted_rightwards_arrows: Merge pull request #19 from reviewdog/add-auto-format-option [`7854a02`](https://github.com/reviewdog/action-black/commit/7854a0223bd5288b5f0b5c6fa34170bde21d7615)
* :sparkles: Adds ability to also format the code [`2d3fcd1`](https://github.com/reviewdog/action-black/commit/2d3fcd14c4eccf82ef584084d0a6d4f02869dd05)
* :bug: Fixes docker input arguments bug [`7a8c881`](https://github.com/reviewdog/action-black/commit/7a8c881dae3ee394d7b2df183bf24136a039a5b5)

#### [v1.1.2](https://github.com/reviewdog/action-black/compare/v1.1.1...v1.1.2)

> 24 December 2020
- :memo: Removes github-pr-review warning [`93e7934`](https://github.com/reviewdog/action-black/commit/93e79341fe169dc7c8b1c46503542cebd8bda58f)
* :memo: Removes github-pr-review warning [`93e7934`](https://github.com/reviewdog/action-black/commit/93e79341fe169dc7c8b1c46503542cebd8bda58f)

#### [v1.1.1](https://github.com/reviewdog/action-black/compare/v1.1...v1.1.1)

Expand All @@ -79,9 +79,9 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

> 24 December 2020
- :fire: Removes redundant code [`f4530fe`](https://github.com/reviewdog/action-black/commit/f4530fe6b3d3bc4c3799745505e35206c0c44516)
- :memo: Removes github-pr-review warning [`93e7934`](https://github.com/reviewdog/action-black/commit/93e79341fe169dc7c8b1c46503542cebd8bda58f)
- :twisted_rightwards_arrows: Merge branch 'master' of github.com:reviewdog/action-black [`8945e02`](https://github.com/reviewdog/action-black/commit/8945e02b07654522cab03fafd9a138544138c673)
* :fire: Removes redundant code [`f4530fe`](https://github.com/reviewdog/action-black/commit/f4530fe6b3d3bc4c3799745505e35206c0c44516)
* :memo: Removes github-pr-review warning [`93e7934`](https://github.com/reviewdog/action-black/commit/93e79341fe169dc7c8b1c46503542cebd8bda58f)
* :twisted_rightwards_arrows: Merge branch 'master' of github.com:reviewdog/action-black [`8945e02`](https://github.com/reviewdog/action-black/commit/8945e02b07654522cab03fafd9a138544138c673)

#### [v1.1.0](https://github.com/reviewdog/action-black/compare/v1...v1.1.0)

Expand All @@ -91,11 +91,11 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

> 28 December 2020
- :bug: Fixes bug in the is_formatted output argument [`#22`](https://github.com/reviewdog/action-black/pull/22)
- :sparkles: Adds is_formatted output argument [`#21`](https://github.com/reviewdog/action-black/pull/21)
- :memo: Updates changelog [`a075d2a`](https://github.com/reviewdog/action-black/commit/a075d2a718c33d40b3dd3d38687a5059cef24a9e)
- :twisted_rightwards_arrows: Merge branch 'master' of github.com:reviewdog/action-black [`a87842e`](https://github.com/reviewdog/action-black/commit/a87842e921aca34830b744e197f9e3bd440d8b80)
- :twisted_rightwards_arrows: Merge branch 'fixes_error_catch_behavoir' [`f133a93`](https://github.com/reviewdog/action-black/commit/f133a93598433abd142bb21d8c994351da2930df)
* :bug: Fixes bug in the is_formatted output argument [`#22`](https://github.com/reviewdog/action-black/pull/22)
* :sparkles: Adds is_formatted output argument [`#21`](https://github.com/reviewdog/action-black/pull/21)
* :memo: Updates changelog [`a075d2a`](https://github.com/reviewdog/action-black/commit/a075d2a718c33d40b3dd3d38687a5059cef24a9e)
* :twisted_rightwards_arrows: Merge branch 'master' of github.com:reviewdog/action-black [`a87842e`](https://github.com/reviewdog/action-black/commit/a87842e921aca34830b744e197f9e3bd440d8b80)
* :twisted_rightwards_arrows: Merge branch 'fixes_error_catch_behavoir' [`f133a93`](https://github.com/reviewdog/action-black/commit/f133a93598433abd142bb21d8c994351da2930df)

#### [v1.0](https://github.com/reviewdog/action-black/compare/v1.0.0...v1.0)

Expand All @@ -105,9 +105,9 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

> 14 December 2020
- :bug: Fixes reviewdog not installed bug and updates documentation [`bc012f8`](https://github.com/reviewdog/action-black/commit/bc012f831b15d6543dfea0b1f0963d30b1ebfe1b)
- :sparkles: Adds github-pr-review not supported warning [`5d8a3ba`](https://github.com/reviewdog/action-black/commit/5d8a3bab08445cc4fd3568e136a2203eb0f46b26)
- :bug: Fixes dockerfile bug [`4ca541d`](https://github.com/reviewdog/action-black/commit/4ca541deb11d6df2b905c71ba9130c8c1a384963)
* :bug: Fixes reviewdog not installed bug and updates documentation [`bc012f8`](https://github.com/reviewdog/action-black/commit/bc012f831b15d6543dfea0b1f0963d30b1ebfe1b)
* :sparkles: Adds github-pr-review not supported warning [`5d8a3ba`](https://github.com/reviewdog/action-black/commit/5d8a3bab08445cc4fd3568e136a2203eb0f46b26)
* :bug: Fixes dockerfile bug [`4ca541d`](https://github.com/reviewdog/action-black/commit/4ca541deb11d6df2b905c71ba9130c8c1a384963)

#### [v0.0.1](https://github.com/reviewdog/action-black/compare/v0.0...v0.0.1)

Expand All @@ -121,6 +121,6 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

> 14 December 2020
- Initial commit [`100b5c2`](https://github.com/reviewdog/action-black/commit/100b5c205439e57216df919a9b2240b23f6ba085)
- :white_check_mark: Adds test files [`dd52b41`](https://github.com/reviewdog/action-black/commit/dd52b418d75d7962d1eb544de45f85610f4c6488)
- :bug: Fixes action.yml file bug and adds additional arguments [`c871e9d`](https://github.com/reviewdog/action-black/commit/c871e9d26198deb2ecc3931da5ccc639c36e1640)
* Initial commit [`100b5c2`](https://github.com/reviewdog/action-black/commit/100b5c205439e57216df919a9b2240b23f6ba085)
* :white_check_mark: Adds test files [`dd52b41`](https://github.com/reviewdog/action-black/commit/dd52b418d75d7962d1eb544de45f85610f4c6488)
* :bug: Fixes action.yml file bug and adds additional arguments [`c871e9d`](https://github.com/reviewdog/action-black/commit/c871e9d26198deb2ecc3931da5ccc639c36e1640)
107 changes: 57 additions & 50 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
set -e
set -eu # Increase bash strictness
set -o pipefail

if [[ -n "${GITHUB_WORKSPACE}" ]]; then
cd "${GITHUB_WORKSPACE}" || exit
Expand All @@ -16,64 +17,68 @@ fi

# Run black with reviewdog
black_exit_val="0"
reviewdog_error="false"
reviewdog_exit_val="0"
if [[ "${INPUT_ANNOTATE}" = 'true' ]]; then
if [[ "${INPUT_REPORTER}" = 'github-pr-review' ]]; then
echo "[action-black] Checking python code with the black formatter and reviewdog..."
# work only fix diff suggestion (std_out: diff, std_err: console output)
black --diff --quiet --check "${INPUT_WORKDIR}/${black_args}" |
reviewdog -f="diff" \
-f.diff.strip=0 \
-name="${INPUT_TOOL_NAME}-fix" \
-reporter="github-pr-review" \
-filter-mode="diff_context" \
-level="${INPUT_LEVEL}" \
-fail-on-error="${INPUT_FAIL_ON_ERROR}" \
${INPUT_REVIEWDOG_FLAGS} || reviewdog_error="true"
black_exit_val="${PIPESTATUS[0]}"
black_check_output="$(black --diff --quiet --check ${INPUT_WORKDIR}/${black_args})" ||
black_exit_val="$?"

# Check whether black found formatting errors
if [[ "${black_exit_val}" -eq "0" ]]; then
black_error="false"
elif [[ "${black_exit_val}" -eq "1" ]]; then
black_error="true"
else
echo "Something went wrong while trying to run the black command (error code: ${black_exit_val})."
exit 1
fi
# Intput black formatter output to reviewdog
echo "${black_check_output}" | reviewdog -f="diff" \
-f.diff.strip=0 \
-name="${INPUT_TOOL_NAME}-fix" \
-reporter="github-pr-review" \
-filter-mode="diff_context" \
-level="${INPUT_LEVEL}" \
-fail-on-error="${INPUT_FAIL_ON_ERROR}" \
${INPUT_REVIEWDOG_FLAGS} || reviewdog_exit_val="$?"
else
echo "[action-black] Checking python code with the black formatter and reviewdog..."
black --check "${INPUT_WORKDIR}/${black_args}" 2>&1 |
reviewdog -f="black" \
-name="${INPUT_TOOL_NAME}" \
-reporter="${INPUT_REPORTER}" \
-filter-mode="${INPUT_FILTER_MODE}" \
-fail-on-error="${INPUT_FAIL_ON_ERROR}" \
-level="${INPUT_LEVEL}" \
${INPUT_REVIEWDOG_FLAGS} || reviewdog_error="true"
black_exit_val="${PIPESTATUS[0]}"
black_check_output="$(black --check ${INPUT_WORKDIR}/${black_args} 2>&1)" ||
black_exit_val="$?"

# Check whether black found formatting errors
if [[ "${black_exit_val}" -eq "0" ]]; then
black_error="false"
elif [[ "${black_exit_val}" -eq "1" ]]; then
black_error="true"
else
echo "[action-black] ERROR: Something went wrong while trying to run the black formatter (error code: ${black_exit_val})."
exit 1
fi
# Intput black formatter output to reviewdog
echo "${black_check_output}" | reviewdog -f="black" \
-name="${INPUT_TOOL_NAME}" \
-reporter="${INPUT_REPORTER}" \
-filter-mode="${INPUT_FILTER_MODE}" \
-fail-on-error="${INPUT_FAIL_ON_ERROR}" \
-level="${INPUT_LEVEL}" \
${INPUT_REVIEWDOG_FLAGS} || reviewdog_exit_val="$?"
fi
else
echo "[action-black] Checking python code using the black formatter..."
black --check "${INPUT_WORKDIR}/${black_args}" 2>&1 || black_exit_val="$?"
fi

# Check whether black found formatting errors
if [[ "${black_exit_val}" -eq "0" ]]; then
black_error="false"
elif [[ "${black_exit_val}" -eq "1" ]]; then
black_error="true"
# Check for black/reviewdog errors
if [[ "${black_exit_val}" -eq "0" && "${reviewdog_exit_val}" -eq "0" ]]; then
black_error="false"
reviewdog_error="false"
elif [[ "${black_exit_val}" -eq "1" && "${reviewdog_exit_val}" -eq "0" ]]; then
black_error="true"
reviewdog_error="false"
elif [[ "${black_exit_val}" -eq "1" && "${reviewdog_exit_val}" -eq "1" ]]; then
black_error="true"
reviewdog_error="true"
elif [[ "${black_exit_val}" -eq "0" && "${reviewdog_exit_val}" -eq "1" ]]; then
black_error="false"
reviewdog_error="true"
else
if [[ "${black_exit_val}" -ne "0" && "${black_exit_val}" -ne "1" && \
"${reviewdog_exit_val}" -ne "0" && "${reviewdog_exit_val}" -ne "1" ]]; then
echo "[action-black] ERROR: Something went wrong while trying to run the black" \
"formatter while annotating the changes using reviewdog (black error code:" \
"${black_exit_val}, reviewdog error code: ${reviewdog_exit_val})."
exit 1
elif [[ "${black_exit_val}" -ne "0" && "${black_exit_val}" -ne "1" ]]; then
echo "[action-black] ERROR: Something went wrong while trying to run the black" \
"formatter (error code: ${black_exit_val})."
exit 1
else
echo "[action-black] ERROR: Something went wrong while trying to run the black formatter (error code: ${black_exit_val})."
echo "[action-black] ERROR: Something went wrong while trying to run the" \
"reviewdog error annotator (error code: ${reviewdog_exit_val})."
exit 1
fi
fi
Expand All @@ -85,24 +90,26 @@ 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="$?"

# Check whether black found formatting errors
# Check whether black formatting was succesfull
if [[ "${black_format_exit_val}" -eq "0" ]]; then
echo "::set-output name=is_formatted::true"
elif [[ "${black_format_exit_val}" -eq "1" ]]; then
black_error="true"
echo "::set-output name=is_formatted::false"
else
echo "[action-black] ERROR: Something went wrong while trying to run the black formatter (error code: ${black_format_exit_val})."
echo "[action-black] ERROR: Something went wrong while trying to run the" \
"black formatter (error code: ${black_format_exit_val})."
exit 1
fi
elif [[ "${INPUT_FORMAT}" = 'true' && "${black_error}" != 'true' ]]; then
elif [[ "${INPUT_FORMAT}" = 'true' && "${black_error}" = 'false' ]]; then
echo "[action-black] Formatting not needed."
echo "::set-output name=is_formatted::false"
else
echo "::set-output name=is_formatted::false"
fi

# Throw error if an error occurred and fail_on_error is true
if [[ ("${reviewdog_error}" = 'true' || "${black_error}") && "${INPUT_FAIL_ON_ERROR}" = 'true' ]]; then
if [[ ("${black_error}" = 'true' || "${reviewdog_error}" = 'true') && \
"${INPUT_FAIL_ON_ERROR}" = 'true' ]]; then
exit 1
fi

0 comments on commit 8252fd2

Please sign in to comment.