Automated GitHub Pull Request Review Agent powered by Lyzr AI
An intelligent backend system that analyzes GitHub pull requests and generates comprehensive, actionable code review comments using multi-agent AI reasoning.
- π Real-Time Webhook Integration: Automatically analyzes PRs when opened or updated via GitHub webhooks
- π€ Automated PR Analysis: Fetch and analyze GitHub PR diffs automatically
- π§ Multi-Agent AI Review: Powered by Lyzr Agent Studio for intelligent code analysis
- π Comprehensive Issue Detection: Identifies logic errors, security vulnerabilities, performance issues, and best practice violations
- π¬ Structured Feedback: Generates clear, actionable review comments with severity levels
- π¨ Beautiful Web UI: Modern dashboard for testing and monitoring
- π GitHub Integration: Automatically posts review comments to GitHub PRs
- π Review History: Stores all reviews in a database for future reference
- π RESTful API: Clean, well-documented API endpoints
- π Production Ready: Built with FastAPI, includes logging, error handling, CORS support, and security
- Python 3.9+
- GitHub Personal Access Token with
reposcope - Lyzr Agent Studio account and API key
- SQLite (default) or PostgreSQL (optional)
cd "c:\Users\Nirupam\Desktop\PR Agent Backend"python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activatepip install -r requirements.txtThe .env file is already configured with your credentials. Verify it contains:
# GitHub Configuration
GITHUB_TOKEN=your_github_token
GITHUB_WEBHOOK_SECRET=your_webhook_secret
# Lyzr Configuration
LYZR_API_KEY=your_lyzr_api_key
LYZR_USER_ID=your_email@example.com
LYZR_AGENT_ID=your_agent_id
# Application
SECRET_KEY=your_secret_key
DATABASE_URL=sqlite:///./pr_reviews.db
PORT=8000Windows:
start.batLinux/Mac:
chmod +x start.sh
./start.shDevelopment Mode:
source venv/Scripts/activate # Windows: venv\Scripts\activate
python main.pyOr using uvicorn directly:
uvicorn main:app --reload --host 127.0.0.1 --port 8000Production Mode:
uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4- π¨ Web UI Dashboard: http://127.0.0.1:8000
- π API Documentation: http://127.0.0.1:8000/docs
- π§ ReDoc: http://127.0.0.1:8000/redoc
- β€οΈ Health Check: http://127.0.0.1:8000/api/v1/health
A beautiful, modern web interface is available for easy testing and interaction:
Features:
- π Real-time system health monitoring
- π Analyze PRs with a simple form
- οΏ½ View review history
- π― Beautiful, responsive design
- π± Mobile-friendly interface
Access the Dashboard: Open your browser and navigate to: http://127.0.0.1:8000
- Web UI Dashboard: http://127.0.0.1:8000 β NEW!
- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
GET /api/v1/healthCheck the health status of the application and its dependencies.
POST /api/v1/reviews/analyze
Content-Type: application/json
{
"pr_url": "https://github.com/owner/repo/pull/123",
"post_to_github": true
}Analyzes a GitHub PR and optionally posts the review to GitHub.
Response:
{
"review_id": 1,
"pr_url": "https://github.com/owner/repo/pull/123",
"status": "completed",
"summary": "Overall review summary...",
"total_issues": 5,
"issues": [
{
"file": "src/main.py",
"line": 42,
"category": "Security",
"severity": "HIGH",
"issue": "SQL injection vulnerability detected",
"suggestion": "Use parameterized queries",
"code_example": "cursor.execute('SELECT * FROM users WHERE id = ?', (user_id,))"
}
],
"positive_feedback": "Good code structure and documentation",
"pr_metadata": {...},
"created_at": "2025-11-15T10:30:00",
"completed_at": "2025-11-15T10:30:45"
}POST /api/v1/reviews/analyze-by-details
Content-Type: application/json
{
"owner": "username",
"repo": "repository",
"pr_number": 123,
"post_to_github": true
}Alternative endpoint to analyze PR by owner, repo, and PR number.
GET /api/v1/reviews/{review_id}Retrieve a specific review by ID.
GET /api/v1/reviews?skip=0&limit=50List all reviews with pagination.
DELETE /api/v1/reviews/{review_id}Delete a review by ID.
PR Agent Backend/
βββ app/
β βββ api/ # API routes
β β βββ reviews.py # Review endpoints
β β βββ health.py # Health check endpoints
β βββ core/ # Core configuration
β β βββ config.py # Settings and environment variables
β β βββ logger.py # Logging configuration
β βββ models/ # Database models
β β βββ review.py # PRReview and ReviewComment models
β βββ schemas/ # Pydantic schemas
β β βββ review.py # Request/response schemas
β βββ services/ # Business logic
β β βββ github_service.py # GitHub API integration
β β βββ lyzr_service.py # Lyzr Agent integration
β βββ utils/ # Utilities
β βββ database.py # Database utilities
βββ main.py # Application entry point
βββ requirements.txt # Python dependencies
βββ .env # Environment variables (configured)
βββ .env.example # Environment template
βββ README.md # This file
| Variable | Description | Required | Default |
|---|---|---|---|
GITHUB_TOKEN |
GitHub Personal Access Token | Yes | - |
GITHUB_WEBHOOK_SECRET |
Webhook secret for validation | No | - |
LYZR_API_KEY |
Lyzr API key | Yes | - |
LYZR_USER_ID |
Lyzr user email | Yes | - |
LYZR_AGENT_ID |
Lyzr agent ID | Yes | - |
LYZR_BASE_URL |
Lyzr API base URL | No | https://agent.api.lyzr.app/v3 |
SECRET_KEY |
Application secret key | Yes | - |
DATABASE_URL |
Database connection string | No | sqlite:///./pr_reviews.db |
PORT |
Server port | No | 8000 |
LOG_LEVEL |
Logging level | No | INFO |
MAX_DIFF_SIZE |
Max diff lines to process | No | 10000 |
Your Lyzr Agent should be configured with:
- Agent Name: PR Code Review Agent
- Agent Role: Senior Code Reviewer and Quality Assurance Specialist
- Agent Goal: Provide comprehensive code review feedback
- Agent Instructions: See Lyzr agent configuration section in setup docs
Set up webhooks to automatically analyze PRs when they're opened or updated!
-
Install ngrok (for local development):
# Download from https://ngrok.com/download ngrok http 8000 -
Copy ngrok URL (e.g.,
https://abc123.ngrok.io) -
Configure GitHub Webhook:
- Go to your repo β Settings β Webhooks β Add webhook
- Payload URL:
https://abc123.ngrok.io/webhook/github - Content type:
application/json - Secret: Your
GITHUB_WEBHOOK_SECRETfrom.env - Events: Select "Pull requests"
-
Test it: Open a PR and watch the magic happen! π
π Detailed Guide: See WEBHOOK_SETUP.md for complete instructions.
# Test webhook endpoint
python test_webhook.pycurl -X POST "http://localhost:8000/api/v1/reviews/analyze" \
-H "Content-Type: application/json" \
-d '{
"pr_url": "https://github.com/owner/repo/pull/123",
"post_to_github": false
}'import requests
response = requests.post(
"http://localhost:8000/api/v1/reviews/analyze",
json={
"pr_url": "https://github.com/owner/repo/pull/123",
"post_to_github": False
}
)
print(response.json())The application uses SQLite by default. The database file pr_reviews.db is created automatically on first run.
pr_reviews table:
id: Primary keypr_url: GitHub PR URLpr_number: PR numberrepository: Repository namepr_title: PR titlepr_author: PR authorstatus: Review status (pending/in_progress/completed/failed)review_summary: Overall review summarytotal_issues: Number of issues foundreview_data: Complete review JSONcreated_at: Creation timestampupdated_at: Update timestampcompleted_at: Completion timestamperror_message: Error message if failed
review_comments table:
id: Primary keyreview_id: Foreign key to pr_reviewsfile_path: File pathline_number: Line numbercategory: Issue categoryseverity: Issue severityissue_description: Issue descriptionsuggestion: Suggested fixcode_example: Code examplecreated_at: Creation timestamp
The API returns standard HTTP status codes:
200 OK: Successful GET request201 Created: Successful POST request204 No Content: Successful DELETE request400 Bad Request: Invalid input404 Not Found: Resource not found422 Unprocessable Entity: Validation error500 Internal Server Error: Server error
Error responses include:
{
"error": "Error type",
"detail": "Detailed error message",
"status_code": 500
}Logs are output to stdout. In development mode, logs are human-readable. In production mode, logs are in JSON format for easier parsing.
Log levels: DEBUG, INFO, WARNING, ERROR, CRITICAL
- GitHub token is never exposed in logs or responses
- All sensitive data is stored in environment variables
- CORS is configured for specific origins
- Database credentials are protected
- Input validation using Pydantic schemas
- Add webhook support for real-time PR monitoring
- Implement rate limiting
- Add Redis caching
- Support for multiple LLM providers
- Custom review templates
- Slack/Discord notifications
- Review metrics and analytics
- Support for GitLab and Bitbucket
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
This project is part of the Lyzr AI Backend Engineering Intern Challenge.
Built for the Lyzr AI Backend Engineering Intern Challenge
- FastAPI: Modern Python web framework
- Lyzr AI: Multi-agent orchestration platform
- PyGithub: GitHub API wrapper
- SQLAlchemy: SQL toolkit and ORM
For issues or questions:
- Check the API documentation at
/docs - Review the logs for error details
- Check GitHub token permissions
- Verify Lyzr agent configuration
Built with β€οΈ using FastAPI and Lyzr AI