見出し画像

GithubActionsでreviewdogを飼ってみた

アイミツ開発チームでエンジニアリングをしている deliku です!
前回の記事で、Larastan を導入し、NextActionで CIで実行することを掲げていましたので、今回は実際にそれに取り組んでみた記事になります。

▶︎ 前回の記事

▶︎ reviewdog

reviewdog は、linterなどの実行結果をGitHubのプルリクエストにコメントしてくれるもので、PHPの静的解析ツールであるPHPstanの実行結果を渡すことで、静的解析した結果をコメントしてくれるようになります。

reviewdog provides a way to post review comments to code hosting service, such as GitHub, automatically by integrating with any linter tools with ease. It uses an output of lint tools and posts them as a comment if findings are in diff of patches to review. reviewdog also supports run in the local environment to filter an output of lint tools by diff.

https://github.com/reviewdog/reviewdog

▶︎ GithubActionsでreviewdogを動かしてみる

PullRequestをトリガーに解析するように下記のようなyml .github/workflows/php-stan-check.yml を作成します。(実際に使っている ymlとは少し違います)

name: 'php stan check with reviewdog'

on:
  pull_request:

jobs:
  phpstan:
    runs-on: ubuntu-latest
    env:
      ROOT_DIRECTORY: ./
      LARAVEL_DIRECTORY: ./laravel
    steps:
      - uses: actions/checkout@v2
      - name: Setup PHP version
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.0'
      - name: copy .env for laravel
        run: cp .laravel.local.env ./laravel/.env
        working-directory: ${{env.ROOT_DIRECTORY}}
      - name: composer install
        run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
        working-directory: ${{env.LARAVEL_DIRECTORY}}
      - uses: reviewdog/action-setup@v1
        with:
          reviewdog_version: latest
      - name: run phpstan with reviewdog
        env:
          REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: ./vendor/bin/phpstan analyse --level 0 --error-format=raw --no-progress | reviewdog -reporter=github-pr-review -f=phpstan -filter-mode=file -fail-on-error=true
        working-directory: ${{env.LARAVEL_DIRECTORY}}

解析結果により指摘事項があった場合、slackに通知するようにしています。 

指摘事項があるPullRequestを確認すると、Access to an undefined property を検知していることがわかります。

▶︎ 最後に

PullRequest時点で些細な記述ミスやLinterで指摘するような内容をレビュワーが行うことは効率的でないと感じていたため、仕組みで解決 できるのが一番かなと思い、今回のような取り組みを行なってみました。

▶ 【PR】ユニラボ に興味がある方へ

今回の記事を読んでユニラボに興味を持っていただけた方は、まずはカジュアル面談でざっくりお話させていただければと思います!


この記事が気に入ったらサポートをしてみませんか?