From 3a9dc6e261e73cd0c8b2d27188143cc2bd55578d Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Fri, 12 May 2023 08:30:13 +0200 Subject: [PATCH] Added plugin logic --- plugin.sh | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 plugin.sh diff --git a/plugin.sh b/plugin.sh new file mode 100644 index 0000000..3299efe --- /dev/null +++ b/plugin.sh @@ -0,0 +1,55 @@ +#!/bin/sh + +set -euo pipefail + + +if [ "${PLUGIN_DOMAIN:-null}" = null ]; then + echo "No gitea domain provided"; + exit 1; +fi +if [ "${PLUGIN_APIKEY:-null}" = null ]; then + echo "No api key provided"; + exit 2; +fi +if [ "${PLUGIN_SOURCE:-null}" = null ]; then + echo "No source branch provided, falling back to branch that triggered ci"; + exit 3; +fi +if [ "${PLUGIN_TARGET:-null}" = null ]; then + echo "No target branch provided"; + exit 4; +fi +if [ "${PLUGIN_TITLE:-null}" = null ]; then + PLUGIN_TITLE="$PLUGIN_SOURCE -> $PLUGIN_TARGET"; +fi + +RES=$(curl --request POST \ + --url "https://$PLUGIN_DOMAIN/api/v1/repos/$DRONE_REPO/pulls" \ + --header "Content-Type: application/json" \ + --header "Authorization: token $APIKEY" \ + -F \ + --data "{ + \"title\": \"$PLUGIN_TITLE\", + \"body\": \"Automatic merge of $PLUGIN_SOURCE into $PLUGIN_TARGET\", + \"base\": \"$PLUGIN_TARGET\", + \"head\": \"$PLUGIN_SOURCE\" +}") +echo "PR created"; + +if [ "${PLUGIN_MERGE:-false}" = true ]; then + if [ "${PLUGIN_DELETE_SOURCE:-null}" = null ]; then + echo "Falling back to not deleting source"; + PLUGIN_DELETE_SOURCE=false; + fi + + curl --request POST \ + --url "https://$PLUGIN_DOMAIN/api/v1/repos/$DRONE_REPO/pulls/$ID/merge" \ + --header 'Content-Type: application/json' \ + --header "Authorization: token $APIKEY" \ + -F \ + --data "{ + \"do\": \"merge\", + \"delete_branch_after_merge\": $PLUGIN_DELETE_SOURCE + }" + echo "PR merged"; +fi \ No newline at end of file