From 78f191931bdf6096179409b12901e552d7d1002a Mon Sep 17 00:00:00 2001 From: rickstaa Date: Mon, 28 Dec 2020 16:30:40 +0100 Subject: [PATCH 1/5] :sparkles: Adds the ability to automatically format the code This commit adds one big feature and several small fixes. Features: - Adds formatting support Small changes: - Shell was changed from dash to bash - The remark_flags argument was replaced with the often used args argument. --- CHANGELOG.md | 32 +++++++-------- Dockerfile | 4 +- README.md | 105 +++++++++++++++++++++++++++++++++----------------- action.yml | 42 +++++++++++--------- entrypoint.sh | 69 ++++++++++++++++++++++++++------- 5 files changed, 169 insertions(+), 83 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f4f191..6ba1e56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,34 +8,34 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). > 13 December 2020 -- :wrench: Updates dockerfile reviewdog install to nightly branch [`#5`](https://github.com/rickstaa/action-remark-lint/pull/5) -- Add --frail argument to remark to exit with 1 on warnings [`#3`](https://github.com/rickstaa/action-remark-lint/pull/3) -- Fix reviewdog exit code bug and error format syntax problem [`#4`](https://github.com/rickstaa/action-remark-lint/pull/4) -- Change version and reporter [`#1`](https://github.com/rickstaa/action-remark-lint/pull/1) -- :see_no_evil: Updates gitignore [`2b9ef56`](https://github.com/rickstaa/action-remark-lint/commit/2b9ef565dc342eb7b01170c22d0c8efbf26adda5) -- :sparkles: Adds reviewdog ci actions, testdata and removes github-pr-review warning [`3fe02eb`](https://github.com/rickstaa/action-remark-lint/commit/3fe02eb8f74cba56966ea63532354e99f5b23cb1) -- :sparkles: Adds additional reviewdog arguments and updates documentation [`a4425dc`](https://github.com/rickstaa/action-remark-lint/commit/a4425dccec04b3eb28d9756b49227aef7e630aed) +* :wrench: Updates dockerfile reviewdog install to nightly branch [`#5`](https://github.com/rickstaa/action-remark-lint/pull/5) +* Add --frail argument to remark to exit with 1 on warnings [`#3`](https://github.com/rickstaa/action-remark-lint/pull/3) +* Fix reviewdog exit code bug and error format syntax problem [`#4`](https://github.com/rickstaa/action-remark-lint/pull/4) +* Change version and reporter [`#1`](https://github.com/rickstaa/action-remark-lint/pull/1) +* :see_no_evil: Updates gitignore [`2b9ef56`](https://github.com/rickstaa/action-remark-lint/commit/2b9ef565dc342eb7b01170c22d0c8efbf26adda5) +* :sparkles: Adds reviewdog ci actions, testdata and removes github-pr-review warning [`3fe02eb`](https://github.com/rickstaa/action-remark-lint/commit/3fe02eb8f74cba56966ea63532354e99f5b23cb1) +* :sparkles: Adds additional reviewdog arguments and updates documentation [`a4425dc`](https://github.com/rickstaa/action-remark-lint/commit/a4425dccec04b3eb28d9756b49227aef7e630aed) #### [v0.0.5](https://github.com/rickstaa/action-remark-lint/compare/v0.0.4...v0.0.5) > 25 January 2020 -- Re-fixed args passed to remark (what a painful piece of software) [`abb616b`](https://github.com/rickstaa/action-remark-lint/commit/abb616bd9d9ec698d38bb680e5caa4c16f6380ff) -- Revert "Added --no-stdout --no-color args to remark call" [`3c2e844`](https://github.com/rickstaa/action-remark-lint/commit/3c2e844aaa86e3bbe96a93bebd3f44b629c37e10) +* Re-fixed args passed to remark (what a painful piece of software) [`abb616b`](https://github.com/rickstaa/action-remark-lint/commit/abb616bd9d9ec698d38bb680e5caa4c16f6380ff) +* Revert "Added --no-stdout --no-color args to remark call" [`3c2e844`](https://github.com/rickstaa/action-remark-lint/commit/3c2e844aaa86e3bbe96a93bebd3f44b629c37e10) #### [v0.0.4](https://github.com/rickstaa/action-remark-lint/compare/v0.0.3...v0.0.4) > 25 January 2020 -- Added --no-stdout --no-color args to remark call [`4d68733`](https://github.com/rickstaa/action-remark-lint/commit/4d687330c3b96a6261a3426878c584059bb7ff5d) +* Added --no-stdout --no-color args to remark call [`4d68733`](https://github.com/rickstaa/action-remark-lint/commit/4d687330c3b96a6261a3426878c584059bb7ff5d) #### [v0.0.3](https://github.com/rickstaa/action-remark-lint/compare/v0.0.2...v0.0.3) > 25 January 2020 -- Revert "Added Makefile and release script" [`e078250`](https://github.com/rickstaa/action-remark-lint/commit/e0782504aad6bc4bd4970a6d236e13254d2a1fac) -- Added Makefile and release script [`f4ca24a`](https://github.com/rickstaa/action-remark-lint/commit/f4ca24a8b145313efa4c88e749fecc01e33b49f7) -- Fixed remark CLI usage [`09a9260`](https://github.com/rickstaa/action-remark-lint/commit/09a9260dcbc40e639ad089664070a6ea9be63e04) +* Revert "Added Makefile and release script" [`e078250`](https://github.com/rickstaa/action-remark-lint/commit/e0782504aad6bc4bd4970a6d236e13254d2a1fac) +* Added Makefile and release script [`f4ca24a`](https://github.com/rickstaa/action-remark-lint/commit/f4ca24a8b145313efa4c88e749fecc01e33b49f7) +* Fixed remark CLI usage [`09a9260`](https://github.com/rickstaa/action-remark-lint/commit/09a9260dcbc40e639ad089664070a6ea9be63e04) #### [v0.0.2](https://github.com/rickstaa/action-remark-lint/compare/v0.0.1...v0.0.2) @@ -53,6 +53,6 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). > 25 January 2020 -- Initial Commit [`9427de7`](https://github.com/rickstaa/action-remark-lint/commit/9427de75ca49d1c667e37c01974f46391dea78f0) -- Revert "Added Makefile and release script" [`e078250`](https://github.com/rickstaa/action-remark-lint/commit/e0782504aad6bc4bd4970a6d236e13254d2a1fac) -- Added Makefile and release script [`f4ca24a`](https://github.com/rickstaa/action-remark-lint/commit/f4ca24a8b145313efa4c88e749fecc01e33b49f7) +* Initial Commit [`9427de7`](https://github.com/rickstaa/action-remark-lint/commit/9427de75ca49d1c667e37c01974f46391dea78f0) +* Revert "Added Makefile and release script" [`e078250`](https://github.com/rickstaa/action-remark-lint/commit/e0782504aad6bc4bd4970a6d236e13254d2a1fac) +* Added Makefile and release script [`f4ca24a`](https://github.com/rickstaa/action-remark-lint/commit/f4ca24a8b145313efa4c88e749fecc01e33b49f7) diff --git a/Dockerfile b/Dockerfile index 7c401e5..606f79e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,9 @@ FROM prologic/remark-lint:latest ENV REVIEWDOG_VERSION=v0.11.0-nightly20201213+85edbc6 -RUN wget -O - -q https://raw.githubusercontent.com/reviewdog/nightly/master/install.sh| sh -s -- -b /usr/local/bin/ ${REVIEWDOG_VERSION} +RUN apt-get install bash + +RUN wget -O - -q https://raw.githubusercontent.com/reviewdog/nightly/master/install.sh | sh -s -- -b /usr/local/bin/ ${REVIEWDOG_VERSION} RUN apk --no-cache -U add git diff --git a/README.md b/README.md index 443d45b..abca031 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ [![reviewdog](https://github.com/reviewdog/action-remark-lint/workflows/reviewdog/badge.svg)](https://github.com/reviewdog/action-remark-lint/actions?query=workflow%3Areviewdog) [![depup](https://github.com/reviewdog/action-remark-lint/workflows/depup/badge.svg)](https://github.com/reviewdog/action-remark-lint/actions?query=workflow%3Adepup) [![release](https://github.com/reviewdog/action-remark-lint/workflows/release/badge.svg)](https://github.com/reviewdog/action-remark-lint/actions?query=workflow%3Arelease) -[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/reviewdog/action-remark-lint?logo=github&sort=semver)](https://github.com/reviewdog/action-remark-lint/releases) -[![action-bumpr supported](https://img.shields.io/badge/bumpr-supported-ff69b4?logo=github&link=https://github.com/haya14busa/action-bumpr)](https://github.com/haya14busa/action-bumpr) +[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/reviewdog/action-remark-lint?logo=github\&sort=semver)](https://github.com/reviewdog/action-remark-lint/releases) +[![action-bumpr supported](https://img.shields.io/badge/bumpr-supported-ff69b4?logo=github\&link=https://github.com/haya14busa/action-bumpr)](https://github.com/haya14busa/action-bumpr) ![action screenshot](https://user-images.githubusercontent.com/17570430/102060312-4ee5e000-3df2-11eb-8c82-767afeccd8db.png) ![action screenshot](https://user-images.githubusercontent.com/17570430/102059912-d3842e80-3df1-11eb-9b0a-2e04eab5e294.png) @@ -43,29 +43,25 @@ See the Inputs section below for details on the defaults and optional configurat **Optional**. The directory to run remark-lint in. Default is `.`. -### remark_flags - -**Optional**. Additional flags you want to pass to remark-lint. Default is ` `. - ### `level` -**Optional**. Report level for reviewdog \[info, warning, error\]. +**Optional**. Report level for reviewdog \[info, warning, error]. It's same as `-level` flag of reviewdog. ### `reporter` -**Optional**. Reporter of reviewdog command \[github-pr-check, github-pr-review, github-check\]. +**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. **NB:** Only `github-pr-check` is supported currently. #### `filter_mode` -**Optional**. Filtering mode for the reviewdog command \[added, diff_context, file, nofilter\]. Default = `"added"`. +**Optional**. Filtering mode for the reviewdog command \[added, diff_context, file, nofilter]. Default = `"added"`. #### `fail_on_error` -**Optional**. Exit code for reviewdog when errors are found \[`true`, `false`\]. Default = `false`. +**Optional**. Exit code for reviewdog when errors are found \[`true`, `false`]. Default = `false`. #### `reviewdog_flags` @@ -75,36 +71,75 @@ Default is github-pr-check. github-pr-review can use Markdown and add a link to **Optional**. Tool name to use for reviewdog reporter. Default = `remark-lint`. -## Development - -### Release - -#### [haya14busa/action-bumpr](https://github.com/haya14busa/action-bumpr) +## Docker input args -You can bump version on merging Pull Requests with specific labels (bump:major,bump:minor,bump:patch). -Pushing tag manually by yourself also work. +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). -#### [haya14busa/action-update-semver](https://github.com/haya14busa/action-update-semver) - -This action updates major/minor release tags on a tag push. e.g. Update v1 and v1.2 tag when released v1.2.3. -ref: - -### Lint - reviewdog integration - -This reviewdog action template itself is integrated with reviewdog to run lints -which is useful for Docker container based actions. +```yaml +runs: + using: 'docker' + image: 'Dockerfile' + args: ". --verbose" +``` -![reviewdog integration](https://user-images.githubusercontent.com/3797062/72735107-7fbb9600-3bde-11ea-8087-12af76e7ee6f.png) +## Advance use cases -Supported linters: +This action can be combined with [peter-evans/create-pull-request](https://github.com/peter-evans/create-pull-request) or [stefanzweifel/git-auto-commit-action](https://github.com/stefanzweifel/git-auto-commit-action) to also apply the annotated changes to the repository. -- [reviewdog/action-shellcheck](https://github.com/reviewdog/action-shellcheck) -- [reviewdog/action-hadolint](https://github.com/reviewdog/action-hadolint) -- [reviewdog/action-misspell](https://github.com/reviewdog/action-misspell) +### Commit changes -### Dependencies Update Automation +```yaml +name: reviewdog +on: [pull_request] +jobs: + name: runner / remark-lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + ref: ${{ github.head_ref }} + - name: Check files using remark-lint linter + uses: reviewdog/action-remark-lint@v1 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + reporter: github-check + level: error + fail_on_error: true + format: true + - name: Commit remark-lint formatting results + if: failure() + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: ":art: Format markdown code with remark-lint push" +``` -This repository uses [reviewdog/action-depup](https://github.com/reviewdog/action-depup) to update -reviewdog version. +### Create pull request -[![reviewdog depup demo](https://user-images.githubusercontent.com/3797062/73154254-170e7500-411a-11ea-8211-912e9de7c936.png)](https://github.com/reviewdog/action-template/pull/6) +```yaml +name: reviewdog +on: [pull_request] +jobs: + name: runner / remark-lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Check files using remark-lint linter + uses: reviewdog/action-remark-lint@v1 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + reporter: github-check + level: error + fail_on_error: true + format: true + - name: Create Pull Request + if: failure() + uses: peter-evans/create-pull-request@v3 + with: + token: ${{ secrets.GITHUB_TOKEN }} + title: "Format markdown code with remark-lint linter" + commit-message: ":art: Format markdown code with remark-lint linter" + body: | + There appear to be some python formatting errors in ${{ github.sha }}. This pull request + uses the [remark-lint](https://github.com/remarkjs/remark-lint) linter to fix these issues. + branch: actions/remark-lint +``` diff --git a/action.yml b/action.yml index 0880fc6..1ba02fb 100644 --- a/action.yml +++ b/action.yml @@ -2,30 +2,42 @@ name: "Run remark-lint with reviewdog" description: "🐶 Run remark-lint with reviewdog on pull requests to improve code review experience." author: "prologic" inputs: - github_token: - description: "GITHUB_TOKEN." - required: true workdir: description: "Working directory relative to the root directory." required: false default: "." - remark_flags: - description: "Additional flags for remark-lint" + format: + description: | + If true, remark-lint format files and commit are creatable (use other Action). + Defaults to 'false'. required: false - default: "" + default: "false" + fail_on_error: + description: | + Exit code for reviewdog when errors are found [true, false]. Defaults to 'false'. + required: false + default: "false" + # Reviewdog related inputs + annotate: + description: "Annotate remark-lint changes using reviewdog. Defaults to 'true'." + required: false + default: "true" + github_token: + description: "The automatically created secret github action token." + required: true + default: ${{ github.token }} tool_name: - description: "Tool name to use for reviewdog reporter" + description: "Tool name to use for reviewdog reporter. Defaults to 'black-format'." required: false default: "remark-lint" level: - description: "Report level for reviewdog [info,warning,error]" + description: "Report level for reviewdog [info, warning, error]. Defaults to 'error'." required: false default: "error" reporter: description: | - 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. + Reporter of reviewdog command [github-pr-check, github-pr-review, github-check]. + Defaults to 'github-pr-check'. required: false default: "github-pr-check" filter_mode: @@ -34,14 +46,8 @@ inputs: Default is added. required: false default: "added" - fail_on_error: - description: | - Exit code for reviewdog when errors are found [true, false] - Default is `false`. - required: false - default: "false" reviewdog_flags: - description: "Additional reviewdog flags" + description: "Additional reviewdog flags." required: false default: "" runs: diff --git a/entrypoint.sh b/entrypoint.sh index 5ded20f..4ab9dbe 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,19 +1,62 @@ -#!/bin/sh +#!/bin/bash set -e # Exit immediately if a command exits with a non-zero status -if [ -n "${GITHUB_WORKSPACE}" ] ; then - cd "${GITHUB_WORKSPACE}/${INPUT_WORKDIR}" || exit +if [[ -n "${GITHUB_WORKSPACE}" ]]; then + cd "${GITHUB_WORKSPACE}" || exit fi export REVIEWDOG_GITHUB_API_TOKEN="${INPUT_GITHUB_TOKEN}" -remark --frail --quiet --use=remark-preset-lint-recommended . ${INPUT_REMARK_FLAGS} 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:-github-pr-check}"\ - -filter-mode="${INPUT_FILTER_MODE}" \ - -fail-on-error="${INPUT_FAIL_ON_ERROR}" \ - -level="${INPUT_LEVEL}" \ - -tee \ - ${INPUT_REVIEWDOG_FLAGS} \ No newline at end of file +# If no arguments are given use current working directory +if [[ "$#" -eq 0 ]]; then + remark_args="." +else + remark_args="$*" +fi + +# Check if formatting is requested +if [[ "${INPUT_FORMAT}" = 'true' ]]; then + format_str="Format" + format_arg="-o" +else + format_str="Check" + format_arg="" +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." + fi +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 +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 + exit 1 +fi From e7fd133786db44ab1ca6b90ea450423a3e4886d7 Mon Sep 17 00:00:00 2001 From: rickstaa Date: Mon, 28 Dec 2020 16:33:54 +0100 Subject: [PATCH 2/5] :memo: Updates README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index abca031..2cf5826 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,7 @@ ![action screenshot](https://user-images.githubusercontent.com/17570430/102060312-4ee5e000-3df2-11eb-8c82-767afeccd8db.png) ![action screenshot](https://user-images.githubusercontent.com/17570430/102059912-d3842e80-3df1-11eb-9b0a-2e04eab5e294.png) -This action runs [remark-lint](https://github.com/remarkjs/remark-lint) with [reviewdog](https://github.com/reviewdog/reviewdog) on pull requests to improve -code review experience. +This action runs [remark-lint](https://github.com/remarkjs/remark-lint) with [reviewdog](https://github.com/reviewdog/reviewdog) on pull requests to improve code review experience. It can be used to format your code and/or annotate possible changes that would be made during this formatting. ## Quickstart From d7e5d999329caecf6c16f1a67ae7931f59811ef0 Mon Sep 17 00:00:00 2001 From: rickstaa Date: Mon, 28 Dec 2020 16:48:56 +0100 Subject: [PATCH 3/5] :bug: Fixes DockerFile syntax error --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 606f79e..a1a0d0f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM prologic/remark-lint:latest ENV REVIEWDOG_VERSION=v0.11.0-nightly20201213+85edbc6 -RUN apt-get install bash +RUN apk add --no-cache bash RUN wget -O - -q https://raw.githubusercontent.com/reviewdog/nightly/master/install.sh | sh -s -- -b /usr/local/bin/ ${REVIEWDOG_VERSION} From 42f42342897f0c818df6227ddd12ee5bf0e15942 Mon Sep 17 00:00:00 2001 From: rickstaa Date: Fri, 1 Jan 2021 16:00:42 +0100 Subject: [PATCH 4/5] :art: Seperates linting and formatting steps The linting and formatting steps are now seperated such that the formatting output does not get passed to the reviewdog error parser. --- README.md | 54 ++++++++++---------- action.yml | 9 +++- entrypoint.sh | 133 ++++++++++++++++++++++++++++++++++---------------- 3 files changed, 129 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index 2cf5826..5dc8eac 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/action.yml b/action.yml index 1ba02fb..24e4233 100644 --- a/action.yml +++ b/action.yml @@ -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: @@ -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'." @@ -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" diff --git a/entrypoint.sh b/entrypoint.sh index 4ab9dbe..bde0e01 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -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 From a57ccbdce2da14863628bdb24075f93f8917441c Mon Sep 17 00:00:00 2001 From: rickstaa Date: Sat, 2 Jan 2021 13:40:44 +0100 Subject: [PATCH 5/5] :green_heart: Updates gh-action test to test formatting option --- .github/workflows/test.yml | 66 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9713ce5..a9351cb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,6 +15,27 @@ jobs: github_token: ${{ secrets.github_token }} reporter: github-check level: info + workdir: ./testdata/ + + test-check-format: + name: runner / markdown-lint (github-check-format) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: ./ + with: + github_token: ${{ secrets.github_token }} + reporter: github-check + level: info + format: true + workdir: ./testdata/ + - name: Check if formatting was successful + run: | + changed_files=$(git status --porcelain --untracked-files=no | wc -l) + if [[ ${changed_files} -eq 0 ]]; then + echo "No changes detected!" + exit 1 + fi test-pr-check: if: github.event_name == 'pull_request' @@ -27,6 +48,28 @@ jobs: github_token: ${{ secrets.github_token }} reporter: github-pr-check level: warning + workdir: ./testdata/ + + test-pr-check-format: + if: github.event_name == 'pull_request' + name: runner / markdown-lint (github-pr-check-format) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: ./ + with: + github_token: ${{ secrets.github_token }} + reporter: github-pr-check + level: warning + format: true + workdir: ./testdata/ + - name: Check if formatting was successful + run: | + changed_files=$(git status --porcelain --untracked-files=no | wc -l) + if [[ ${changed_files} -eq 0 ]]; then + echo "No changes detected!" + exit 1 + fi test-pr-review: if: github.event_name == 'pull_request' @@ -40,3 +83,26 @@ jobs: reporter: github-pr-review level: error reviewdog_flags: -filter-mode=file -fail-on-error + workdir: ./testdata/ + + test-pr-review-format: + if: github.event_name == 'pull_request' + name: runner / markdown-lint (github-pr-review-format) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: ./ + with: + github_token: ${{ secrets.github_token }} + reporter: github-pr-review + level: error + reviewdog_flags: -filter-mode=file -fail-on-error + format: true + workdir: ./testdata/ + - name: Check if formatting was successful + run: | + changed_files=$(git status --porcelain --untracked-files=no | wc -l) + if [[ ${changed_files} -eq 0 ]]; then + echo "No changes detected!" + exit 1 + fi \ No newline at end of file