Automated PR Code Reviews with LLM
Automate pull request reviews using n8n for workflow orchestration, Google Gemini 2.5 Flash for LLM-based code analysis, and GitHub for PR triggers, comments, and labels.
Demo Video
Source: https://youtu.be/g02Ips9WGPo
Overview
Automate pull request reviews using n8n for workflow orchestration, Google Gemini 2.5 Flash for LLM-based code analysis, and GitHub for PR triggers, comments, and labels. This setup delivers fast, contextual feedback directly on your PRs.
"Automation is not just about efficiency; it's about creating consistent, high-quality processes that scale with your team."
The Problem
Manual code reviews are essential for maintaining code quality, but they can become bottlenecks in fast-moving development teams. Reviewers need time to context-switch, understand changes, and provide meaningful feedback. This often leads to delayed deployments and slower iteration cycles.
The Solution: AI-Powered Code Reviews
This automated system combines the power of Google's Gemini 2.5 Flash with GitHub's robust API and n8n's workflow orchestration to create an intelligent code review assistant that provides instant, contextual feedback on every pull request.
Workflow Summary
- Trigger - GitHub PR opened/updated
- Fetch Diff - Get code diffs via GitHub API
- Prompt Creation - Format diffs into a review prompt
- LLM Review - Send prompt to Gemini 2.5 Flash
- Post Review - Comment back on the PR
- Labeling - Add labels like
auto-reviewed
Tech Stack
The system is built with a carefully selected technology stack:
Core Components
- n8n - Workflow automation and orchestration platform
- GitHub - PR triggers, comments, and label management
- Gemini 2.5 Flash - Advanced LLM for code analysis and review generation
Integration Benefits
- Real-time Processing - Instant reviews upon PR creation or updates
- Scalable Architecture - Handles multiple repositories and team sizes
- Contextual Intelligence - Understands code patterns and best practices
- Seamless GitHub Integration - Works within existing development workflows
Step-by-Step Implementation
This workflow automates pull request code reviews using n8n, GitHub, and Google Gemini 2.5 Flash. Below is a breakdown of how each part of the system works:
1. GitHub Trigger (PR Event)
The GitHub Trigger node initiates the workflow whenever a pull_request
event (opened, synchronized, reopened) is fired on the connected repository.
- This enables real-time automation
- It listens for pull requests to start the review process instantly
- Supports multiple event types for comprehensive coverage
// GitHub Webhook Configuration
{
"events": ["pull_request"],
"active": true,
"config": {
"url": "https://your-n8n-instance.com/webhook/github-pr",
"content_type": "json"
}
}
2. Get PR Diffs (Changed Files & Code)
The HTTP Request node fetches a list of changed files and their corresponding diffs from the PR using the GitHub REST API. It dynamically uses details from the PR trigger payload (like repo, PR number, sender).
API Endpoint Used:
https://api.github.com/repos/{{$json.body.sender.login}}/{{$json.body.repository.name}}/pulls/{{$json.body.number}}/files
This step provides raw content needed for code review:
// Sample API Response Structure
{
"files": [
{
"filename": "src/utils/helper.js",
"status": "modified",
"additions": 15,
"deletions": 3,
"changes": 18,
"patch": "@@ -10,7 +10,7 @@ function processData(input) ..."
}
]
}
3. Create Prompt from Diffs
The Code Node processes the retrieved diffs and constructs a structured prompt for the AI model.
What it does:
- Parses the raw diff content
- Formats each file's changes clearly
- Embeds the diffs in a contextual message aimed at the AI agent
Prompt Example:
// n8n Code Node - Prompt Construction
const files = $input.all()[0].json.files;
let prompt = `You are a senior Software Engineer. Please review the following code changes:
Repository: ${$json.body.repository.name}
PR Title: ${$json.body.pull_request.title}
Author: ${$json.body.pull_request.user.login}
Files Changed:
`;
files.forEach(file => {
prompt += `
## ${file.filename}
Status: ${file.status}
Changes: +${file.additions} -${file.deletions}
\`\`\`diff
${file.patch}
\`\`\`
`;
});
prompt += `
Please provide:
1. Code quality assessment
2. Potential bugs or issues
3. Performance considerations
4. Best practice recommendations
5. Security implications (if any)
Focus on constructive feedback that helps improve the code.`;
return { prompt };
4. Code Review Agent + Google Gemini 2.5 Flash
The prompt is passed to a Code Review Agent, which routes it to the Google Gemini Chat Model (Gemini 2.5 Flash).
- Gemini processes the prompt and responds with code review comments
- These comments highlight issues, suggestions, and potential improvements
- The model understands context and provides actionable feedback
// Gemini API Configuration
{
"model": "gemini-2.5-flash",
"temperature": 0.3,
"maxTokens": 2048,
"systemInstruction": "You are an expert code reviewer. Provide constructive, actionable feedback."
}
5. GitHub Comment Poster
The response from Gemini is posted back as a comment on the pull request using the GitHub API.
- Creates a single comment that includes the full AI-generated review
- Provides immediate feedback within the GitHub PR interface
- Maintains conversation history for reference
// GitHub API - Create Review Comment
const reviewBody = `## 🤖 AI Code Review
${geminiResponse}
---
*This review was generated automatically using Google Gemini 2.5 Flash*`;
await fetch(`https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}/reviews`, {
method: 'POST',
headers: {
'Authorization': `token ${githubToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
body: reviewBody,
event: 'COMMENT'
})
});
6. PR Labeler (Optional)
After the comment is posted, the Add Label to PR node applies a label like:
ai-reviewed
automated-review
reviewed-by-gemini
This helps maintainers and contributors easily identify PRs that have been automatically reviewed by the AI agent.
Setup Instructions
Prerequisites
- n8n instance (local or cloud)
- GitHub repository with admin access
- Google Cloud account with Gemini API access
Step-by-Step Setup
-
Clone the repository
git clone https://github.com/manasdutta04/automated-PR-Reviews-with-llm cd automated-PR-Reviews-with-llm
-
Run n8n locally or deploy to the cloud
# Local setup npx n8n # Or using Docker docker run -it --rm --name n8n -p 5678:5678 n8nio/n8n
-
Configure credentials
- GitHub token with repository permissions
- Gemini API key from Google Cloud Console
-
Import the provided workflow JSON
- Use the visual editor in n8n
- Import the workflow file from the repository
-
Create a test PR to see automatic reviews in action
Environment Variables
# .env file
GITHUB_TOKEN=your_github_personal_access_token
GEMINI_API_KEY=your_google_gemini_api_key
N8N_WEBHOOK_URL=your_n8n_webhook_endpoint
Example PR Review Output
Here's an example of what the AI-generated review looks like:
🤖 AI Code Review
Overall Assessment
The changes look good overall! I've identified a few areas for improvement:
src/utils/helper.js
- Line 15: Consider adding input validation for the
data
parameter- Line 23: The error handling could be more specific
- Performance: The nested loop could be optimized using a Map for O(1) lookups
Security Considerations
- Ensure user input is sanitized before processing
- Consider rate limiting for API endpoints
This review was generated automatically using Google Gemini 2.5 Flash
See live AI review comments on a sample PR: Sample PR Comments
Advanced Configuration
Custom Review Templates
You can customize the review prompts based on your team's coding standards:
// Custom prompt templates
const templates = {
security: "Focus on security vulnerabilities and potential exploits",
performance: "Analyze performance implications and optimization opportunities",
testing: "Review test coverage and suggest additional test cases",
documentation: "Check for adequate documentation and code comments"
};
Multi-Language Support
The system supports multiple programming languages:
- JavaScript/TypeScript
- Python
- Java
- Go
- Rust
- And more...
Integration with CI/CD
Combine with existing CI/CD pipelines:
# GitHub Actions integration
name: AI Code Review
on:
pull_request:
types: [opened, synchronize]
jobs:
ai-review:
runs-on: ubuntu-latest
steps:
- name: Trigger n8n Workflow
run: |
curl -X POST ${{ secrets.N8N_WEBHOOK_URL }} \
-H "Content-Type: application/json" \
-d '{"pr_number": "${{ github.event.number }}"}'
Benefits and Impact
Development Team Benefits
- Faster Feedback - Instant reviews reduce waiting time for developers
- Consistent Quality - AI applies the same standards across all PRs
- Learning Opportunity - Developers learn from AI suggestions
- Reduced Reviewer Fatigue - Human reviewers can focus on complex logic
Project Management Benefits
- Improved Velocity - Faster review cycles accelerate development
- Quality Metrics - Track review patterns and common issues
- Documentation - Automated reviews create knowledge base
- Scalability - System scales with team growth
Limitations and Considerations
Current Limitations
- AI may miss complex business logic issues
- Context understanding limited to provided diffs
- May generate false positives for specialized domains
- Requires human oversight for critical changes
Best Practices
- Use as a first-pass review, not replacement for human review
- Regularly update prompts based on team feedback
- Monitor AI suggestions for accuracy and relevance
- Combine with static analysis tools for comprehensive coverage
Future Enhancements
Planned improvements for the system:
- Multi-Repository Support - Centralized review system across projects
- Custom Rule Engine - Team-specific coding standards enforcement
- Integration with IDEs - Real-time feedback during development
- Analytics Dashboard - Review metrics and trend analysis
- Collaborative Learning - AI learns from human reviewer feedback
Conclusion
Automated PR code reviews with LLM technology represent a significant step forward in development workflow optimization. By combining Google Gemini's advanced language understanding with GitHub's robust platform and n8n's flexible automation, teams can achieve faster, more consistent code reviews while maintaining high quality standards.
This system doesn't replace human reviewers but augments their capabilities, allowing them to focus on higher-level architectural decisions and complex business logic while the AI handles routine quality checks and best practice enforcement.
Whether you're a startup looking to scale your development process or an enterprise team aiming to improve code quality consistency, this automated review system provides a solid foundation for modern development workflows.
"The future of code review isn't about replacing human expertise—it's about amplifying it with intelligent automation that learns, adapts, and scales with your team."