ぷらすのブログ

Claude Code ActionにPRのCode Suggestionをしてもらうプロンプト

#生成AI#Claude Code#GitHub

こんにちは、ぷらす(@p1ass)です。

世の中は Claude Code の話題で溢れかえっていますが、例に漏れず私も Claude Code / Claude Code Action を使って色々試行錯誤しています。 今回は Claude Code Action を使って PR の Code Suggestion をしてもらうプロンプトを作成したので紹介します。

背景

Claude Code Action は GitHub Action で Claude Code を呼び出せる Action です。 OSS で公開されており、Anthropic の公式ドキュメントにはいくつかの Example が掲載されています。

しかし、この Example のプロンプトをそのまま使うと、GitHub の Pull Request Review (Approve や Request Changes、コード行を指定した Comment や Suggestion)をしてくれず、単なる単一の Issue Comment になってしまいます。

これをやりたい これをやりたい (GitHub 公式ページより)

公式の Example コード

Claude Code に Suggestion を出してもらう

プロンプトを改善して Suggestion を出してもらうようにします。 Suggestion は gh pr review コマンドではできず、 gh api コマンドを使わなければなりません。

私は以下のようなプロンプトを作成して利用しています。

permissions:
  contents: read
  pull-requests: write # PR レビューを行うために必要

# 中略

- name: Automatic PR Review
        uses: anthropics/claude-code-action@beta
        with:
          model: claude-sonnet-4@20250514
          github_token: ${{ steps.app-token.outputs.token }}
          timeout_minutes: "60"
          allowed_tools: Bash
          disallowed_tools: | # セキュリティ上懸念があるものを除外する。実際にはもっと多くのコマンドを除外している
            Bash(curl:*),Bash(wget:*),
            Bash(rm:*),Bash(rmdir:*),
            Bash(sudo:*),Bash(su:*)
          direct_prompt: |
            レビューガイドラインに記載されている内容を元にPull Requestを厳しくレビューしてください。

            レビューは必ずPull Request Review として実施してください。具体的には以下の手順で行ってください:

            1. `gh api` コマンドを使用して、指摘場所(path, lineを指定)にreviewsをPOSTする。
            2. `gh pr review` コマンドの `--comment` で数行のサマリー文章を送信する(`--approve`  `--request-changes` は使わず `--comment` を使ってください)

            単なるIssue Commentではなく、必ずPull Request Review APIを使用してレビューを実施してください。

            Pull Request Review APIのサンプルコードは以下の通りです。
            ```
            gh api \
              --method POST \
              --header "Accept: application/vnd.github+json" \
              /repos/${OWNER}/${REPO}/pulls/${PR_NUMBER}/reviews \
              --field event="COMMENT" \
              --field comments[0][path]="main.go" \
              --field comments[0][line]=10 \
              --field comments[0][body]="```suggestion
            func improvedFunction() {
                return \"better implementation\"
            }
            ```

ポイント

おわりに

Claude Code Action を使った PR の自動レビュー Action を作成し、今のところ順調に動いています。 行指定でコメントを出してくれると、一気に人間のレビューらしくなるのでおすすめです。

← 個人ブログをHugoからHonoに移行しました
Topへ戻る