Skip to main content

A simple tool for automatically creating merge requests in GitLab

Project description

Pipeline Status Coverage PyPI Project License PyPI Project Version

GitLab Auto MR

This is a simple Python cli script that allows you create MR in GitLab automatically. It is intended to be used during your CI/CD. However you can chose to use it however you wish.

It is based on the script and idea of Riccardo Padovani, which he introduced with his blog post How to automatically create new MR on Gitlab with Gitlab CI.

Usage

First you need to create a personal access token, more information here. With the scope api, so it can create the MR using your API. This access token is passed to the script with the --private-token argument.

  pip install gitlab-auto-mr
  gitlab_auto_mr --help

Usage: gitlab_auto_mr [OPTIONS]

  Gitlab Auto MR Tool.
Options:
--private-token TEXT

Private GITLAB token, used to authenticate when calling the MR API. [required]

--source-branch TEXT

The source branch to merge into. [required]

--project-id INTEGER

The project ID on GitLab to create the MR for. [required]

--gitlab-url TEXT

The GitLab URL i.e. gitlab.com. [required]

--user-id INTEGER

The GitLab user ID(s) to assign the created MR to. [required]

--reviewer-id INTEGER

The GitLab user ID(s) to assign the created MR to as reviewer(s).

-k, --insecure

Do not verify server SSL certificate.

-t, --target-branch TEXT

The target branch to merge onto.

-c, --commit-prefix TEXT

Prefix for the MR title. Default: ‘Draft’.

-r, --remove-branch

If set will remove the source branch after MR.

-s, --squash-commits

If set will squash commits on merge.

-d, --description TEXT

Path to file to use as the description for the MR.

--title TEXT

Custom tile for the MR.

-i, --use-issue-name

If set will use information from issue in branch name, must be in the form #issue-number, i.e feature/#6.

-a, --allow-collaboration

If set allow, commits from members who can merge to the target branch.

--mr-exists

If set, checks if a merge request exists for the given ‘source’ and ‘target’ branch and exits without creating it.

--help

Show this message and exit.

gitlab_auto_mr --private-token $(private_token) --source-branch feature/test --project-id 5 \
                --gitlab-url https://gitlab.com/ --user-id 5

GitLab CI

GITLAB_PRIVATE_TOKEN Set a secret variable in your GitLab project with your private token. Name it GITLAB_PRIVATE_TOKEN (CI/CD > Environment Variables). This is necessary to raise the Merge Request on your behalf. More information click here. An example CI using this can be found here, look for the create:merge-request job.

Add the following to your .gitlab-ci.yml file:

stages:
  - open

open_merge_request:
  image: registry.gitlab.com/gitlab-automation-toolkit/gitlab-auto-mr
  before_script: [] # We do not need any setup work, let's remove the global one (if any)
  stage: open
  script:
    - gitlab_auto_mr -t master -c Draft -d ./.gitlab/merge_request/merge_request.md -r -s --use-issue-name
Predefined Variables

Please note some of the arguments can be filled in using environment variables defined during GitLab CI. For more information click here <https://docs.gitlab.com/ee/ci/variables/predefined_variables.html>_.

  • If --private-token is not set the script will look for the ENV variable GITLAB_PRIVATE_TOKEN

  • If --source-branch is not set the script will look for the ENV variable CI_COMMIT_REF_NAME

  • If --project-id is not set it will look for for the ENV variable CI_PROJECT_ID

  • If --gitlab-url is not set it will look for for the ENV variable CI_PROJECT_URL

  • If --user-id is not set it will look for for the ENV variable GITLAB_USER_ID

Setup Development Environment

git clone git@gitlab.com:gitlab-automation-toolkit/gitlab-auto-mr.git
cd gitlab-auto-mr
pip install tox
make install-venv
source .venv/bin/activate
make install-dev

Changelog

You can find the changelog here.

Appendix

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gitlab-auto-mr-1.2.0.tar.gz (10.8 kB view details)

Uploaded Source

File details

Details for the file gitlab-auto-mr-1.2.0.tar.gz.

File metadata

  • Download URL: gitlab-auto-mr-1.2.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for gitlab-auto-mr-1.2.0.tar.gz
Algorithm Hash digest
SHA256 08f18fb383d551a6e89e003ff1a213d8c69eb9ae0bb145970181077c2988f360
MD5 c310445dc0d20d7fd19331c6901cee3e
BLAKE2b-256 63fc822977daa5921956ff7212eef71075644e66df565eb80fc5b6346fb5ae18

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page