Open12

reviewdogについて学ぶ

Kumamoto-HamachiKumamoto-Hamachi

2. GitHub Actionsでreviewdogを飼って静的解析してみる

https://zenn.dev/peraichi_blog/articles/01fy360dgteynbfv5tj3q6smv5

各種 linter 解析の検出結果を Pull Request にコメントする形で指摘してくれるツールです。
詳しくはreviewdog開発者様の記事を参考にしてください。
reviewdog のうれしい点はなんといっても、 Pull Request にコメント形式での指摘を簡単に実現できる点です。
前述の Sider などでは解析の結果エラーとなった場合に、その内容を知るのに画面遷移が必要でした。
この一手間を削減できるのはかなり大きいと思っています。reviewdog は設定次第で各 CI サービスに組み込むことができます

http://haya14busa.com/reviewdog/

name: reviewdog

on: [pull_request]

jobs:
  reviewdog:
    name: reviewdog
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '7.2.34'
      - uses: reviewdog/action-setup@v1
        with:
          reviewdog_version: latest
      - name: phpstan install
        run: composer require phpstan/phpstan
      - name: run phpstan and reviewdog
        env:
          REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: ./vendor/bin/phpstan analyse --error-format=raw --no-progress | reviewdog -reporter=github-pr-review -f=phpstan
Kumamoto-HamachiKumamoto-Hamachi

3. reviewdogによるコードレビューの省力化

https://qiita.com/tatsuya4559/items/927a6e82d39c78113cbe

「これ、elseいらないけど。でもわざわざ指摘するほどでもないかな、、」
こういうのは事前にlintして潰しておいてほしいと思いつつも、すべてのPRがlinterの指摘を修正しているとは保証できません。
このツールはlinterの指摘を変更のあった行だけに絞ってくれます。
さらににGitHubにコメントしてくれるので指摘を確認しやすいというオマケ付き。

Kumamoto-HamachiKumamoto-Hamachi

4. GitHub Actionsにreviewdogを飼ってみた!(eslint編)

https://dev.classmethod.jp/articles/shuntaka-github-actions-reviewdog/

チェック結果が「エラー」になるコミットは、commitできないのでpushされない。一方で「警告」になるコミットはpushされる設定です。

  • おまけ:masterにpushされたらlintする設定
on:
  push:
    branches:
      - master

name: test frontend sample app
jobs:
  lint:
    name: Lint check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@master
      - uses: actions/setup-node@v1
        with:
          node-version: 12
      - run: yarn install
      - run: yarn lint
  • 警告をプルリクエストのコメントに出力する設定
name: reviewdog
on: [pull_request]
jobs:
  eslint:
    name: runner / eslint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: eslint
        uses: reviewdog/action-eslint@v1
        with:
          github_token: ${{ secrets.github_token }}
          reporter: github-pr-review # Change reporter.
          eslint_flags: 'src/**/*.{ts,tsx}'

reporter: github-pr-reviewがポイントそう

Kumamoto-HamachiKumamoto-Hamachi

5. reviewdogによるGoのコードレビュー

https://swet.dena.com/entry/2018/09/18/142413

一言でいうとコードレビューを補助してくれるコマンドラインツールです。私たちのチームではほとんどがGo言語による開発なため、golintなどの結果を表示するために使っています。似たようなツールであるDangerはPull Request全体に関わるチェックを得意としており、Pull RequestのTitleおよびDescription
Pull Requestのファイル数や行数
Pull Requestに含まれるファイル名
などを元にする時にはとても便利ですが、linterとの連携という点ではreviewdogの方が簡単に使えます。

以下の3点からreviewdogの導入を決めました。

  1. lintの結果をPull Requestのレビューコメントに表示できる
  2. 改修のあった部分のみを警告できる
  3. 任意のlinterを使用できる

reviewdogの使い方

$ go get -u github.com/haya14busa/reviewdog/cmd/reviewdog

Go製のツールなので、もしくはGitHubのRleasesページからダウンロードしたバイナリをPATHの通った場所に配置することでインストールできます。
(CIで利用する場合、バージョンを固定できるこちらが推奨)

インストール後は

$ <linter> | reviewdog [<flags>] (-reporter=github-pr-review | -diff=<diff command>)
Kumamoto-HamachiKumamoto-Hamachi

