diff --git a/.phorge_bot_telegram_config.example b/.phorge_bot_telegram_config.example new file mode 100644 index 0000000..bc8f832 --- /dev/null +++ b/.phorge_bot_telegram_config.example @@ -0,0 +1,7 @@ +# Contact the Telegram Bot Father to create your Telegram bot. +# Insert here your Telegram bot father parameters. +# Put here your Telegram Chat ID (document yourself about how to get your chat ID). +# TODO: remove the Chat ID from here and customize from Phorge itself. +# Then save this file as ~/.phorge_bot_telegram_config +TELEGRAM_BOT_TOKEN="123" +TELEGRAM_CHAT_ID="-1001282771908" diff --git a/publish-latest-branch-build.sh b/publish-latest-branch-build.sh index f36ab23..5ecb3c6 100755 --- a/publish-latest-branch-build.sh +++ b/publish-latest-branch-build.sh @@ -1,100 +1,146 @@ #!/bin/sh ################################################################ # This script just take a built APK and move it to a directory # ################################################################ # # Currently in use here: -# # https://gitpull.it/harbormaster/step/view/6/ # +# Source code: +# https://gitpull.it/P23 +# # -- Valerio B., Wed 26 Feb 2020 11:54:30 PM CET # ################################################################ # # Usage: the first argument is the Differential ID number # or empty if working on master. # # die on errors set -e +# Require the Telegram +if [ -f ~/.phorge_bot_telegram_config ]; then + echo "[INFO] including .env file" + . ~/.phorge_bot_telegram_config +else + echo "[WARN] skipping non-existing ~/.phorge_bot_telegram_config file for user $(whoami) with home $HOME" +fi + # expected base URL EXPECTED_DEPLOY_URL=https://lab.reyboz.it/builds/libre-busto/ # pathname to the repository built apk file #APK_PATH=./build/outputs/apk/release/libre-busto-release-unsigned.apk #APK_PATH=./app/build/outputs/apk/debug/libre-busto-debug.apk #APK_PATH=./app/build/outputs/apk/debug/app-debug.apk APK_PATH=./app/build/outputs/apk/gitpull/app-gitpull-signed.apk # repository build pathname BUILD_DIR=/home/www-data/reyboz.it/lab/builds/libre-busto -# current directory -CURRENT_DIR=$(pwd) - # document id of the page to be commented document_id= # get the branch name branch_name=asd if [ -n "$1" ]; then # it's a Differential revision branch_name=D"$1" document_id="$branch_name" else # get a ref name (probably "HEAD") # remotes/origin/bigupdate remote_shit=$(git name-rev --name-only HEAD) # bigupdate branch_name=$(basename "$remote_shit") fi # give a name of this built apk -APK_NAME="$branch_name"-libre-busto-unsigned.apk +#APK_NAME="$branch_name"-libre-busto-unsigned.apk +APK_NAME="$branch_name"-libre-busto.apk # complete build URL BUILD_URL="$EXPECTED_DEPLOY_URL""$APK_NAME" # pathname to the APK file in the HTTP build directory BUILD_APK_FILE="$BUILD_DIR"/"$APK_NAME" # pathname to the git show file in the HTTP build directory BUILD_GIT_SHOW_FILE="$BUILD_DIR"/"$branch_name"-git-show.txt +echo "Current directory:" +echo " $(pwd)" + +echo "Calculating sha256sum of:" +echo " $APK_PATH" + +echo "File info" +ls -l "$APK_PATH" + # get the digital signature of the last version -APK_SHA256=$(sha256sum "$APK_PATH") +APK_SHA256=$(sha256sum "$APK_PATH" | head -c 64) # overwrite the latest built APK for this branch name (force date update) +echo "Cleaning old builds" rm --force "$BUILD_APK_FILE" cp "$APK_PATH" "$BUILD_APK_FILE" # overwrite the latest git show for this branch name (force date update) rm --force "$BUILD_GIT_SHOW_FILE" git show > "$BUILD_GIT_SHOW_FILE" -# update checksums +echo "Update checksums" cd "$BUILD_DIR" rm -f sha1sum.txt sha1sum * > sha1sum.txt cd - > /dev/null # show the build URL in the log echo "Published here:" echo "$BUILD_URL" +apix='`' + # add a comment to the build URL if [ -n "$document_id" ] && [ -z "$NO_COMMENT" ]; then - phab-comment.php "$document_id" "$BUILD_URL" "$APK_SHA256" + + # Escaping texts for Telegram :( + # https://github.com/topkecleon/telegram-bot-bash/blob/8f23bca8a1f733aa5d69c648082b59716d7c1fa7/bashbot.sh#L105 + document_id_escaped=$(printf "%s" "$document_id" | sed -E -e 's/\r//g' -e 's/([-"`´,§$%&/(){}#@!?*.\t])/\\\1/g') + BUILD_URL_ESCAPED=$(printf "%s" "$BUILD_URL" | sed -E -e 's/\r//g' -e 's/([-"`´,§$%&/(){}#@!?*.\t])/\\\1/g') + APK_NAME_ESCAPED=$(printf "%s" "$APK_NAME" | sed -E -e 's/\r//g' -e 's/([-"`´,§$%&/(){}#@!?*.\t])/\\\1/g') + + # Send message to Telegram groups. + telegram_chat_content="⚙️ Dear Android hackers, please review this promising patch: +https://gitpull\.it/$document_id_escaped + +Here the related fresh test build: +[Download $APK_NAME_ESCAPED]($BUILD_URL_ESCAPED) + +APK sha256: +$apix$APK_SHA256$apix" + + echo "DEBUG TELEGRAM CONTENT" + echo "$telegram_chat_content" + telegram-comment.sh "$TELEGRAM_BOT_TOKEN" "$TELEGRAM_CHAT_ID" "$telegram_chat_content" || true + + # Send message to Phorge. + echo "Adding bipbop Comment to $document_id" + phab-comment.php "$document_id" "$BUILD_URL" "$APK_SHA256" || true fi # delete very old files +echo "Cleaning" find "$BUILD_DIR" -type f -mtime +120 -delete # clean old APK in the repository to never re-use it by mistake # as it happened when we moved everything to the /app directory rm "$APK_PATH" + +echo "Done" diff --git a/telegram-comment.sh b/telegram-comment.sh new file mode 100755 index 0000000..840a4ce --- /dev/null +++ b/telegram-comment.sh @@ -0,0 +1,22 @@ +#!/bin/sh -e + +if [ -z "$1" ]; then + echo "Missing Telegram Bot ID" + exit 1 +fi + +if [ -z "$2" ]; then + echo "Missing Chat ID" + exit 1 +fi + +if [ -z "$3" ]; then + echo "Missing message text" + exit 1 +fi + +curl \ + --data parse_mode=MarkdownV2 \ + --data chat_id="$2" \ + --data text="$3" \ + --request POST https://api.telegram.org/bot"$1"/sendMessage