1
1
お題は不問!Qiita Engineer Festa 2024で記事投稿!
Qiita Engineer Festa20242024年7月17日まで開催中!

【GitHub Action】GoのカバレッジをPRにコメントする

Last updated at Posted at 2024-07-06

はじめに

GoのカバレッジをPRにコメントする方法を2つご紹介したいと思います。

CodeCov

出力したカバレッジを可視化してPull Requestにコメントします。
Go以外にも色々な言語に対応しており、出力すると以下のようにPRのコメントが追加されます。

image.png

ちなみに、複数ユーザでの使用など条件によっては料金がかかります。

設定ファイルはcodecov.ymlに記述し、自分は以下のように設定しています。

codecov.yml
coverage:
  status:
    project:
      default:
        informational: true
    patch:
      default:
        informational: true

comment:
  layout: "reach, diff, flags, files, header, footer"
  behavior: default
  require_changes: false
  require_base: true
  require_head: true
  hide_project_coverage: false

自分は閾値など設定してしまうと、不要なコードを削除するだけのPull RequestだとActionが落ちてしまうので、informational: trueの箇所で失敗しないようにしております。

自分は少しハマったのですが、設定ファイルの設定値が間違っていた場合は、エラーがGitHub Action上で確認できません。CodeCovのHP上では確認できます。また、開発環境で以下を叩くことで、設定ファイルが有効化を確認できます。

curl --data-binary @codecov.yml https://codecov.io/validate

設定値については【Codecov】codecov.ymlの設定についてのあれこれが参考になりました。

GitHub Actionで動かすには、Codec GitHub app で自分のリポジトリにインストール。次に、Codecovにログインして、secretsCODECOV_TOKEN を登録する必要があります。

ワークフローは以下のようになります。

  - uses: actions/checkout@v4
    with:
      ref: ${{ github.event.pull_request.head.sha }}
  - uses: actions/setup-go@v5
    with:
      go-version-file: go.mod
  - name: Run Tests
    run: go list -f '{{.Dir}}/...' -m | xargs go test -v -race -coverprofile=coverage.out -covermode=atomic
  - name: Upload coverage to Codecov
    uses: codecov/codecov-action@v4
    with:
      fail_ci_if_error: true
      token: ${{ secrets.CODECOV_TOKEN }}

また、ログインすると、バッジも取得できます。

codecov

k1LoW/octocov-action@v1

シンプルで見やすいコメントをつけてくれます。こちらもGo以外の言語でも使用できます。CodeCovと異なり、外部サイトにログインする必要やsecretsの登録などの手間が必要ないので、簡単に導入できます。

image.png

ちなみに、以下は作成者の記事です。

GitHub Actionのワークフローは以下のようになります。

  - uses: actions/checkout@v4
    with:
      ref: ${{ github.event.pull_request.head.sha }}
  - uses: actions/setup-go@v5
    with:
      go-version-file: go.mod
  - name: Run Tests
    run: go list -f '{{.Dir}}/...' -m | xargs go test -v -race -coverprofile=coverage.out -covermode=atomic
  - name: create report
    uses: k1LoW/octocov-action@v1

設定ファイルは.octocov.ymlに作成します。
以下は自分の例です。

.octocov.yml
coverage:
  paths:
    - coverage.out
codeToTestRatio:
  code:
    - "**/*.go"
    - "!**/*_test.go"
  test:
    - "**/*_test.go"
testExecutionTime:
  if: true
comment:
  if: is_pull_request
report:
  if: is_default_branch
  datastores:
    - artifact://${GITHUB_REPOSITORY}
diff:
  datastores:
    - artifact://${GITHUB_REPOSITORY}

参考文献

  1. https://about.codecov.io
  2. 【Codecov】codecov.ymlの設定についてのあれこれ
  3. コードメトリクスを計測・可視化する - Pepabo Tech Portal
  4. k1LoW/octocov-action: :octocat: GitHub Action for octocov
  5. うわっ…私のカバレッジ、低すぎ…? - GitHub ActionsでRSpecのカバレッジレポートを出力する
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1