Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add formatting feature #25

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 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}

RUN apk --no-cache -U add git

Expand Down
108 changes: 71 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
[![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)

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

Expand Down Expand Up @@ -43,29 +42,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`

Expand All @@ -75,36 +70,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: <https://help.github.com/en/articles/about-actions#versioning-your-action>

### 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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not available for this action users, but just for the developer of this action.
Can you remove it or do you think it's useful for users?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@haya14busa Thanks a lot for pointing this out I will try to create another way for users to add additional flags.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@haya14busa I removed this option from the readme. The user now can supply additional arguments using the remarks_flags argument.

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this feature or use reviewdog -f=diff instead to support code suggestion instead of committing whole changes w/o reviewdog?

If it just runs the formatter and don't need reviewdog, you should create a different action which doesn't require reviewdog and users should use the action instead.

You can refer to https://github.com/reviewdog/action-suggester

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@haya14busa I now changed the action such that the formatting action is seperate from the annotation action. As a result, the formatting is only done when the user explicitly asks for the files to be formatted. Consequently, review dog only receives the unformatted output. What do you think about this implementation?

Copy link
Member Author

@rickstaa rickstaa Jan 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@haya14busa I agree that committing back the changes to the pull request or repository is bad practice. I use this feature to format + annotate the code while also creating a pull-request containing the formatted code. We can also remove the stefanzweifel/git-auto-commit-action action from the README and keep the peter-evans/create-pull-request action.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, it's fine to create an action that format the file and use commit / pull-request actions, but in this case, you don't need to use reviewdog, so it should be better to create a separate repo.
Users can just install or run simple formatted action and then use commit / pull-request actions instead of using this reviewdog action.

My second point is that reviewdog supports code-suggestion based on diff (see action-suggester for example as written above), so if you want to support this feature, I'm very much welcome to have such feature in reviewdog actions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@haya14busa Ah I see! I will revert the formatting related changes (both in this repository and in the action-black repository) and only add the new remark_flags argument. I will try out if I can use the https://github.com/reviewdog/action-suggester with my actions and add a small note to this repository pointing to that action if people want to also autoformat their code.


- [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
```
42 changes: 24 additions & 18 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
69 changes: 56 additions & 13 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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}
# 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