summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2025-05-27 13:06:51 +0300
committerAndrew Dolgov <fox@fakecake.org>2025-05-27 13:09:29 +0300
commita26d12ba3bd4b031c4eb79424b623b2630ab3f61 (patch)
tree92e43c78e2ca919631bdf05919e50343c2759bd2
parent0688e6dadd74f635b16f4e592dc48f80486e5d6e (diff)
auto create MR on push into weblate-integration
-rw-r--r--.gitlab-ci.yml12
-rwxr-xr-xutils/autoMergeRequest.sh35
2 files changed, 47 insertions, 0 deletions
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 59ee64326..a0e28830d 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -249,3 +249,15 @@ update-prod:
ACCESS_TOKEN: ${PROD_HELM_TOKEN}
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $PROD_HELM_TOKEN != null
+
+# https://about.gitlab.com/blog/how-to-automatically-create-a-new-mr-on-gitlab-with-gitlab-ci/
+weblate-integration-auto-mr:
+ image: ${INFRA_IMAGE}
+ stage: publish
+ rules:
+ - if: $CI_COMMIT_BRANCH == "weblate-integration" && $AUTO_MR_TOKEN != null
+ script:
+ - HOST=${CI_PROJECT_URL} CI_PROJECT_ID=${CI_PROJECT_ID}
+ CI_COMMIT_REF_NAME=${CI_COMMIT_REF_NAME}
+ GITLAB_USER_ID=${GITLAB_USER_ID}
+ PRIVATE_TOKEN=${AUTO_MR_TOKEN} ./utils/autoMergeRequest.sh
diff --git a/utils/autoMergeRequest.sh b/utils/autoMergeRequest.sh
new file mode 100755
index 000000000..bd33db0ca
--- /dev/null
+++ b/utils/autoMergeRequest.sh
@@ -0,0 +1,35 @@
+#!/usr/bin/env bash
+# Extract the host where the server is running, and add the URL to the APIs
+[[ $HOST =~ ^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:${PRIVATE_TOKEN}" | python3 -c "import sys, json; print(json.load(sys.stdin)['default_branch'])"`;
+
+# 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}\",
+ \"assignee_id\":\"${GITLAB_USER_ID}\"
+}";
+
+# Require a list of all the merge request and take a look if there is already
+# one with the same source branch
+LISTMR=`curl --silent "${HOST}${CI_PROJECT_ID}/merge_requests?state=opened" --header "PRIVATE-TOKEN:${PRIVATE_TOKEN}"`;
+COUNTBRANCHES=`echo ${LISTMR} | grep -o "\"source_branch\":\"${CI_COMMIT_REF_NAME}\"" | wc -l`;
+
+# No MR found, let's create a new one
+if [ ${COUNTBRANCHES} -eq "0" ]; then
+ curl -X POST "${HOST}${CI_PROJECT_ID}/merge_requests" \
+ --header "PRIVATE-TOKEN:${PRIVATE_TOKEN}" \
+ --header "Content-Type: application/json" \
+ --data "${BODY}";
+
+ echo "Opened a new merge request: WIP: ${CI_COMMIT_REF_NAME} and assigned to you";
+ exit;
+fi
+
+echo "No new merge request opened";