diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..69b1bb3 --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +TELEGRAM_BOT_TOKEN="this from Telegram bot father" +TELEGRAM_CHAT_ID="this is a negative number for the Telegram group" +KEYSTORE_PASSPHRASE="this is the Java keystore password" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f10862a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.env diff --git a/bin/publish-latest-branch-build.php b/bin/publish-latest-branch-build.php new file mode 100755 index 0000000..e4274aa --- /dev/null +++ b/bin/publish-latest-branch-build.php @@ -0,0 +1,258 @@ +#!/usr/bin/env php + sha1sum.txt'); +chdir('..'); + +$apix = '`'; + +// add a comment to the build URL +if (!empty($phorge_document_id) && empty(getenv('NO_COMMENT'))) { + // We have a Differentials revision under review! + $phorge_document_id_escaped = escapeshellarg($phorge_document_id); + + $phorge_comment_header_lines = [ + "⚙ Dear Android hackers, please review this promising patch:", + "https://gitpull.it/$phorge_document_id_escaped", + "", + "Here the related fresh test builds:", + ]; + + // Build Phorge comment. + // There is a fixed header, and multiple lines about each APK. + $phorge_apk_lines = []; + foreach ($generated_apks as $generated_apk) { + foreach ($generated_apk->getPhorgeCommentLines() as $apk_line) { + $phorge_apk_lines[] = $apk_line; + } + } + $telegram_chat_content = implode("\n", array_merge([ + $phorge_comment_header_lines, + $phorge_apk_lines, + ])); + + // Send message to Telegram groups. + echo "DEBUG TELEGRAM CONTENT\n"; + echo "$telegram_chat_content\n"; + shell_exec(sprintf( + "~/bin/telegram-comment.sh %s %s %s", + escapeshellarg($TELEGRAM_BOT_TOKEN), + escapeshellarg($TELEGRAM_CHAT_ID), + escapeshellarg($telegram_chat_content) + )); + + // Send message to Phorge. + echo "Adding bipbop Comment to $phorge_document_id\n"; + shell_exec(sprintf( + "~/bin/phab-comment.php %s %s %s", + escapeshellarg($phorge_document_id), + escapeshellarg($apk_destination_url), + escapeshellarg($apk_sha256) + )); +} else { + // We have NOT a Differentials revision under review! + // So this is an atomic commit. + $git_commit_title_escaped = escapeshellarg($git_commit_title); + + $telegram_chat_content = implode("\n", [ + "🌚 New commit: *$git_commit_title_escaped*", + "https://gitpull.it/R4:$git_commit", + "", + "Try this fresh test APK:", + "[Download $apk_destination_name_escaped]($apk_destination_url_escaped)", + "", + "APK fingerprint sha256:", + "$apix$apk_sha256$apix" + ]); + + + echo "DEBUG TELEGRAM CONTENT\n"; + echo "$telegram_chat_content\n"; + shell_exec(sprintf( + "~/bin/telegram-comment.sh %s %s %s", + escapeshellarg($TELEGRAM_BOT_TOKEN), + escapeshellarg($TELEGRAM_CHAT_ID), + escapeshellarg($telegram_chat_content) + )); +} + +// delete very old files +print_log_info("Cleaning old artifacts"); +shell_exec(sprintf( + "find %s -type f -mtime +120 -delete", + escapeshellarg($EXPECTED_DEPLOY_PATH) +)); + +// clean old APK in the repository to never re-use it by mistake +// as it happened when we moved everything to the /app directory +unlink($apk_path); + +print_log_info("Done"); diff --git a/bin/publish-latest-branch-build.sh b/bin/publish-latest-branch-build.sh new file mode 100755 index 0000000..1a49b74 --- /dev/null +++ b/bin/publish-latest-branch-build.sh @@ -0,0 +1,43 @@ +#!/bin/bash -e +# The "-e" means to die in case of any errors. +################################################################ +# 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/source/libre-busto-ci-cd/ +################################################################ + +# +# Usage: the first argument is the Differential ID number +# or empty if working on master. +# + + +# Get this very specific directory of this script. +# Credits: https://stackoverflow.com/a/246128/3451846 +CWD=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + +# Allow to set a custom ENV_FILE=/something/else. +# As default, assume the '.env' file from the root directory of this repository. +if [ -z "$ENV_FILE" ]; then + ENV_FILE="$CWD"/../.env + echo "[WARN] You have not specified an ENV_FILE variable. Assuming the default $ENV_FILE" +fi + +# Require the Env file, exporting all its variables. +if [ -f "$ENV_FILE" ]; then + echo "[INFO] including env file: $ENV_FILE" + set -o allexport + . "$ENV_FILE" + set +o allexport +else + echo "[WARN] skipping non-existing $ENV_FILE file for user $(whoami) with home $HOME" +fi + +# We rewrote the script in PHP since it's more reliable asd. +# We pass all the arguments there. +php "$CWD"/publish-latest-branch-build.php "$@" diff --git a/include/APK.php b/include/APK.php new file mode 100644 index 0000000..9b153d8 --- /dev/null +++ b/include/APK.php @@ -0,0 +1,53 @@ +name = $name; + $this->url = $url; + $this->sha256 = $sha265; + } + + /** + * @return array + */ + public function getPhorgeCommentLines() { + return [ + $this->getPhorgeDownloadLink(), + '', // Empty newline between download link and signature + $this->getPhorgeAPKSignatureLine() + ]; + } + + /** + * Get a very generic download link for Phorge. + * @return string + */ + private function getPhorgeDownloadLink() { + return sprintf( + "[Download %s](%s)", + $this->name, + $this->url + ); + } + + /** + * Get a very generic line about the sha256sum of this APK. + * @return string + */ + private function getPhorgeAPKSignatureLine() { + return sprintf( + "APK sha256: `%s`", + $this->sha256 + ); + } +} + diff --git a/include/CommandResult.php b/include/CommandResult.php new file mode 100644 index 0000000..291ee8c --- /dev/null +++ b/include/CommandResult.php @@ -0,0 +1,21 @@ +exitStatus = $exitStatus; + $this->stdout = $stdout; + $this->stderr = $stderr; + } +} + diff --git a/include/autoload.php b/include/autoload.php new file mode 100644 index 0000000..e3214a0 --- /dev/null +++ b/include/autoload.php @@ -0,0 +1,12 @@ +stdout); +} + +/** + * Execute a shell command and get the first output line. + * @param string $command e.g. 'git' + * @param string $args Command line arguments not-escaped (we will escape them) e.g. ['status'] + * @return string First line of output from git status. + */ +function shell_line(string $command, array $args) { + $lines = shell_lines($command, $args); + foreach ($lines as $line) { + return trim($line); + } + return null; +} + +/** + * Show a stupidly formatted log line. + * @param string $msg + */ +function print_log($level, $msg) { + printf("[%s][%s] %s\n", + $level, + date('Y-m-d H:i:s'), + $msg + ); +} + +/** + * Show a stupidly formatted log INFO line. + * @param string $msg + */ +function print_log_info($msg) { + print_log('INFO', $msg); +} + +/** + * Show a stupidly formatted log ERROR line. + * @param string $msg + */ +function print_log_error($msg) { + print_log('ERROR', $msg); +}