An intelligent automated code review system that integrates with Gerrit and uses Google Gemini AI to provide comprehensive code analysis across 16 different quality criteria.
🤖 AI-Powered Analysis: Uses Google Gemini to provide intelligent code reviews 📊 Comprehensive Scoring: Evaluates code across 16 different criteria 🔗 Gerrit Integration: Seamlessly integrates with Gerrit via webhooks 🛡️ Security Analysis: Identifies potential security vulnerabilities 📈 Complexity Analysis: Measures cyclomatic complexity and code smells 🔍 Rule-Based Checks: Combines AI analysis with traditional rule-based checks 📝 Detailed Feedback: Provides actionable suggestions and recommendations ⚡ Real-time Processing: Processes reviews asynchronously via background queue
The system evaluates code changes across these 16 criteria:
- Are Code Changes Optimized - Performance and efficiency evaluation
- Are Code Changes Relative - Relevance to intended functionality
- Is Code Formatted - Style consistency and formatting
- Is Code Well Written - Overall code quality and readability
- Are Comments Written - Adequate and meaningful comments
- Cyclomatic Complexity Score - Code complexity measurement
- Missing Elements - Missing components like error handling
- Loopholes - Logic gaps and edge cases
- Is Commit Message Well Written - Commit message quality
- Is Naming Convention Followed - Adherence to naming standards
- Are There Any Spelling Mistakes - Spelling errors in code/comments
- Security Concerns Any - Potential security vulnerabilities
- Is Code Duplicated - Code duplication detection
- Are Constants Defined Centrally - Proper constant management
- Is Code Modular - Modularity and separation of concerns
- Is Logging Done Properly - Proper logging implementation
- Python 3.8 or higher
- Gerrit server with webhook support
- Google Gemini API key
-
Clone or download the project:
# The system is already set up in this directory cd code-review
-
Install dependencies:
pip install -r requirements.txt
-
Configure environment variables:
cp .env.example .env # Edit .env with your configuration -
Set up your environment file (.env):
# Gemini API Configuration GEMINI_API_KEY=your_gemini_api_key_here GEMINI_MODEL=gemini-1.5-flash # Gerrit Configuration GERRIT_HOST=your-gerrit-server.com GERRIT_PORT=8080 GERRIT_USERNAME=your-gerrit-username GERRIT_PASSWORD=your-gerrit-password # Application Configuration APP_HOST=0.0.0.0 APP_PORT=5000 AUTO_POST_REVIEW=true MIN_REVIEW_SCORE=7.0
python src/main.pyThe service will start and listen for webhooks on the configured port (default: 5000).
- GET /health - Health check endpoint
- GET /status - System status and queue information
- GET /config - Current configuration
- POST /webhook - Gerrit webhook endpoint
- POST /manual-review - Manually trigger a review
Configure Gerrit to send webhooks to your service:
- In Gerrit, go to Projects → Your Project → General
- Add webhook URL:
http://your-server:5000/webhook - Select events:
patchset-created - Save configuration
You can manually trigger a review using the REST API:
curl -X POST http://localhost:5000/manual-review \\
-H "Content-Type: application/json" \\
-d '{
"change_id": "your-change-id",
"revision_id": "current",
"project": "your-project",
"branch": "main"
}'| Variable | Description | Default |
|---|---|---|
GEMINI_API_KEY |
Google Gemini API key | Required |
GEMINI_MODEL |
Gemini model to use | gemini-1.5-flash |
GERRIT_HOST |
Gerrit server hostname | Required |
GERRIT_PORT |
Gerrit server port | 8080 |
GERRIT_USERNAME |
Gerrit username | Required |
GERRIT_PASSWORD |
Gerrit password | Required |
APP_HOST |
Application host | 0.0.0.0 |
APP_PORT |
Application port | 5000 |
AUTO_POST_REVIEW |
Auto-post reviews to Gerrit | true |
MIN_REVIEW_SCORE |
Minimum score for approval | 7.0 |
LOG_LEVEL |
Logging level | INFO |
You can customize review criteria in config/review_criteria.json:
{
"review_criteria": {
"areCodeChangesOptimized": {
"label": "Are Code Changes Optimized",
"weight": 1.0,
"enabled": true,
"thresholds": {
"excellent": 9,
"good": 7,
"acceptable": 5,
"poor": 3
}
}
}
}The system consists of several key components:
main.py- Flask web server and webhook handlergerrit_client.py- Gerrit REST API integrationllm_client.py- Google Gemini AI integrationreview_evaluator.py- Main orchestration logicutils.py- Utility functions and helpersconfig.py- Configuration management
- Webhook Reception - Gerrit sends webhook on code push
- Change Extraction - System extracts change information
- Code Analysis - Combined AI and rule-based analysis
- Review Generation - Comprehensive review with scores
- Result Posting - Review posted back to Gerrit (optional)
GET /healthResponse:
{
"status": "healthy",
"services": {
"gerrit_client": "ok",
"gemini_client": "ok",
"review_evaluator": "ok"
},
"queue_size": 0,
"timestamp": "2025-01-07 12:00:00 UTC"
}POST /manual-review
Content-Type: application/json
{
"change_id": "your-change-id",
"revision_id": "current",
"project": "your-project",
"branch": "main"
}code-review/
├── src/
│ ├── __init__.py
│ ├── main.py # Main application
│ ├── config.py # Configuration management
│ ├── gerrit_client.py # Gerrit integration
│ ├── llm_client.py # Gemini AI integration
│ ├── review_evaluator.py # Review orchestration
│ └── utils.py # Utility functions
├── config/
│ ├── review_criteria.json # Review criteria config
│ └── README.md
├── logs/ # Log files
├── requirements.txt # Python dependencies
├── .env.example # Environment template
└── README.md # This file
- Update
config.py- Add new criterion toReviewCriteria.CRITERIA - Update prompt - Modify prompt in
llm_client.py - Add rule-based checks - Implement in
review_evaluator.py - Update configuration - Modify
config/review_criteria.json
# Install test dependencies
pip install pytest pytest-cov
# Run tests
pytest
# Run with coverage
pytest --cov=srcThe system provides comprehensive logging:
- Console output - Real-time status and errors
- File logging - Detailed logs in
logs/code_review.log - Review storage - Individual reviews saved as JSON files
- Store API keys securely in environment variables
- Use HTTPS for production deployments
- Implement webhook authentication if needed
- Regularly rotate API keys and passwords
- Monitor logs for suspicious activity
-
Gemini API errors
- Verify API key is correct
- Check API quotas and limits
- Ensure model name is valid
-
Gerrit connection issues
- Verify hostname and port
- Check credentials
- Ensure Gerrit REST API is enabled
-
Webhook not received
- Check Gerrit webhook configuration
- Verify network connectivity
- Check firewall settings
Enable debug mode for verbose logging:
export APP_DEBUG=true
export LOG_LEVEL=DEBUG
python src/main.py- Reviews are processed asynchronously in background queue
- Configurable timeouts prevent hanging requests
- File size limits prevent oversized analyses
- Connection pooling for efficient API usage
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is available under the MIT License.
For issues and questions:
- Check the troubleshooting section
- Review the logs for error details
- Open an issue with detailed information
Happy Code Reviewing! 🚀