Files
ewoooc/CODE_REVIEW_GUIDE.md
ogt a96306fba2 Fix Telegram bot natural language communication issue
- Install python-telegram-bot dependency
- Start Telegram bot service successfully
- Confirm correct group ID (MOMO PRO - small shrimp group)
- Bot now running with all commands and button interface functional
- Natural language processing restored with keyword matching

Fixes issue where Telegram group could not communicate using natural language.
2026-04-22 14:27:50 +08:00

5.6 KiB
Raw Blame History

📋 MOMO 系統 - Aider Code Review 使用指南

🎯 功能概述

本系統整合了 Aider AI Code Review 功能,提供多種自動化程式碼審查方式,確保程式碼品質與安全性。

🚀 使用方式

1. Git Hooks 自動觸發

# 在 commit 時自動執行 Code Review
git add .
git commit -m "feat: 新增功能"
  • 自動檢查暫存檔案
  • Review 失敗時阻止 commit
  • 支援跳過:git commit --no-verify

2. 手動快速觸發

# 互動式選單
./scripts/quick_review.sh

# 直接指定檔案
./scripts/quick_review.sh app.py routes/sales_routes.py

# 自動 Review 暫存檔案
python3 scripts/code_review.py --auto

3. Web 介面操作

# 啟動 MOMO 系統
python3 app.py

# 訪問 Code Review 頁面
http://localhost:5000/code-review/

功能:

  • 📁 檔案選擇器(支援變更檔案/所有檔案)
  • 🔍 三種 Review 類型
  • 📊 即時 Review 輸出
  • 📈 Review 歷史記錄
  • 📄 報告查看功能

4. GitHub Actions CI/CD

# 手動觸發
# 在 GitHub Actions 頁面選擇 "Aider Code Review" workflow

# 自動觸發
# Push 到 main/develop 分支
# 建立 Pull Request

🔧 Review 類型

基礎檢查 (Basic)

  • 程式碼品質與最佳實踐
  • 潛在的 bug 與問題
  • 程式碼可讀性與維護性
  • 效能優化建議

安全檢查 (Security)

  • SQL 注入風險
  • XSS 攻擊風險
  • 認證與授權問題
  • 敏感資料洩露風險
  • 輸入驗證不足

效能檢查 (Performance)

  • 演算法效率
  • 資料庫查詢優化
  • 記憶體使用
  • 並發處理
  • 快取策略

📁 檔案結構

momo-pro-system/
├── scripts/
│   ├── code_review.py              # 核心 Code Review 腳本
│   └── quick_review.sh             # 快速觸發腳本
├── routes/
│   └── code_review_routes.py       # Web 路由
├── templates/
│   └── code_review.html            # Web 介面
├── .git/hooks/
│   └── pre-commit                  # Git Hook
├── .github/workflows/
│   └── code-review.yml            # GitHub Actions
└── logs/
    ├── code_review.log             # Review 日誌
    └── review_*.md                 # Review 報告

🛠️ 進階設定

自訂 Review 提示詞

編輯 scripts/code_review.py 中的 message 參數:

# 基礎檢查範例
"--message", "請重點檢查:\n1. 程式碼架構設計\n2. 錯誤處理機制\n3. 日誌記錄完整性"

調整超時時間

# 在 code_review.py 中修改
timeout=600  # 10分鐘超時

支援的檔案類型

allowed_extensions = {
    '.py', '.js', '.ts', '.jsx', '.tsx', '.html', '.css'
}

📊 查看結果

命令行查看

# 查看 Review 日誌
tail -f logs/code_review.log

# 查看最新報告
ls -la logs/review_*.md | tail -1
cat logs/review_*.md | tail -1

Web 介面查看

  1. 訪問 /code-review/
  2. 點擊「載入歷史記錄」
  3. 點擊報告的「查看」按鈕

GitHub Actions 查看

  1. 前往 GitHub Actions 頁面
  2. 查看 "Aider Code Review" workflow
  3. 下載 "code-review-reports" artifact

⚠️ 注意事項

安全性

  • 所有敏感資料已過濾
  • Review 結果僅儲存在本地
  • GitHub Actions 使用隔離環境

效能

  • ⚠️ 大型專案建議分批 Review
  • ⚠️ 複雜檔案可能需要更長時間
  • ⚠️ 同時執行多個 Review 可能影響效能

相容性

  • 支援 Python 3.8+
  • 需要 Aider 已安裝
  • 相容 Git 2.0+

🔧 故障排除

常見問題

Q: Aider 執行失敗

# 檢查 Aider 安裝
which aider
aider --version

# 重新安裝
pip install --upgrade aider-chat

Q: Git Hook 不執行

# 檢查權限
ls -la .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

# 測試執行
.git/hooks/pre-commit

Q: Web 介面無法載入

# 檢查 Blueprint 註冊
grep -n "code_review_bp" app.py

# 檢查路由
python3 -c "from routes.code_review_routes import code_review_bp; print(code_review_bp.url_prefix)"

除錯模式

# 啟用詳細日誌
export DEBUG=1
python3 scripts/code_review.py --auto --type basic

# 測試模式(不實際執行 Aider
python3 -c "
from scripts.code_review import AiderCodeReview
reviewer = AiderCodeReview()
print('變更檔案:', reviewer.get_changed_files())
"

📚 API 參考

命令行參數

python3 scripts/code_review.py [OPTIONS]

OPTIONS:
  --files, -f           指定要 Review 的檔案
  --type, -t            Review 類型 (basic|security|performance)
  --project-root, -p    專案根目錄路徑
  --auto, -a            自動 Review 暫存檔案
  --help, -h            顯示說明

Web API 端點

GET  /code-review/                    # 主頁面
POST /code-review/api/start-review    # 開始 Review
GET  /code-review/api/get-changed-files # 獲取變更檔案
GET  /code-review/api/review-history  # Review 歷史
GET  /code-review/api/view-report/<filename> # 查看報告
GET  /code-review/api/get-project-files # 專案檔案列表

🎉 開始使用

  1. 確保 Aider 已安裝

    pip install aider-chat
    
  2. 測試基本功能

    ./scripts/quick_review.sh
    
  3. 設定 Git Hooks可選

    # 已自動設定,測試:
    git add .
    git commit -m "test: 測試 Code Review"
    
  4. 啟動 Web 介面

    python3 app.py
    # 訪問 http://localhost:5000/code-review/
    

如有問題,請查看 logs/code_review.log 獲取詳細錯誤資訊。