drone-gitea-pr/plugin.sh

63 lines
1.7 KiB
Bash

#!/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 $PLUGIN_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
if [ "${PLUGIN_SQUASH:-null}" = null ]; then
echo "Merging PR";
ACTION="merge";
else
echo "Squashing PR";
ACTION="squash";
fi
ID=$(echo $RES | jq '.number')
curl --request POST \
--url "https://$PLUGIN_DOMAIN/api/v1/repos/$DRONE_REPO/pulls/$ID/merge" \
--header 'Content-Type: application/json' \
--header "Authorization: token $PLUGIN_APIKEY" \
-f \
--data "{
\"do\": \"$ACTION\",
\"delete_branch_after_merge\": $PLUGIN_DELETE_SOURCE
}"
echo "PR merged";
fi