mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-11-26 03:37:14 +01:00
Compare commits
5 Commits
b7667b741c
...
0af8daef03
| Author | SHA1 | Date | |
|---|---|---|---|
| 0af8daef03 | |||
| a078383b60 | |||
| 31ef21c763 | |||
| 4e6822d714 | |||
| 251bba887e |
@ -32,6 +32,15 @@ releases is free. The heading *name* is checked rather than trusted —
|
||||
and the announcement reads `popes.json` regardless, so a typo here fails the
|
||||
suite instead of reaching a group.
|
||||
|
||||
**Preview the announcement before you bump.** Because a bump is a publish,
|
||||
the announcement was the one feature that could never be exercised first.
|
||||
`tool/preview_release.py testbot` posts exactly what the next release would
|
||||
say, into one test group, touching neither `RELEASE_ORDINAL` nor any
|
||||
`announced_version` row; `--print-only` renders it without sending. It is
|
||||
fail-closed on `SELFTEST_GROUPS`, so it cannot post a release that does not
|
||||
exist into the group that would believe it. Write the `##` section below
|
||||
first -- the preview reads it, so what you see is what ships.
|
||||
|
||||
**A release announcement is a photo now.** It carries the pope's portrait, his
|
||||
Latin name and his pl.wikipedia article, the same three things `!papież` posts.
|
||||
Only for the pope in the header: a backlog announcement lists up to ten
|
||||
@ -62,6 +71,14 @@ this repo: describe what changes for them. 0.0.1 shipped a bullet about
|
||||
`/healthz`, an endpoint bound to 127.0.0.1 that nobody in a Signal group can
|
||||
reach, and it was rightly called out.
|
||||
|
||||
## 0.1.5 — Victor I
|
||||
|
||||
- ogłoszenie nowej wersji przychodzi teraz ze zdjęciem papieża, po którym ta
|
||||
wersja się nazywa, i z linkiem do jego strony na polskiej Wikipedii —
|
||||
dokładnie tak, jak robi to `!papież`
|
||||
- wersje nazywają się od teraz po łacinie: ta jest «Victor I», a nie
|
||||
«Wiktor I», a poprzednia była «Eleutherius», nie «Eleuteriusz»
|
||||
|
||||
## 0.1.4 — Eleutherius
|
||||
|
||||
- `!pomoc` (albo `!help`) wypisuje wszystkie komendy i mówi jednym zdaniem,
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
[project]
|
||||
name = "signal-bot"
|
||||
version = "0.1.4"
|
||||
version = "0.1.5"
|
||||
description = "A Signal group bot: image commands and daily message tallies."
|
||||
readme = "README.md"
|
||||
license = "MIT"
|
||||
|
||||
@ -100,13 +100,17 @@ async def announce_to(
|
||||
text = render(pope, pending)
|
||||
try:
|
||||
image = await portraits.get_base64(pope)
|
||||
await transport.send_image(image, group_id, caption=text)
|
||||
except Exception:
|
||||
# The marker is already written, so this must never propagate: a
|
||||
# Commons outage costs the portrait, not the release notes.
|
||||
logger.exception("portrait for %s unavailable; announcing %s as text", version, group_id)
|
||||
# Both the download and the send are inside this: the marker is
|
||||
# already written, so either failing would otherwise cost the group
|
||||
# its release notes permanently. The attachment turns a 2 KB text
|
||||
# post into a megabyte of base64, which signal-cli can refuse on its
|
||||
# own terms while it is perfectly able to take the text -- and a
|
||||
# duplicate post, if the send failed after delivery, is far cheaper
|
||||
# than release notes nobody ever sees.
|
||||
logger.exception("photo announcement of %s failed for %s; sending text", version, group_id)
|
||||
await transport.send_text(text, group_id)
|
||||
return
|
||||
await transport.send_image(image, group_id, caption=text)
|
||||
|
||||
|
||||
async def announce_all(
|
||||
|
||||
@ -30,7 +30,7 @@ PATCHES_PER_MINOR: Final = 8
|
||||
|
||||
# Which release this build is. Bump by one per release; everything else,
|
||||
# including the version string in pyproject.toml, follows from it.
|
||||
RELEASE_ORDINAL: Final = 13
|
||||
RELEASE_ORDINAL: Final = 14
|
||||
|
||||
_POPES_FILE: Final = Path(__file__).with_name("popes.json")
|
||||
|
||||
|
||||
@ -8,7 +8,7 @@ from typing import TYPE_CHECKING
|
||||
|
||||
import httpx
|
||||
|
||||
from signal_bot.announce import ANNOUNCED_KEY, announce_all, announce_to, render
|
||||
from signal_bot.announce import ANNOUNCED_KEY, announce_all, announce_to
|
||||
from signal_bot.changelog import parse
|
||||
from signal_bot.http_client import ImageFetcher
|
||||
from signal_bot.portraits import PortraitCache
|
||||
@ -30,7 +30,6 @@ DOCUMENT = """\
|
||||
- pierwsza zmiana
|
||||
"""
|
||||
RELEASES = parse(DOCUMENT)
|
||||
PETRUS = pope_for(1)
|
||||
LINUS = pope_for(2)
|
||||
|
||||
|
||||
@ -69,63 +68,6 @@ async def _announce(
|
||||
await announce_to(transport, store, GROUP, RELEASES, pope, cache)
|
||||
|
||||
|
||||
def test_a_single_release_is_rendered_flat() -> None:
|
||||
"""The header already names it; a per-release block would just repeat it."""
|
||||
message = render(LINUS, RELEASES[1:])
|
||||
assert message == (
|
||||
"Wersja 0.0.1 «Linus» wydana\n"
|
||||
"https://pl.wikipedia.org/wiki/Linus_%28papie%C5%BC%29\n"
|
||||
"Changelog:\n- druga zmiana"
|
||||
)
|
||||
|
||||
|
||||
def test_a_long_backlog_is_capped_and_says_so() -> None:
|
||||
"""A group added years from now must not be handed 200 releases."""
|
||||
document = "\n\n".join(
|
||||
f"## {v} — Pope{i}\n\n- zmiana {i}"
|
||||
for i, v in enumerate(
|
||||
[
|
||||
"0.0.0",
|
||||
"0.0.1",
|
||||
"0.0.2",
|
||||
"0.0.3",
|
||||
"0.0.4",
|
||||
"0.0.5",
|
||||
"0.0.6",
|
||||
"0.0.7",
|
||||
"0.1.0",
|
||||
"0.1.1",
|
||||
"0.1.2",
|
||||
"0.1.3",
|
||||
]
|
||||
)
|
||||
)
|
||||
message = render(pope_for(12), parse(document))
|
||||
assert "(pominieto 2 starszych wersji)" in message
|
||||
assert "zmiana 0" not in message
|
||||
assert "zmiana 11" in message
|
||||
|
||||
|
||||
def test_a_block_heading_ignores_the_name_typed_in_the_document() -> None:
|
||||
"""The heading says Pope3; popes.json is what the group is actually told."""
|
||||
message = render(pope_for(12), parse("## 0.0.2 — Pope3\n\n- x\n\n## 0.0.3 — Pope4\n\n- y"))
|
||||
assert "0.0.2 «Anacletus»" in message
|
||||
assert "0.0.3 «Clemens I»" in message
|
||||
assert "Pope3" not in message
|
||||
|
||||
|
||||
def test_render_names_the_version_and_lists_every_pending_line() -> None:
|
||||
message = render(LINUS, RELEASES)
|
||||
assert message.startswith("Wersja 0.0.1 «Linus» wydana")
|
||||
assert "- pierwsza zmiana" in message
|
||||
assert "- druga zmiana" in message
|
||||
|
||||
|
||||
def test_render_links_the_polish_article() -> None:
|
||||
"""The same article !papież posts, built from the disambiguated title."""
|
||||
assert "https://pl.wikipedia.org/wiki/Piotr_Aposto%C5%82" in render(PETRUS, RELEASES)
|
||||
|
||||
|
||||
async def test_a_group_told_nothing_yet_hears_the_ladder_from_peter(
|
||||
store: Store, transport: FakeTransport
|
||||
) -> None:
|
||||
@ -165,6 +107,29 @@ async def test_an_unreachable_portrait_costs_the_photo_not_the_announcement(
|
||||
assert store.meta(GROUP, ANNOUNCED_KEY) == "0.0.1"
|
||||
|
||||
|
||||
async def test_a_refused_attachment_still_lands_the_notes_as_text(
|
||||
store: Store, transport: FakeTransport
|
||||
) -> None:
|
||||
"""A megabyte of base64 is refusable in ways a 2 KB text post is not."""
|
||||
|
||||
class RefusingImages(FakeTransport):
|
||||
async def send_image(
|
||||
self,
|
||||
image_base64: str,
|
||||
recipient: str,
|
||||
quote: Quote | None = None,
|
||||
caption: str = "",
|
||||
) -> None:
|
||||
raise RuntimeError(recipient)
|
||||
|
||||
refusing = RefusingImages()
|
||||
await _announce(store, refusing)
|
||||
assert len(refusing.sent) == 1
|
||||
assert refusing.sent[0].image_base64 is None
|
||||
assert "Wersja 0.0.1 «Linus» wydana" in (refusing.sent[0].text or "")
|
||||
assert store.meta(GROUP, ANNOUNCED_KEY) == "0.0.1"
|
||||
|
||||
|
||||
async def test_a_group_already_on_this_version_hears_nothing(
|
||||
store: Store, transport: FakeTransport
|
||||
) -> None:
|
||||
@ -199,6 +164,9 @@ async def test_the_version_is_recorded_before_the_send(store: Store) -> None:
|
||||
"""A crash between the two must not re-announce on every restart."""
|
||||
|
||||
class Exploding(FakeTransport):
|
||||
async def send_text(self, text: str, recipient: str, quote: Quote | None = None) -> None:
|
||||
raise RuntimeError(recipient, text)
|
||||
|
||||
async def send_image(
|
||||
self,
|
||||
image_base64: str,
|
||||
@ -217,6 +185,11 @@ async def test_one_failing_group_does_not_silence_the_others(store: Store) -> No
|
||||
other = "group.two"
|
||||
|
||||
class RefusingFirst(FakeTransport):
|
||||
async def send_text(self, text: str, recipient: str, quote: Quote | None = None) -> None:
|
||||
if recipient == GROUP:
|
||||
raise RuntimeError(recipient)
|
||||
await super().send_text(text, recipient, quote)
|
||||
|
||||
async def send_image(
|
||||
self,
|
||||
image_base64: str,
|
||||
|
||||
78
tests/test_announce_render.py
Normal file
78
tests/test_announce_render.py
Normal file
@ -0,0 +1,78 @@
|
||||
# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
|
||||
"""What a release announcement says: the Latin name, the article, the ladder."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from signal_bot.announce import render
|
||||
from signal_bot.changelog import parse
|
||||
from signal_bot.versioning import pope_for
|
||||
|
||||
DOCUMENT = """\
|
||||
## 0.0.1 — Linus
|
||||
|
||||
- druga zmiana
|
||||
|
||||
## 0.0.0 — Petrus
|
||||
|
||||
- pierwsza zmiana
|
||||
"""
|
||||
RELEASES = parse(DOCUMENT)
|
||||
PETRUS = pope_for(1)
|
||||
LINUS = pope_for(2)
|
||||
|
||||
|
||||
def test_a_single_release_is_rendered_flat() -> None:
|
||||
"""The header already names it; a per-release block would just repeat it."""
|
||||
message = render(LINUS, RELEASES[1:])
|
||||
assert message == (
|
||||
"Wersja 0.0.1 «Linus» wydana\n"
|
||||
"https://pl.wikipedia.org/wiki/Linus_%28papie%C5%BC%29\n"
|
||||
"Changelog:\n- druga zmiana"
|
||||
)
|
||||
|
||||
|
||||
def test_a_long_backlog_is_capped_and_says_so() -> None:
|
||||
"""A group added years from now must not be handed 200 releases."""
|
||||
document = "\n\n".join(
|
||||
f"## {v} — Pope{i}\n\n- zmiana {i}"
|
||||
for i, v in enumerate(
|
||||
[
|
||||
"0.0.0",
|
||||
"0.0.1",
|
||||
"0.0.2",
|
||||
"0.0.3",
|
||||
"0.0.4",
|
||||
"0.0.5",
|
||||
"0.0.6",
|
||||
"0.0.7",
|
||||
"0.1.0",
|
||||
"0.1.1",
|
||||
"0.1.2",
|
||||
"0.1.3",
|
||||
]
|
||||
)
|
||||
)
|
||||
message = render(pope_for(12), parse(document))
|
||||
assert "(pominieto 2 starszych wersji)" in message
|
||||
assert "zmiana 0" not in message
|
||||
assert "zmiana 11" in message
|
||||
|
||||
|
||||
def test_a_block_heading_ignores_the_name_typed_in_the_document() -> None:
|
||||
"""The heading says Pope3; popes.json is what the group is actually told."""
|
||||
message = render(pope_for(12), parse("## 0.0.2 — Pope3\n\n- x\n\n## 0.0.3 — Pope4\n\n- y"))
|
||||
assert "0.0.2 «Anacletus»" in message
|
||||
assert "0.0.3 «Clemens I»" in message
|
||||
assert "Pope3" not in message
|
||||
|
||||
|
||||
def test_render_names_the_version_and_lists_every_pending_line() -> None:
|
||||
message = render(LINUS, RELEASES)
|
||||
assert message.startswith("Wersja 0.0.1 «Linus» wydana")
|
||||
assert "- pierwsza zmiana" in message
|
||||
assert "- druga zmiana" in message
|
||||
|
||||
|
||||
def test_render_links_the_polish_article() -> None:
|
||||
"""The same article !papież posts, built from the disambiguated title."""
|
||||
assert "https://pl.wikipedia.org/wiki/Piotr_Aposto%C5%82" in render(PETRUS, RELEASES)
|
||||
155
tool/preview_release.py
Executable file
155
tool/preview_release.py
Executable file
@ -0,0 +1,155 @@
|
||||
#!/usr/bin/env python3
|
||||
# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
|
||||
"""Post the *next* release's announcement into a test group, without releasing.
|
||||
|
||||
DOCS-changelog.md says a version bump is a publish, not a checkpoint: bumping
|
||||
RELEASE_ORDINAL and restarting posts into every watched group at once,
|
||||
including the real one. That leaves the announcement itself as the one feature
|
||||
which could never be exercised before it shipped -- and it is now the feature
|
||||
carrying a portrait, a Latin name and a link, so "it renders fine" is no longer
|
||||
a safe assumption.
|
||||
|
||||
This sends exactly the bytes the real announcement would send, to one named
|
||||
test group, and touches nothing else: RELEASE_ORDINAL is not bumped, no
|
||||
group_meta row is written or read, and every other group hears nothing. Run it,
|
||||
look at Signal, then bump for real.
|
||||
|
||||
tool/preview_release.py testbot
|
||||
tool/preview_release.py testbot --ordinal 20 --print-only
|
||||
|
||||
Fail-closed the same way scripts/selftest.sh is: the group must be named in
|
||||
SELFTEST_GROUPS, so this cannot post a release that does not exist into the
|
||||
group that would believe it.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
from typing import Final
|
||||
|
||||
import httpx
|
||||
|
||||
from signal_bot.announce import render
|
||||
from signal_bot.app import HTTP_TIMEOUT_SECONDS, USER_AGENT
|
||||
from signal_bot.changelog import load, since
|
||||
from signal_bot.config_env import load_config
|
||||
from signal_bot.http_client import ImageFetcher
|
||||
from signal_bot.portraits import PortraitCache
|
||||
from signal_bot.selftest import allowed_groups
|
||||
from signal_bot.transport_rest import RestTransport
|
||||
from signal_bot.versioning import (
|
||||
POPES,
|
||||
RELEASE_ORDINAL,
|
||||
__version__,
|
||||
latin_name,
|
||||
pope_for,
|
||||
version_for,
|
||||
)
|
||||
|
||||
EXIT_REFUSED: Final = 2
|
||||
|
||||
|
||||
class PreviewError(RuntimeError):
|
||||
"""Raised when the preview cannot be sent as asked."""
|
||||
|
||||
|
||||
async def group_id_for(client: httpx.AsyncClient, groups_url: str, name: str) -> str:
|
||||
"""Return the `group.<base64>=` id the transport addresses a group by.
|
||||
|
||||
Not internal_id, which is what selftest.resolve_group wants: that one goes
|
||||
into an inbound envelope, this one goes into an outbound send.
|
||||
"""
|
||||
response = await client.get(groups_url)
|
||||
response.raise_for_status()
|
||||
for entry in response.json():
|
||||
if entry.get("name") == name:
|
||||
group_id = str(entry.get("id") or "")
|
||||
if not group_id:
|
||||
message = f"group {name!r} has no addressable id"
|
||||
raise PreviewError(message)
|
||||
return group_id
|
||||
message = f"no group named {name!r}"
|
||||
raise PreviewError(message)
|
||||
|
||||
|
||||
def announcement_for(ordinal: int) -> str:
|
||||
"""Return the message a release at ``ordinal`` would post, for a group here.
|
||||
|
||||
Keyed off the running version, because that is what every group is
|
||||
recorded at, so this is the delta they would actually be shown.
|
||||
"""
|
||||
if not 1 <= ordinal <= len(POPES):
|
||||
message = f"no pope with ordinal {ordinal}; the list has {len(POPES)}"
|
||||
raise PreviewError(message)
|
||||
pending = since(load(), __version__)
|
||||
if not pending:
|
||||
message = (
|
||||
f"DOCS-changelog.md has nothing newer than {__version__}; "
|
||||
f"add the ## {version_for(ordinal)} section first"
|
||||
)
|
||||
raise PreviewError(message)
|
||||
return render(pope_for(ordinal), pending)
|
||||
|
||||
|
||||
async def send_preview(group: str, ordinal: int, *, print_only: bool) -> None:
|
||||
"""Render the announcement and, unless asked not to, post it."""
|
||||
permitted = allowed_groups(os.environ)
|
||||
if group not in permitted:
|
||||
listed = ", ".join(sorted(permitted)) or "(none)"
|
||||
message = f"{group!r} is not in SELFTEST_GROUPS; allowed: {listed}"
|
||||
raise PreviewError(message)
|
||||
|
||||
pope = pope_for(ordinal)
|
||||
text = announcement_for(ordinal)
|
||||
sys.stdout.write(f"== {version_for(ordinal)} «{latin_name(pope)}» -> {group}\n")
|
||||
sys.stdout.write(f"== caption: {len(text)} chars, {len(text.encode())} bytes\n")
|
||||
sys.stdout.write(f"{text}\n")
|
||||
if print_only:
|
||||
sys.stdout.write("== not sent (--print-only)\n")
|
||||
return
|
||||
sys.stdout.flush()
|
||||
|
||||
config = load_config()
|
||||
# The app's client, headers and all: Wikimedia answers 403 to httpx's
|
||||
# default User-Agent, so a bare client here would fail exactly where the
|
||||
# bot succeeds -- which is the opposite of what a preview is for.
|
||||
async with httpx.AsyncClient(
|
||||
timeout=HTTP_TIMEOUT_SECONDS,
|
||||
headers={"User-Agent": USER_AGENT},
|
||||
) as client:
|
||||
group_id = await group_id_for(client, config.groups_url, group)
|
||||
portraits = PortraitCache(ImageFetcher(client), config.commons_api_url)
|
||||
image = await portraits.get_base64(pope)
|
||||
await RestTransport(config, client).send_image(image, group_id, caption=text)
|
||||
sys.stdout.write(f"== sent to {group}; look at it in Signal\n")
|
||||
|
||||
|
||||
def main() -> int:
|
||||
"""Parse arguments and run one preview."""
|
||||
parser = argparse.ArgumentParser(
|
||||
description=__doc__,
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
)
|
||||
parser.add_argument("group", help="test group name; must be in SELFTEST_GROUPS")
|
||||
parser.add_argument(
|
||||
"--ordinal",
|
||||
type=int,
|
||||
default=None,
|
||||
help="papal ordinal to preview (default: the next one after this build)",
|
||||
)
|
||||
parser.add_argument("--print-only", action="store_true", help="render it, send nothing")
|
||||
args = parser.parse_args()
|
||||
ordinal = RELEASE_ORDINAL + 1 if args.ordinal is None else args.ordinal
|
||||
try:
|
||||
asyncio.run(send_preview(args.group, ordinal, print_only=args.print_only))
|
||||
except (PreviewError, httpx.HTTPError) as error:
|
||||
sys.stderr.write(f"preview_release: {error}\n")
|
||||
return EXIT_REFUSED
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Loading…
Reference in New Issue
Block a user