Skip to content

Commit 5372338

Browse files
committed
auto create MR on push into weblate-integration
1 parent 9b39aa0 commit 5372338

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

.gitlab-ci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,15 @@ include:
22
- project: 'ci/ci-templates'
33
ref: master
44
file: .ci-build-apk-fdroid.yml
5+
6+
# https://about.gitlab.com/blog/how-to-automatically-create-a-new-mr-on-gitlab-with-gitlab-ci/
7+
weblate-integration-auto-mr:
8+
image: ${INFRA_IMAGE}
9+
stage: deploy
10+
rules:
11+
- if: $CI_COMMIT_BRANCH == "weblate-integration" && $AUTO_MR_TOKEN != null
12+
script:
13+
- HOST=${CI_PROJECT_URL} CI_PROJECT_ID=${CI_PROJECT_ID}
14+
CI_COMMIT_REF_NAME=${CI_COMMIT_REF_NAME}
15+
GITLAB_USER_ID=${GITLAB_USER_ID}
16+
PRIVATE_TOKEN=${AUTO_MR_TOKEN} ./utils/autoMergeRequest.sh

utils/autoMergeRequest.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
# Extract the host where the server is running, and add the URL to the APIs
3+
[[ $HOST =~ ^https?://[^/]+ ]] && HOST="${BASH_REMATCH[0]}/api/v4/projects/"
4+
5+
# Look which is the default branch
6+
TARGET_BRANCH=`curl --silent "${HOST}${CI_PROJECT_ID}" --header "PRIVATE-TOKEN:${PRIVATE_TOKEN}" | python3 -c "import sys, json; print(json.load(sys.stdin)['default_branch'])"`;
7+
8+
# The description of our new MR, we want to remove the branch after the MR has
9+
# been closed
10+
BODY="{
11+
\"id\": ${CI_PROJECT_ID},
12+
\"source_branch\": \"${CI_COMMIT_REF_NAME}\",
13+
\"target_branch\": \"${TARGET_BRANCH}\",
14+
\"remove_source_branch\": true,
15+
\"title\": \"WIP: ${CI_COMMIT_REF_NAME}\",
16+
\"assignee_id\":\"${GITLAB_USER_ID}\"
17+
}";
18+
19+
# Require a list of all the merge request and take a look if there is already
20+
# one with the same source branch
21+
LISTMR=`curl --silent "${HOST}${CI_PROJECT_ID}/merge_requests?state=opened" --header "PRIVATE-TOKEN:${PRIVATE_TOKEN}"`;
22+
COUNTBRANCHES=`echo ${LISTMR} | grep -o "\"source_branch\":\"${CI_COMMIT_REF_NAME}\"" | wc -l`;
23+
24+
# No MR found, let's create a new one
25+
if [ ${COUNTBRANCHES} -eq "0" ]; then
26+
curl -X POST "${HOST}${CI_PROJECT_ID}/merge_requests" \
27+
--header "PRIVATE-TOKEN:${PRIVATE_TOKEN}" \
28+
--header "Content-Type: application/json" \
29+
--data "${BODY}";
30+
31+
echo "Opened a new merge request: WIP: ${CI_COMMIT_REF_NAME} and assigned to you";
32+
exit;
33+
fi
34+
35+
echo "No new merge request opened";

0 commit comments

Comments
 (0)