LoginSignup
3
3

GitHub Actions で Go言語 のテストカバレッジをプルリクエストにコメントする

Posted at

はじめに

Goプロジェクトにおいて、テストカバレッジを定期的に監視し続けることは、コード品質を維持する上で非常に重要です。GitHub Actionsを利用すれば、プルリクエストに対するテストカバレッジを自動でコメントとして付与することが可能になり、これによりレビュアーがカバレッジ情報を即座に確認できるようになります。この記事では、その具体的な手法を紹介します。具体的には、GitHub Actionsを活用してGoのテストカバレッジをプルリクエストに自動コメントする方法、そして、プルリクエストの最後に新しいコメントを追加し、既存のコメントを更新する方法について詳しく解説します。

実装

  1. リポジトリのルートディレクトリに.github/workflowsという名前のフォルダを作成し、その中にcoverage.ymlという名前のファイルを作成します。このファイルにGitHub Actionsワークフローを記述します。
  2. coverage.ymlに以下のYAMLコードを追加します。
name: coverage
on: pull_request
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          ref: ${{ github.event.pull_request.head.sha }}
      - uses: actions/setup-go@v3
        with:
          go-version: 1.18.0
      - name: run coverage
        run: |
          make coverage
      - name: Find existing comment
        id: find_comment
        uses: peter-evans/find-comment@v2
        with:
          issue-number: ${{ github.event.number }}
          body-includes: "## Test Coverage Report"
      - name: Delete existing comment
        if: steps.find_comment.outputs.comment-id
        run: |
          curl \
            -X DELETE \
            -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
            -H "Accept: application/vnd.github+json" \
            "https://api.github.com/repos/${{ github.repository }}/issues/comments/${{ steps.find_comment.outputs.comment-id }}"
      - name: Comment coverage on PR
        uses: peter-evans/create-or-update-comment@v3
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          issue-number: ${{ github.event.number }}
          body-path: 'coverage.txt'
  1. actions/checkout@v3: このステップでは、リポジトリをチェックアウトします。つまり、GitHubリポジトリのコードをGitHub Actionsの実行環境にダウンロードします。ここでのref: ${{ github.event.pull_request.head.sha }}は、プルリクエストのHEADコミットを指すようにリファレンスを設定しています。

  2. actions/setup-go@v3: Go環境をセットアップします。ここではGoのバージョンを1.18.0に設定しています。

  3. run coverage: このステップでは、make coverageコマンドを実行してテストカバレッジを生成します。

  4. Find existing comment: ここでは、peter-evans/find-comment@v2アクションを使用して、既存のテストカバレッジコメントを探します。コメントの本文に"## Test Coverage Report"が含まれているものを探します。既存のコメントが見つかった場合、そのコメントIDは次のステップで使用されます。

  5. Delete existing comment: 前のステップで見つかったコメントIDがある場合にのみこのステップが実行され、そのコメントは削除されます。これはcurlコマンドを使ってGitHub APIを呼び出し、コメントを削除するために行われます。

  6. Comment coverage on PR: ここではpeter-evans/create-or-update-comment@v3アクションを使用して新しいテストカバレッジコメントをプルリクエストに追加します。コメントの本文は、前のステップで生成されたcoverage.txtファイルの内容になります。

Makefileのコマンド

coverage:
	go test -short -v -covermode=count -coverprofile=coverage.out  | tee test_output.txt
	go tool cover -func=coverage.out | awk '/total/ {print "| **" $$1 "** | **" $$3 "** |"}' | tee coverage.txt
	cat test_output.txt | grep 'ok.*coverage' | awk '{print "| " $$2 " | " $$5 " |"}' | tee -a coverage.txt
	echo "## Test Coverage Report" > coverage_with_header.txt
	echo "| Package           | Coverage |" >> coverage_with_header.txt
	echo "|-------------------|----------|" >> coverage_with_header.txt
	cat coverage.txt >> coverage_with_header.txt
	mv coverage_with_header.txt coverage.txt
	rm test_output.txt

出力されるtxtファイルの中身の例

## Test Coverage Report
| Package           | Coverage |
|-------------------|----------|
| **total:** | **67.5%** |
| apihandler | 75.3% |
| utils | 60.2% |
| database | 70.1% |

追加されるコメントのサンプルは下記のようなものです。
スクリーンショット 2023-05-19 20.32.52.png

まとめ

この記事を通じて、GitHub Actionsを活用してGoのテストカバレッジをプルリクエストに自動的にコメントする手法、そして既存コメントの更新方法を学びました。このアプローチにより、コードレビューの過程でテストカバレッジの確認が非常に容易になります。さらに、プルリクエストが更新されるたびにコメントが最新の状態に保たれるため、テストカバレッジの変化を効率的に追跡することができます。これにより、プロジェクトの品質を一層向上させることが可能となります。

3
3
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
3
3