Skip to content
Snippets Groups Projects
Commit 3bbd21cd authored by jurgenhaas's avatar jurgenhaas
Browse files

gitlab-ci-cd/drupal#28 Add script to merge current branch upstream

parent e523e26d
No related branches found
Tags v2.2.9
1 merge request!3Merging develop into main
#!/bin/bash
function gitlab() {
output="$(curl --write-out "\nHTTP-Code:%{http_code}" -X "$2" -s -H "Private-Token: ${GITLAB_PRIVATE_TOKEN}" "${GITLAB_URL}"/api/v4/"$3" "$@")"
result="$(echo "$output" | grep "{")"
code="$(echo "$output" | grep -m 1 "HTTP-Code:" | cut -d: -f2)"
if [[ $code -eq 200 ]]; then
return
fi
echo "$2 request to $3 did not succeed! Responds with code $code instead of $1"
exit 99
}
function isNumeric() {
re='^[0-9]+$'
if [[ $1 =~ $re ]]; then
return 0
fi
return 1
}
function parseUrl() {
# extract the protocol
proto="$(echo "$1" | grep :// | sed -e's,^\(.*://\).*,\1,g')"
# remove the protocol
url="${1/$proto/}"
# extract the user (if any)
user="$(echo "$url" | grep @ | cut -d@ -f1)"
# extract the host and port
hostport="$(echo "${url/$user@/}" | cut -d/ -f1)"
# by request host without port
# shellcheck disable=SC2001
host="$(echo "$hostport" | sed -e 's,:.*,,g')"
# by request - try to extract the port
port="$(echo "$hostport" | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')"
# extract the path (if any)
path="$(echo "$url" | sed -e 's,:,/,g' | grep / | cut -d/ -f2-)"
}
SOURCEBRANCH=$(git rev-parse --abbrev-ref HEAD)
if [[ "$SOURCEBRANCH" == "main" ]]; then
echo "Main branch can not be merged."
exit 1
elif [[ "$SOURCEBRANCH" == "develop" ]]; then
TARGETBRANCH=main
else
TARGETBRANCH=develop
fi
if [[ "x${GITLAB_URL}" == "x" ]]; then
echo "Missing GITLAB_URL environment variable, should be set to e.g. https://gitlab.lakedrops.com"
exit 2
fi
if [[ "x${GITLAB_PRIVATE_TOKEN}" == "x" ]]; then
echo "Missing GITLAB_PRIVATE_TOKEN environment variable"
exit 3
fi
parseUrl "$GITLAB_URL"
host1=$host
parseUrl "$(git config remote.origin.url)"
host2=$host
if [[ "$host1" != "$host2" ]]; then
echo "This repository doesn't match the provided GitLab instance"
exit 4
fi
# shellcheck disable=SC2001
path="$(echo "$path" | sed -e 's,\..*,,g')"
gitlab 200 GET projects?search="$path"
PRJID=$(echo "$result" | jq -r .[0]."id")
if [[ $(isNumeric "$PRJID") -eq 1 ]]; then
echo "Can not find project ID"
exit 5
fi
# Create MR
gitlab 200 POST projects/"$PRJID"/merge_requests --data "source_branch=$SOURCEBRANCH" --data "target_branch=$TARGETBRANCH" --data "title=Merging $SOURCEBRANCH into $TARGETBRANCH"
MRID=$(echo "$result" | jq -r ."iid")
if [[ $(isNumeric "$MRID") -eq 1 ]]; then
echo "Can not create merge request"
exit 6
fi
echo "Created MR at $(echo "$result" | jq -r ."web_url")"
# Merge MR
gitlab 200 PUT projects/"$PRJID"/merge_requests/"$MRID"/merge --data "should_remove_source_branch=0"
echo "Merged successfully!"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment