4.7 KiB
PR Reviewer
An automated pull request review system using CrewAI and MCP (Model Context Protocol).
Overview
This system provides automated code, security, and infrastructure reviews for pull requests using a multi-agent approach. It leverages CrewAI for orchestrating specialized review agents and MCP (Model Context Protocol) for integrating with various static analysis tools.
Features
- Code Review: Uses Semgrep (via MCP) to check code quality, best practices, and maintainability
- Security Review: Uses Trivy (native MCP) to identify security vulnerabilities
- Infrastructure Review: Uses Hadolint and Checkov (via MCP wrappers) to review Dockerfiles and Kubernetes manifests
- Contextual Review: Incorporates customizable guidelines for code, security, and infrastructure reviews
- Automated Orchestration: Uses CrewAI Flows to manage the review process
- REST API: FastAPI endpoint for triggering reviews
- Containerized: Docker support for easy deployment
Architecture
The system follows a modular architecture with:
- State management using Pydantic models
- LLM factory for flexible provider support (OpenAI, Anthropic, Ollama)
- Context resolution system for incorporating review guidelines
- Crew-based implementation for each review type (code, security, infrastructure)
- MCP server integrations for static analysis tools
- Flow-based orchestration for managing the review process
- RESTful API for integration with CI/CD systems
Installation
Prerequisites
- Python 3.10-3.13
- UV package manager
- Git
- Docker (optional, for containerized deployment)
Local Development
- Clone the repository
- Install UV package manager:
curl -LsSf https://astral.sh/uv/install.sh | sh - Activate UV environment:
source $HOME/.local/bin/env - Create virtual environment:
uv venv .venv - Activate virtual environment:
source .venv/bin/activate - Install dependencies:
uv pip install -e . - Configure environment variables (see
.env.example)
Docker Deployment
- Build the Docker image:
docker build -t pr-reviewer . - Run the container:
docker run -p 8000:8000 --env-file .env pr-reviewer
Usage
API Endpoints
Health Check
GET /api/v1/health
Returns the health status of the service.
Trigger PR Review
POST /api/v1/review
Initiates a pull request review.
Request Body:
{
"pr_id": "123",
"title": "Add new feature",
"description": "This PR adds a new feature to the application",
"repo": {
"name": "my-repo",
"url": "https://github.com/user/my-repo"
},
"source": {
"branch": "feature/new-feature",
"commit": "abc123"
},
"target": {
"branch": "main",
"commit": "def456"
},
"files": [
{
"path": "src/main.py",
"content": "print('Hello World')",
"status": "modified",
"additions": 1,
"deletions": 0
}
],
"context": {
"code_review": "Follow PEP8 guidelines",
"security_review": "Check for SQL injection vulnerabilities",
"infra_review": "Ensure Dockerfile follows best practices"
}
}
Response:
{
"review_id": "uuid-string",
"status": "completed",
"timestamp": "2023-05-08T10:00:00Z",
"results": {
"code_review": "Code review results...",
"security_review": "Security review results...",
"infra_review": "Infrastructure review results...",
"summary": "Synthesized review summary..."
},
"metadata": {
"processing_time_seconds": 45.2,
"pr_id": "123",
"repo": {
"name": "my-repo",
"url": "https://github.com/user/my-repo"
}
}
}
Configuration
Environment Variables
See .env.example for detailed configuration options.
Context Files
Default review guidelines are located in contexts/defaults/:
code_review.md: Coding practice guidelinessecurity_review.md: Security guidelinesinfra_review.md: Infrastructure guidelines
These can be overridden via the API context parameter.
Development
Running Tests
# Run unit tests
pytest
# Run tests with coverage
pytest --cov=src.pr_reviewer
# Run specific test categories
pytest tests/unit/
pytest tests/integration/
Code Style
The project uses Black for code formatting and Flake8 for linting.
Run formatting:
black src/
Run linting:
flake8 src/
Deployment
Kubernetes
Kubernetes manifests are available in the k8s/ directory:
- Secret for LLM configuration
- Deployment for the PR Reviewer service
- Service for exposing the API
Gitea Actions
GitHub Actions workflow for CI/CD is available in .gitea/workflows/deploy.yaml.
License
MIT
Contributing
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Open a pull request