An AI-powered GitLab merge request reviewer bot that uses Google's Gemini LLM to provide intelligent code reviews. The bot automatically analyzes merge requests and posts constructive feedback as comments.
- 🤖 AI-Powered Reviews: Uses Google Gemini to analyze code changes and provide intelligent feedback
- 🔗 GitLab Integration: Seamless integration with GitLab webhooks
- 🚀 Automatic Comments: Posts review comments directly on merge requests
- 📍 Positioned Comments: AI can comment on specific lines in diffs for precise feedback
- ✨ GitLab Suggestions: Provides one-click code fix suggestions that developers can apply instantly
- 📋 Custom Review Guidance: Supports repository-specific review criteria via .whytho/guidance.md files
- 🎚️ Severity Filtering: Configure minimum severity threshold to control comment volume
- 🔒 Secure: Supports webhook signature verification
- 📊 Comprehensive Analysis: Reviews code quality, security, performance, and best practices
- 📝 Structured Logging: Uses logrus for comprehensive structured logging
- 🎯 Path Exclusions: Exclude specific files/directories from review via .whytho/config.yaml
- 📈 Token Tracking: Monitor AI token usage per review for cost management
- 🐳 Containerized: Ready-to-deploy Docker setup
- Go 1.21 or later
- GitLab access token with API permissions
- Google Gemini API key
- Docker (optional, for containerized deployment)
git clone https://github.com/vinamra28/whytho.git
cd whythoCopy the example environment file and fill in your credentials:
cp .env.example .envEdit .env with your configuration:
GITLAB_TOKEN=your_gitlab_access_token_here
GITLAB_BASE_URL=https://gitlab.com
GEMINI_API_KEY=your_gemini_api_key_here
WEBHOOK_SECRET=your_webhook_secret_here
PORT=8080go mod tidygo run cmd/main.godocker-compose up --builddocker build -t whytho .
docker run -p 8080:8080 --env-file .env whytho- Go to your GitLab project/group settings
- Navigate to Webhooks
- Add a new webhook with:
- URL:
http://your-server:8080/webhook - Secret Token: Your
WEBHOOK_SECRETvalue - Trigger: Select "Merge request events"
- SSL Verification: Enable if using HTTPS
- URL:
- GitLab sends a webhook when a merge request is opened, reopened, or updated
- The bot validates the webhook signature (if configured)
- Fetches the merge request changes via GitLab API
- Attempts to fetch custom review guidance from
.whytho/guidance.mdin the target repository - Sends the code changes to Google Gemini for analysis with custom or default guidance
- Posts AI-generated review comments back to the merge request (both general and line-specific positioned comments)
POST /webhook- GitLab webhook endpointGET /health- Health check endpoint
├── cmd/
│ └── main.go # Application entry point
├── internal/
│ ├── config/
│ │ └── config.go # Configuration management
│ ├── handlers/
│ │ └── webhook.go # Webhook handlers
│ ├── models/
│ │ └── models.go # Data structures
│ ├── server/
│ │ └── server.go # HTTP server setup
│ └── services/
│ ├── gitlab.go # GitLab API client
│ └── review.go # Gemini AI integration
├── Dockerfile # Docker configuration
├── docker-compose.yml # Docker Compose setup
├── go.mod # Go module definition
└── README.md # This file
The bot supports repository-specific review criteria by reading a .whytho/guidance.md file from the target repository. This allows each project to customize the AI reviewer's focus areas and criteria.
Create a .whytho/guidance.md file in your repository with your custom review guidelines:
# Code Review Guidance
## Review Focus Areas
### 1. Go-Specific Best Practices
- Ensure proper error handling with explicit error returns
- Check for potential goroutine leaks
- Verify proper use of context.Context for cancellation
### 2. Security Considerations
- Never log sensitive information (tokens, passwords, secrets)
- Validate all input parameters
- Use secure defaults for configuration
### 3. Performance and Reliability
- Look for potential memory leaks
- Check for inefficient loops or operations
- Validate retry logic and backoff strategiesIf no .whytho/guidance.md file is found, the bot uses comprehensive default review criteria covering:
- Code quality and maintainability
- Security vulnerabilities
- Performance issues
- Best practices
- Potential bugs
- Documentation needs
The bot supports excluding specific files and directories from review using a .whytho/config.yaml file in your repository.
Create a .whytho/config.yaml file in your repository root to specify which paths should be excluded from review:
excludePaths:
- "vendor/**"
- "*.generated.go"
- "docs/**"
- "test/fixtures/**"- Exact matches:
vendor/module.go - Glob patterns:
*.go,test_*.py - Directory exclusions:
vendor/**(excludes all files in vendor directory and subdirectories) - File extensions:
*.min.js,*.generated.*
The bot checks for .whytho/config.yaml in the following order:
- Modified in MR: If the config file is changed in the current merge request, uses the new version
- Target branch: If not modified, fetches the config from the target branch (e.g.,
main) - Fallback: If no config file exists, reviews all files
# Exclude paths/files from code review
excludePaths:
- "vendor/**" # Exclude all vendor dependencies
- "*.pb.go" # Exclude generated protobuf files
- "*.generated.go" # Exclude all generated Go files
- "docs/**" # Exclude documentation directory
- "test/fixtures/**" # Exclude test fixtures
- "*.min.js" # Exclude minified JavaScript
- "migrations/**" # Exclude database migrations
# Minimum severity threshold for comments to be posted
# Options: LOW, MEDIUM, HIGH, CRITICAL
# Only comments meeting or exceeding this threshold will be posted
commentSeverityThreshold: "MEDIUM" # Post only MEDIUM, HIGH, and CRITICAL commentsControl which review comments are posted based on their severity level using the commentSeverityThreshold configuration:
Severity Levels (from lowest to highest):
LOW: Style improvements, documentation suggestions, minor optimizationsMEDIUM: Code quality issues, maintainability concerns, minor bugsHIGH: Performance bottlenecks, significant bugs, architectural issuesCRITICAL: Security vulnerabilities, potential data loss, system crashes
Examples:
commentSeverityThreshold: "CRITICAL"- Only post critical security/reliability issuescommentSeverityThreshold: "HIGH"- Post high-priority and critical issuescommentSeverityThreshold: "MEDIUM"- Post medium, high, and critical issues (default if not specified)commentSeverityThreshold: "LOW"- Post all comments including low-severity style suggestionscommentSeverityThreshold: ""- Defaults to MEDIUM (same as omitting the field)
Use Cases:
- Production branches: Set to
HIGHorCRITICALto focus only on serious issues - Development branches: Use default
MEDIUMfor balanced feedback - Learning/training: Set to
LOWto get comprehensive feedback including style suggestions - Noisy repositories: Set to
HIGHto reduce comment volume
When files are excluded, the bot logs:
- Number of excluded files
- List of excluded file paths
- Remaining files for review
If all files are excluded, the bot posts a summary comment explaining the exclusion.
Create a GitLab access token with the following scopes:
api- Full API accessread_api- Read API accessread_repository- Read repository access
- Go to Google AI Studio
- Create a new API key
- Use this key as your
GEMINI_API_KEY
- Always use HTTPS in production
- Set a strong
WEBHOOK_SECRETfor webhook verification - Keep your GitLab token and Gemini API key secure
- Consider rate limiting for the webhook endpoint
- Run the application behind a reverse proxy (nginx, etc.)
- Webhook not triggering: Check that the webhook URL is accessible and the secret matches
- API errors: Verify your GitLab token has the required permissions
- Gemini errors: Ensure your API key is valid and you have sufficient quota
- Connection issues: Check network connectivity and firewall settings
The application uses structured JSON logging via logrus and logs important events including:
- Webhook received events
- GitLab API calls (merge request changes, posting comments)
- Gemini API interactions
- Custom guidance fetching from repositories
- Positioned comment processing
- Error conditions and fallback behaviors
Logs include structured fields for easy filtering and monitoring:
{
"level": "info",
"msg": "Code review completed",
"project_id": 123,
"mr_iid": 45,
"general_comments_count": 2,
"positioned_comments_count": 5,
"time": "2025-01-15T10:30:00Z"
}- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License. See the LICENSE file for details.
For issues and questions:
- Check the troubleshooting section
- Open an issue on GitHub
- Check GitLab and Gemini API documentation