A GitHub Action that automatically reviews pull requests using Google's Gemini AI API. This bot provides intelligent, constructive feedback on code changes to help improve code quality and catch potential issues early.
- Intelligent Code Review: Uses Google's Gemini 1.5 Flash model for comprehensive code analysis
- Smart File Filtering: Automatically excludes lock files, large files, and other non-reviewable content
- Detailed Feedback: Provides structured feedback covering code quality, security, performance, and best practices
- Rich Context: Includes PR title, description, and change statistics in the review
- Professional Formatting: Well-formatted comments with emojis and clear structure
- Error Handling: Robust error handling with detailed logging
- Configurable: Easy to customize and extend
- Go to Google AI Studio
- Create a new API key
- Copy the key for later use
Create a .github/workflows/ai-review.yml file in your repository:
name: AI Code Review
on:
pull_request:
types: [opened, synchronize, reopened]
jobs:
ai-review:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- name: AI Code Review
uses: your-username/ai-code-review-bot@main
with:
gemini_api_key: ${{ secrets.GEMINI_API_KEY }}- Go to your repository's Settings → Secrets and variables → Actions
- Click New repository secret
- Name:
GEMINI_API_KEY - Value: Your Gemini API key from step 1
The action will automatically run on:
- New pull requests (
opened) - New commits to existing PRs (
synchronize) - Reopened pull requests (
reopened)
| Input | Description | Required | Default |
|---|---|---|---|
gemini_api_key |
Your Gemini API key | No* | - |
*Required if GEMINI_API_KEY environment variable is not set
The bot will post a comment like this on your PR:
🤖 AI Code Review
📊 Review Summary:
- Files reviewed: 3/5
- Total changes: 45 lines
- Files excluded: 2 (large files, lock files, etc.)
## Overall Assessment
The changes look good overall with some minor improvements needed...
## Specific Issues
1. **Line 23**: Consider adding input validation...
2. **Line 45**: This function could benefit from error handling...
## Suggestions for Improvement
- Add JSDoc comments for better documentation
- Consider extracting the validation logic into a separate function
- Add unit tests for the new functionality
## Security Considerations
- No security issues detected
- Input sanitization looks appropriate
## Performance Implications
- The new caching mechanism should improve performance
- Consider memoization for expensive calculations
The bot automatically excludes certain file types from review:
- Lock files:
package-lock.json,yarn.lock,pnpm-lock.yaml - Minified files:
*.min.js,*.min.css - Bundle files:
*.bundle.js,*.bundle.css - System files:
.DS_Store,.log,.tmp,.temp - Large files: Files larger than 50,000 characters
You can customize the bot by modifying the source code:
- Review Prompt: Edit the
createReviewPromptfunction to change how the AI analyzes code - File Filtering: Modify
EXCLUDED_FILE_PATTERNSto include/exclude different file types - File Size Limit: Change
MAX_FILE_SIZEto adjust the maximum file size for review - AI Model: Switch to a different Gemini model by changing the API endpoint
- Node.js 20+
- npm or yarn
- TypeScript
- Clone the repository:
git clone https://github.com/your-username/ai-code-review-bot.git
cd ai-code-review-bot- Install dependencies:
npm install- Build the project:
npm run buildnpm run build- Build the TypeScript code and bundle with nccnpm run build:watch- Build in watch mode for developmentnpm run clean- Remove the dist directory
├── src/
│ └── index.ts # Main action code
├── dist/ # Built files (generated)
├── action.yml # GitHub Action metadata
├── package.json # Dependencies and scripts
├── tsconfig.json # TypeScript configuration
└── README.md # This file
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes
- Build and test:
npm run build - Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request