Skip to content

Commit

Permalink
🔥 Removes formatting feature
Browse files Browse the repository at this point in the history
After a discussion with @haya14busa we decided to remove the formatting
action again as this should be left inside a seperate repository
(see reviewdog/action-remark-lint#25). This
repository can be found on https://github.com/rickstaa/action-black.
  • Loading branch information
rickstaa committed Jan 7, 2021
1 parent dbde0e2 commit 2fae880
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 206 deletions.
171 changes: 47 additions & 124 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,75 +9,9 @@

![github-pr-check demo](https://user-images.githubusercontent.com/17570430/102082175-c6773780-3e11-11eb-9af9-d7ee07ca353a.png)

This action runs the [black formatter](https://github.com/psf/black) with 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.
This action runs the [black formatter](https://github.com/psf/black) with reviewdog on pull requests to improve code review experience.

## Inputs

```yaml
inputs:
workdir:
description: "Working directory relative to the root directory. Defaults to '.'."
required: false
default: "."
format:
description: |
If true, black format files and commit are creatable (use other Action).
Defaults to 'false'.
required: false
default: "false"
fail_on_error:
description: |
Exit code when black formatting errors are found [true, false]. Defaults to 'false'.
required: false
default: "false"
black_flags:
description: "Additional black flags."
required: false
default: ""
# Reviewdog related inputs
annotate:
description: "Annotate black 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. Defaults to 'black-format'."
required: false
default: "black-format"
level:
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].
Defaults to 'github-pr-check'.
required: false
default: "github-pr-check"
filter_mode:
description: |
Filtering mode for the reviewdog command [added, diff_context, file, nofilter].
Default is added.
required: false
default: "added"
reviewdog_flags:
description: "Additional reviewdog flags."
required: false
default: ""
```
## Outputs
```yml
outputs:
is_formatted:
description: "Whether the files were formatted using the black formatter."
```
## Basic usage
## Quick start

In it's simplest form this action can be used to annotate the changes the black formatter would make if it was run on the code.

Expand All @@ -100,64 +34,53 @@ jobs:
level: warning
```
## Advanced use cases
## Inputs
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.
### `github_token`

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

```yaml
name: reviewdog
on: [pull_request]
jobs:
name: runner / black
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.head_ref }}
- name: Check files using black formatter
uses: reviewdog/action-black@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
reporter: github-check
level: error
fail_on_error: true
format: true
- name: Commit black formatting results
if: failure()
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: ":art: Format Python code with psf/black push"
```
### `workdir`

### Create pull request
**Optional**. The directory to run remark-lint in. Defaults to `.`.

```yaml
name: reviewdog
on: [pull_request]
jobs:
name: runner / black
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Check files using black formatter
uses: reviewdog/action-black@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 Python code with psf/black push"
commit-message: ":art: Format Python code with psf/black"
body: |
There appear to be some python formatting errors in ${{ github.sha }}. This pull request
uses the [psf/black](https://github.com/psf/black) formatter to fix these issues.
branch: actions/black
```
### `format`

**Optional**. If true, remark-lint format files and commit are creatable (use other Action). Defaults to `false`.

#### `fail_on_error`

**Optional**. Exit code for when remark-lint errors are found \[`true`, `false`]. Defaults to `false`.

#### `black_flags`

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

### `annotate`

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

#### `tool_name`

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

### `level`

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

### `reporter`

**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.

### `filter_mode`

**Optional**. Filtering mode for the reviewdog command \[added, diff_context, file, nofilter]. Defaults to `"added"`.

### `reviewdog_flags`

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

## Format your code

This action is meant to annotate any possible changes that would need to be made to make your code adhere to the [black formatting guidelines](github.com/psf/black). It does not apply these changes to your codebase. If you also want to apply the changes to your repository, you can use the [reviewdog/action-suggester](https://github.com/reviewdog/action-suggester). You can find examples of how this is done can be found in [rickstaa/action-black](https://github.com/rickstaa/action-black/)
13 changes: 0 additions & 13 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ inputs:
description: "Working directory relative to the root directory. Defaults to '.'."
required: false
default: "."
format:
description: |
If true, black format files and commit are creatable (use other Action).
Defaults to 'false'.
required: false
default: "false"
fail_on_error:
description: |
Exit code when black formatting errors are found [true, false]. Defaults to 'false'.
Expand All @@ -22,10 +16,6 @@ inputs:
required: false
default: ""
# Reviewdog related inputs
annotate:
description: "Annotate black changes using reviewdog. Defaults to 'true'."
required: false
default: "true"
github_token:
description: "The automatically created secret github action token."
required: true
Expand Down Expand Up @@ -54,9 +44,6 @@ inputs:
description: "Additional reviewdog flags."
required: false
default: ""
outputs:
is_formatted:
description: "Whether the files were formatted using the black formatter."
runs:
using: "docker"
image: "Dockerfile"
Expand Down
124 changes: 55 additions & 69 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,50 +21,48 @@ fi
# Run black with reviewdog
black_exit_val="0"
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..."
black_check_output=$(black --diff --quiet --check ${black_args[@]}) ||
black_exit_val="$?"
if [[ "${INPUT_REPORTER}" = 'github-pr-review' ]]; then
echo "[action-black] Checking python code with the black formatter and reviewdog..."
black_check_output=$(black --diff --quiet --check ${black_args[@]}) ||
black_exit_val="$?"

# 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
# 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

# Remove '-q' and '--quiet' form the black arguments
# NOTE: Having these flags in the action prevents the action from working.
black_args_tmp=()
for item in "${black_args[@]}"; do
if [[ "${item}" != "-q" && "${item}" != "--quiet" ]]; then
black_args_tmp+=("${item}") #Quotes when working with strings
fi
done
# Remove '-q' and '--quiet' form the black arguments
# NOTE: Having these flags in the action prevents the action from working.
black_args_tmp=()
for item in "${black_args[@]}"; do
if [[ "${item}" != "-q" && "${item}" != "--quiet" ]]; then
black_args_tmp+=("${item}") #Quotes when working with strings
fi
done

echo "[action-black] Checking python code with the black formatter and reviewdog..."
black_check_output=$(black --check ${black_args_tmp[@]} 2>&1) ||
black_exit_val="$?"
echo "[action-black] Checking python code with the black formatter and reviewdog..."
black_check_output=$(black --check ${black_args_tmp[@]} 2>&1) ||
black_exit_val="$?"

# 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 ${black_args[@]} 2>&1 || black_exit_val="$?"
# 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

black_exit_val="2"
reviewdog_exit_val="2"

# Check for black/reviewdog errors
if [[ "${black_exit_val}" -eq "0" && "${reviewdog_exit_val}" -eq "0" ]]; then
black_error="false"
Expand All @@ -78,52 +76,40 @@ elif [[ "${black_exit_val}" -eq "1" && "${reviewdog_exit_val}" -eq "1" ]]; then
elif [[ "${black_exit_val}" -eq "0" && "${reviewdog_exit_val}" -eq "1" ]]; then
black_error="false"
reviewdog_error="true"
elif [[ "${black_exit_val}" -eq "123" && "${reviewdog_exit_val}" -eq "1" ]]; then
black_error="true"
reviewdog_error="true"
echo "[action-black] ERROR: Black found a syntax error when checking the" \
"files (error code: ${black_exit_val})."
elif [[ "${black_exit_val}" -eq "123" && "${reviewdog_exit_val}" -eq "0" ]]; then
black_error="true"
reviewdog_error="false"
echo "[action-black] ERROR: Black found a syntax error when checking the" \
"files (error code: ${black_exit_val})."
else
if [[ "${black_exit_val}" -ne "0" && "${black_exit_val}" -ne "1" && \
if [[ "${black_exit_val}" -eq "123" && "${reviewdog_exit_val}" -ne "0" && \
"${reviewdog_exit_val}" -ne "1" ]]; then
echo "[action-black] ERROR: Black found a syntax error when checking the" \
"files (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})."
elif [[ "${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" \
"reviewdog error annotator (error code: ${reviewdog_exit_val})."
exit 1
fi
fi

# Also format code if this is requested
# NOTE: Useful for writing back changes or creating a pull request.
black_format_exit_val="0"
if [[ "${INPUT_FORMAT,,}" = 'true' && "${black_error}" = 'true' ]]; then
echo "[action-black] Formatting python code using the black formatter..."
black ${black_args[@]} || black_format_exit_val="$?"

# 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})."
exit 1
fi
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"
exit 1
fi

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

0 comments on commit 2fae880

Please sign in to comment.