diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000000000000000000000000000000000000..ae7657814edf25d36f36403c8007c748af3087b2 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +/.gitlab-ci.yml +/.hadolint.yaml +/README.md diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/.hadolint.yaml b/.hadolint.yaml new file mode 100644 index 0000000000000000000000000000000000000000..8f7e23e459df52ebbd66d768be22912a79d80eca --- /dev/null +++ b/.hadolint.yaml @@ -0,0 +1,2 @@ +ignored: + - DL3008 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..16083228892fa7afd80c5de84f5c03cbd9b3c84b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM registry.cyberbrain.pw/docker/alpine:latest + +RUN set -ex && \ + apk --no-cache add bash curl jq && \ + rm -rf /var/cache/apk/* + +COPY entrypoint.sh /entrypoint.sh + +ENTRYPOINT [ "bash", "/entrypoint.sh" ] diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000000000000000000000000000000000000..f7845ed4602ff8c75507b0fd3a17563fe215f641 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +# To/Die/For =) +unset TO_DIE +die() { + RED='\033[0;31m' + NC='\033[0m' # No Color + echo -e "${RED}$1${NC}" + TO_DIE=1 +} +########################################################################################################################## + +test -z "${CI_API_V4_URL}" && die "CI_API_V4_URL is missing" +test -z "${CI_PROJECT_ID}" && die "CI_PROJECT_ID is missing" +test -z "${CI_COMMIT_REF_NAME}" && die "CI_COMMIT_REF_NAME is missing" +test -z "${TARGET_BRANCH}" && die "TARGET_BRANCH is missing" +test -z "${PRIVATE_TOKEN}" && die "PRIVATE_TOKEN is missing" + +########################################################################################################################## +test -n "${TO_DIE}" && exit 1 +########################################################################################################################## + +# Extract the host where the server is running, and add the URL to the APIs +HOST="${CI_API_V4_URL}/projects/" + +# Look which is the default branch +TARGET_BRANCH=`curl --silent "${HOST}${CI_PROJECT_ID}" --header "PRIVATE-TOKEN:${PRIVATE_TOKEN}" | jq .default_branch` + +# 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}" \ + --form "id=${CI_PROJECT_ID}" \ + --form "source_branch=${CI_COMMIT_REF_NAME}" \ + --form "target_branch=${TARGET_BRANCH}" \ + --form "title=\"WIP: ${CI_COMMIT_REF_NAME}\"" \ + --form "remove_source_branch=true" + + echo "Opened a new merge request: WIP: ${CI_COMMIT_REF_NAME}" + exit +fi + +echo "No new merge request opened"