Claude Code Action でインラインコードレビューを行う
Claude Code Action のレビューで、PR 全体へのコメントだけでなく、修正してほしい行にコメントを付けてもらう設定を紹介します。PR の差分取得とレビュー作成のツールを使うようプロンプトで指示し、GitHub Actions のワークフローに組み込みます。
背景
Claude Code Action は、CLI Coding Agent の Claude Code を GitHub Actions 上で実行するものです。Claude Code Action を使うと、GitHub 上で Claude に指示でき、コードレビューやコード生成ができます。
デフォルトの設定でコードレビューをすると、PR 全体に対してコメントをするだけです。指摘を PR 全体へのコメントで受け取ると、開発者は対象の行を探す必要があります。修正してほしい行にインラインコメントを付けてもらえば、その手間を減らせます。
Claude Code Action でインラインコードレビューを行うには、次の 2 つのツールを使います。変更点を取得したうえでインラインコメントを追加する、という 2 段階の処理です。
| ツール | 役割 |
get_pull_request_diff | PR の変更点を取得する |
create_pending_pull_request_review | 該当行にインラインコメントを追加するよう指示する |
例
| name: Claude Code Action
permissions:
contents: write
pull-requests: write
issues: write
id-token: write
on:
workflow_dispatch:
issue_comment:
types: [created]
pull_request_review_comment:
types: [created]
issues:
types: [opened, assigned]
pull_request:
types: [opened, reopened, ready_for_review]
jobs:
claude-pr:
if: |
(github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude')) ||
(github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) ||
(github.event_name == 'issues' && contains(github.event.issue.body, '@claude')) ||
(github.event_name == 'pull_request' && github.event.pull_request.draft == false &&
github.event.pull_request.user.type != 'Bot' &&
!endsWith(github.event.pull_request.user.login, 'bot') &&
!endsWith(github.event.pull_request.user.login, '[bot]'))
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Generate GitHub App token
id: app-token
uses: actions/create-github-app-token@v2
with:
app-id: ${{ secrets.CLAUDE_CODE_APP_ID }}
private-key: ${{ secrets.CLAUDE_CODE_APP_PRIVATE_KEY }}
- uses: anthropics/claude-code-action@beta
with:
model: MY_MODEL_NAME
github_token: ${{ steps.app-token.outputs.token }}
trigger_phrase: "@claude"
timeout_minutes: "60"
allowed_tools: |
mcp__github__create_pending_pull_request_review
mcp__github__add_comment_to_pending_review
mcp__github__submit_pending_pull_request_review
mcp__github__get_pull_request_diff
direct_prompt: |
このPRをレビューしてください。
github mcp serverの`get_pull_request_diff`と`create_pending_pull_request_review` を使うとPRの変更を取得し、インラインコメントとしてレビューを作成できます。
変更点を確認し、以下のフィードバックを提供してください
- コードのクオリティと最適化のためのベストプラクティス
- 予期せぬバグや問題がある可能性があるコードがある場合
- コードの改善点を提案する場合
- アーキテクチャやデザインの決定要素に関するコメント
- ドキュメントの一貫性: README.mdやその他のドキュメントファイルが変更された場合、変更されたコードに対してドキュメントが更新されていることを確認してください(特に新しい入力、機能、または構成オプションがある場合)
コードの改善点を提案する場合は、適切な場所にインラインコメントを付けてください。
|
公式の workflowにあるプロンプトを日本語にしたうえで、get_pull_request_diffとcreate_pending_pull_request_reviewの利用を追加で記載しました。get_pull_request_diffで PR の変更点を取得し、create_pending_pull_request_reviewでインラインコメントを追加する狙いです。 紹介したワークフローを実行すると、インラインコメントでコードレビューをしてくれるようになりました。