Claude Codeに自動でコードレビューさせるいい感じのGitHub Actionsを書いたので紹介する

に公開

2025/10/6追記

Claude Code GitHub Actionsがv1になった&いくつか改良を加えたので改良版を乗せておきます

claude-auto-review.yml
name: Claude Auto Review

on:
  pull_request:
    types: [ opened, ready_for_review ]

jobs:
  auto-review:
    runs-on: ubuntu-latest
    if: ${{ !github.event.pull_request.draft }}
    permissions:
      contents: read
      pull-requests: write
      id-token: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v5
        with:
          fetch-depth: 1

      # 利用環境に応じてトークン等適当に設定する
      # https://github.com/anthropics/claude-code-action
      # https://docs.anthropic.com/ja/docs/claude-code/github-actions

      - name: Automatic PR Review
        uses: anthropics/claude-code-action@v1
        with:
          github_token: ...
          prompt: |
            REPO: ${{ github.repository }}
            PR NUMBER: ${{ github.event.pull_request.number }}
            Note: The PR branch is already checked out in the current working directory.
            
            Review this PR.
            - Use `mcp__github_inline_comment__create_inline_comment` for lines that have actual issues requiring fixes.
              - Do NOT add inline comments for lines that are working correctly or for positive feedback.
              - Only comment on lines with bugs, errors, or clear improvement needs.
              - Use suggestion blocks to provide code fixes. Suggestion blocks replace the entire selected line range with the provided code, which must be syntactically complete and valid as a drop-in replacement. NEVER include surrounding context lines for suggestion blocks.
                - Single line fix: Select target line and provide replacement code for that line only.
                  ```suggestion
                  <REPLACED_CODE_FOR_SINGLE_LINE>
                  ```
                - Multi-line fix: Select the line range (startLine to endLine) and provide replacement code for the entire range.
                  ```suggestion
                  <REPLACED_CODE_FOR_ENTIRE_RANGE>
                  ```
            - Use `gh pr comment` for top-level feedback.
            
            The feedback MUST be this format:
            ```
            ## <RESULT>
            <RESULT_COMMENT>
            
            ### Reviewed Changes
            <OVERVIEW_OF_CHANGES>
            
            | File | Description |
            | ---- | ----------- |
            | foo.kt | <EMOJI> <DESCRIPTION> |
            | Bar.kt | ... |
            ```
            
            - <RESULT>: Choose one of the following with an emoji:
              - ⛔️ Request changes: Any bugs or issues that need to be fixed. If you add inline comments with code suggestions, it should be marked as "Request changes", not "Weak accept"
              - ⚠️ Weak accept: Functionally fine but with minor style issues, small performance optimizations, readability improvements, or non-essential best practices.
              - ✅ LGTM: No issues at all, perfectly fine as is.
            - <RESULT_COMMENT>: Brief summary of the review result.
            - <OVERVIEW_OF_CHANGES>: Concise summary of the what the PR changes.
          claude_args: |
            --allowedTools mcp__github_inline_comment__create_inline_comment,Bash(gh pr comment:*),Bash(gh pr diff:*),Bash(gh pr view:*)
  • mcp__github_inline_comment__create_inline_comment を使い、特定の行に対してコメントするよう修正
  • RESULTがLGTMになりがちなので判定基準を調整
  • レビュー項目やレビューガイドラインなどを削除
    • 明示しなくても動作するし、明示することでかえって「それ以外」に甘くなる可能性があるので

以下は古い内容です

今までGitHub Copilot code reviewを使っていたのですが、以下のような課題がありました。

  • false positive/negativeが多い
  • システムプロンプトが強いのか、カスタム命令が思うように反映されない

代わりにClaude Code(GitHub Actions)によるコードレビューを試行錯誤したところ、いい感じに仕上がったので紹介します。

動作例


実装

