reviewdog
JenkinsのようなCIツールで実行したツールの吐き出したコードの解析結果を食わせると、GitHubのコメントとしてつけてくれる
コードレビューの自動化につかう
PHPStanなどと組み合わせて使う
CircleCIなどのCIサービスにも対応している
GitHub Actionsも提供している(サーバは作者のhaya14busaさんが運営している)
haya14busa/reviewdog: Automated code review tool integrated with any code analysis tools regardless of programming language
2019-04-25 メンテが止まっていたが、organizationに移動するなど動きがあった
Looking for maintainers · Issue #196 · reviewdog/reviewdog
試す(2018-10-30)
最新のリリースバイナリを落とす
Releases · haya14busa/reviewdog
64bit macOSなら https://github.com/reviewdog/reviewdog#macos
2019-11-06 Homebrew出てた
https://github.com/reviewdog/reviewdog#homebrew--linuxbrew
ビルトインエラーフォーマット
reviewdog -list
eslintやPHPStanに対応している
Homestead上でphp-cs-fixerを実行し、ローカルのreviewdogに食べさせる
php-cs-fixerはcheckstyleフォーマットに対応している
code:zsh
vagrant ssh -c "cd code && ./vendor/bin/php-cs-fixer fix --dry-run --diff --format=checkstyle app/User.php"
考え方
reviewdogのコアの機能は
PHPStanなどのLinterで指摘された行数が
自分のコードの変更(Gitのdiff)に
一致する箇所の情報を取る
この情報を使って通知をする
ハマり
Larastan + reviewdog + CircleCIをやろうとしてハマったこと
PHPStanモード(-f=phpstan)で実行する場合、reviewdogでは--error-format=rawが期待されている
https://github.com/haya14busa/errorformat/pull/22/files
reviewdog -listでも確認できる
vagrant上のPHPStanの実行をlocalのreviewdogに食わせることはできない
vagrantの出力のpathとローカルでreviewdogが推定するpathが違うので動かない例
code:zsh
vagrant ssh 470c5f7 -c "cd code && ./vendor/bin/phpstan analyze -c phpstan.neon --error-format raw app/User.php" | sed -e '1,2d' | reviewdog -f=phpstan -diff="git diff master"
(動作例)vagrant上にreviewdogを動かしてこんな感じにすれば動く
code:vagrant上で
vagrant@homestead:~/code$ ./vendor/bin/phpstan analyse --error-format=raw --no-progress | ./bin/reviewdog -f=phpstan -name="PHPSTAN" -diff="git diff master"
動作確認のコツ
./vendor/bin/phpstan analyse --error-format=raw --no-progressの結果を見て、指摘されている業のコードに変更を加えてから上のコマンドを実行する
php-cs-fixerのcheckstyleはそのまま食わせることができない
checkstyle形式だからみんなおんなじだと思った?
前提知識
reviewdogの期待するcheckstyleはこのような形式
問題
PHP CS Fixerは出力をcheckstyleにすることができるが、そのときこのような出力になる
code:zsh
$ ./vendor/bin/php-cs-fixer fix --dry-run --diff --format=checkstyle app/User.php
// 出力
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
<file name="app/User.php">
<error severity="warning" source="PHP-CS-Fixer.function_declaration" message="Found violation(s) of type: function_declaration"/>
<error severity="warning" source="PHP-CS-Fixer.visibility_required" message="Found violation(s) of type: visibility_required"/>
<error severity="warning" source="PHP-CS-Fixer.braces" message="Found violation(s) of type: braces"/>
</file>
</checkstyle>
Why?
php-cs-fixerにcheckstyleが追加された経緯を追ってみる
Add checkstyle output format · Issue #2889 · FriendsOfPHP/PHP-CS-Fixer
追加されたときのPRを見ると、スキーマとしてはcolumnやlineがあるが、生成時にそれらを入れていないためにこういう出力になっていることがわかる
軽く見た限りでは、そもそもphp-cs-fixerは行の情報などを持っていないっぽいkadoyau.icon
ちなみにPHPStanもcheckstyle形式で吐き出すことができて、こちらはうまくいく
code:zsh
$ ./vendor/bin/phpstan analyze --error-format checkstyle --no-progress -c phpstan.neon app/User.php
// 出力
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
<file name="User.php">
<error line="45" column="1" severity="error" message="Undefined variable: $a" />
</file>
</checkstyle>