Claude Code Action でのインラインコードレビューを行う
背景
Claude Code Action は、CLI Coding Agent の Claude Code を GitHub Actions 上で実行するもの。これを使うと、GitHub 上で Claude に指示することができ、コードレビューやコード生成ができる。 デフォルトの設定でコードレビューをすると、PR 全体に対してコメントをするだけで詳細にどの部分を改善すべきかを指摘してくれない。修正箇所を具体的にインラインコメントとして指摘してもらった方が、指摘を受けた側も修正しやすい。 Claude Code Action でインラインコードレビューを行うには、get_pull_request_diff
とcreate_pending_pull_request_review
の 2 つのツールを使って PR の変更点を取得し、指摘がある該当の行にインラインコメントとしてレビューをするように指示すると良い。
例
| 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
を使うことを追加で記載している。 このワークフローを実行すると、インラインコメントでコードレビューをしてくれるようになった。