你也受夠「明明很簡單的 bug 卻在 PR 才被抓到」嗎?我把 Claude Code 的 Code Review 概念拆成一個可重現的 Git Hook。

先講結論

  • Code Review 不是要取代人,而是先擋掉爛錯:拼字、變數命名、明顯的 null 風險。
  • 用 API 的方式最穩,不用依賴特定 IDE 或 GUI
  • 我會用它做「commit 前檢查」,PR 還是給人看。

這個工具解決什麼問題

這週 Claude Blog 提到 Code Review 進到 Claude Code(3/9)。我最想解的痛點是:

  • 小錯誤(漏處理 null / 沒有 await)早點被擋掉
  • code review 時間縮短,剩下「架構跟風格」才留給人

安裝與設定

環境需求

  • Node.js >= 20
  • ANTHROPIC_API_KEY

安裝步驟

npm i @anthropic-ai/sdk

Git Hook 腳本(commit 前自動跑)

mkdir -p .githooks
# .githooks/pre-commit
#!/usr/bin/env bash
set -e
node scripts/claude-review.mjs
git config core.hooksPath .githooks
chmod +x .githooks/pre-commit

實際使用

Use Case:檢查 staged diff

// scripts/claude-review.mjs
import { execSync } from "child_process";
import Anthropic from "@anthropic-ai/sdk";
 
const diff = execSync("git diff --cached", { encoding: "utf8" }).trim();
if (!diff) {
  console.log("No staged diff, skip.");
  process.exit(0);
}
 
const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
 
const res = await client.messages.create({
  model: "claude-sonnet-4-6",
  max_tokens: 800,
  temperature: 0.2,
  messages: [{
    role: "user",
    content: `你是嚴格的 code reviewer。請針對以下 diff 提出:\n1) 必改問題\n2) 可能的 bug\n3) 風格建議\n\nDIFF:\n${diff}`
  }]
});
 
const text = res.content[0].text;
console.log("\n=== Claude Review ===\n" + text + "\n======================\n");
 
// 有「必改」就直接擋住 commit
if (text.includes("必改")) {
  console.error("❌ Claude found critical issues. Fix before commit.");
  process.exit(1);
}

結果

  • 會在 commit 前把 obvious bug 先擋掉
  • 低級錯誤減少,review 討論變得更有價值

跟現有工具比較

  • ESLint:擅長格式跟規則,抓不到「邏輯會出事」的地方
  • Claude Review:會提醒邏輯風險,但不一定知道你的 domain

我目前的做法是:ESLint → Claude Review → 人工 PR

結論

  • 推薦程度:Must Try
  • 影響評估:Incremental(幫你省時間,但不會改變產品)
  • 我們的場景適用度:直接可用(ClawdBot 的 repo 我先接上)
  • 會繼續用嗎:會,至少在核心 repo 保留

基於實際安裝測試撰寫,非業配。