S1rt3ge/code-review-agent

GitHub: S1rt3ge/code-review-agent

一个基于多智能体并行审查的自动化 GitHub Pull Request 代码分析平台。

Stars: 0 | Forks: 0

# AI 代码审查代理 自动使用四个专用 AI 代理并行审查 GitHub 拉取请求。审查结果以结构化 PR 评论形式返回,并在 React 仪表板中可视化。 ![Python](https://img.shields.io/badge/Python-3.12-blue) ![React](https://img.shields.io/badge/React-19-61dafb) ![FastAPI](https://img-shields.io/badge/FastAPI-0.104-009688) ![Tests](https://img.shields.io/badge/backend_tests-148_passing-brightgreen) ![License](https://img.shields.io/badge/license-MIT-green) ## 工作原理 ``` GitHub PR opened │ ▼ POST /api/github/webhook (HMAC-SHA256 verified) │ ▼ FastAPI creates Review record, returns 202 │ ▼ (background task) LangGraph Orchestrator │ ├──► Security Agent ─┐ ├──► Performance Agent─┤ (parallel, 30s timeout each) ├──► Style Agent ─┤ └──► Logic Agent ─┘ │ ▼ Result Aggregator (dedup + severity sort) │ ┌─────────┴──────────┐ ▼ ▼ Dashboard UI PR Comment posted (real-time via WS) to GitHub ``` **四个代理,各自专注于一个关注点:** | 代理 | 检测内容 | |---|---| | 安全 | SQL 注入、XSS、硬编码密钥、弱加密、身份验证绕过 | | 性能 | N+1 查询、O(n²) 循环、内存泄漏、不必要的大拷贝 | | 风格 | 命名规范、行长度、缺少文档字符串、未使用的导入 | | 逻辑 | 索引越界、空指针解引用、类型不匹配、边界错误 | ## 功能特性 - 并行代理执行,带每个代理的超时控制 - 多 LLM 支持:Claude Opus 4.6(主用)、GPT(备用)、Ollama(本地/私有) - 分析运行时通过 WebSocket 实时推送进度更新 - GitHub App 集成 — Webhook 触发并自动评论 PR - React 仪表板,包含审查历史、发现结果表格和每个代理的统计 - JWT 认证、加密的 API 密钥存储(Fernet) - 148 个后端测试 + 30 个前端测试 ## 技术栈 | 层级 | 技术 | |---|---| | 后端 | Python 3.12、FastAPI、LangGraph、SQLAlchemy(异步) | | 数据库 | PostgreSQL 16 | | 前端 | React 19、JavaScript + JSDoc、TailwindCSS、Zustand | | LLM | Claude Opus 4.6、OpenAI GPT、Ollama(Qwen2.5-Coder) | | 认证 | JWT(HS256)、Fernet 加密存储的密钥 | | 基础设施 | Docker、docker-compose | ## 快速开始 ### 先决条件 - Docker + Docker Compose - GitHub 账户(用于 App 集成) - Anthropic 或 OpenAI API 密钥(可选 — 也可以使用本地 Ollama) ### 1. 克隆并配置 ``` git clone https://github.com/S1rt3ge/code-review-agent cd code-review-agent cp .env.example .env ``` 打开 `.env` 并设置所需值: ``` # Required JWT_SECRET=any-long-random-string-change-this FERNET_KEY= # Optional but needed for real analysis ANTHROPIC_API_KEY=sk-ant-... OPENAI_API_KEY=sk-... # Required for GitHub App integration GITHUB_APP_ID=123456 GITHUB_APP_PRIVATE_KEY=-----BEGIN RSA PRIVATE KEY-----... GITHUB_WEBHOOK_SECRET=your-webhook-secret ``` ### 2. 启动所有服务 ``` docker compose up --build ``` | 服务 | URL | |---|---| | 前端 | http://localhost:5173 | | 后端 API | http://localhost:8000 | | API 文档(Swagger) | http://localhost:8000/docs | | pgAdmin | http://localhost:5050 | ### 3. 创建账户 打开 http://localhost:5173 并注册。或通过 curl: ``` # Register curl -s -X POST http://localhost:8000/api/auth/register \ -H "Content-Type: application/json" \ -d '{"email":"you@example.com","username":"you","password":"YourPass123!"}' # Get a JWT token curl -s -X POST http://localhost:8000/api/auth/token \ -d "username=you@example.com&password=YourPass123!" ``` ## GitHub App 设置 这用于 Webhook 触发以及将评论返回到 PR。 1. 前往 https://github.com/settings/apps/new 并创建一个新的 GitHub App: - **Webhook URL:** `https://your-domain.com/api/github/webhook` - **权限:** 拉取请求 → 读写,内容 → 只读 - **订阅事件:** `pull_request` 2. 生成私钥并下载 `.pem` 文件。 3. 添加到 `.env`: ``` GITHUB_APP_ID=123456 GITHUB_APP_PRIVATE_KEY=<.pem 文件内容,将换行替换为 \n> GITHUB_WEBHOOK_SECRET=<在 App 设置中设置的密钥> ``` 4. 从应用的安装页面将 App 安装到你的仓库。 安装后,打开一个 PR 会自动触发审查。 ## 运行测试 ``` # All backend tests (requires running Postgres via docker compose) docker compose --profile test run --rm tests # Specific test file docker compose --profile test run --rm tests pytest backend/tests/test_integration.py -v # Frontend tests (no Docker needed) cd frontend && npm test -- --run ``` **当前覆盖率:** - 后端:148 个测试 — 代理、服务、认证、审查、设置、仪表板、Webhook、集成 - 前端:30 个测试 — 组件(StatusBadge、FindingsTable)、存储(auth)、钩子(useApi) ## 项目结构 ``` code-review-agent/ ├── backend/ │ ├── agents/ │ │ ├── orchestrator.py # LangGraph async dispatch (parallel) │ │ ├── security_agent.py │ │ ├── performance_agent.py │ │ ├── style_agent.py │ │ ├── logic_agent.py │ │ └── llm_router.py # Claude / GPT / Ollama selection │ ├── routers/ │ │ ├── auth.py # register, token (OAuth2), /me │ │ ├── reviews.py # CRUD + analyze + post-comment │ │ ├── settings.py # LLM config, encrypted key storage │ │ ├── dashboard.py # aggregate stats │ │ └── github.py # webhook receiver │ ├── services/ │ │ ├── analyzer.py # background analysis runner │ │ ├── github_api.py # GitHub App auth + REST calls │ │ ├── code_extractor.py # unified diff → CodeChunk │ │ ├── result_aggregator.py # dedup + severity ranking │ │ ├── pr_commenter.py # markdown comment builder │ │ └── ws_manager.py # WebSocket broadcast manager │ ├── models/ │ │ ├── db_models.py # SQLAlchemy ORM │ │ └── schemas.py # Pydantic request/response schemas │ └── utils/ │ ├── auth.py # JWT + PBKDF2 password hashing │ ├── crypto.py # Fernet encrypt/decrypt │ └── webhooks.py # HMAC-SHA256 signature verification ├── frontend/ │ └── src/ │ ├── pages/ # Dashboard, ReviewDetail, Settings, Login │ ├── components/ # FindingsTable, AgentStatus, Navbar, StatusBadge │ ├── hooks/ # useApi, useWebsocket, useSettings │ └── store/ # Zustand: useAuthStore, useSettingsStore, useUiStore ├── supabase/migrations/ # 001–005 SQL migrations (auto-applied by Postgres container) ├── .github/workflows/ci.yml # CI: backend tests + frontend build on push/PR ├── Dockerfile # Multi-stage: Node build → Python runtime ├── docker-compose.yml ├── requirements.txt └── .env.example ``` ## API 参考 | 方法 | 路径 | 认证 | 描述 | |---|---|---|---| | POST | `/api/auth/register` | — | 创建账户,返回 JWT | | POST | `/api/auth/token` | — | 登录(OAuth2 表单),返回 JWT | | GET | `/api/auth/me` | JWT | 当前用户资料 | | GET | `/api/reviews` | JWT | 列出审查(分页) | | POST | `/api/reviews` | JWT | 手动创建审查 | | GET | `/api/reviews/{id}` | JWT | 审查详情 + 发现结果 | | POST | `/api/reviews/{id}/analyze` | JWT | 触发分析 | | POST | `/api/reviews/{id}/post-comment` | JWT | 将发现结果发布到 GitHub PR | | GET | `/api/settings` | JWT | 获取 LLM 配置 | | PUT | `/api/settings` | JWT | 更新 LLM 配置和 API 密钥 | | POST | `/api/settings/test-llm` | JWT | 测试 LLM 连接性 | | GET | `/api/dashboard/stats` | JWT | 按用户聚合统计 | | POST | `/api/github/webhook` | 签名 | GitHub Webhook 接收器 | | WS | `/ws/progress/{review_id}` | — | 实时代理进度 | 交互式文档:**http://localhost:8000/docs** ## 环境变量 | 变量 | 是否必需 | 描述 | |---|---|---| | `DATABASE_URL` | 是 | `postgresql+psycopg://user:pass@host/db` | | `JWT_SECRET` | 是 | 用于签名 JWT 令牌的密钥 | | `FERNET_KEY` | 是 | 用于加密存储 API 密钥的密钥 | | `ANTHROPIC_API_KEY` | 否 | Claude API 密钥(应用级备用) | | `OPENAI_API_KEY` | 否 | OpenAI API 密钥(应用级备用) | | `OLLAMA_HOST` | 否 | Ollama 基础 URL(默认:`http://localhost:11434`) | | `GITHUB_APP_ID` | 否 | GitHub App ID | | `GITHUB_APP_PRIVATE_KEY` | 否 | GitHub App RSA 私钥 | | `GITHUB_WEBHOOK_SECRET` | 否 | Webhook HMAC 密钥 | | `CORS_ORIGINS` | 否 | 允许的来源(默认:localhost 开发端口) | ## 许可证 MIT
标签:AI, AI代码审查, AI编程辅助, AI风险缓解, AV绕过, Claude, CVE检测, Dashboard, FastAPI, GitHub PR审查, GPT, HMAC-SHA256, LangGraph, LLM评估, Ollama, PyRIT, Python, React, SEO: AI代码审查, SEO: GitHub助手, SEO: 自动化审查, Syscalls, Webhook, WebSocket, 代码风格审查, 依赖分析, 力导向图, 后台任务, 多LLM支持, 多智能体系统, 安全审查, 实时可视化, 并行处理, 性能审查, 无后门, 服务枚举, 本地模型, 测试用例, 漏洞管理, 私有部署, 结构化评论, 自动化代码审查, 请求拦截, 逆向工具, 逻辑审查