diff --git a/README.md b/README.md index a3fdbbb..9bcadf8 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,21 @@ Open Merge Request: You can see this in action at [`.gitlab-ci.yml` of this project](.gitlab-ci.yml). +### Additional features + +- You can set prefix for your merge request title like "WIP:" + +```yaml + ... + + variables: + GIT_STRATEGY: none + REMOVE_BRANCH_AFTER_MERGE: true # Remove branch after merge + COMMIT_PREFIX: WIP # Additional prefix + ... +``` +- If source and target branch is same then merge request will be fail. + ## Docker images The images are hosted on [Docker Hub](https://hub.docker.com/r/tmaier/gitlab-auto-merge-request). diff --git a/merge-request.sh b/merge-request.sh index 760f791..eea463e 100755 --- a/merge-request.sh +++ b/merge-request.sh @@ -7,20 +7,40 @@ if [ -z "$GITLAB_PRIVATE_TOKEN" ]; then exit 1 fi +# Conditional commit prefix, etc: WIP +if [ -z "$COMMIT_PREFIX" ]; then + COMMIT_TITLE="${CI_COMMIT_REF_NAME}" +else + COMMIT_TITLE="${COMMIT_PREFIX}: ${CI_COMMIT_REF_NAME}" +fi + +# Conditional remove branch after merge +if [ -z "$REMOVE_BRANCH_AFTER_MERGE" ]; then + REMOVE_BRANCH_AFTER_MERGE=false +fi + # Extract the host where the server is running, and add the URL to the APIs [[ $CI_PROJECT_URL =~ ^https?://[^/]+ ]] && HOST="${BASH_REMATCH[0]}/api/v4/projects/" # Look which is the default branch TARGET_BRANCH=`curl --silent "${HOST}${CI_PROJECT_ID}" --header "PRIVATE-TOKEN:${GITLAB_PRIVATE_TOKEN}" | jq --raw-output '.default_branch'`; +# If Source and Target branch is same then exit. +if [ "${CI_COMMIT_REF_NAME}" -eq "${TARGET_BRANCH}" ]; then + echo "Source and Target branch is must be different!" + echo "Source: ${CI_COMMIT_REF_NAME}" + echo "Target: ${TARGET_BRANCH}" + exit 1 +fi + # The description of our new MR, we want to remove the branch after the MR has # been closed BODY="{ \"id\": ${CI_PROJECT_ID}, \"source_branch\": \"${CI_COMMIT_REF_NAME}\", \"target_branch\": \"${TARGET_BRANCH}\", - \"remove_source_branch\": true, - \"title\": \"WIP: ${CI_COMMIT_REF_NAME}\", + \"remove_source_branch\": \"${REMOVE_BRANCH_AFTER_MERGE}\", + \"title\": \"${COMMIT_TITLE}\", \"assignee_id\":\"${GITLAB_USER_ID}\" }"; @@ -36,7 +56,7 @@ if [ ${COUNTBRANCHES} -eq "0" ]; then --header "Content-Type: application/json" \ --data "${BODY}"; - echo "Opened a new merge request: WIP: ${CI_COMMIT_REF_NAME} and assigned to you"; + echo "Opened a new merge request: ${COMMIT_TITLE} and assigned to you"; exit; fi