Skip to content

Commit

Permalink
🎨 Seperates linting and formatting steps
Browse files Browse the repository at this point in the history
The linting and formatting steps are now seperated such that the
formatting output does not get passed to the reviewdog error parser.
  • Loading branch information
rickstaa committed Jan 1, 2021
1 parent d7e5d99 commit 42f4234
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 67 deletions.
54 changes: 29 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,48 +38,52 @@ See the Inputs section below for details on the defaults and optional configurat

**Required**. Must be in form of `github_token: ${{ secrets.github_token }}`'.

### workdir
### `workdir`

**Optional**. The directory to run remark-lint in. Default is `.`.
**Optional**. The directory to run remark-lint in. Defaults to `.`.

### `level`
### `format`

**Optional**. Report level for reviewdog \[info, warning, error].
It's same as `-level` flag of reviewdog.
**Optional**. If true, remark-lint format files and commit are creatable (use other Action). Defaults to `false`.

### `reporter`
#### `fail_on_error`

**Optional**. Reporter of reviewdog command \[github-pr-check, github-pr-review, github-check].
Default is github-pr-check. github-pr-review can use Markdown and add a link to rule page in reviewdog reports.
**Optional**. Exit code for when remark-lint errors are found \[`true`, `false`]. Defaults to `false`.

**NB:** Only `github-pr-check` is supported currently.
#### `remark_flags`

#### `filter_mode`
**Optional**. Additional remark-lint flags. Defaults to `""`.

**Optional**. Filtering mode for the reviewdog command \[added, diff_context, file, nofilter]. Default = `"added"`.
### `annotate`

#### `fail_on_error`
**Optional**. Annotate remark-lint changes using reviewdog. Defaults to `true`.

**Optional**. Exit code for reviewdog when errors are found \[`true`, `false`]. Default = `false`.
#### `tool_name`

#### `reviewdog_flags`
**Optional**. Tool name to use for reviewdog reporter. Defaults to `remark-lint`.

**Optional**. Additional reviewdog flags. Default = `""`.
### `level`

#### `tool_name`
**Optional**. Report level for reviewdog \[info, warning, error]. It's same as `-level` flag of reviewdog. Defaults to `error`.

**Optional**. Tool name to use for reviewdog reporter. Default = `remark-lint`.
### `reporter`

## Docker input args
**Optional**. Reporter of reviewdog command \[github-pr-check, github-pr-review, github-check].
Default is github-pr-check. github-pr-review can use Markdown and add a link to rule page in reviewdog reports.

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

```yaml
runs:
using: 'docker'
image: 'Dockerfile'
args: ". --verbose"
```
**Optional**. Filtering mode for the reviewdog command \[added, diff_context, file, nofilter]. Defaults to `"added"`.

### `reviewdog_flags`

**Optional**. Additional reviewdog flags. Defaults to `""`.

## Outputs

### `is_formatted`

Whether the files were formatted using the remark-lint linter.

## Advance use cases

Expand Down
9 changes: 8 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: "🐶 Run remark-lint with reviewdog on pull requests to improve co
author: "prologic"
inputs:
workdir:
description: "Working directory relative to the root directory."
description: "Working directory relative to the root directory. Defaults to '.'."
required: false
default: "."
format:
Expand All @@ -17,6 +17,10 @@ inputs:
Exit code for reviewdog when errors are found [true, false]. Defaults to 'false'.
required: false
default: "false"
remark_flags:
description: "Additional remark-lint flags."
required: false
default: ""
# Reviewdog related inputs
annotate:
description: "Annotate remark-lint changes using reviewdog. Defaults to 'true'."
Expand Down Expand Up @@ -50,6 +54,9 @@ inputs:
description: "Additional reviewdog flags."
required: false
default: ""
outputs:
is_formatted:
description: "Whether the files were formatted using the remark-lint linter."
runs:
using: "docker"
image: "Dockerfile"
Expand Down
133 changes: 92 additions & 41 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,62 +1,113 @@
#!/bin/bash
set -e # Exit immediately if a command exits with a non-zero status
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
remark_args="."
remark_args=(".")
if [[ "$#" -eq 0 && "${INPUT_REMARK_FLAGS}" != "" ]]; then
remark_args+=(${INPUT_REMARK_FLAGS})
elif [[ "$#" -ne 0 && "${INPUT_REMARK_FLAGS}" != "" ]]; then
remark_args+=($* ${INPUT_REMARK_FLAGS})
elif [[ "$#" -ne 0 && "${INPUT_REMARK_FLAGS}" == "" ]]; then
remark_args+=($*)
fi

# Run remark-lint with reviewdog
remark_exit_val="0"
reviewdog_exit_val="0"
if [[ "${INPUT_ANNOTATE,,}" = 'true' ]]; then
echo "[action-remark-lint] Checking markdown code using the remark-lint linter and reviewdog..."
remark_lint_output=$(remark --frail --quiet --use=remark-preset-lint-recommended ${remark_args[@]} 2>&1) ||
remark_exit_val="$?"

# Input remark formatter output to reviewdog
echo "${remark_lint_output}" |
sed 's/\x1b\[[0-9;]*m//g' | # Removes ansi codes see https://github.com/reviewdog/errorformat/issues/51
reviewdog -f=remark-lint \
-name="${INPUT_TOOL_NAME}" \
-reporter="${INPUT_REPORTER}" \
-filter-mode="${INPUT_FILTER_MODE}" \
-fail-on-error="${INPUT_FAIL_ON_ERROR,,}" \
-level="${INPUT_LEVEL}" \
-tee \
${INPUT_REVIEWDOG_FLAGS} || reviewdog_exit_val="$?"
else
remark_args="$*"
echo "[action-remark-lint] Checking markdown code using the remark-lint linter..."
remark --frail --quiet --use=remark-preset-lint-recommended \
${remark_args[@]} 2>&1 || remark_exit_val="$?"
fi

