This project provides a Review Helper script that interacts with GitLab to perform automated code reviews on Merge Requests (MRs) using OpenAI's ChatGPT. And Telegram Bot, that can be invoked via Telegram messages or commands to review MRs based on specific rules and guidelines.
- Automatically reviews GitLab Merge Requests with ChatGPT based on your coding guidelines.
- Allows usage of Telegram bot commands to request reviews.
- Supports multi-part reviews for large MRs that exceed API token limits.
- Configurable file extension filtering to skip unnecessary files.
- Logs errors and retries requests on rate limiting with exponential backoff.
src/
├── bots/ # Telegram bot and handlers
│ └── tg_bot.py # Main bot script to interact with users on Telegram
├── common/ # Common modules
│ └── config_loader.py # Loading configuration from ENV or config.ini
├── config/ # Configuration files
│ ├── config.ini # Configuration settings (tokens, URLs, etc.)
│ └── prompt.txt # Custom prompt message for ChatGPT API
├── helpers/ # Helper modules
│ └── gitlabreviewhelper.py # GitLab and ChatGPT integration for review automation
└── main.py # Main entry point
setup.py # Installation script
- Python 3.8+
- Python libraries:
python-dotenv,python-telegram-bot,gitlab,tiktoken,openai,pydantic - Access to a GitLab instance and a Telegram bot token from BotFather
-
Clone the Repository
git clone https://github.com/MayVortex/gitlab_ai_reviewer . cd code
-
*** all in one prepare ***
pip install -e .- Update
src/config/config.iniwith the necessary API keys, GitLab information, and model settings. - Customize the
prompt.txtin theconfigfolder for any specific instructions for the ChatGPT model.
- Update
-- OR -- 3. Set up a virtual environment:
python -m venv venv
source venv/bin/activate
# venv\Scripts\activate # For Windows-
Configure environment variables and configuration files:
- Update
src/config/config.iniwith the necessary API keys, GitLab information, and model settings. - Customize the
prompt.txtin theconfigfolder for any specific instructions for the ChatGPT model.
- Update
-
Install Dependencies
python setup.py install
All configuration options can be specified in the config.ini file or as environment variables.
Use config.ini for more persistent configurations.
[settings]
TELEGRAM_TOKEN = your_telegram_bot_token
GITLAB_URL = https://gitlab.yourdomain.com
GITLAB_TOKEN = your_gitlab_token
OPENAI_API_KEY = your_openai_api_key
PROJECT_ID = your_gitlab_project_id
MODEL = gpt-4o
SKIP_EXTENSIONS = .md, .txt # Optional: Skip files with these extensions
PROMPT_FILE = /your/path/to/prompt.txt| Parameter | Description |
|---|---|
TELEGRAM_TOKEN |
Telegram bot token, required to interact with Telegram API. |
GITLAB_URL |
URL to the GitLab instance. |
GITLAB_TOKEN |
Private GitLab token for API access. |
OPENAI_API_KEY |
API key for OpenAI's ChatGPT API. |
PROJECT_ID |
GitLab project ID to pull and review MRs. |
MODEL |
OpenAI model to use for reviews (default: gpt-4). |
SKIP_EXTENSIONS |
Comma-separated list of file extensions to skip (e.g., .md, .txt). |
PROMPT_FILE |
Path to prompt (/your/path/to/prompt.txt |
gitlab_ai_reviewer_startgitlab_ai_reviewer_botThe bot recognizes two types of messages:
- /review
- Direct Commands:
/review <MR_ID>- Example:
/review 1234
- Example:
- Direct Commands:
/review <MR_ID>: [task title and description]- Example:
/review 1234: Update API endpoints
- Example:
- /review@your_bot_name
- Direct Commands:
/review <MR_ID>- Example:
/review@your_bot_name 1234
- Example:
- Direct Commands:
/review <MR_ID>: [task title and description]- Example:
/review@your_bot_name 1234: Update API endpoints
- Example:
- @botusername
- Mentions in Group:
@botusername <MR_ID>- Example:
@botusername 1234
- Example:
- Mentions in Group:
@botusername <MR_ID> [task title and description]- Example:
@botusername 1234: Fix logging issue
- Example:
The bot will process these commands, review the MR in GitLab, and respond with results.
To run the bot as a service, create a systemd service file:
# /etc/systemd/system/gitlab-review-bot.service
[Unit]
Description=GitLab Review Bot Service
After=network.target
[Service]
Type=simple
# If using system Python (pip install -e .) - use this:
ExecStart=/usr/local/bin/gitlab_ai_reviewer_bot
# Else - Path to your Python virtual environment (or system Python if not using a venv)
# ExecStart=/path/to/your/venv/bin/gitlab_ai_reviewer_bot
# Set the working directory to the project directory (optional)
# WorkingDirectory=/path/to/your/project
# Optional: Environment variables can be set here if needed
Environment=PYTHONUNBUFFERED=1
# Environment=TELEGRAM_TOKEN=your_telegram_token_here
# Environment=GITLAB_TOKEN=your_gitlab_token_here
# ...
Restart=on-failure
User=your_username # Replace with your username
Group=your_group # Replace with your user group
[Install]
WantedBy=multi-user.targetThen, enable and start the service:
sudo systemctl start gitlab_ai_reviewer_bot
sudo systemctl enable gitlab_ai_reviewer_bot
sudo systemctl status gitlab_ai_reviewer_bot
journalctl -u gitlab_ai_reviewer_bot -f- Rate Limiting: If you see
RateLimitError, the script will retry with exponential backoff. You may need to wait a few minutes. - Invalid Configurations: If you encounter missing configuration errors, verify
.envandconfig.inifor correctness. - Unexpected Output or No Review: Check logs for detailed error messages. Make sure
review_commentsis correctly formatted as a list before callingpost_review_comments.
Logs are stored in tg-bot.log (if enabled) and contain detailed information about the bot's operations.
# Example to enable file logging
logging.basicConfig(filename="tg-bot.log", level=logging.INFO)Feel free to open issues, create pull requests, or suggest enhancements to improve the bot's functionality.
This bot simplifies GitLab Merge Request reviews by automating the process with ChatGPT, making code quality management seamless for your team.