Skip to content

Instantly share code, notes, and snippets.

@ColCh
Last active April 5, 2022 12:06
Show Gist options
  • Save ColCh/35c495786e25e33976706016eed7f200 to your computer and use it in GitHub Desktop.
Save ColCh/35c495786e25e33976706016eed7f200 to your computer and use it in GitHub Desktop.
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
Copy link

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