# Check if formatting is requested
if [[ "${INPUT_FORMAT}" = 'true' ]]; then
format_str="Format"
format_arg="-o"
# Check for remark/reviewdog errors
if [[ "${remark_exit_val}" -eq "0" && "${reviewdog_exit_val}" -eq "0" ]]; then
remark_error="false"
reviewdog_error="false"
elif [[ "${remark_exit_val}" -eq "1" && "${reviewdog_exit_val}" -eq "0" ]]; then
remark_error="true"
reviewdog_error="false"
elif [[ "${remark_exit_val}" -eq "1" && "${reviewdog_exit_val}" -eq "1" ]]; then
remark_error="true"
reviewdog_error="true"
elif [[ "${remark_exit_val}" -eq "0" && "${reviewdog_exit_val}" -eq "1" ]]; then
remark_error="false"
reviewdog_error="true"
else
format_str="Check"
format_arg=""
if [[ "${remark_exit_val}" -ne "0" && "${remark_exit_val}" -ne "1" && \
"${reviewdog_exit_val}" -ne "0" && "${reviewdog_exit_val}" -ne "1" ]]; then
# NOTE: Should not occur but just to be sure
echo "[action-remark-lint] ERROR: Something went wrong while trying to run the remark" \
"linter while annotating the changes using reviewdog (remark error code:" \
"${remark_exit_val}, reviewdog error code: ${reviewdog_exit_val})."
exit 1
elif [[ "${remark_exit_val}" -ne "0" && "${remark_exit_val}" -ne "1" ]]; then
# NOTE: Should not occur but just to be sure
echo "[action-remark-lint] ERROR: Something went wrong while trying to run the remark" \
"linter (error code: ${remark_exit_val})."
exit 1
else
echo "[action-remark-lint] ERROR: Something went wrong while trying to run the" \
"reviewdog error annotator (error code: ${reviewdog_exit_val})."
exit 1
fi
fi

# Run black with reviewdog
remark_lint_error="false"
reviewdog_error="false"
if [[ "${INPUT_ANNOTATE}" = 'true' ]]; then
echo "[action-remark-lint] ${format_str} markdown code using the remark-lint linter..."
remark --frail --quiet --use=remark-preset-lint-recommended "${format_arg}" "${INPUT_WORKDIR}/${remark_args}" 2>&1 |
sed 's/\x1b\[[0-9;]*m//g' | # Removes ansi codes see https://github.com/reviewdog/errorformat/issues/51
reviewdog -f=remark-lint \
-name="${INPUT_TOOL_NAME}" \
-reporter="${INPUT_REPORTER}" \
-filter-mode="${INPUT_FILTER_MODE}" \
-fail-on-error="${INPUT_FAIL_ON_ERROR}" \
-level="${INPUT_LEVEL}" \
-tee \
${INPUT_REVIEWDOG_FLAGS} || reviewdog_error="true"
remark_exit_val="${PIPESTATUS[0]}"
if [[ "${remark_exit_val}" -ne "0" ]]; then
remark_lint_error="true"
elif [[ "${remark_exit_val}" -eq "0" && "${INPUT_FORMAT}" = 'true' ]]; then
echo "[action-remark-lint] Formatting not needed."
# Also format code if this is requested
# NOTE: Useful for writing back changes or creating a pull request.
remark_format_exit_val="0"
if [[ "${INPUT_FORMAT,,}" = 'true' && "${remark_error}" = 'true' ]]; then
echo "[action-remark-lint] Formatting python code using the remark linter..."
remark --quiet --output --use=remark-preset-lint-recommended \
${remark_args[@]} 2>&1 || remark_format_exit_val="$?"

# Check whether remark-lint formatting was succesfull
if [[ "${remark_format_exit_val}" -eq "0" ]]; then
remark_error="false"
echo "::set-output name=is_formatted::true"
elif [[ "${remark_format_exit_val}" -eq "1" ]]; then
remark_error="true"
echo "::set-output name=is_formatted::false"
else
# NOTE: Should not occur but just to be sure
echo "[action-remark-lint] ERROR: Something went wrong while trying to format the" \
"code using the remark linter (error code: ${remark_format_exit_val})."
exit 1
fi
elif [[ "${INPUT_FORMAT,,}" = 'true' && "${remark_error}" = 'false' ]]; then
echo "[action-remark-lint] Formatting not needed."
echo "::set-output name=is_formatted::false"
else
echo "[action-remark-lint] ${format_str} markdown code using the remark-lint linter..."
remark --frail --quiet \
--use=remark-preset-lint-recommended "${format_arg}" "${INPUT_WORKDIR}/${remark_args}" 2>&1 || reviewdog_error="true"
remark_exit_val="${PIPESTATUS[0]}"
if [[ "${remark_exit_val}" -ne "0" ]]; then
remark_lint_error="true"
elif [[ "${remark_exit_val}" -eq "0" && "${INPUT_FORMAT}" = 'true' ]]; then
echo "[action-remark-lint] Formatting not needed."
fi
echo "::set-output name=is_formatted::false"
fi

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

0 comments on commit 42f4234

Please sign in to comment.