name: code-review-gate version: 0.2.1 summary: 自動對 git diff 執行 7 維度結構化程式碼審查,輸出分級報告並阻塞 Critical 問題 description: | AI 程式碼審查門禁 — 對 git diff 執行全面的靜態分析,覆蓋功能正確性、安全性、 效能、可讀性、可維護性、測試覆蓋、文件同步 7 個維度。按 Critical / Important / Minor 三級嚴重度輸出結構化報告,存在 Critical 問題時門禁阻塞(exit code 1)。 tags: - code-review - quality-gate - ci-cd - static-analysis - security-scan - developer-tools - python author: Community license: MIT homepage: https://github.com/Terr123123/code-review-gate repository: https://github.com/Terr123123/code-review-gate runtime: python requires_python: ">=3.10" requires_git: ">=2.0" permissions: read: - git_diff # 讀取 git diff 內容 - filesystem # 讀取設計文件(--design 引數) - subprocess # 呼叫 git diff 命令 write: [] # 不執行任何寫操作 network: [] # 無網路請求 security_notes: | 本技能僅執行本地靜態分析,不修改任何檔案。 使用 subprocess.run 僅呼叫 git diff(只讀命令),不接受外部輸入。 所有正則匹配在本地記憶體中執行,不傳送任何資料到外部服務。
OpenClaw Skill: 自動對 git diff 執行結構化程式碼審查,輸出分級報告並阻塞 Critical 問題。
| 維度 | 嚴重級別 | 說明 |
|---|---|---|
| 功能正確性 (functional) | Critical | 邏輯錯誤、邊界條件、錯誤處理、併發問題 |
| 安全性 (security) | Critical | SQL隱碼攻擊、XSS、命令注入、敏感資訊洩露、許可權控制 |
| 效能 (performance) | Important | O(n²)複雜度、N+1查詢、正則回溯、資源洩漏 |
| 可讀性 (readability) | Important | 命名規範、職責單一、註釋質量、程式碼簡潔 |
| 可維護性 (maintainability) | Important | SOLID原則、依賴關係、配置外部化、日誌規範 |
| 測試覆蓋 (testing) | Critical | 單元測試、邊界測試、異常測試、斷言清晰 |
| 文件同步 (documentation) | Minor | API文件、變更記錄、README同步 |
openclaw run code-review-gate --base HEAD~1 --head HEAD
openclaw run code-review-gate --base abc1234 --head def5678
openclaw run code-review-gate --files "src/api/*.py,src/services/*.py" --design design.md
openclaw run code-review-gate --base main --head feature-branch --design openspec/changes/feat-001/design.md
| 引數 | 必需 | 說明 |
|---|---|---|
--base |
是* | git diff 基準 commit/branch |
--head |
是* | git diff 目標 commit/branch |
--files |
否 | 限定審查的檔案路徑(glob 模式) |
--design |
否 | 設計文件路徑,用於對比實現一致性 |
--severity |
否 | 最低報告級別: critical / important / minor (預設 important) |
--format |
否 | 輸出格式: markdown / json / terminal (預設 markdown) |
--max-lines |
否 | 單次審查最大行數限制 (預設 1000) |
*
--base+--head與--files二選一
審查完成後生成結構化報告:
## Code Review Report — [timestamp]
**Range:** abc1234..def5678
**Files Changed:** 12 | **Lines:** +345 -120
**Design Doc:** openspec/changes/feat-001/design.md
---
### Strengths
- Clean separation of concerns in service layer
- Comprehensive error handling with proper fallbacks
- Well-structured test cases covering edge scenarios
### Issues
#### Critical (Must Fix — 2 issues)
1. **SQL Injection in user query** [src/api/users.py:45]
- What: Raw string formatting used in SQL WHERE clause
- Risk: Allows arbitrary SQL execution via crafted input
- Fix: Use parameterized queries with `?` placeholders
2. **Missing auth check** [src/api/admin.py:120]
- What: Admin endpoint lacks authentication middleware
- Risk: Unauthenticated access to sensitive admin operations
- Fix: Add `@require_auth` decorator
#### Important (Should Fix — 3 issues)
1. **N+1 query in list endpoint** [src/services/order.py:78]
- ...
#### Minor (Nice to Have — 2 issues)
1. **Inconsistent variable naming** [src/utils/parser.py:33]
- ...
### Design Consistency Check
- ✅ API signature matches design doc
- ⚠️ One endpoint (`GET /api/v2/users`) not documented in design
- ✅ Data model matches schema definition
### Recommendations
- Add input sanitization middleware
- Consider query batching for list endpoints
### Assessment
**Gate: ❌ BLOCKED**
**Reasoning:** 2 critical issues must be resolved before merge — SQL injection and missing authentication. Important issues should be addressed but do not block.
| 退出碼 | 含義 | CI 行為 |
|---|---|---|
| 0 | 通過 — 無 Critical 問題 | 允許合併 |
| 1 | 阻塞 — 存在 Critical 問題 | 阻止合併 |
| 2 | 錯誤 — 工具自身異常 | 標記為 CI 失敗 |
| 3 | 跳過 — 變更不符合審查條件 | 允許合併 |
git — 命令列工具,用於獲取 diffbandit — Python 安全掃描 (可選,增強安全檢測)radon — 程式碼複雜度分析 (可選)小蔥技能7w4.net有更新,你可以訪問看下。
本 Skill 可作為 OpenClaw 流水線中的獨立階段:
import { defineAgent } from "openclaw";
import { CodeReviewGateSkill } from "@community/code-review-gate";
const agent = defineAgent({
name: "dev-workflow-agent",
description: "Development workflow with code review gate",
model: "claude-sonnet-4-20250514",
skills: [
new CodeReviewGateSkill({
severity: "critical",
maxLines: 1000,
}),
],
});
通過 gate.config.yaml 自定義檢查項:
# gate.config.yaml — 可選配置檔案
severity_threshold: critical # 阻塞級別
max_diff_lines: 1000 # 單次最大審查行數
skip_patterns: # 跳過審查的檔案模式
- "*.md"
- "*.json"
- "docs/**"
- "*.lock"
require_design_doc: true # 是否強制要求設計文件
enabled_checks: # 啟用的檢查維度
- functional
- security
- performance
- testing
- documentation
auto_fix_suggestions: true # 是否生成修復建議
這個 Skill 整體質量不錯,能自動檢查程式碼的安全、效能、可讀性等多個方面,輸出清晰的報告並設定質量門禁。主要優點是覆蓋面廣、配置靈活、攔截機制有效;不足之處是智慧程度有限,對複雜問題的識別能力有待提升。