4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GitHub Actions で、レビューが完了してないプルリクに対してリマインドする

Last updated at Posted at 2024-12-01

エンジニアは忙しいとプルリクエストのレビューを後回しにしがちで、レビュー待ちのプルリクがどんどん溜まっていってしまうため、レビューを優先して行う時間を作ることに決めました。

ただそれでも忘れるのが人間という生き物なので、GitHub Actions でリマインドを行ってレビューを促したいです。

Action の概要

プルリクエストの一覧を取得して、レビューを行っていないレビュワーがいるプルリクエストに対して下記のようなコメントを挿入してレビューを促すようにする

@kazumacchi
レビューお願いいたします

GitHub Workflows

完成品は下記の通り

name: Review Reminder

on:
  workflow_dispatch:
  schedule:
    # UTC なので動かしたい時間の +9時間を加算
    - cron: '0 1 * * 2'

jobs:
  notify_reviewers:
    permissions:
        pull-requests: read

    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      # プルリク一覧を取得、レビューが完了してないレビュワー一覧も取得
      - name: Get open pull requests
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          curl -H "Authorization: Bearer $GITHUB_TOKEN" \
              -H "Accept: application/vnd.github.v3+json" \
              https://api.github.com/repos/${{ github.repository }}/pulls \
              | jq '[.[] | select(.requested_reviewers | length > 0) | {number: .number, title: .title, url: .html_url, reviewers: [.requested_reviewers[].login]}]' \
              > pull_request.json

      - name: Notify reviewers
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          # pull_request が取得できない場合は終了
          if [ ! -f pull_request.json ]; then
            echo "pull_request.json が存在しません。処理を終了します。"
            exit 1
          fi

          cat pull_request.json | jq -c '.[]' | while read -r row; do
            # 必要な情報を取得。現状は reviewers と pr_number だけ使用
            # メンションしたいので、reviewr のユーザー名の頭に @ を付与
            pr_number=$(echo "$row" | jq -r '.number')
            pr_title=$(echo "$row" | jq -r '.title')
            reviewers=$(echo "$row" | jq -r '.reviewers | map("@\(. // empty)") | join(" ")')

            # レビュワーが空の場合はスキップ
            if [ -z "$reviewers" ]; then
              echo "PR #${pr_number} にはレビューが完了していないレビュワーは存在しません"
              continue
            fi

            message="${reviewers}\nレビューお願いいたします。"

            # コメントの送信
            response=$(curl -s -o /dev/null -w "%{http_code}" -X POST \
              -H "Authorization: Bearer $GITHUB_TOKEN" \
              -H "Content-Type: application/json" \
              -d "{\"body\": \"$message\"}" \
              "https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${pr_number}/comments")

            # レスポンスコードを確認
            if [ "$response" -eq 201 ]; then
              echo "PR #${pr_number} へのレビュー依頼が完了しました"
            else
              echo "PR #${pr_number} へのレビュー依頼に失敗しました"
            fi
          done
4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?