Skip to content

Instantly share code, notes, and snippets.

@bench
Last active September 1, 2022 04:20
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save bench/f27e0c5ea171d1f548041c45e0f304b1 to your computer and use it in GitHub Desktop.
Save bench/f27e0c5ea171d1f548041c45e0f304b1 to your computer and use it in GitHub Desktop.
A script to create gitlab MR from a shell
#!/bin/bash
# Check if jq is installed
if ! type "jq" > /dev/null; then
echo please install jq
fi
title="$(git log --oneline | head -1 | cut -f 2- -d ' ')"
source_branch="$(git branch | grep '*' | cut -f 2 -d ' ')"
target_branch=master
description=
while [ "$#" -gt 0 ]; do
case "$1" in
--source-branch)
source_branch="$2"
shift
shift
;;
--target-branch)
target_branch="$2"
shift
shift
;;
--title)
title="$2"
shift
shift
;;
--description)
description="$2"
shift
shift
;;
-h)
echo "Usage:"
echo " $ git merge-request [OPTIONS]"
echo ""
echo "Options:"
echo " --source-branch Source branch of the merge request. (default: current branch)"
echo " --target-branch Target branch of the merge request. (default: master)"
echo " --title Title of the merge request. (default: first line of last commit log)"
echo " --description Description of the merge request. (default: empty)"
echo ""
exit 0
;;
esac
done
gitlab_url="$(git config --get gitlab.url)"
if [ "$gitlab_url" == "" ]; then
echo "Gitlab url is not set"
echo " $ git config --replace-all gitlab.url <url>"
exit 1
fi
project_id="$(git config --local --get gitlab.id)"
if [ "$project_id" == "" ] || [ "$project_id" -eq 1 ]; then
echo "Project id is not set, you will find it in gitlab project settings"
echo " $ git config --local --replace-all gitlab.id <id>"
exit 1
fi
repo="$(git config --local --get gitlab.repo)"
if [ "$repo" == "" ]; then
echo "Repository name is not set"
echo " $ git config --local --replace-all gitlab.repo <repository_namespace>/<repository_name>"
exit 1
fi
private_token="$(git config --get gitlab.privatetoken)"
if [ "$private_token" == "" ]; then
echo "Private token is not set"
echo " $ git config --local --add gitlab.privatetoken <private token>"
exit 1
fi
remote="$(git config --get gitlab.remote)"
if [ "$remote" == "" ]; then
remote=origin
fi
if ! git push -u ${remote} ${source_branch}; then
echo "Failed to push ${target_branch}"
exit 1
fi
merge_request_id=$(curl \
-vv \
--insecure \
-s \
--header "PRIVATE-TOKEN: $private_token" \
-d source_branch="${source_branch}" \
-d target_branch="${target_branch}" \
-d title="${title}" \
"${gitlab_url}/api/v3/projects/${project_id}/merge_requests" \
| jq '.iid')
echo "Open ${gitlab_url}/${repo}/merge_requests/${merge_request_id}"
@mjahmed-wd
Copy link

what should be the command?

@bench
Copy link
Author

bench commented Aug 25, 2022

Hi @mjahmed-wd,
you will get details on how to exec the command with git merge-request -h.

       -h)
            echo "Usage:"
            echo "  $ git merge-request [OPTIONS]"
            echo ""
            echo "Options:"
            echo "  --source-branch    Source branch of the merge request. (default: current branch)"
            echo "  --target-branch    Target branch of the merge request. (default: master)"
            echo "  --title            Title of the merge request. (default: first line of last commit log)"
            echo "  --description      Description of the merge request. (default: empty)"
            echo ""
            exit 0

Just be aware that this script is a few year old and gitlab API might have changed.
Don't hesitate to give me a feedback ;)

@mjahmed-wd
Copy link

mjahmed-wd commented Sep 1, 2022

I made a command like this

git push -o merge_request.create -o merge_request.target="$targetBranch" -o merge_request.remove_source_branch -o merge_request.title="$1"

you can change $targetBranch and $1 as wish

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