Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional features #4

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
26 changes: 23 additions & 3 deletions merge-request.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}\"
}";

Expand All @@ -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

Expand Down