MergeBot is an automated merge request bot for GitLab that enforces repository-specific rules and helps streamline your code review process.
!merge- Merges MR if all repository rules are satisfied!check- Validates whether the MR meets all rules!update- Updates the branch from the target branch (e.g., main/master)!rerun- Re-run pipeline, e.g.!rerun #123123333or!rerun 123123333, command will run pipeline against the branch of the merge request with variables of provided pipeline (e.g. 123123333)!spin- Assign random reviewers, e.g.!spin 2will assign 2 random reviewers, if number is not provided, it will use reviewer_number from config file. Default is 2.
- Create environment file (
bot.env):
GITLAB_TOKEN=your_personal_access_token
# Optional: Enable TLS
# TLS_ENABLED=true
# TLS_DOMAIN=your-domain.com
# GITLAB_URL=https://your-gitlab-instance.com- Run the container:
docker-compose up -d- Configure webhook: Follow the Webhook Configuration instructions, but use your own bot URL
Add the Helm repository:
helm repo add merge-bot https://gasoid.github.io/helm-charts
helm repo updateInstall the chart:
helm install my-merge-bot merge-bot/merge-botFor webhook configuration, follow the Webhook Configuration instructions.
- Set environment variables:
export GITLAB_TOKEN="your_token"
export GITLAB_URL="" # Optional: for self-hosted GitLab
export TLS_ENABLED="true" # Optional
export TLS_DOMAIN="bot.yourdomain.com" # Optional
export REDIS_URL="redis://localhost:6379" # Optional- Run the bot:
go run ./Available CLI flags:
-debug
Enable debug logging (also via DEBUG)
-gitlab-token string
GitLab personal access token (also via GITLAB_TOKEN)
-gitlab-url string
GitLab instance URL for self-hosted (also via GITLAB_URL)
-gitlab-max-repo-size string
Maximum repository size (default: 500Mb, also via GITLAB_MAX_REPO_SIZE)
-tls-domain string
Domain for SSL certificate (also via TLS_DOMAIN)
-tls-enabled
Enable TLS with Let's Encrypt (also via TLS_ENABLED)
-sentry-enabled
Enable Sentry error reporting (default: true, also via SENTRY_ENABLED)
-redis-url string
Redis URL as cache storage, it needs for distributed locking, if you have more than 1 instance (also via REDIS_URL)
-plugins string
Comma-separated list of plugin config URLs or paths (also via PLUGINS)
-version
Shows version and build time
- Invite the bot: Add a bot to your repository with Maintainer role
- Configure webhook:
- URL:
https://merge-bot-url/mergebot/webhook/gitlab/ - Trigger events: Comments and Merge Request events
- URL:
- Create configuration: Add
.mrbot.yamlto your repository root (see Config File) - Start using: Create an MR and use commands like
!checkand!spinin comments to interact with the bot
- Bot role: Maintainer (required for commenting, merging, and deleting branches)
- Access token scopes:
api,read_repository,write_repository
Enhance security by using webhook secrets:
- Create CI/CD variable: Add
MERGE_BOT_SECRETto your GitLab project variables - Configure webhook: Set the same secret value in your webhook configuration
- Verification: The bot will validate incoming webhooks against this secret
Create .mrbot.yaml in your repository root on the default branch:
# all settings are optional, defaults are shown below
rules:
enabled: true
approvers: [] # Specific users who must approve (empty = any approver)
min_approvals: 1 # Minimum number of approvals required
allow_empty_description: true # Allow empty MR descriptions
allow_failing_pipelines: true # Allow merging with failed pipelines
title_regex: ".*" # Title validation regex pattern
greetings:
enabled: false # Send welcome message on new MRs
resolvable: false # Whether greeting message can be updated and resolved
template: "## π€ MergeBot commands\n\n - !update merges new changes from destination branch (master)\n - !rerun re-runs pipeline on branch with vars from pipeline, e.g. !rerun #12323 (#12323 is pipeline id)\n - !spin assignes random reviewers, default is 2, e.g. `!spin 3` chooses 3 reviewers\n```\n{{ .Cookie }}\n```"
auto_master_merge: false # Auto-update branch from target branch
auto_approve:
enabled: false # Automatically approve MRs
patterns: [] # List of wildcard patterns to match MR files for auto-approval
review_roulette:
enabled: false # Randomly assign reviewers
use_codeowners: true
reviewer_number: 2
exclude_usernames: []
stale_branches_deletion:
enabled: false # Clean up stale branches after merge
exclude_branches: [] # List of branch names to exclude from deletion
protected: false # Whether to consider protected branches for deletion
days: 90 # Consider branches stale after N days
batch_size: 5 # Number of branches can be deleted at once
wait_days: 1 # Wait N days before MR/branch deletion, merge-bot:stale label is set
plugin_vars: {} # Custom variables for pluginsrules:
enabled: true
approvers:
- alice
- bob
min_approvals: 2
allow_empty_description: false
allow_failing_pipelines: false
title_regex: "^(feat|fix|docs|style|refactor|test|chore):" # Conventional commits
greetings:
enabled: true
resolvable: true
template: |
## π€ MergeBot commands
- !update merges new changes from destination branch (master)
- !rerun re-runs pipeline on branch with vars from pipeline, e.g. !rerun #12323 (#12323 is pipeline id)
- !spin assignes random reviewers, default is 2,e.g. `!spin 3` chooses 3 reviewers
```
{{ .Cookie }}
```
auto_master_merge: true
auto_approve:
enabled: true
patterns:
- "docs/**"
- "README.md"
review_roulette:
enabled: true
reviewer_number: 3
stale_branches_deletion:
enabled: true
exclude_branches:
- main
- develop
protected: true
days: 30
batch_size: 2
wait_days: 1
plugin_vars:
var_name: value # Custom variables for pluginsNote
Available in 3.11.0+ Bot version
Automatically approve merge requests after the bot is assigned as a reviewer. You can specify wildcard patterns for merge request files. The bot approves the merge request when every changed file matches at least one configured pattern.
Note
Available in 3.10.0+ Bot version
Enable random reviewer assignment for new merge requests. You can specify the number of reviewers to assign and whether to use CODEOWNERS for selection. Exclude specific usernames from being assigned as reviewers. Also you can use !spin command to assign new random reviewers to the merge request.
If user has status: ooo, vacation, travel and parental leave in GitLab, they will be excluded from review roulette. Also emoji status is supported, if user has emoji status: ποΈ, π΄, β, π΄ they will be excluded from review roulette as well.
Note
Available in 3.8.0+ Bot version
Please read docs for more information.
When enabled, the bot automatically deletes stale branches after each successful merge/update. Branches are considered stale based on the configured number of days since their last activity.
Customize welcome messages for new merge requests using Go templates. Available variables:
{{ .MinApprovals }}{{ .TitleRegex }}{{ .Approvers }}{{ .Cookie }}
You can also enable the resolvable option to allow the bot to update and resolve the greeting message once all requirements are met. Merge Request will be blocked until requirements are met. (You need to enable "All threads must be resolved" in project settings for this feature to work.)
.Cookie is fortune cookie message, you can use it to make your greeting more fun and friendly.
The bot creates 2 labels:
- merge-bot:stale
- merge-bot:auto-update
Use merge-bot:auto-update label if you need to update merge request when target branch (master) is updated.
Test the bot on our public demo repository: https://gitlab.com/Gasoid/sugar-test
Contributions are welcome! Please feel free to submit issues and pull requests.
