Code Review Gate

👤 terr123123 📦 v1.0.1 ⭐ 4.4 ⬇️ 281 下載
💻 開發程式設計 免費

📖 技能介紹


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(只讀命令),不接受外部輸入。 所有正則匹配在本地記憶體中執行,不傳送任何資料到外部服務。


Code Review Gate — AI 程式碼審查門禁 Skill

OpenClaw Skill: 自動對 git diff 執行結構化程式碼審查,輸出分級報告並阻塞 Critical 問題。

場景描述 (When to Use)

  • 開發者完成一輪程式碼修改,準備合併到主分支前
  • CI/CD 流水線中需要自動化程式碼審查門禁
  • Pull Request 提交後,需要 AI 先做一輪預審
  • 任何需要確保程式碼質量、安全性和設計一致性的場景

決策規則 (Decision Rules)

  1. 阻塞條件: 任何 Critical 級別問題(安全漏洞/功能錯誤/資料風險)未修復 → 門禁不通過,禁止進入下一階段
  2. 警告條件: Important 級別問題存在但無不阻塞,記錄並建議修復
  3. 通過條件: 所有 mandatory 檢查項均通過 AND blocking_issues == 0
  4. 跳過條件: 變更規模為 docs/config/prompt/chore 且風險 ≤ low 時,本門禁可跳過(由上游流程判斷)

審查維度 (Check Categories)

維度 嚴重級別 說明
功能正確性 (functional) Critical 邏輯錯誤、邊界條件、錯誤處理、併發問題
安全性 (security) Critical SQL隱碼攻擊、XSS、命令注入、敏感資訊洩露、許可權控制
效能 (performance) Important O(n²)複雜度、N+1查詢、正則回溯、資源洩漏
可讀性 (readability) Important 命名規範、職責單一、註釋質量、程式碼簡潔
可維護性 (maintainability) Important SOLID原則、依賴關係、配置外部化、日誌規範
測試覆蓋 (testing) Critical 單元測試、邊界測試、異常測試、斷言清晰
文件同步 (documentation) Minor API文件、變更記錄、README同步

使用示例 (Usage)

基礎用法 — 審查當前未提交的改動

openclaw run code-review-gate --base HEAD~1 --head HEAD

審查指定 commit 範圍

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 二選一

輸出格式 (Output)

審查完成後生成結構化報告:

## 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 — 命令列工具,用於獲取 diff
  • Python ≥ 3.10
  • bandit — Python 安全掃描 (可選,增強安全檢測)
  • radon — 程式碼複雜度分析 (可選)

與 OpenClaw 框架的整合

小蔥技能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        # 是否生成修復建議

🤖 AI 評測

這個 Skill 整體質量不錯,能自動檢查程式碼的安全、效能、可讀性等多個方面,輸出清晰的報告並設定質量門禁。主要優點是覆蓋面廣、配置靈活、攔截機制有效;不足之處是智慧程度有限,對複雜問題的識別能力有待提升。

📊 多維度評分

適應性4.5
規範性4.3
有效性4.4
可靠性4.2
可信度5

📁 包含檔案 (22 個)

📄 LICENSE 1 KB
📄 SKILL.md 7.3 KB
📄 _meta.json 135 B
📄 package.json 1.3 KB
📄 pyproject.toml 350 B
📄 setup.cfg 168 B
📄 src/__init__.py 69 B
📄 src/checks/__init__.py 663 B
📄 src/checks/base.py 2.3 KB
📄 src/checks/documentation.py 3.5 KB
📄 src/checks/functional.py 6.7 KB
📄 src/checks/maintainability.py 5.6 KB
📄 src/checks/performance.py 6 KB
📄 src/checks/readability.py 5.1 KB
📄 src/checks/security.py 9.5 KB
📄 src/checks/testing.py 4.6 KB
📄 src/gate.py 6.9 KB
📄 src/models.py 2.1 KB
📄 src/reporter.py 7.1 KB
📄 tests/mock_bad_code.py 5.6 KB
📄 tests/test_gate.py 6.9 KB
📄 tests/verify_mock.py 1.4 KB