[小ネタ]Claude Code のSub agentでレビュー& prにコメントさせる

[小ネタ]Claude Code のSub agentでレビュー& prにコメントさせる

2025.07.25

Introduction

Claude Codeの1.0.60でSub agents機能がリリースされました。

すでに各所で解説されているように、Sub agents機能は特定のタスクを処理するための専用AIです。
メインのClaude Codeとの会話とは別に、
独自のコンテキストウィンドウを持ち、許可されたツールのみで安全に動作させたり専用のプロンプトで動作します。

本記事では、Sub agentsを作成してgithubにレビュー結果を反映させてみました。

Sub-Agents?

Sub-Agentsは、特定のタスクに特化したAIアシスタントを作れる機能です。
メインのClaude Codeとは別のコンテキストで動作するので、
専門的なタスクを任せるのに最適です。

「コードレビュー専門のアシスタント」や「データ分析専門のアシスタント」など任意の役割をもったagentを作れます。

Environment

  • Claude Code : 1.0.60
  • GitHub CLI (gh) : 2.73.0

Setup

まず、ghコマンドでGitHubにログインします。

% brew install gh
% gh auth login

Try

実際にやってみましょう。
まずはSub Agentの作成です。

1. レビュアーAgent作成

まず、Claude Codeで /agents コマンドを実行して、レビュアーを作成します。

% claude
> /agents

名前はcode-reviewerにして、説明文に「GitHubのPRにレビューコメントを投稿する」という内容を含めました。
(ghを使うのでツールはBashを含める)

2. code-reviewer内容をカスタマイズ

デフォルトでレビュアーとして必要な内容が生成されますが、
今回はgithubのprに対してコメントさせたいので、./claude/agents/code-reviewer.mdに以下のような記述を追加しました。
(全文は最後に記述)

生成された .claude/agents/code-reviewer.md を編集して、PRレビューのルールを追加。


・・・省略・・・

## GitHub PR Review Format (MANDATORY when PR is mentioned)

Your response MUST have exactly TWO sections:

### Section 1: Standard Review
Provide your detailed code review using the format above.

### Section 2: GitHub API JSON
End your response with this EXACT format:

## GitHub PR Review Data
{
  "body": "Overall review summary in 2-3 sentences",
  "event": "COMMENT",
  "comments": [
    {
      "path": "exact/file/path.js",
      "line": 2,
      "side": "RIGHT",
      "body": "🟡 **Major**: Use `let` instead of `var` for better scoping"
    },
    {
      "path": "exact/file/path.js",
      "line": 5,
      "side": "RIGHT",
      "body": "🔴 **Critical**: Add input validation to prevent runtime errors"
    }
  ]
}

## CRITICAL Requirements:

1. **JSON is MANDATORY** - If input mentions PR, you MUST include the JSON section
2. **Use exact GitHub API keys**: `body`, `event`, `comments` (not review_body, etc.)
3. **Always use**: `"event": "COMMENT"` to avoid API errors
4. **For each line comment include**:
   - `path`: exact file path from the PR
   - `line`: line number in the new file
   - `side`: always "RIGHT" for new/modified lines
   - `body`: your comment with severity prefix
5. **Line numbers must match** the actual line numbers in the code
6. **Comments array can be empty** if no line-specific issues

FAILURE TO INCLUDE THE JSON SECTION WHEN REVIEWING A PR WILL RESULT IN AN INCOMPLETE REVIEW.

このコードは、GitHubのprにレビューコメントをpostするためのSub agentの設定です。

動作の流れは以下。

  1. 変更内容の確認
    git diffコマンドで変更内容を把握。

  2. レビューの実施
    コードの問題点や改善案を見つける
    Markdown形式でフィードバック作成。

  3. GitHubへの投稿
    gh pr commentコマンドを使って、レビューコメントをpost。
    eventでREQUEST_CHANGESでなくCOMMENTを常に使うように指示してますが、
    自身へREQUEST_CHANGESのpostができないので、CONNENTにしてます。
    この部分はプロンプトで調整すればREQUEST_CHANGESでも対応可能と思われます。

3. 実際に使ってみる

適当なPRを作って、レビューさせてみました。

#適当なコードをcommit&pushしてpr作成
% git add bad-code.js
% git commit -m "Add function"
% git push origin feature/bad-code
% gh pr create --title "Add bad code" --body "Please review"

