Build a GitHub PR Reviewer Bot that Summarizes & Fixes Code Automatically
Pull Request (PR) reviews are vital for maintaining code quality, but they can quickly become a bottleneck for busy engineering teams. Senior developers spend hours checking syntax, reviewing boilerplate logic, and hunting for common security oversights.
In this guide, we will build an automated GitHub PR Reviewer using GitHub Actions, Python, and Velona's unified AI Gateway. When a developer opens or updates a Pull Request, our bot will automatically analyze the git diff, write a concise summary, and post actionable code fix suggestions directly as a PR comment.
Architecture Overview
The automated review workflow operates seamlessly within your CI/CD pipeline:
- A developer opens or updates a Pull Request on GitHub.
- GitHub Actions triggers a workflow that extracts the PR git diff using the GitHub REST API.
- The Python script sends the git diff to a specialized coding model via Velona's
/gateway/v1/inference/runendpoint. - The AI evaluates security, readability, and logic, then formats structured review markdown.
- The bot posts the feedback back to the Pull Request.
Step 1: Set Up Your Velona Account & API Key
To power the AI reviewer backend:
- Create an account on the Velona Registration Page.
- Navigate to your Wallet Dashboard and top up ₹10 or more via UPI, RuPay, or GPay.
- Go to API Key Management and generate a dedicated key named
GitHub-PR-Reviewer. - Set a monthly spending limit (e.g., ₹200) on the key to prevent unexpected API costs during busy sprint cycles.
Step 2: Create the Python Review Script
Create a file named review_pr.py in your project directory. This script fetches the PR diff, sends it to Velona's gateway, and posts the resulting analysis back to GitHub.
import os
import requests
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
PR_NUMBER = os.getenv("PR_NUMBER")
REPO = os.getenv("GITHUB_REPOSITORY")
VELONA_API_KEY = os.getenv("VELONA_API_KEY")
# 1. Fetch Pull Request Diff from GitHub API
gh_headers = {
"Authorization": f"token {GITHUB_TOKEN}",
"Accept": "application/vnd.github.v3.diff"
}
diff_url = f"https://api.github.com/repos/{REPO}/pulls/{PR_NUMBER}"
diff_response = requests.get(diff_url, headers=gh_headers)
pr_diff = diff_response.text
# 2. Construct Prompt for Velona AI Gateway
prompt = f"""You are an expert Senior Staff Software Engineer reviewing a GitHub Pull Request.
Analyze the following git diff and produce a clean Markdown review containing:
1. **Summary**: Brief summary of changes.
2. **Key Risks & Bugs**: Potential security flaws, edge cases, or performance issues.
3. **Suggested Fixes**: Specific code snippets improving readability or correctness.
Git Diff:
```diff
{pr_diff[:8000]} # Truncate if diff exceeds standard context limits
```"""
# 3. Call Velona Unified Gateway
velona_headers = {
"Authorization": f"Bearer {VELONA_API_KEY}",
"Content-Type": "application/json"
}
payload = {
"model": "deepseek/deepseek-coder",
"turns": [
{"role": "user", "content": prompt}
]
}
velona_url = "https://velona.in/gateway/v1/inference/run"
ai_response = requests.post(velona_url, json=payload, headers=velona_headers)
review_markdown = ai_response.json()["choices"][0]["message"]["content"]
# 4. Post Review Comment Back to PR
comment_url = f"https://api.github.com/repos/{REPO}/issues/{PR_NUMBER}/comments"
post_headers = {
"Authorization": f"token {GITHUB_TOKEN}",
"Accept": "application/vnd.github.v3+json"
}
requests.post(comment_url, json={"body": review_markdown}, headers=post_headers)
print("PR Review posted successfully!")
Step 3: Configure GitHub Action Workflow
Create a workflow file at .github/workflows/pr-reviewer.yml inside your repository:
name: AI Pull Request Reviewer
on:
pull_request:
types: [opened, synchronize]
jobs:
review:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install Dependencies
run: pip install requests
- name: Run AI PR Reviewer
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
VELONA_API_KEY: ${{ secrets.VELONA_API_KEY }}
run: python review_pr.py
Note: Store your VELONA_API_KEY securely in your GitHub repository's Settings > Secrets and variables > Actions section.
Step 4: Choosing the Best Model for Code Review
Not all LLMs evaluate code logic equally. When choosing a model for code review tasks, you need a balance between deep reasoning and token cost:
- DeepSeek Coder / DeepSeek R1: Outstanding reasoning accuracy for logic bugs and algorithm optimizations at ultra-low per-token costs.
- Claude 3.5 Sonnet: Industry gold standard for complex refactoring and architecture-level PR reviews.
- Llama 3.1 70B: High-speed option for simple linting, typos, and standard documentation checks.
You can check real-time INR prices on the Velona Model Pricing Page or evaluate model performance side-by-side using the AI Benchmark Leaderboard.
Debugging & Free Developer Utilities
When tweaking prompt schemas or testing string escapes for git diff payloads, make use of Velona's 49 Life Time Free Developer Tools. Utilities like the JSON Formatter, Text Diff Tool, and Regex Evaluator are 100% free and run without deducting anything from your wallet balance.
If you want to test prompt iterations before deploying the GitHub Action, run your queries in our Free Public Chat Playground or compare outputs across multiple models in the Model Evaluator.
Conclusion
Automating initial PR reviews reduces developer workload and catches security oversights before code reaches production. By using Velona's unified gateway, Indian engineering teams can access top-tier coding models with domestic UPI billing and zero forex fees.
Ready to automate your CI/CD workflow? Sign up for a free Velona account and start building today!