diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index b4f595ac..fad71cba 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -56,7 +56,8 @@ jobs: run: go test -count=1 ./config/ ./config/node/ ./lib/tn/ ./lib/constructs/validator_set/ ./stacks/ # Tests the check that release.yaml and publish-node-image.yaml run on each - # release page. + # release page, and the Telegram announcement that release.yaml posts. The + # announcement test sends only to a local fake Telegram. release-upgrade-line: runs-on: ubuntu-latest if: ${{ !github.event.pull_request.draft }} @@ -64,6 +65,8 @@ jobs: - uses: actions/checkout@v4 - name: Test the upgrade line check run: scripts/release/test-check-upgrade-line.sh + - name: Test the Telegram announcement + run: scripts/release/test-announce-telegram.sh acceptance-test: runs-on: ubuntu-latest diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index e3434d51..0017029f 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -35,6 +35,26 @@ jobs: RELEASE_BODY: ${{ github.event.release.body }} run: printf '%s' "$RELEASE_BODY" | scripts/release/check-upgrade-line.sh + # The last step announces the release in Telegram only when the release + # had no assets before this run: on its publish, or on the edit that + # fixes a release whose first run failed, but not on later edits. This + # reads the release from the API, not from the event, so a run that + # waited behind an earlier one sees the binaries that run uploaded. A + # failed read leaves assets unset, so nothing is announced. + - name: Count the release's assets + id: before + if: github.event_name == 'release' + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + RELEASE_ID: ${{ github.event.release.id }} + run: | + if count=$(gh api "repos/$GITHUB_REPOSITORY/releases/$RELEASE_ID/assets" --jq length); then + echo "The release has $count asset(s)." + echo "assets=$count" >> "$GITHUB_OUTPUT" + else + echo "::warning::Could not read the release's assets, so this run does not announce the release in Telegram." + fi + - name: Set Version id: vars run: | @@ -94,6 +114,7 @@ jobs: rm -rf ./.build/kwild - name: Release + id: upload uses: softprops/action-gh-release@v2 if: startsWith(github.ref, 'refs/tags/') with: @@ -103,3 +124,22 @@ jobs: ./.build/tn_${{ env.VERSION }}_linux_amd64.tar.gz ./.build/tn_${{ env.VERSION }}_linux_arm64.tar.gz token: ${{ secrets.GITHUB_TOKEN }} + + # One message to the Telegram chat and topic that the node alerts use + # (docs/release-process.md). Without the secrets, as in a fork, the + # script skips with a notice. A failure here does not fail the release. + - name: Announce the release in Telegram + if: github.event_name == 'release' && steps.before.outputs.assets == '0' && steps.upload.outcome == 'success' + continue-on-error: true + env: + TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }} + TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }} + TELEGRAM_TOPIC_ID: ${{ secrets.TELEGRAM_TOPIC_ID }} + RELEASE_TAG: ${{ github.event.release.tag_name }} + RELEASE_URL: ${{ github.event.release.html_url }} + RELEASE_BODY: ${{ github.event.release.body }} + run: | + if ! scripts/release/announce-telegram.sh "$RELEASE_TAG" "$RELEASE_URL" <<<"$RELEASE_BODY"; then + echo "::warning::The release was not announced in Telegram. To post it by hand, see docs/release-process.md#the-telegram-announcement." + exit 1 + fi diff --git a/docs/release-process.md b/docs/release-process.md index 5ed3b31c..c3ebdfca 100644 --- a/docs/release-process.md +++ b/docs/release-process.md @@ -70,12 +70,30 @@ A required release must run every block before its block the same way as the rel Release notices go out on the [release page](https://github.com/trufnetwork/node/releases). Operators get them by choosing **Watch → Custom → Releases** on this repository, or from the feed at . +`release.yaml` also posts one message about each release to Telegram; see [The Telegram Announcement](#the-telegram-announcement). + +## The Telegram Announcement + +After `release.yaml` uploads the binaries, it posts one message to the Telegram chat and topic that the node alerts use. The repository secrets `TELEGRAM_BOT_TOKEN`, `TELEGRAM_CHAT_ID` and `TELEGRAM_TOPIC_ID` set the bot, the chat and the topic. The message is built from the upgrade line and the tag. For a required release, it names the networks, the block and its time on each, and the version that `kwild version` prints after the upgrade, and it links to the release page and to [Required In-Place Upgrade](node-upgrade-guide.md#required-in-place-upgrade). For an optional release, it says that the upgrade is optional and links to the release page. + +The message goes out once per release. `release.yaml` reads the release's assets before it builds, and posts only when the release had none and its upload succeeded. That is the run of the publish or, when that run failed, the run of the edit that fixes the release. Later edits find the binaries in place and post nothing, and a manual run of the workflow posts nothing. + +A failed post does not fail the release; the run shows a warning. To see the message, or to post it by hand, run the script with the tag, the release page URL and the notes: + +```bash +scripts/release/announce-telegram.sh --dry-run v2.6.9 https://github.com/trufnetwork/node/releases/tag/v2.6.9 < notes.md +``` + +Without `--dry-run`, it posts the message to the chat in `TELEGRAM_CHAT_ID`, in the topic in `TELEGRAM_TOPIC_ID` when that is set, with the bot in `TELEGRAM_BOT_TOKEN`. Without the token or the chat id, as in a fork, it posts nothing and exits without an error. It refuses notes that `check-upgrade-line.sh` refuses. + +`scripts/release/test-announce-telegram.sh` tests the messages and the post against a local fake Telegram. CI runs it. + ## Publishing a Release 1. Draft the release from its tag, generate the notes, and put the upgrade line first. 2. Check the notes with `scripts/release/check-upgrade-line.sh`. -3. Publish. `release.yaml` builds the four `kwild` tarballs and uploads them to the release. `publish-node-image.yaml` pushes the image as `v`, ``, `sha-` and `latest`. -4. If the line was missing or wrong, both workflows fail before they build. Edit the release to fix the line. `release.yaml` runs again on the edit and uploads the binaries. `publish-node-image.yaml` runs only when a release is published, so run **Publish Node Image** by hand on the release tag with **Also tag this build as latest**. A manual run has no release notes to read, so it skips the check. +3. Publish. `release.yaml` builds the four `kwild` tarballs, uploads them to the release and posts the Telegram announcement. `publish-node-image.yaml` pushes the image as `v`, ``, `sha-` and `latest`. +4. If the line was missing or wrong, both workflows fail before they build. Edit the release to fix the line. `release.yaml` runs again on the edit, uploads the binaries and posts the announcement. `publish-node-image.yaml` runs only when a release is published, so run **Publish Node Image** by hand on the release tag with **Also tag this build as latest**. A manual run has no release notes to read, so it skips the check. 5. After a required release, run **Build AMI** so that nodes launched from the AMI start on it. -Every edit of the release notes runs `release.yaml` again, which builds and uploads the binaries again. +Every edit of the release notes runs `release.yaml` again, which builds and uploads the binaries again. It does not post the announcement again. diff --git a/scripts/release/announce-telegram.sh b/scripts/release/announce-telegram.sh new file mode 100755 index 00000000..accfdebc --- /dev/null +++ b/scripts/release/announce-telegram.sh @@ -0,0 +1,147 @@ +#!/usr/bin/env bash +# Posts one message about a release to Telegram, to the chat and topic that the +# node alerts use. The message says whether operators must install the release, +# read from the upgrade line that opens the release body (docs/release-process.md). +# release.yaml runs it once per release, after it uploads the binaries. +# +# Usage: scripts/release/announce-telegram.sh [--dry-run] TAG RELEASE_URL < release-notes.md +# +# --dry-run print the message and send nothing +# +# Settings, from the environment: +# TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID where the message goes; without both, +# it prints a notice and sends nothing +# TELEGRAM_TOPIC_ID topic (thread) in a forum chat, optional +# TELEGRAM_API for tests: another Telegram server +# +# Exits 1 when the body does not open with the upgrade line or Telegram does not +# take the message, and 2 on wrong arguments. +set -euo pipefail + +guide_url=https://github.com/trufnetwork/node/blob/main/docs/node-upgrade-guide.md#required-in-place-upgrade + +usage() { + echo "usage: $0 [--dry-run] TAG RELEASE_URL < release-notes.md" >&2 + exit 2 +} + +dry_run=no +if [ "${1:-}" = --dry-run ]; then + dry_run=yes + shift +fi +[ $# -eq 2 ] && [ -n "$1" ] && [ -n "$2" ] || usage +tag=$1 release_url=$2 + +notice() { + if [ "${GITHUB_ACTIONS:-}" = true ]; then + echo "::notice::$1" >&2 + else + echo "notice: $1" >&2 + fi +} + +# Escapes the characters that Telegram's HTML parse mode reads as markup. +# The replacements are quoted so that bash 5.2 does not read & in them as the +# matched text. +html() { + local s=$1 + s=${s//'&'/'&'} + s=${s//'<'/'<'} + s=${s//'>'/'>'} + printf '%s' "$s" +} + +# Writes a block number with thousands separators: 3300000 -> 3,300,000. +commas() { + local n=$1 out='' + while [ ${#n} -gt 3 ]; do + out=,${n: -3}$out + n=${n:0:${#n}-3} + done + printf '%s' "$n$out" +} + +line='' +IFS= read -r line || true +line=${line%$'\r'} +# The same check that release.yaml runs before it builds; it reads only the +# first line. Its "Upgrade line:" output goes to stderr so that --dry-run +# prints the message alone. +printf '%s\n' "$line" | "$(dirname "$0")/check-upgrade-line.sh" >&2 + +declare -A block=() when=() +for net in mainnet testnet; do + re="required on $net from block ([0-9]+) \\(about ([^)]*)\\)" + if [[ $line =~ $re ]]; then + block[$net]=$(commas "${BASH_REMATCH[1]}") + when[$net]=$(html "${BASH_REMATCH[2]}") + fi +done + +name=$(html "$tag") +version=$(html "${tag#v}") +links="Release: $(html "$release_url")" +stops="A node still on an older release stops at that block. After upgrading, kwild version prints ${version}." + +if [ -n "${block[mainnet]:-}" ] && [ -n "${block[testnet]:-}" ]; then + text="REQUIRED: TN node ${name} + +Install it before: +- mainnet block ${block[mainnet]} (about ${when[mainnet]}) +- testnet block ${block[testnet]} (about ${when[testnet]}) + +${stops} + +${links} +How to upgrade: ${guide_url}" +elif [ -n "${block[mainnet]:-}" ] || [ -n "${block[testnet]:-}" ]; then + if [ -n "${block[mainnet]:-}" ]; then + net=mainnet other=testnet + else + net=testnet other=mainnet + fi + text="REQUIRED on ${net}: TN node ${name} + +Install it before ${net} block ${block[$net]} (about ${when[$net]}). On ${other} it is optional. + +${stops} + +${links} +How to upgrade: ${guide_url}" +else + text="TN node ${name} is out. Optional: nodes on older releases keep following the chain, so upgrade when it suits you. + +${links}" +fi + +if [ "$dry_run" = yes ]; then + printf '%s\n' "$text" + exit 0 +fi + +if [ -z "${TELEGRAM_BOT_TOKEN:-}" ] || [ -z "${TELEGRAM_CHAT_ID:-}" ]; then + notice "TELEGRAM_BOT_TOKEN or TELEGRAM_CHAT_ID is not set, so ${tag} is not announced in Telegram." + exit 0 +fi + +topic=() +if [ -n "${TELEGRAM_TOPIC_ID:-}" ]; then + topic=(--data-urlencode "message_thread_id=${TELEGRAM_TOPIC_ID}") +fi +answer=$(mktemp) +trap 'rm -f "$answer"' EXIT +# The URL holds the token, so it goes to curl through stdin, not the command line. +code=$(printf 'url = "%s/bot%s/sendMessage"\n' "${TELEGRAM_API:-https://api.telegram.org}" "$TELEGRAM_BOT_TOKEN" | + curl -sS -m 20 -o "$answer" -w '%{http_code}' -K - \ + --data-urlencode "chat_id=${TELEGRAM_CHAT_ID}" ${topic[@]+"${topic[@]}"} \ + --data-urlencode parse_mode=HTML \ + --data-urlencode disable_web_page_preview=true \ + --data-urlencode "text=${text}") || code=000 +if [ "$code" != 200 ]; then + # Telegram explains a refusal in "description", such as a wrong topic id. + why=$(sed -n 's/.*"description":"\([^"]*\)".*/\1/p' "$answer" | head -c 300) + echo "announce-telegram: Telegram answered HTTP ${code}${why:+: $why}" >&2 + exit 1 +fi +echo "Announced ${tag} in Telegram." diff --git a/scripts/release/test-announce-telegram.sh b/scripts/release/test-announce-telegram.sh new file mode 100755 index 00000000..ae588467 --- /dev/null +++ b/scripts/release/test-announce-telegram.sh @@ -0,0 +1,313 @@ +#!/usr/bin/env bash +# Tests scripts/release/announce-telegram.sh: the message for each form of the +# upgrade line, block separators, HTML escaping, refused bodies and arguments, +# a missing token or chat id, and sends to a local fake Telegram that records +# each request. A curl wrapper records curl's command line, which must never +# hold the token. Needs bash, python3 and curl. +# +# Usage: scripts/release/test-announce-telegram.sh +set -euo pipefail + +dir=$(cd "$(dirname "$0")" && pwd) +announce=$dir/announce-telegram.sh +guide=$dir/../../docs/node-upgrade-guide.md +tmp=$(mktemp -d) +server="" +trap '[ -z "$server" ] || kill "$server" 2>/dev/null || true; rm -rf "$tmp"' EXIT +mkdir -p "$tmp/bin" "$tmp/req" + +# Every run below goes to the fake server, never to Telegram. +unset TELEGRAM_BOT_TOKEN TELEGRAM_CHAT_ID TELEGRAM_TOPIC_ID TELEGRAM_API GITHUB_ACTIONS + +# The fake Telegram: each POST becomes $tmp/req//, with the request path in +# "path", the sorted field names in "fields" and each field's value in a file of +# its name. With $tmp/telegram-fail present it refuses the message as Telegram +# does. +exec 3< <(exec python3 -I - "$tmp" <<'EOF' +import http.server, os, sys, urllib.parse +d = sys.argv[1] +class Fake(http.server.BaseHTTPRequestHandler): + def log_message(self, *args): + pass + def reply(self, code, body): + self.send_response(code) + self.send_header('content-type', 'application/json') + self.send_header('content-length', str(len(body))) + self.end_headers() + self.wfile.write(body) + def do_POST(self): + body = self.rfile.read(int(self.headers.get('content-length', 0))).decode() + fields = urllib.parse.parse_qs(body, keep_blank_values=True) + req = os.path.join(d, 'req', str(len(os.listdir(os.path.join(d, 'req'))) + 1)) + os.mkdir(req) + with open(os.path.join(req, 'path'), 'w') as f: + f.write(self.path) + with open(os.path.join(req, 'fields'), 'w') as f: + f.write(' '.join(sorted(fields))) + for name, values in fields.items(): + if name.isidentifier(): + with open(os.path.join(req, name), 'wb') as f: + f.write(values[0].encode()) + if os.path.exists(os.path.join(d, 'telegram-fail')): + return self.reply(400, b'{"ok":false,"error_code":400,"description":"Bad Request: message thread not found"}') + self.reply(200, b'{"ok":true,"result":{"message_id":7}}') +server = http.server.HTTPServer(('127.0.0.1', 0), Fake) +print(server.server_address[1], flush=True) +server.serve_forever() +EOF +) +server=$! +read -r port <&3 +api=http://127.0.0.1:$port + +real_curl=$(command -v curl) +cat >"$tmp/bin/curl" <>"$tmp/argv" +exec "$real_curl" "\$@" +EOF +chmod +x "$tmp/bin/curl" + +token=123456:fake-token-for-tests +chat=-1001234 +url=https://github.com/trufnetwork/node/releases/tag/v2.6.9 +notes=$'\n## What\'s Changed\n* chore: an example change by @someone in https://github.com/trufnetwork/node/pull/1\n' +both='Upgrade: required on mainnet from block 3300000 (about 2026-12-10 12:00 UTC), required on testnet from block 16300000 (about 2026-12-03 12:00 UTC).' +mainnet='Upgrade: required on mainnet from block 3300000 (about 2026-12-10 12:00 UTC), optional on testnet.' +testnet='Upgrade: optional on mainnet, required on testnet from block 16300000 (about 2026-12-03 12:00 UTC).' + +want_both=$(cat <<'EOF' +REQUIRED: TN node v2.6.9 + +Install it before: +- mainnet block 3,300,000 (about 2026-12-10 12:00 UTC) +- testnet block 16,300,000 (about 2026-12-03 12:00 UTC) + +A node still on an older release stops at that block. After upgrading, kwild version prints 2.6.9. + +Release: https://github.com/trufnetwork/node/releases/tag/v2.6.9 +How to upgrade: https://github.com/trufnetwork/node/blob/main/docs/node-upgrade-guide.md#required-in-place-upgrade +EOF +) +want_mainnet=$(cat <<'EOF' +REQUIRED on mainnet: TN node v2.6.9 + +Install it before mainnet block 3,300,000 (about 2026-12-10 12:00 UTC). On testnet it is optional. + +A node still on an older release stops at that block. After upgrading, kwild version prints 2.6.9. + +Release: https://github.com/trufnetwork/node/releases/tag/v2.6.9 +How to upgrade: https://github.com/trufnetwork/node/blob/main/docs/node-upgrade-guide.md#required-in-place-upgrade +EOF +) +want_testnet=$(cat <<'EOF' +REQUIRED on testnet: TN node v2.6.9 + +Install it before testnet block 16,300,000 (about 2026-12-03 12:00 UTC). On mainnet it is optional. + +A node still on an older release stops at that block. After upgrading, kwild version prints 2.6.9. + +Release: https://github.com/trufnetwork/node/releases/tag/v2.6.9 +How to upgrade: https://github.com/trufnetwork/node/blob/main/docs/node-upgrade-guide.md#required-in-place-upgrade +EOF +) +want_optional=$(cat <<'EOF' +TN node v2.6.9 is out. Optional: nodes on older releases keep following the chain, so upgrade when it suits you. + +Release: https://github.com/trufnetwork/node/releases/tag/v2.6.9 +EOF +) + +fails=0 +settings=() +pass() { echo "ok $1"; } +fail() { + echo "FAIL $1" + fails=$((fails + 1)) +} + +# is NAME GOT WANT +is() { + if [ "$2" = "$3" ]; then + pass "$1" + else + fail "$1" + diff <(printf '%s\n' "$3") <(printf '%s\n' "$2") | sed 's/^/ /' || true + fi +} + +# has NAME TEXT PART +has() { + if [[ $2 == *"$3"* ]]; then + pass "$1" + else + fail "$1: no '$3' in: $2" + fi +} + +# run BODY ARG...: runs the script with BODY on stdin, the settings in +# $settings and the fake Telegram; sets status, out and err. Clears the requests +# of the run before. +run() { + rm -rf "${tmp:?}/req/"* + printf '%s' "$1" >"$tmp/body" + shift + status=0 + env TELEGRAM_API="$api" PATH="$tmp/bin:$PATH" ${settings[@]+"${settings[@]}"} \ + "$announce" "$@" <"$tmp/body" >"$tmp/out" 2>"$tmp/err" || status=$? + out=$(<"$tmp/out") + err=$(<"$tmp/err") + cat "$tmp/out" "$tmp/err" >>"$tmp/all-output" +} + +sent() { ls "$tmp/req"; } + +# message NAME WANT BODY: the dry run prints WANT and sends nothing. +message() { + settings=(TELEGRAM_BOT_TOKEN="$token" TELEGRAM_CHAT_ID="$chat") + run "$3" --dry-run v2.6.9 "$url" + is "$1: exit" "$status" 0 + is "$1: message" "$out" "$2" + is "$1: nothing sent" "$(sent)" "" +} + +message 'required on both networks' "$want_both" "$both$notes" +message 'required on mainnet only' "$want_mainnet" "$mainnet$notes" +message 'required on testnet only' "$want_testnet" "$testnet$notes" +message 'optional' "$want_optional" "Upgrade: optional.$notes" +message 'optional, line alone' "$want_optional" 'Upgrade: optional.' +message 'required on both, CRLF line ends' "$want_both" "$both"$'\r\n## What\'s Changed\r\n' + +# Thousands separators, past what bash arithmetic holds. +settings=() +for pair in 1=1 999=999 1000=1,000 12345=12,345 999999=999,999 1000000=1,000,000 \ + 3300000=3,300,000 16300000=16,300,000 12345678901234567890=12,345,678,901,234,567,890; do + run "Upgrade: required on mainnet from block ${pair%%=*} (about 2026-12-10 12:00 UTC), optional on testnet." --dry-run v2.6.9 "$url" + has "block ${pair%%=*} prints as ${pair#*=}" "$out" "Install it before mainnet block ${pair#*=} (about 2026-12-10 12:00 UTC)." +done +run "Upgrade: optional on mainnet, required on testnet from block 1000 (about 2026-12-03 12:00 UTC)." --dry-run v2.6.9 "$url" +has 'testnet block 1000 prints as 1,000' "$out" "Install it before testnet block 1,000 (about 2026-12-03 12:00 UTC)." + +# The tag and the release URL are escaped for Telegram's HTML parse mode. +run "$mainnet" --dry-run 'v2.6.9&x' 'https://example.test/r?a=1&b=<2>' +has 'tag escaped in the title' "$out" 'REQUIRED on mainnet: TN node v2.6.9<b>&x' +has 'version escaped' "$out" 'kwild version prints 2.6.9<b>&x.' +has 'release URL escaped' "$out" 'Release: https://example.test/r?a=1&b=<2>' +run 'Upgrade: optional.' --dry-run 'v1&2' "$url" +has 'tag escaped in the optional form' "$out" 'TN node v1&2 is out.' +run 'Upgrade: optional.' --dry-run 2.6.9 "$url" +has 'a tag without v' "$out" 'TN node 2.6.9 is out.' + +# The upgrade link points at a heading that exists in the guide, as GitHub +# names its anchors. +anchor=${want_both##*#} +anchors=$(sed -n 's/^#\{1,6\} //p' "$guide" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9 _-]//g; s/ /-/g') +if grep -qxF -- "$anchor" <<<"$anchors"; then + pass "the guide has the heading #$anchor" +else + fail "no heading in docs/node-upgrade-guide.md has the anchor #$anchor" +fi + +# A body that check-upgrade-line.sh refuses is refused too, and nothing is sent. +settings=(TELEGRAM_BOT_TOKEN="$token" TELEGRAM_CHAT_ID="$chat") +refused() { + run "$2" v2.6.9 "$url" + is "refused, $1: exit" "$status" 1 + has "refused, $1: says why" "$err" 'must open with the upgrade line' + is "refused, $1: nothing sent" "$(sent)" "" +} +refused 'empty body' '' +refused 'generated notes only' "${notes#$'\n'}" +refused 'line in the second line' $'## Notes\nUpgrade: optional.' +refused 'required without blocks' 'Upgrade: required.' +refused 'block with separators' 'Upgrade: required on mainnet from block 3,300,000 (about 2026-12-10 12:00 UTC), optional on testnet.' +refused 'bold' '**Upgrade: optional.**' +run '' --dry-run v2.6.9 "$url" +is 'refused in a dry run too' "$status" 1 +is 'refused in a dry run: no message' "$out" "" + +# Wrong arguments. +for args in '' 'v2.6.9' '--dry-run v2.6.9' 'v2.6.9 url extra'; do + read -r -a argv <<<"$args" + run 'Upgrade: optional.' ${argv[@]+"${argv[@]}"} + is "arguments '$args': exit" "$status" 2 + has "arguments '$args': usage" "$err" 'usage:' +done +run 'Upgrade: optional.' '' "$url" +is 'empty tag: exit' "$status" 2 + +# Without a token or a chat id it skips with a notice, and the release goes on. +for missing in TELEGRAM_BOT_TOKEN TELEGRAM_CHAT_ID; do + settings=(TELEGRAM_BOT_TOKEN="$token" TELEGRAM_CHAT_ID="$chat" TELEGRAM_TOPIC_ID=42 "$missing=") + run "$both" v2.6.9 "$url" + is "no $missing: exit" "$status" 0 + has "no $missing: notice" "$err" 'notice: TELEGRAM_BOT_TOKEN or TELEGRAM_CHAT_ID is not set, so v2.6.9 is not announced in Telegram.' + is "no $missing: nothing sent" "$(sent)" "" +done +settings=(GITHUB_ACTIONS=true) +run "$both" v2.6.9 "$url" +is 'no settings in Actions: exit' "$status" 0 +has 'no settings in Actions: notice annotation' "$err" '::notice::TELEGRAM_BOT_TOKEN or TELEGRAM_CHAT_ID is not set' +is 'no settings in Actions: nothing sent' "$(sent)" "" + +# A real send to the fake Telegram. +# sends NAME BODY WANT FIELDS [TOPIC] +sends() { + local req=$tmp/req/1 + run "$2" v2.6.9 "$url" + is "$1: exit" "$status" 0 + is "$1: output" "$out" 'Announced v2.6.9 in Telegram.' + is "$1: one request" "$(sent)" 1 + is "$1: path" "$(cat "$req/path")" "/bot$token/sendMessage" + is "$1: fields" "$(cat "$req/fields")" "$4" + is "$1: chat_id" "$(cat "$req/chat_id")" "$chat" + is "$1: parse_mode" "$(cat "$req/parse_mode")" HTML + is "$1: disable_web_page_preview" "$(cat "$req/disable_web_page_preview")" true + is "$1: text" "$(cat "$req/text"; echo .)" "$3." + if [ -n "${5:-}" ]; then + is "$1: message_thread_id" "$(cat "$req/message_thread_id")" "$5" + fi +} +settings=(TELEGRAM_BOT_TOKEN="$token" TELEGRAM_CHAT_ID="$chat" TELEGRAM_TOPIC_ID=42) +sends 'send to a topic' "$both$notes" "$want_both" 'chat_id disable_web_page_preview message_thread_id parse_mode text' 42 +sends 'send, optional' "Upgrade: optional.$notes" "$want_optional" 'chat_id disable_web_page_preview message_thread_id parse_mode text' 42 +settings=(TELEGRAM_BOT_TOKEN="$token" TELEGRAM_CHAT_ID="$chat") +sends 'send without a topic' "$mainnet$notes" "$want_mainnet" 'chat_id disable_web_page_preview parse_mode text' +settings=(TELEGRAM_BOT_TOKEN="$token" TELEGRAM_CHAT_ID="$chat" TELEGRAM_TOPIC_ID=) +sends 'send with an empty topic' "$testnet$notes" "$want_testnet" 'chat_id disable_web_page_preview parse_mode text' + +# Telegram refuses the message, or does not answer. +settings=(TELEGRAM_BOT_TOKEN="$token" TELEGRAM_CHAT_ID="$chat" TELEGRAM_TOPIC_ID=999) +touch "$tmp/telegram-fail" +run "$both" v2.6.9 "$url" +rm -f "$tmp/telegram-fail" +is 'Telegram refuses: exit' "$status" 1 +has 'Telegram refuses: says why' "$err" 'Telegram answered HTTP 400: Bad Request: message thread not found' +settings=(TELEGRAM_BOT_TOKEN="$token" TELEGRAM_CHAT_ID="$chat" TELEGRAM_API=http://127.0.0.1:1) +run "$both" v2.6.9 "$url" +is 'Telegram unreachable: exit' "$status" 1 +has 'Telegram unreachable: says so' "$err" 'Telegram answered HTTP 000' + +# The token went to curl through stdin only, and no output printed it. +if [ -s "$tmp/argv" ] && grep -q -- '--data-urlencode' "$tmp/argv"; then + pass 'curl ran through the wrapper' +else + fail 'curl did not run through the wrapper' +fi +if grep -qF -- "$token" "$tmp/argv"; then + fail "the token is on curl's command line" +else + pass "the token is not on curl's command line" +fi +if grep -qF -- "$token" "$tmp/all-output"; then + fail 'the token is in the output' +else + pass 'the token is not in the output' +fi + +if [ "$fails" -ne 0 ]; then + echo "$fails check(s) failed" + exit 1 +fi +echo "all checks passed"