Skip to content

10HQ/ai-code-reviewer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AI代码审查助手

基于大语言模型的智能代码审查工具,支持多种编程语言。

功能特性

  • 🔍 自动代码审查 - 一键分析代码质量
  • 🐛 Bug检测 - 发现潜在的错误和漏洞
  • 🔒 安全检查 - 识别安全隐患
  • 性能优化 - 提供性能改进建议
  • 📊 代码统计 - 代码复杂度、行数等统计
  • 📝 审查报告 - 生成详细的审查报告

支持语言

  • Python
  • JavaScript/TypeScript
  • Java
  • C/C++
  • Go
  • Rust
  • PHP
  • Ruby

安装

# 克隆仓库
git clone https://github.com/10HQ/ai-code-reviewer.git

# 进入目录
cd ai-code-reviewer

# 安装依赖
pip install -r requirements.txt

使用方法

命令行使用

# 审查单个文件
python reviewer.py --file main.py

# 审查整个目录
python reviewer.py --dir ./src

# 指定语言
python reviewer.py --file app.js --language javascript

# 生成报告
python reviewer.py --file main.py --report report.md

Python API使用

from reviewer import CodeReviewer

# 初始化审查器
reviewer = CodeReviewer()

# 审查代码
result = reviewer.review_file("main.py")

# 打印结果
print(result.summary)
print(result.issues)
print(result.suggestions)

配置

创建 config.json 文件:

{
  "api_key": "your-api-key",
  "model": "deepseek-coder",
  "max_tokens": 4096,
  "temperature": 0.3,
  "rules": {
    "security": true,
    "performance": true,
    "style": true,
    "complexity": true
  }
}

审查规则

安全检查

  • SQL注入风险
  • XSS漏洞
  • 硬编码密码
  • 不安全的随机数
  • 路径遍历漏洞

性能检查

  • 循环优化
  • 内存泄漏
  • 数据库查询优化
  • 缓存建议

代码质量

  • 代码复杂度
  • 重复代码
  • 命名规范
  • 注释质量

输出示例

==================================================
📊 代码审查报告
==================================================
文件: main.py
语言: Python
行数: 156
复杂度: 12

🔍 发现问题: 3个
  ⚠️ [安全] 第45行: 使用eval()函数存在安全风险
  ⚠️ [性能] 第78行: 循环内重复计算,建议提取到循环外
  ⚠️ [质量] 第102行: 函数复杂度过高,建议拆分

💡 优化建议: 5条
  1. 使用参数化查询替代字符串拼接
  2. 添加输入验证
  3. 使用生成器减少内存占用
  4. 添加类型注解提高可读性
  5. 使用async/await优化IO操作

📊 代码统计
  - 总行数: 156
  - 代码行: 120
  - 注释行: 25
  - 空行: 11
  - 注释率: 16%
==================================================

高级功能

自定义规则

from reviewer import CodeReviewer, Rule

# 自定义规则
custom_rules = [
    Rule(
        name="no_print",
        pattern=r"print\(",
        message="生产代码不应使用print,请使用logging",
        severity="warning"
    ),
    Rule(
        name="no_magic_number",
        pattern=r"\b\d{4,}\b",
        message="避免使用魔法数字,请定义为常量",
        severity="info"
    )
]

reviewer = CodeReviewer(custom_rules=custom_rules)

CI/CD集成

# .github/workflows/code-review.yml
name: Code Review
on: [push, pull_request]
jobs:
  review:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Setup Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.9'
      - name: Install dependencies
        run: pip install -r requirements.txt
      - name: Run code review
        run: python reviewer.py --dir ./src --report review.md
      - name: Upload report
        uses: actions/upload-artifact@v2
        with:
          name: code-review-report
          path: review.md

许可证

MIT License

作者

洹(灵台未央)- AI全栈开发者

贡献

欢迎提交Issue和Pull Request!

About

基于大语言模型的智能代码审查工具,支持多种编程语言

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages