Skip to content

Instantly share code, notes, and snippets.

@ColCh
Last active Feb 20, 2021
Embed
What would you like to do?
Create merge request on Gitlab in command line for current branch

Gitlab merge request script

Creates merge request on Gitlab for you

Installation

Download it, add executable perms and place into PATH:

# Place it into ~/.bin
mkdir ~/.bin
curl https://gist.githubusercontent.com/ColCh/35c495786e25e33976706016eed7f200/raw/gitlab-merge-request > ~/.bin/gitlab-merge-request
chmod +x ~/.bin/gitlab-merge-request

Then install some gems for gitlab:

sudo gem install git gitlab

Then define environment variables for your Project(I use autoenv with zsh):

# Gitlab api endpoint
export GITLAB_API_ENDPOINT="https://gitlab.com/api/v3"
# Get it in https://gitlab.com/profile/account
export GITLAB_API_PRIVATE_TOKEN="PLACE_IT_HERE"
# Get it from https://gitlab.com/api/v3/projects/?private_token=YOUR_PROVATE_TOKEN
export GITLAB_PROJECT_ID="1397044"

Usage

Push your branch on remote first:

git push

Then

gitlab-merge-request

Yes! It will prompt you for Merge Request title.

#!/bin/bash
# sudo gem install git gitlab first
if ! which gitlab >/dev/null 2>&1; then
echo "gitlab command not found. Install gitlab gem first"
echo " sudo gem install git gitlab"
exit 1
fi
if [[ ! $GITLAB_PROJECT_ID ]]; then
echo "Define project id in GITLAB_PROJECT_ID environment variable"
exit 1
fi
# Get current branch from GIT
branch_name=$(git symbolic-ref -q HEAD)
branch_name=${branch_name##refs/heads/}
branch_name=${branch_name:-HEAD}
BRANCH=$branch_name
echo "Merge Request will go from branch $BRANCH to branch master!"
echo -n "Enter your title for Merge Request: "
read merge_request_title
echo
# See https://github.com/NARKOZ/gitlab
# see: http://www.rubydoc.info/gems/gitlab/3.4.0/Gitlab/Client/MergeRequests#create_merge_request-instance_method
gitlab create_merge_request $GITLAB_PROJECT_ID "$merge_request_title" "{source_branch: "$BRANCH", target_branch: 'master'}"
@shrave

This comment has been minimized.

Copy link

@shrave shrave commented Mar 22, 2017

Added pdf doc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment