Skip to content

Instantly share code, notes, and snippets.

@mayconvm
Last active March 21, 2019 04:54
Show Gist options
  • Save mayconvm/23198d7576f1fbdab18bc9c4a39412e5 to your computer and use it in GitHub Desktop.
Save mayconvm/23198d7576f1fbdab18bc9c4a39412e5 to your computer and use it in GitHub Desktop.
#!/bin/bash
#### INSTALL ####
#1 - Defina as 3 variáveis abaixo
# Gitlab api endpoint
export GITLAB_API_ENDPOINT="https://gitlab.com/api/v4"
# Get it in https://gitlab.com/profile/account
export GITLAB_API_PRIVATE_TOKEN=""
# Get it from https://gitlab.com/api/v3/projects/?private_token=YOUR_PROVATE_TOKEN
export GITLAB_PROJECT_ID=""
#2 - Salve esse script na pasta .git/hooks do projeto com o nome gitla_merge_request
#3 - Execute os comandos:
# chmod +x .git/hook/gitlab_merge_request
#4 - Registre um alias no git:
# git config alias.merge-request '!sh .git/hooks/gitlab_merge_request'
# Como usar
# git merge-request <MESSAGE> <BRANCH>
# Exemplo: git merge-request "My first commit" master
# 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
# get branch target
BRANCH_TARGET="revamp"
if [ "$2" != "" ]; then
BRANCH_TARGET=$2
fi
# [A-Z]+-[0-9]+ -
BRANCH_TAG="[`echo $BRANCH | sed "s/\([A-Z]*-[0-9]*\)\(.*\)/\1/"`]"
# get message
merge_request_title=$1
echo "Será realizar o merge request do branch $BRANCH para o branch $BRANCH_TARGET";
if [ "$merge_request_title" = '' ]; then
echo "Você não digitou nenhuma mensagem."
echo -n "Digite agora: $BRANCH_TAG "
read merge_request_title
fi
# enviar repositório
echo "Enviando o branch $BRANCH"
git push origin $BRANCH 2> /dev/null
echo "Criando o Merge Request"
MR=`gitlab create_merge_request $GITLAB_PROJECT_ID "'$BRANCH_TAG $merge_request_title'" "{source_branch: "$BRANCH", target_branch: "$BRANCH_TARGET", assignee_id: 611428, remove_source_branch: true}" | grep -oE "https://.+/[0-9]+"`
echo "URL para MERGE REQUEST: "$MR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment