drone-changelogger/plugin.sh

91 lines
2.9 KiB
Bash

#!/bin/sh
echo "Starting release script"
CURRENTTAG=$(git tag --sort=-creatordate | head -n 1)
PREVIOUSTAG=$(git tag --sort=-creatordate | head -n 2 | tail -n 1)
echo "Diff: $PREVIOUSTAG..$CURRENTTAG"
echo "Generating changelog..."
ALLCHANGES=$(npx -y @philippdormann/changelogen@1.0.0 --from $PREVIOUSTAG --to $CURRENTTAG --no-output | sed 's/"/\\"/g' | tail -n +6)
echo "Changelog: $ALLCHANGES"
if [ "${PLUGIN_GITEA_DOMAIN:+set}" = set ]; then
echo "Detected gitea domain, creating release";
if [ "${PLUGIN_GITEA_APIKEY:-null}" = null ]; then
echo "No api key provided";
exit 1;
fi
if [ "${PLUGIN_REPO:-null}" = null ]; then
echo "Falling back to ci repo name: ${CI_REPO}";
PLUGIN_REPO=$CI_REPO
fi
if [ "${PLUGIN_GITEA_DRAFT:-null}" = null ]; then
PLUGIN_GITEA_DRAFT=false
fi
if [ "${PLUGIN_GITEA_PRERELEASE:-null}" = null ]; then
PLUGIN_GITEA_PRERELEASE=true
fi
curl --request POST \
--url "https://$PLUGIN_GITEA_DOMAIN/api/v1/repos/$PLUGIN_REPO/releases" \
--header "Content-Type: application/json" \
--header "Authorization: token $PLUGIN_GITEA_APIKEY" \
-f \
--data "{
\"name\": \"$CURRENTTAG\",
\"body\": \"$ALLCHANGES\",
\"draft\": $PLUGIN_GITEA_DRAFT,
\"prerelease\": $PLUGIN_GITEA_PRERELEASE,
\"tag_name\": \"$CURRENTTAG\"
}"
echo "Release created";
fi
if [ "${PLUGIN_MATRIX_SERVER:+set}" = set ]; then
echo "Detected matrix server, sending notification";
echo "Detected matrix server";
if [ "${PLUGIN_MATRIX_USER:-null}" = null ]; then
echo "No matrix username provided";
exit 3;
fi
if [ "${PLUGIN_REPO:-null}" = null ]; then
echo "No matrix password provided";
exit 4;
fi
if [ "${PLUGIN_MATRIX_ROOM:-null}" = null ]; then
echo "No matrix room id provided";
exit 4;
fi
MATRIX_LOGIN=$(curl --request POST \
--url "https://$PLUGIN_MATRIX_SERVER/_matrix/client/r0/login" \
--header "Content-Type: application/json" \
-f \
--data "{
\"type\": \"m.login.password\",
\"user\": \"$PLUGIN_MATRIX_USER\",
\"password\": \"$PLUGIN_MATRIX_PASSWORD\"
}")
MATRIX_TOKEN=$(echo $MATRIX_LOGIN | jq .access_token | sed 's/"//g' )
if [ "${PLUGIN_VERSION_PREFIX:-null}" = null ]; then
echo "Falling back to empty version prefix";
PLUGIN_VERSION_PREFIX=""
fi
FORMATTED_CHANGES=$(echo "# $PLUGIN_VERSION_PREFIX$CURRENTTAG \n $ALLCHANGES" | pandoc -f markdown -t html | sed 's/"/'"'"'/g' | xargs echo -n)
curl --request POST \
--url "https://$PLUGIN_MATRIX_SERVER/_matrix/client/r0/rooms/$PLUGIN_MATRIX_ROOM/send/m.room.message?access_token=$MATRIX_TOKEN" \
--header "Content-Type: application/json" \
-f \
--data "{
\"msgtype\": \"m.text\",
\"body\": \"$(echo "# $PLUGIN_VERSION_PREFIX$CURRENTTAG \n $ALLCHANGES" | xargs echo -n)\",
\"format\": \"org.matrix.custom.html\",
\"formatted_body\": \"$FORMATTED_CHANGES\"
}"
echo "Notification sent";
fi