PR review agent is an AI-powered code review assistant that leverages Google's Gemini LLM to analyze GitHub Pull Requests and repositories. It automatically reviews code changes, identifies issues, and provides constructive feedback by posting comments directly to pull requests or creating GitHub issues.
- 🔍 Pull Request Analysis: Automatically reviews PR diffs and posts feedback as comments
- 📊 Repository Scanning: Analyzes entire repositories to identify code quality issues
- 🚀 Issue Creation: Generates GitHub issues from analysis findings
- 🛠️ Customizable Analysis: Focus on bugs, performance issues, security vulnerabilities, and more
- 💬 Clear Feedback: Well-formatted markdown output with actionable recommendations
- Python 3.8+
- GitHub account with access to target repositories
- Google Cloud account (for Gemini API access)
-
Clone the repository:
git clone https://github.com/Benji918/PR-review-agent.git cd PR-review-agent -
Install dependencies:
pip install -r requirements.txt
-
Create a
.envfile in the project root with the following variables:GITHUB_PERSONAL_ACCESS_TOKEN=your_github_token GEMINI_API_KEY=your_gemini_api_key
- Go to your GitHub account settings: https://github.com/settings/tokens
- Click "Generate new token" (classic)
- Give it a descriptive name (e.g., "CodeReviewBot")
- Select the following scopes:
repo(Full control of private repositories) orpublic_repo(for public repositories only)read:org(if working with organization repositories)
- Click "Generate token"
- Copy the token and add it to your
.envfile
- Visit the Google AI Studio: https://aistudio.google.com/
- Create or sign in to your Google Cloud account
- Navigate to the API Keys section
- Create a new API key
- Copy the key and add it to your
.envfile
Start the FastAPI server:
uvicorn main:app --reloadThe server will be available at http://localhost:8000.
To analyze a pull request and post feedback as a comment:
GET /fetch_pr_diff?owner={OWNER}&repo={REPO}&pr_number={PR_NUMBER}
Example:
GET /fetch_pr_diff?owner=yourusername&repo=yourproject&pr_number=42
To analyze an entire repository and create issues:
POST /analyze-repo?owner={OWNER}&repo={REPO}
Example:
POST /analyze-repo?owner=yourusername&repo=yourproject
You can customize the behavior of CodeReviewBot by modifying the following parameters:
Edit the prompt in analyze_diff_with_gemini() to focus on specific aspects of code review:
prompt = f"""
You are a helpful and critical code reviewer powered by Google Gemini 2.0 Flash.
Review the following GitHub Pull Request diff. Provide constructive feedback focusing on:
- Potential bugs or logical errors
- Code clarity and readability
- Adherence to common best practices
- Possible performance issues
- Security vulnerabilities
- Suggestions for improvement
# Add or remove focus areas as needed
"""Adjust file filtering in fetch_repo_contents() to include or exclude specific file types:
# Skip binary files, large files, and files we probably don't want to analyze
if (item['size'] < 100000 and
not item['name'].endswith(('.png', '.jpg', '.jpeg', '.gif', '.svg', '.ico',
'.woff', '.woff2', '.ttf', '.eot', '.pdf', '.zip',
'.tar.gz', '.min.js', '.min.css'))):
# Process fileContributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
