【Github】reviewdog ~ 入門編 ~

■ はじめに

reviewdog を触ってみる

目次

【1】reviewdog
【2】reviewdog の使い方
 1)環境変数「REVIEWDOG_GITHUB_API_TOKEN」
 2)Github Actions への実装 ~ 基本 ~
【3】reviewdog コマンド
【4】サンプル
 例1:Hello world

【1】reviewdog

作者の方のブログ より抜粋

http://haya14busa.com/reviewdog/

linter などのチェックツールの結果を自動で GitHub の Pull Request にコメントしたり
ローカルでも diff の結果から新たに導入されたエラーだけを表示するように
フィルタリングできるツール

Github
https://github.com/orgs/reviewdog/repositories?type=all

【2】reviewdog の使い方

1)環境変数「REVIEWDOG_GITHUB_API_TOKEN」

* GITHUB_TOKEN シークレットを 
 環境変数「REVIEWDOG_GITHUB_API_TOKEN」に設定
* その際、「permissions」で「pull-requests: write」を付加しておく
 => GITHUB_TOKEN の詳細については、以下の関連記事を参照のこと

https://dk521123.hatenablog.com/entry/2024/04/22/221027

サンプル

jobs:
  demo-pull-request:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
    steps:
      env:
        REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - uses: reviewdog/action-setup@v1
        with:
          reviewdog_version: latest

2)Github Actions への実装 ~ 基本 ~

* 基本は、以下の2Stepで使える
 => この方法以外もある(詳細は、以下の関連記事を参照)

https://dk521123.hatenablog.com/entry/2024/04/18/161200
Step1: Install reviewdog

      - uses: reviewdog/action-setup@v1
        with:
          reviewdog_version: latest

Step2: Run linter(s) and reviewdog

      - name: Lint with pylint
        run: |
          pylint --rcfile=~/.pylintrc *.py | reviewdog -efm="%f:%l:%c: %m" -reporter=github-pr-review

[a] -efm: errorformat

* 今回は、Input Format の内の「errorformat」を使っている

https://github.com/reviewdog/reviewdog?tab=readme-ov-file#errorformat

-efm="%f:%l:%c: %m"
 => {file}:{line number}:{column number}: {message}
 => 以下「pylint のエラーメッセージ例」と比較すると分かりやすいかも

pylint のエラーメッセージ例

demo1.py:5:0: C0304: Final newline missing (missing-final-newline)

[b] -reporter: Reporter
https://github.com/reviewdog/reviewdog?tab=readme-ov-file#reporters

* -reporter=local: Local
* -reporter=github-pr-check: GitHub Checks
* -reporter=github-check: GitHub Checks
* -reporter=github-pr-review: GitHub PullRequest review comment
* -reporter=gitlab-mr-discussion: GitLab MergeRequest discussions
* -reporter=gitlab-mr-commit: GitLab MergeRequest commit
* -reporter=bitbucket-code-report: Bitbucket Code Insights Reports

【3】reviewdog コマンド

* reviewdog コマンドについては、資料がないので、
 「reviewdog -help」や「reviewdog -list」をみた方がいい

reviewdog -help

reviewdog -list

reviewdog -name="sqlfluff" -tee -fail-on-error -f=rdjson -reporter=github-pr-review
Options Explanations
-name="Name" ツール名を指定する
-tee tee コマンド(標準出力+ファイル出力)を指定
-fail-on-error WARNING/ERROR時にexitコードで「1」を返す
-f ="FormatName" フォーマット名(reviewdog -listで確認)を指定
-efm="ErrorFormat" エラーフォーマットを指定 (e.g. %f:%l:%c: %m)
-reporter="Reporter" どういう出力にするかを指定

【4】サンプル

例1:Hello world

.github/workflows/demo_reviewdogs.yml

name: DemoForReviewdogs

on:
  - pull_request
env:
  REVIEWDOG_GITHUB_API_TOKEN: "${{ secrets.REVIEWDOG_GITHUB_API_TOKEN }}"
jobs:
  demo-job:
    name: lint
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
    steps:
      env:
        REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Run checkout
        uses: actions/checkout@v4
      - name: Setup Python
        uses: actions/setup-python@v4
        with:
            python-version: '3.10'
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install pylint
          pylint --generate-rcfile > ~/.pylintrc
      # Step1: Install reviewdog
      - uses: reviewdog/action-setup@v1
        with:
          reviewdog_version: latest
      # Step2: Run linter(s) and reviewdog
      - name: Lint with pylint
        run: |
          pylint --rcfile=~/.pylintrc *.py | reviewdog -efm="%f:%l:%c: %m" -reporter=github-pr-review

demo.py

def func(is_true):
    if is_true:
        return 'Hello world!'
    else:
        return None

参考文献

https://buildersbox.corp-sansan.com/entry/2023/12/13/110000
https://kujilabo.com/2023/03/13/2023/03/python_003/

関連記事

reviewdog ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2024/04/18/161200
reviewdog ~ RDFormat ~
https://dk521123.hatenablog.com/entry/2024/04/19/121312
Github ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/07/18/234652
Github Actions ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2021/11/04/142835
Github Actions ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2022/06/16/151443
Github Actions ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2023/12/22/195715
Github Actions ~ pull_request / pull_request_target ~
https://dk521123.hatenablog.com/entry/2024/04/10/152101
Github Actions ~ Python関連 ~
https://dk521123.hatenablog.com/entry/2022/06/21/143624
Github Actions ~ GITHUB_TOKEN / permissions ~
https://dk521123.hatenablog.com/entry/2024/04/22/221027
GitHub CLI ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2024/02/17/233836
Python解析ツール ~ Ruff ~
https://dk521123.hatenablog.com/entry/2024/03/13/000021