プロンプト以外の部分はプロジェクトに合わせて適当に置き換えてください。
プロンプトではGitHub Copilot code reviewに似たフォーマットでレビューを書かせ、Claude Codeデフォルトのコードレビューでノイズが多い部分を取り除いています。

claude-auto-review.yml
name: Claude Auto Review

on:
  pull_request:
    types: [opened, ready_for_review]

jobs:
  auto-review:
    runs-on: ubuntu-latest
    if: ${{ !github.event.pull_request.draft }}
    permissions:
      contents: read
      pull-requests: read
      id-token: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 1

      # 利用環境に応じてトークン等適当に設定する
      # https://github.com/anthropics/claude-code-action
      # https://docs.anthropic.com/ja/docs/claude-code/github-actions

      - name: Automatic PR Review
        uses: anthropics/claude-code-action@beta
        with:
          timeout_minutes: "60"
          github_token: ...
          model: "claude-sonnet-4@20250514"
          direct_prompt: |
            Review this pull request and provide comprehensive feedback in Japanese.

            Focus on:
            - Potential bugs or issues
            - Code quality and best practices
            - Performance considerations
            - Security implications
            - Test coverage
            - Documentation updates if needed

            Guidelines:
            - Focus primarily on high-severity issues.
            - Minor improvements can be summarized briefly in a single comment.
            - Provide specific, actionable suggestions for improvement.
            - Use inline comments to highlight specific areas of concern.
            
            Feedback Format must follow this structure:
            
            ```
            ## <RESULT>
            <RESULT_COMMENT>
            
            ### Suggestions
            <list any points that need to be addressed; leave blank if none.>
            
            ### Reviewed Changes
            <OVERVIEW_OF_CHANGES>
            
            | File | Description |
            | ---- | ----------- |
            | foo.kt | <EMOJI> <DESCRIPTION> |
            | Bar.kt | ... |
            
            ----
            
            #### Task List(optional)
            - [ ] <task 1>
            ```
            
            - <RESULT>: Choose one of the following with an emoji:
              - ✅ LGTM: Approve and recommend merging these changes.
              - ⚠️ Weak Accept: Provide feedback without explicit approval.
              - ⛔️ Request changes: Highlight issues that must be addressed before merging.
            - <RESULT_COMMENT>: Brief summary of the review result.
            - <OVERVIEW_OF_CHANGES>: Concise summary of the what the PR changes.
            - Remove the TODO and task lists as possible to avoid unnecessary clutter. If you need to include such a checklist, place it at the bottom of the feedback for readability.
          allowed_tools: "mcp__github__create_pending_pull_request_review,mcp__github__add_pull_request_review_comment_to_pending_review,mcp__github__submit_pending_pull_request_review,mcp__github__get_pull_request_diff"

Copilotと比べてどうか

体感ですが、現時点での性能はClaude Codeのほうが高いように思えます。
Copilotと比べてバグの検出率が上がり、間違った指摘の頻度も減りました。
間違った指摘でも「まあ確かにそういう間違いは起こり得るか、、」と納得できるものが多いと感じています。

とはいえ、Claude Codeが検出できなかったバグをCopilotが指摘することも稀にあるので、現状はCopilotとClaude Codeのレビューを併用しています。(併用すると「Copilotの間違った指摘をClaude Codeが信じてさらに間違える」といったことが起こるのが少々悩ましいです。)

あとClaude Codeのレビューは褒めるのが上手(?)です。
どうでもいいように聞こえますが、最近はどんな褒め言葉を貰えるのか楽しみにClaude Codeのレビューを見ている感じもあるので、意外と重要なのかもしれません。

GitHubとの連携では、現状はCopilotに軍配が上がります。

Copilotではsuggetionを使って特定の行の修正を提案することができますが、Claude Codeでは現状できません。(Claude CodeのRoadmapにはv1.0で対応予定と書いてあります。)
https://github.com/anthropics/claude-code-action/blob/main/ROADMAP.md

NOT A HOTEL

Discussion