4. レビュアーを呼び出す

Claude Codeでレビュアーを使います。
以下のようにPRのURLを指定してレビューコメントつけてもらうように指示。

% claude
> code-reviewer subagentを使用して、<prのURL>のレビューをお願いします。
prに対してレビューコメント追加してください。

5. 結果

指示すると以下のようにClaude Codeのsub agentが実行されます。

⏺ PRのレビューを実行します。まずGitHub情報を取得してから、co
  de-reviewerエージェントでレビューを行います。

⏺ Bash(gh pr view 2 --json title,body,files,additions,deletions
      ,changedFiles,headRefName,baseRefName){                             
       "additions": 7,
       "baseRefName": "main",
     … +13 lines (ctrl+r to expand)

・・・

⏺ レビューが完了しました!エージェントが正しくJSON形式を出力し
  ました。しかし、"event": 
  "REQUEST_CHANGES"を使用しているため、APIエラーを避けるために"
  COMMENT"に変更してGitHubに投稿します:

・・・

⏺ Bash(gh api repos/pulls/2/reviews
      --method POST --input /tmp/pr2_review.json){                                   
       "id": 3054846627,
       "node_id": "xxxxx",
     … +56 lines (ctrl+r to expand)

⏺ Bash(rm /tmp/pr2_review.json)(No content)

⏺ 成功!

  PR #2のレビューが完了しました:

GitHubのPRページを見ると、Sub-Agentがコメントを投稿していました。
(ユーザーは自分ですが)

cc-pr.png

Summary

Claude Code 1.0.60で追加されたSub-Agents機能、
簡単につかえるし、チームでの共有も簡単なので非常に便利ではないかと思います。

今回のように、単にレビューしてくれるだけでなく他のツールもAgentに使わせることでひと手間加えた処理をさせることが可能です。
レビュアー以外にも、テストランナーやドキュメント生成など、工夫次第で様々な用途に活用できそうです。

References

Appendix

code-reviewer.mdの全文。

---
name: code-reviewer
description: Code review agent for quality, best practices, bugs, and security issues
color: green
---

You are a code reviewer. When input contains "PR #", "pull request", or "GitHub PR", you MUST follow GitHub PR format.

## Standard Code Review Format

For regular code reviews, analyze:
- Code quality and structure
- Potential bugs and security issues
- Performance implications
- Best practices adherence

Use severity levels:
- 🔴 Critical: Must fix (security, major bugs)
- 🟡 Major: Should fix (code quality, maintainability)
- 🟢 Minor: Consider fixing (style, improvements)
- 💡 Suggestion: Optional enhancements

## GitHub PR Review Format (MANDATORY when PR is mentioned)

Your response MUST have exactly TWO sections:

### Section 1: Standard Review
Provide your detailed code review using the format above.

### Section 2: GitHub API JSON
End your response with this EXACT format:

## GitHub PR Review Data
<json>
{
  "body": "Overall review summary in 2-3 sentences",
  "event": "COMMENT",
  "comments": [
    {
      "path": "exact/file/path.js",
      "line": 2,
      "side": "RIGHT",
      "body": "🟡 **Major**: Use `let` instead of `var` for better scoping"
    },
    {
      "path": "exact/file/path.js",
      "line": 5,
      "side": "RIGHT",
      "body": "🔴 **Critical**: Add input validation to prevent runtime errors"
    }
  ]
}
</json>

## CRITICAL Requirements:

1. **JSON is MANDATORY** - If input mentions PR, you MUST include the JSON section
2. **Use exact GitHub API keys**: `body`, `event`, `comments` (not review_body, etc.)
3. **Always use**: `"event": "COMMENT"` to avoid API errors
4. **For each line comment include**:
   - `path`: exact file path from the PR
   - `line`: line number in the new file
   - `side`: always "RIGHT" for new/modified lines
   - `body`: your comment with severity prefix
5. **Line numbers must match** the actual line numbers in the code
6. **Comments array can be empty** if no line-specific issues

FAILURE TO INCLUDE THE JSON SECTION WHEN REVIEWING A PR WILL RESULT IN AN INCOMPLETE REVIEW.

この記事をシェアする

facebookのロゴhatenaのロゴtwitterのロゴ

© Classmethod, Inc. All rights reserved.