Input Format errorformat

reviewdog accepts any compiler or linter result from stdin and parses it with scan-f like 'errorformat', which is the port of Vim's errorformat feature.For example, if the result format is {file}:{line number}:{column number}: {message}, errorformat should be %f:%l:%c: %m and you can pass it as -efm arguments.

-efm arguments.

より複雑な設定はreviewdog/errorformat: Vim's quickfix errorformat implementation in Go

エラーフォーマットの実験はこちらのErrorformat Playgroundを使える。

Kumamoto-HamachiKumamoto-Hamachi

Input Format Available pre-defined 'errorformat'

But, you don't have to write 'errorformat' in many cases. reviewdog supports pre-defined errorformat for major tools.
You can find available errorformat name by reviewdog -list and you can use it with -f={name}.

Reviewdog Diagnostic Format (RDFormat)

診断?

This rdformat supports rich feature like multiline ranged comments, severity, rule code with URL, and code suggestions.
複数行の範囲指定コメント、重大度、URL付きルールコード、コードサジェストなどの豊富な機能をサポート

https://github.com/reviewdog/reviewdog#reviewdog-diagnostic-format-rdformat

https://github.com/reviewdog/reviewdog/tree/master/proto/rdf#wire-formats-of-reviewdog-diagnostic-format

Kumamoto-HamachiKumamoto-Hamachi

Input Format Diff

reviewdog supports diff (unified format) as an input format especially useful for code suggestions. reviewdog can integrate with any code suggestions tools or formatters to report suggestions.

Input Format checkstyle format

reviewdog also accepts checkstyle XML format as well. If the linter supports checkstyle format as a report format, you can use -f=checkstyle instead of using 'errorformat'.

https://checkstyle.sourceforge.io/

Code Suggestions

https://github.com/reviewdog/reviewdog#code-suggestions

Kumamoto-HamachiKumamoto-Hamachi

reviewdog config file

ローカルでCIの結果を先に見ておきたいならこれ使えば良さそう。

reviewdog can also be controlled via the .reviewdog.yml configuration file instead of "-f" or "-efm" arguments.
With .reviewdog.yml, you can run the same commands both CI service and local environment including editor integration with ease.

https://github.com/reviewdog/reviewdog#reviewdog-config-file

Kumamoto-HamachiKumamoto-Hamachi

Reporters

https://github.com/reviewdog/reviewdog#reporters

1. Local(デフォ)

reviewdog can find newly introduced findings by filtering linter results using diff. You can pass diff command as -diff arg.

2. GitHub Checks(-reporter=github-pr-check)

github-pr-check reporter reports results to GitHub Checks.

You can change report level for this reporter by level field in config file or -level flag. You can control GitHub status check result with this feature. (default: error)

報告のレベルをフィルター出来る

3. GitHub Checks (-reporter=github-check)

It's basically same as -reporter=github-pr-check except it works not only for Pull Request but also for commit.

プルリク以外でも動く

4. GitHub PullRequest review comment (-reporter=github-pr-review)

github-pr-review reporter reports results to GitHub PullRequest review comments using GitHub Personal API Access Token. GitHub Enterprise is supported too.

レビューにつく。有料版じゃないとつかないっぽい?

GitLabのは省略

Kumamoto-HamachiKumamoto-Hamachi

GitHub Actionsと

name: reviewdog
on: [pull_request]
jobs:
  reviewdog:
    name: reviewdog
    runs-on: ubuntu-latest
    steps:
      # ...
      - uses: reviewdog/action-setup@v1
        with:
          reviewdog_version: latest # Optional. [latest,nightly,v.X.Y.Z]
      - name: Run reviewdog
        env:
          REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          reviewdog -reporter=github-pr-check -runners=golint,govet
          # or
          reviewdog -reporter=github-pr-review -runners=golint,govet

https://github.com/reviewdog/action-eslint

差分の対象になっていない範囲に lint エラーがあってもコメントは入りません。ただ、filter_mode を nofilter にしておけば Github Actions の Annotations のところには出してくれるようです。

https://zenn.dev/ikawaha/articles/57384e8fc69c7b057f7f

https://github.com/reviewdog/reviewdog#filter-mode