DEV Community

Cover image for Gitlab: Create merge requests from cli
Ahad Birang
Ahad Birang

Posted on • Originally published at farnabaz.ir

Gitlab: Create merge requests from cli

Me and my co-workers are working on a single project. Each one of us creates a branch for a specific task and after doing some magic we had to create a merge request to the project's main branch. Merge request will be merged after another folk did approve its changes.
One thing that bothers me, is that I have to open Gitlab and create a new merge request to the main branch, every time.

I had an idea to create a merge request in CLI, and without having to visit the Gitlab website. And thanks to the Gitlab's team it is really easy to create a merge request from CLI.

As the documentation says:

GitLab supports using Git push options to perform various actions at the same time as pushing changes.

Currently, there are push options available for:

  • Skipping CI jobs
  • Merge requests

NOTICE: You need to have Git 2.10 or newer to use push options.

Using Gitlab's push options we can create a merge request just by pushing our new branch to the remote repository. All we have to do is to add -o merge_request.create option to git push command

git push -o merge_request.create origin my-branch
Enter fullscreen mode Exit fullscreen mode

Executing this command will push my-branch to the remote repository and create a new merge request from out branch to the main branch of the project.

There an option to specify the target branch of the merge request. -o merge_request.target=my-target-branch will do the magic.

git push \
    -o merge_request.create \
    -o merge_request.target=my-target-branch \
    origin my-branch 
Enter fullscreen mode Exit fullscreen mode

Also, we can change the title of the merge request's

git push -o merge_request.title="<title>"
Enter fullscreen mode Exit fullscreen mode

Set the description of the merge request.

git push -o merge_request.description="The description I want"
Enter fullscreen mode Exit fullscreen mode

And set the merge request to remove the source branch when it’s merged.

git push -o merge_request.remove_source_branch
Enter fullscreen mode Exit fullscreen mode

Gitlab push options are awesome and solved my problem. However, I'm too lazy to write all these options every time. I needed to create a script to do it with ease.

I have created a little js file to execute this command, let's call it .create-merge-request.js

var exec = require('child_process').exec;

var targetBranch = process.argv[2] || "develop"

exec("git push origin HEAD \
        -o merge_request.create \
        -o merge_request.remove_source_branch \
        -o merge_request.target=" + targetBranch,
    (error, stdout, stderr) => {
        stdout && console.log(`[stdout]\n${stdout}`);
        stderr && console.log(`[stderr]\n${stderr}`);
        if (error !== null) {
            console.log(`exec error: ${error}`);
        }
    }
);
Enter fullscreen mode Exit fullscreen mode

After this I've updated the project's package.json file and added new script.

{
  "scripts": {
    "merge": "node .create-merge-request.js",
  }
}
Enter fullscreen mode Exit fullscreen mode

Finally, I've created a merge request using this simple command.

yarn merge my-target-branch
Enter fullscreen mode Exit fullscreen mode

NOTICE: Do not push your branch before this command. If you push your branch before this command it will not work. Git will response with Everything up-to-date and merge request will not be created

Top comments (4)

Collapse
 
dino003 profile image
dino003

Good article Guy
But what about assignee ?

Collapse
 
farnabaz profile image
Ahad Birang

Unfortunately based on official docs there isn't any way to assign PR to a user at the time.

Collapse
 
nigredotori profile image
Dmitry Polienko • Edited

You can pass slash commands as a part of the description, so having

/assign @someone
Enter fullscreen mode Exit fullscreen mode

as a part of the description did the trick for me. I'm not sure whether this is a bug or a feature, and whether it works with all GitLab and API versions.

UPD: Though do keep in mind that you can't submit multiline strings via push options, and as far as I can see, GitLab has no workaround that would work with slash commands. See this issue.

Collapse
 
smichaelsen profile image
Sebastian Michaelsen

I wanted to create a CI job, that does some stuff and then creates a MR (in a 2nd repo). I thought I had to fiddle with GitLab API, but this option saved me the struggle. Thanks!