Skip to content

Ayoush/github-ai-reviewer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ€– GitHub AI Code Reviewer (Python)

An intelligent GitHub App that automatically reviews pull requests using Google's Gemini AI. Built with Python, Flask, and the GitHub API.

✨ Features

  • πŸ” Automatic Code Review: Reviews code in pull requests using Gemini AI
  • πŸš€ Real-time Processing: Responds to PR events instantly via webhooks
  • πŸ›‘οΈ Secure: Proper webhook signature verification and GitHub App authentication
  • ⚑ Async Support: Optional async version for better performance
  • πŸ§ͺ Local Testing: Easy local development with ngrok
  • πŸ“¦ Easy Deployment: Ready for Vercel, Heroku, or any Python hosting

πŸš€ Quick Start

1. Clone and Setup

git clone <your-repo>
cd github-ai-reviewer-python

# Run the setup script
python setup.py

2. Manual Setup (Alternative)

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On macOS/Linux
# venv\Scripts\activate   # On Windows

# Install dependencies
pip install -r requirements.txt

# Copy environment template
cp .env.example .env

3. Configure Environment

Edit .env file with your values:

GITHUB_APP_ID=your_app_id_here
GITHUB_INSTALLATION_ID=your_installation_id_here
WEBHOOK_SECRET=your_webhook_secret_here
GEMINI_API_KEY=your_gemini_api_key_here

4. GitHub App Setup

  1. Go to GitHub Settings β†’ Developer settings β†’ GitHub Apps β†’ New GitHub App
  2. Fill in the details:
    • Name: AI Code Reviewer (Dev)
    • Homepage URL: http://localhost:5000
    • Webhook URL: https://your-ngrok-url.ngrok.io/webhook
    • Webhook Secret: Generate a random string
  3. Permissions:
    • Repository permissions:
      • Contents: Read
      • Pull requests: Write
      • Metadata: Read
  4. Subscribe to events: Pull request
  5. Download the private key and save as private-key.pem

5. Local Testing

# Terminal 1: Start ngrok
ngrok http 5000

# Terminal 2: Start the server
python server.py

# Or use the async version for better performance
python async_server.py

6. Test the Setup

  1. Update your GitHub App webhook URL with the ngrok URL
  2. Create a test pull request in a repository where the app is installed
  3. Watch the magic happen! πŸŽ‰

πŸ“ Project Structure

github-ai-reviewer-python/
β”œβ”€β”€ server.py              # Main Flask server
β”œβ”€β”€ async_server.py        # Async version with Quart
β”œβ”€β”€ setup.py              # Setup script
β”œβ”€β”€ test_server.py        # Unit tests
β”œβ”€β”€ requirements.txt      # Python dependencies
β”œβ”€β”€ .env.example         # Environment template
β”œβ”€β”€ vercel.json          # Vercel deployment config
β”œβ”€β”€ private-key.pem      # GitHub App private key (you add this)
└── README.md           # This file

πŸ§ͺ Testing

# Run unit tests
python test_server.py

# Test health endpoint
curl http://localhost:5000/health

# Test webhook (with proper signature)
curl -X POST http://localhost:5000/webhook \
  -H "Content-Type: application/json" \
  -H "X-GitHub-Event: pull_request" \
  -d '{"action": "opened", "pull_request": {...}}'

πŸš€ Deployment

Vercel (Recommended)

# Install Vercel CLI
npm i -g vercel

# Deploy
vercel --prod

# Set environment variables in Vercel dashboard

Heroku

# Create Heroku app
heroku create your-app-name

# Set environment variables
heroku config:set GITHUB_APP_ID=your_app_id
heroku config:set GITHUB_INSTALLATION_ID=your_installation_id
heroku config:set WEBHOOK_SECRET=your_webhook_secret
heroku config:set GEMINI_API_KEY=your_gemini_key

# Deploy
git push heroku main

Docker

# Build image
docker build -t github-ai-reviewer .

# Run container
docker run -p 5000:5000 --env-file .env github-ai-reviewer

πŸ”§ Configuration

Supported File Types

The reviewer currently supports these file extensions:

  • Python: .py
  • JavaScript/TypeScript: .js, .ts, .jsx, .tsx
  • Java: .java
  • C/C++: .c, .cpp
  • Go: .go
  • Rust: .rs
  • PHP: .php
  • Ruby: .rb

Customizing Reviews

Edit the review_with_gemini() function to customize the AI prompts:

prompt = f"""
Please review this {filename} file for:
1. Code quality and best practices
2. Potential bugs or security issues
3. Performance improvements
4. Code style and readability

Your custom instructions here...
"""

πŸ›‘οΈ Security Features

  • βœ… Webhook signature verification
  • βœ… GitHub App authentication (more secure than personal tokens)
  • βœ… Environment variable protection
  • βœ… Input validation and sanitization
  • βœ… Error handling and logging

πŸ“Š Performance

Standard Version (server.py)

  • Simple Flask implementation
  • Synchronous processing
  • Good for low-traffic scenarios

Async Version (async_server.py)

  • Uses Quart (async Flask)
  • Concurrent file processing
  • Better for high-traffic scenarios
  • Faster response times

πŸ› Troubleshooting

Common Issues

  1. "Invalid signature" error

    • Check your WEBHOOK_SECRET matches GitHub App settings
    • Ensure webhook URL is correct
  2. "Authentication failed" error

    • Verify GITHUB_APP_ID and GITHUB_INSTALLATION_ID
    • Check private-key.pem file exists and is valid
  3. "Gemini API error"

    • Verify GEMINI_API_KEY is correct
    • Check API quota and billing
  4. Webhook not receiving events

    • Ensure ngrok is running and URL is updated in GitHub App
    • Check GitHub App is installed on the repository

Debug Mode

# Enable debug logging
export FLASK_ENV=development
python server.py

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request

πŸ“ License

MIT License - see LICENSE file for details

πŸ™ Acknowledgments

  • GitHub API and PyGithub library
  • Google Gemini AI
  • Flask/Quart web frameworks
  • ngrok for local development

Happy coding! πŸš€ If you find this useful, please give it a ⭐!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages