5

my GitLab is installed in one of the server. multiple Developers develop the source code in their branch and push the sources from thier local machines to GitLab. later on developers will login to GitLab account and create a merge request to the default branch.

Now how can i achieve to create an automated merge request to the default branch, when developers push their code from their local machine to GitLab.

mike
  • 1,233
  • 1
  • 15
  • 36
Kiran
  • 221
  • 1
  • 2
  • 10
  • The GitLab documentation doesn't state you can do it automatically, but it does provide a link for a merge request creation once you push your code. Did you check out [this project](https://gitlab.com/tmaier/gitlab-auto-merge-request) here if it will do the trick? – mnestorov Aug 21 '20 at 11:04
  • yes, i checked this linked before but its not understandable. – Kiran Aug 21 '20 at 11:17
  • thank you guys for your response, i will try to implement this – Kiran Aug 27 '20 at 08:08

2 Answers2

5

Unfortunately, there is no feature on GitLab to auto-create merge requests. You have to create them yourself using a bash or python script, for example. I usually just call a create_merge_request job in a setup stage that runs at the start of each pipeline. The steps go something like this:

  1. Read a list of all open merge requests
  2. Count how many of those open MRs map to the source branch
  3. If there are no open MRs, create one with desired default settings

As @mnestorov mentioned, there is an open-source version available here that executes those steps in a bash script. I think the only variable you need to create in your GitLab Variables section is GITLAB_PRIVATE_TOKEN and enter your Personal Access Token.

Alternatively, you can write a Python script using the GitLab API, but you may have to write that yourself. You can find some information at How to create a merge request at the end of a successful pipeline in Gitlab?.

DV82XL
  • 5,350
  • 5
  • 30
  • 59
  • (Possibly a silly question) Does Gitlab CI need to be enabled in order for the .gitlab-ci.yml file (discussed in the _gitlab-auto-merge-request_ project documentation) to be processed? – ryanwebjackson Jan 23 '21 at 22:32
  • @ryanwebjackson If you want the MR to be created as part of your CI/CD pipeline, then yes, you need the `gitlab-ci.yml` file and you need to setup a runner. See [here](https://docs.gitlab.com/ee/ci/enable_or_disable_ci.html). However, you can still run the bash script locally without the CI/CD pipeline. You will need to create a `GITLAB_PRIVATE_TOKEN` environment variable that the bash script uses. – DV82XL Jan 24 '21 at 02:27
4

You can use push options to automatically create a merge-request in GitLab, like so:

$ git push -o merge_request.create ...

I managed to further automate this by adding an alias to my ~/.gitconfig which states:

mr = push -o merge_request.create -o merge_request.remove_source_branch --set-upstream origin HEAD

Then, all I have to do is switch to the branch I want to create a new merge request from and run:

# notice that I don't even need to mention the branch name
$ git mr

The current branch will be pushed, it will be followed locally, a merge request based on that branch will be created, and the option to "Remove source branch" after merge checked on GitLab. There are further options you can explore to configure this as you prefer.

André Anjos
  • 4,641
  • 2
  • 27
  • 34