gitlab-mr.sh

By devin, 10 January, 2016

This is a script to allow quick gitlab merge requests on the cli.

If you create a new branch, commit it, and then run gitlab-mr my new merge request, then a merge request will be created on the appropriate project from the current branch into the develop branch, with title "my new merge request".

There are additional command line options to specify a markdown description, using cat and Ctrl+D to enter the input.

I've set it up to use pwd to find which project I'm in, which then maps to the ID. You would need to modify the script to your own environment. If you only have one project, you could just hard code the ID.

Here is the script:

#!/bin/bash

# need to install https://github.com/NARKOZ/gitlab
# need to set up ~/.bashrc with GITLAB_API_ENDPOINT and GITLAB_API_PRIVATE_TOKEN

# by default, this will create a merge request merging the origin branch matching your current local
# branch into develop, with a description equal to the command's arguments.

TARGET=develop
SOURCE=$(git status | awk 'NR==1{ print $3 }')
if [[ "$(pwd | grep uniland)" ]]; then
  ID=31
elif [[ "$(pwd | grep uniqueway)" ]]; then
  ID=1
fi

while true; do
  case $1 in
    -i|--id) ID=$2; shift; shift ;;
    -s|--source) SOURCE=$2; shift; shift ;;
    -t|--target) TARGET=$2; shift; shift ;;
    -T|--title) TITLE=$2; shift; shift ;;
    -m|-d|--description) DESCRIPTION=$2; shift; shift ;;
    *) break;;
  esac
done

#for quick and dirty mr, allow specifying title directly as bash arguments and skipping description
if [[ $# > 1 ]]; then
  NO_DESC="true"
  TITLE=$@
fi

if [[ -z "$ID" ]]; then
  echo "Problem getting project id"
  echo "uniland is 31, uniqueway is 1"
  read -p "Please enter project id: " ID
fi

if [[ -z "$TITLE" ]]; then
  read -p "Please enter merge request title" TITLE
fi

if [[ -z "$NO_DESC" ]] && [[ -z "$DESCRIPTION" ]]; then
  echo "Please enter a merge request description. Ctrl+D to finish"
  DESCRIPTION=$(cat)
fi

source ~/.bashrc
GITLAB_API_ENDPOINT=${GITLAB_API_ENDPOINT} \
GITLAB_API_PRIVATE_TOKEN=${GITLAB_API_PRIVATE_TOKEN} \
gitlab create_merge_request ${ID} "${TITLE}" "{source_branch: '${SOURCE}', target_branch: '${TARGET}', description: '${DESCRIPTION}'}"

Tags

Plain text

  • No HTML tags allowed.
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.