drone-changelogger/plugin.sh
Nicolai Ort 59d2cf20ec
All checks were successful
continuous-integration/drone/push Build is passing
Added optional version prefix
2023-05-10 20:30:01 +02:00

90 lines
2.9 KiB
Bash

#!/bin/sh
set -euo pipefail
if [[ ! -z "${DRONE_NETRC_MACHINE}" ]]; then
cat <<EOF > ${HOME}/.netrc
machine ${DRONE_NETRC_MACHINE}
login ${DRONE_NETRC_USERNAME}
password ${DRONE_NETRC_PASSWORD}
EOF
fi
if [[ -z "${DRONE_COMMIT_AUTHOR_NAME}" ]]; then
export DRONE_COMMIT_AUTHOR_NAME=drone
fi
if [[ -z "${DRONE_COMMIT_AUTHOR_EMAIL}" ]]; then
export DRONE_COMMIT_AUTHOR_EMAIL=drone@localhost
fi
export GIT_AUTHOR_NAME=${DRONE_COMMIT_AUTHOR_NAME}
export GIT_AUTHOR_EMAIL=${DRONE_COMMIT_AUTHOR_EMAIL}
export GIT_COMMITTER_NAME=${DRONE_COMMIT_AUTHOR_NAME}
export GIT_COMMITTER_EMAIL=${DRONE_COMMIT_AUTHOR_EMAIL}
git fetch --tags
CURRENTTAG=$(git tag --sort=-taggerdate | head -n 1)
PREVIOUSTAG=$(git tag --sort=-taggerdate | head -n 2 | tail -n 1)
ALLCHANGES=$(npx -y @philippdormann/changelogen@latest --from $PREVIOUSTAG --to $CURRENTTAG --no-output | sed 's/"/\\"/g' | tail -n +6)
FORMATTED_CHANGES=$(echo "# $PLUGIN_VERSION_PREFIX$CURRENTTAG \n $ALLCHANGES" | pandoc -f markdown -t html | sed 's/"/'"'"'/g' | xargs echo -n)
if [ "${PLUGIN_GITEA_DOMAIN:+set}" = set ]; then
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 drone repo name";
PLUGIN_REPO=$CI_REPO_NAME
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" \
--data "{
\"name\": \"$CURRENTTAG\",
\"body\": \"$ALLCHANGES\",
\"draft\": false,
\"tag_name\": \"$CURRENTTAG\"
}"
echo "Release created";
fi
if [ "${PLUGIN_MATRIX_SERVER:+set}" = set ]; then
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" \
--data "{
\"type\": \"m.login.password\",
\"user\": \"$PLUGIN_MATRIX_USER\",
\"password\": \"$PLUGIN_MATRIX_PASSWORD\"
}")
MATRIX_TOKEN=$(echo $MATRIX_LOGIN | jq .access_token | sed 's/"//g' )
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" \
--data "{
\"msgtype\": \"m.text\",
\"body\": \"$(echo "# $CURRENTTAG \n $ALLCHANGES" | xargs echo -n)\",
\"format\": \"org.matrix.custom.html\",
\"formatted_body\": \"$FORMATTED_CHANGES\"
}"
echo "Notification sent";
fi