diff --git a/.github/workflows/close-issues.yml b/.github/workflows/close-issues.yml index 58c7fa0..c44c3af 100644 --- a/.github/workflows/close-issues.yml +++ b/.github/workflows/close-issues.yml @@ -1,8 +1,8 @@ name: Close issues -# Closes the issues named with `Closes #` in a PR's description when the -# PR merges to staging. GitHub only acts on that keyword for PRs to the default -# branch. +# Closes the issues named with a closing keyword such as `Closes #` in a +# PR's description or its merged commit message when the PR merges to staging. +# GitHub only acts on these keywords for PRs to the default branch. on: push: branches: [staging] @@ -26,7 +26,9 @@ jobs: const pr = prs.find((p) => p.merged_at && p.base.ref === "staging"); if (!pr) return; - for (const [, issue_number] of (pr.body ?? "").matchAll(/\bcloses:? #(\d+)\b/gi)) { + const text = `${pr.body ?? ""}\n${context.payload.head_commit?.message ?? ""}`; + const pattern = /\b(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?):? #(\d+)\b/gi; + for (const [, issue_number] of text.matchAll(pattern)) { const { data: issue } = await github.rest.issues.get({ owner, repo, issue_number }); if (issue.pull_request || issue.state === "closed") continue; diff --git a/docs/configuration/offline-publishing.md b/docs/configuration/offline-publishing.md index 277f23e..49cf80c 100644 --- a/docs/configuration/offline-publishing.md +++ b/docs/configuration/offline-publishing.md @@ -9,6 +9,7 @@ Each payload is tagged with the protocol it arrived on: ## Rules +- Listing anything other than the protocols above fails `manage.sh check`, and the services refuse to start. - An offline payload from a protocol not in `OFFLINE_PUBLISH_ALLOWED_PROTOCOLS` is discarded. An empty list allows every protocol. - An offline payload over `https` must carry `OFFLINE_PUBLISH_SHARED_SECRET` in the request's `tag` field, or it's discarded. - Leaving `OFFLINE_PUBLISH_ALLOWED_PROTOCOLS` empty, or listing `https`, without the secret fails `manage.sh check`, and the services refuse to start. diff --git a/manage.sh b/manage.sh index 39141a6..cc9981b 100755 --- a/manage.sh +++ b/manage.sh @@ -68,7 +68,7 @@ cmd_status() { # the subshell instead of stopping the script. add_unit() { local name="$1" full="$1" svc - [[ "$name" == *.service ]] || full="relaysms-publisher-${name}.service" + [[ "$name" == *.service ]] || full="$(unit_name_for "relaysms-publisher-${name}.service")" for svc in "${SERVICE_UNITS[@]}"; do if [ "$svc" = "$full" ]; then units+=("$full") diff --git a/publisher/config.py b/publisher/config.py index ea20c52..deb305e 100644 --- a/publisher/config.py +++ b/publisher/config.py @@ -21,6 +21,7 @@ ROOT = Path(__file__).resolve().parents[1] DATABASE_DIALECTS = ("sqlite", "mysql", "postgres") CELERY_BROKERS = ("sqlite", "redis", "rabbitmq") +OFFLINE_PUBLISH_PROTOCOLS = ("https", "smtp") GITHUB_ORG_PATTERN = re.compile(r"^[a-z0-9](?:[a-z0-9-]{0,37}[a-z0-9])?$") # Fill unset variables from .env. Values already in the environment win. @@ -345,7 +346,15 @@ class OfflinePublishConfig(Section): @classmethod def load(cls, read: _Reader) -> Self: - allowed = read.get_list("OFFLINE_PUBLISH_ALLOWED_PROTOCOLS") + names = read.get_list("OFFLINE_PUBLISH_ALLOWED_PROTOCOLS") + allowed = list(dict.fromkeys(name.lower() for name in names)) + for protocol in allowed: + if protocol not in OFFLINE_PUBLISH_PROTOCOLS: + read.fail( + "OFFLINE_PUBLISH_ALLOWED_PROTOCOLS", + f"expected one of {', '.join(OFFLINE_PUBLISH_PROTOCOLS)}, " + f"got {protocol!r}", + ) https_allowed = not allowed or "https" in allowed return cls( allowed_protocols=allowed, diff --git a/requirements-dev.txt b/requirements-dev.txt index 36ee718..b741afd 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -3,6 +3,6 @@ httpx==0.28.1 import-linter==2.15 pre-commit==4.7.0 pyright==1.1.414 -pytest==9.1.1 +pytest==9.2.0 pytest-cov==7.1.0 -ruff==0.17.2 +ruff==0.17.3 diff --git a/requirements-observability.txt b/requirements-observability.txt index 27f8cca..08fe9d3 100644 --- a/requirements-observability.txt +++ b/requirements-observability.txt @@ -5,11 +5,11 @@ # Includes requirements.txt, since both pull in OTel packages that must # resolve to the same versions. -r requirements.txt -opentelemetry-distro==0.66b1 -opentelemetry-exporter-otlp-proto-grpc==1.45.1 -opentelemetry-instrumentation-celery==0.66b1 -opentelemetry-instrumentation-fastapi==0.66b1 -opentelemetry-instrumentation-grpc==0.66b1 -opentelemetry-instrumentation-logging==0.66b1 -opentelemetry-instrumentation-sqlalchemy==0.66b1 -opentelemetry-sdk==1.45.1 +opentelemetry-distro==0.66b2 +opentelemetry-exporter-otlp-proto-grpc==1.45.2 +opentelemetry-instrumentation-celery==0.66b2 +opentelemetry-instrumentation-fastapi==0.66b2 +opentelemetry-instrumentation-grpc==0.66b2 +opentelemetry-instrumentation-logging==0.66b2 +opentelemetry-instrumentation-sqlalchemy==0.66b2 +opentelemetry-sdk==1.45.2 diff --git a/requirements.txt b/requirements.txt index 5822b08..c0a4e4f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,21 +2,21 @@ alembic==1.20.1 argon2-cffi==25.1.0 authres==1.2.0 -cachetools==7.2.5 +cachetools==7.2.8 celery[sqlalchemy,redis]==5.6.3 cryptography==50.0.2 dkimpy==1.1.8 fastapi==0.162.0 GitPython==3.2.1 grpc-interceptor==0.15.4 -grpcio==1.85.0 -grpcio-health-checking==1.85.0 -grpcio-testing==1.85.0 -grpcio-tools==1.85.0 +grpcio==1.85.1 +grpcio-health-checking==1.85.1 +grpcio-testing==1.85.1 +grpcio-tools==1.85.1 imap-tools==1.16.0 msgspec==0.22.0 -opentelemetry-api==1.45.1 -phonenumbers==9.0.41 +opentelemetry-api==1.45.2 +phonenumbers==9.0.42 psycopg2-binary==2.9.13 pymysql==1.2.3 python-dotenv==1.2.4 diff --git a/tests/scripts/test_manage.py b/tests/scripts/test_manage.py new file mode 100644 index 0000000..15d2bcb --- /dev/null +++ b/tests/scripts/test_manage.py @@ -0,0 +1,77 @@ +# SPDX-License-Identifier: GPL-3.0-only + +import os +import shutil +import subprocess + +import pytest + +from tests.scripts import REPO + + +@pytest.fixture +def logs(install, tmp_path): + """Run `manage.sh logs` in the install directory, with journalctl faked.""" + shutil.copy(REPO / "manage.sh", install) + bin_dir = tmp_path / "bin" + bin_dir.mkdir() + journalctl = bin_dir / "journalctl" + journalctl.write_text('#!/usr/bin/env bash\nprintf "%s\\n" "$@"\n') + journalctl.chmod(0o755) + + def run(*args: str, instance: str = "") -> subprocess.CompletedProcess[str]: + if instance: + (install / ".instance-name").write_text(instance) + return subprocess.run( + ["bash", "manage.sh", "logs", "--no-follow", *args], + cwd=install, + env={ + **os.environ, + "NO_COLOR": "1", + "PATH": f"{bin_dir}:{os.environ['PATH']}", + }, + capture_output=True, + text=True, + check=False, + ) + + return run + + +def journal_units(result: subprocess.CompletedProcess[str]) -> list[str]: + assert result.returncode == 0, result.stderr + args = result.stdout.splitlines() + return [args[i + 1] for i, arg in enumerate(args) if arg == "-u"] + + +class TestLogsUnit: + def test_short_name_selects_the_unit(self, logs): + assert journal_units(logs("--unit", "rest")) == [ + "relaysms-publisher-rest.service" + ] + + def test_short_name_selects_the_named_instance_unit(self, logs): + result = logs("--unit", "rest", "-u", "worker", "--unit=beat", instance="acme") + + assert journal_units(result) == [ + "relaysms-publisher-acme-rest.service", + "relaysms-publisher-acme-worker.service", + "relaysms-publisher-acme-beat.service", + ] + + def test_full_name_selects_the_named_instance_unit(self, logs): + result = logs("--unit=relaysms-publisher-acme-grpc.service", instance="acme") + + assert journal_units(result) == ["relaysms-publisher-acme-grpc.service"] + + def test_unknown_name_on_a_named_instance_fails(self, logs): + result = logs("--unit", "nope", instance="acme") + + assert result.returncode != 0 + assert "Unknown service unit: nope" in result.stderr + + def test_unit_of_another_instance_fails(self, logs): + result = logs("--unit", "relaysms-publisher-rest.service", instance="acme") + + assert result.returncode != 0 + assert "Unknown service unit: relaysms-publisher-rest.service" in result.stderr diff --git a/tests/test_config.py b/tests/test_config.py index 2564798..6400f2b 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -198,7 +198,7 @@ def test_offline_shared_secret_must_be_a_key(): assert offline.shared_secret == KEY -@pytest.mark.parametrize("protocols", ["https", "smtp,https", ""]) +@pytest.mark.parametrize("protocols", ["https", "HTTPS", "smtp,https", ""]) def test_offline_https_requires_shared_secret(protocols): allowed = {"OFFLINE_PUBLISH_ALLOWED_PROTOCOLS": protocols} @@ -206,6 +206,33 @@ def test_offline_https_requires_shared_secret(protocols): assert not errors(**allowed, OFFLINE_PUBLISH_SHARED_SECRET=KEY) +@pytest.mark.parametrize( + ("protocols", "expected"), + [ + ("smtp", ["smtp"]), + ("SMTP", ["smtp"]), + ("https,smtp", ["https", "smtp"]), + ("smtp,smtp", ["smtp"]), + ("smtp,SMTP", ["smtp"]), + ("", []), + ], +) +def test_offline_allowed_protocols_are_normalized(protocols, expected): + offline = load( + OfflinePublishConfig, + OFFLINE_PUBLISH_ALLOWED_PROTOCOLS=protocols, + OFFLINE_PUBLISH_SHARED_SECRET=KEY, + ) + assert offline.allowed_protocols == expected + + +@pytest.mark.parametrize("protocols", ["smpt", "SMTP,ftp", "http"]) +def test_offline_allowed_protocols_reject_unknown_entries(protocols): + problems = errors(OFFLINE_PUBLISH_ALLOWED_PROTOCOLS=protocols) + assert "OFFLINE_PUBLISH_ALLOWED_PROTOCOLS" in problems + assert "OFFLINE_PUBLISH_SHARED_SECRET" not in problems + + def test_template_env_paths_match_defaults(): # scripts/lib.sh falls back to template.env for paths unset in .env. template = dotenv_values(config.ROOT / "template.env")