Remove ntfy: notifications go only through the app notification stream
Server: drop ntfy publishing (stage hooks, run outbox, chat attention, chat tool notify, auto-switch and schedule notices), the chat push outbox (its table is dropped at startup), ntfy status/endpoints and PIPELINES_NTFY_*. The app_notifications stream, subscribe/clear and kinds are unchanged. Notification settings keep run_finished/chat_needs_you/chat_reply_finished; ntfy_enabled is accepted and ignored and its stored row is never read. Stage type=ntfy is rejected: "ntfy stages were removed; notifications go to the Pipelines apps — use the stage notify hook". The notify hook remains. Installer: no ntfy build, accounts, config or supervisor child; manifest gets top-level "mode" (legacy ntfy.mode still read). Reinstall retires an old ntfy (safe PID-file stop, deletes binary, config, data dir, token, log, with a note in logs/install.log); uninstall cleans leftovers. App: ntfy settings section replaced by "Notification events"; texts updated. Docs: docs/ntfy.md folded into docs/notifications.md; install, docker, cli, author skill references updated; security review M1 marked resolved. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,136 +2,117 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/stage"
|
||||
)
|
||||
|
||||
func TestRunNotificationAppClickAndWebAction(t *testing.T) {
|
||||
for _, tc := range []struct{ base, view string }{
|
||||
{"https://pipelines.example.com", "https://pipelines.example.com/runs/20"},
|
||||
{"http://10.0.0.75:8095/", "http://10.0.0.75:8095/runs/20"},
|
||||
{"", ""}, // No web origin: the app link alone.
|
||||
{"javascript:alert(1)", ""},
|
||||
{"https://user:password@example.com", ""},
|
||||
{"https://example.com?token=secret", ""},
|
||||
{"https://example.com/#fragment", ""},
|
||||
{"https://example.com/subpath", ""}, // Web app uses origin-root routes.
|
||||
{"/runs", ""},
|
||||
{"https://example.com\r\nClick:bad", ""},
|
||||
} {
|
||||
t.Run(tc.base, func(t *testing.T) {
|
||||
t.Setenv("PIPELINES_WEB_PUBLIC_URL", tc.base)
|
||||
if got := runAttentionClick(20); got != "pipelines://run/20" {
|
||||
t.Fatalf("click = %q", got)
|
||||
}
|
||||
views := runAttentionViews(20)
|
||||
if tc.view == "" {
|
||||
if len(views) != 0 {
|
||||
t.Fatalf("unexpected views %+v", views)
|
||||
}
|
||||
return
|
||||
}
|
||||
if len(views) != 1 || views[0].Label != "Open in browser" || views[0].URL != tc.view {
|
||||
t.Fatalf("views = %+v, want %q", views, tc.view)
|
||||
}
|
||||
})
|
||||
func TestRunNotificationAppClick(t *testing.T) {
|
||||
if got := runAttentionClick(20); got != "pipelines://run/20" {
|
||||
t.Fatalf("click = %q", got)
|
||||
}
|
||||
if got := chatAttentionClick("ses/x"); got != "pipelines://chat/ses%2Fx" {
|
||||
t.Fatalf("chat click = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
type ntfyCapture struct {
|
||||
Title, Message, Click, Actions string
|
||||
// recordingNotes wraps an app notification store and keeps every newly
|
||||
// recorded notification, in order, so a test sees exactly what the app stream
|
||||
// was given (also after a clear). fail makes every record fail, failNext the
|
||||
// next few; calls counts every attempt, failed or not.
|
||||
type recordingNotes struct {
|
||||
appNotificationStore
|
||||
mu sync.Mutex
|
||||
got []protocol.AppNotification
|
||||
fail bool
|
||||
failNext int
|
||||
calls int
|
||||
}
|
||||
|
||||
// newNtfyCapture points the system destination at a mock ntfy endpoint.
|
||||
func newNtfyCapture(t *testing.T) func() []ntfyCapture {
|
||||
func (r *recordingNotes) RecordAppNotification(ctx context.Context, n pipeline.AppNotificationRecord, now time.Time) (protocol.AppNotification, bool, error) {
|
||||
r.mu.Lock()
|
||||
r.calls++
|
||||
fail := r.fail || r.failNext > 0
|
||||
if r.failNext > 0 {
|
||||
r.failNext--
|
||||
}
|
||||
r.mu.Unlock()
|
||||
if fail {
|
||||
return protocol.AppNotification{}, false, errors.New("record refused")
|
||||
}
|
||||
stored, fresh, err := r.appNotificationStore.RecordAppNotification(ctx, n, now)
|
||||
if err == nil && fresh {
|
||||
r.mu.Lock()
|
||||
r.got = append(r.got, stored)
|
||||
r.mu.Unlock()
|
||||
}
|
||||
return stored, fresh, err
|
||||
}
|
||||
|
||||
func (r *recordingNotes) attempts() int {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return r.calls
|
||||
}
|
||||
|
||||
func (r *recordingNotes) recorded() []protocol.AppNotification {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return append([]protocol.AppNotification(nil), r.got...)
|
||||
}
|
||||
|
||||
// newNoteCapture returns an app notifier over store (a scratch store when
|
||||
// nil) and a reader of everything it recorded.
|
||||
func newNoteCapture(t *testing.T, store *pipeline.Store) (*appNotifier, *recordingNotes) {
|
||||
t.Helper()
|
||||
var mu sync.Mutex
|
||||
var got []ntfyCapture
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.HasSuffix(r.URL.Path, "/clear") {
|
||||
// A cleared chat notification's ntfy clear, not a delivery.
|
||||
_ = json.NewEncoder(w).Encode(map[string]string{"id": "clr", "event": "message_clear"})
|
||||
return
|
||||
}
|
||||
decoder := new(mime.WordDecoder)
|
||||
title, _ := decoder.DecodeHeader(r.Header.Get("Title"))
|
||||
message, _ := decoder.DecodeHeader(r.Header.Get("Message"))
|
||||
mu.Lock()
|
||||
got = append(got, ntfyCapture{title, message, r.Header.Get("Click"), r.Header.Get("Actions")})
|
||||
mu.Unlock()
|
||||
_ = json.NewEncoder(w).Encode(map[string]string{"id": "msg", "event": "message"})
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
t.Setenv("PIPELINES_NTFY_URL", srv.URL+"/pipelines")
|
||||
t.Setenv("PIPELINES_NTFY_TOKEN", "")
|
||||
t.Setenv("PIPELINES_NTFY_TOKEN_FILE", "")
|
||||
return func() []ntfyCapture {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
return append([]ntfyCapture(nil), got...)
|
||||
if store == nil {
|
||||
store = newOperatorStore(t)
|
||||
}
|
||||
recorder := &recordingNotes{appNotificationStore: store}
|
||||
return newAppNotifier(recorder), recorder
|
||||
}
|
||||
|
||||
// Rows queued by older servers hold a web URL, the old placeholder deep link or
|
||||
// an empty click (an invalid web origin). Each delivers with the app link and
|
||||
// the current web action.
|
||||
// an empty click (an invalid web origin). Each is recorded with the app link.
|
||||
func TestQueuedLegacyRunNotificationsUseAppLink(t *testing.T) {
|
||||
for _, web := range []string{"https://pipelines.example.com", ""} {
|
||||
t.Run("web="+web, func(t *testing.T) {
|
||||
t.Setenv("PIPELINES_WEB_PUBLIC_URL", web)
|
||||
captured := newNtfyCapture(t)
|
||||
ctx := context.Background()
|
||||
s, err := pipeline.Open(filepath.Join(t.TempDir(), "legacy.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer s.Close()
|
||||
clicks := map[int64]string{7: "https://pipelines.example.com/runs/7", 8: "pipelines://run/8", 9: ""}
|
||||
for id, click := range clicks {
|
||||
if err := s.EnqueueRunNotification(ctx, pipeline.RunNotification{RunID: id, Sequence: 1, Event: "failed", Channel: "system", Title: "Run failed", Body: "Run failed", Click: click}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
r := &runner{ctx: ctx, store: s}
|
||||
r.deliverRunNotifications(time.Now())
|
||||
got := captured()
|
||||
if len(got) != 3 {
|
||||
t.Fatalf("deliveries = %+v", got)
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, n := range got {
|
||||
seen[n.Click] = true
|
||||
want := ""
|
||||
if web != "" {
|
||||
want = "view, Open in browser, " + web + "/runs/" + n.Click[len("pipelines://run/"):]
|
||||
}
|
||||
if n.Actions != want {
|
||||
t.Fatalf("actions = %q, want %q", n.Actions, want)
|
||||
}
|
||||
}
|
||||
for _, click := range []string{"pipelines://run/7", "pipelines://run/8", "pipelines://run/9"} {
|
||||
if !seen[click] {
|
||||
t.Fatalf("missing %s in %+v", click, got)
|
||||
}
|
||||
}
|
||||
for id := range clicks {
|
||||
rows, err := s.RunNotifications(ctx, id)
|
||||
if err != nil || len(rows) != 1 || rows[0].Status != "sent" {
|
||||
t.Fatalf("run %d rows = %+v %v", id, rows, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
ctx := context.Background()
|
||||
s, err := pipeline.Open(filepath.Join(t.TempDir(), "legacy.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer s.Close()
|
||||
clicks := map[int64]string{7: "https://pipelines.example.com/runs/7", 8: "pipelines://run/8", 9: ""}
|
||||
for id, click := range clicks {
|
||||
if err := s.EnqueueRunNotification(ctx, pipeline.RunNotification{RunID: id, Sequence: 1, Event: "failed", Channel: "system", Title: "Run failed", Body: "Run failed", Click: click}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
notes, recorder := newNoteCapture(t, s)
|
||||
r := &runner{ctx: ctx, store: s, notes: notes}
|
||||
r.deliverRunNotifications(time.Now())
|
||||
got := recorder.recorded()
|
||||
if len(got) != 3 {
|
||||
t.Fatalf("deliveries = %+v", got)
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, n := range got {
|
||||
seen[n.Click] = true
|
||||
}
|
||||
for _, click := range []string{"pipelines://run/7", "pipelines://run/8", "pipelines://run/9"} {
|
||||
if !seen[click] {
|
||||
t.Fatalf("missing %s in %+v", click, got)
|
||||
}
|
||||
}
|
||||
for id := range clicks {
|
||||
rows, err := s.RunNotifications(ctx, id)
|
||||
if err != nil || len(rows) != 1 || rows[0].Status != "sent" {
|
||||
t.Fatalf("run %d rows = %+v %v", id, rows, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,14 +126,14 @@ func chatAttentionHub(t *testing.T, sessions ...string) *chatHub {
|
||||
}
|
||||
|
||||
func TestChatQuestionNotifiesOnceWithAppLink(t *testing.T) {
|
||||
t.Setenv("PIPELINES_WEB_PUBLIC_URL", "https://pipelines.example.com")
|
||||
captured := newNtfyCapture(t)
|
||||
store := newOperatorStore(t)
|
||||
if err := store.RecordOperatorSession(context.Background(), "ses_q", t.TempDir(), "Release prep", protocol.ChatSessionTagOperator); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
h := chatAttentionHub(t, "ses_q")
|
||||
h.registry = store
|
||||
notes, recorder := newNoteCapture(t, store)
|
||||
h.notes = notes
|
||||
form := protocol.ChatForm{ID: "que_1", SessionID: "ses_q", Kind: "question", Title: "Which branch\nshould ship?"}
|
||||
frame := protocol.ChatEventFrame{SessionID: "ses_q", Kind: protocol.ChatEventForm, Form: &form}
|
||||
h.emit(frame)
|
||||
@@ -163,27 +144,27 @@ func TestChatQuestionNotifiesOnceWithAppLink(t *testing.T) {
|
||||
h.mu.Unlock()
|
||||
h.emit(frame)
|
||||
h.attentionWG.Wait()
|
||||
got := captured()
|
||||
got := recorder.recorded()
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("deliveries = %+v", got)
|
||||
}
|
||||
want := ntfyCapture{Title: "Release prep needs your answer", Message: "Which branch should ship?", Click: "pipelines://chat/ses_q", Actions: "view, Open in browser, https://pipelines.example.com/chat/ses_q"}
|
||||
if got[0] != want {
|
||||
t.Fatalf("got %+v, want %+v", got[0], want)
|
||||
if n := got[0]; n.Title != "Release prep needs your answer" || n.Message != "Which branch should ship?" || n.Click != "pipelines://chat/ses_q" {
|
||||
t.Fatalf("got %+v", n)
|
||||
}
|
||||
// Resolution and a later stale copy publish nothing more.
|
||||
// Resolution and a later stale copy record nothing more.
|
||||
resolved := form
|
||||
resolved.Resolved = true
|
||||
h.emit(protocol.ChatEventFrame{SessionID: "ses_q", Kind: protocol.ChatEventForm, Form: &resolved})
|
||||
h.attentionWG.Wait()
|
||||
if n := len(captured()); n != 1 {
|
||||
t.Fatalf("resolution republished: %d", n)
|
||||
if n := len(recorder.recorded()); n != 1 {
|
||||
t.Fatalf("resolution re-recorded: %d", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatResolvedOrReplayedFormsDoNotNotify(t *testing.T) {
|
||||
captured := newNtfyCapture(t)
|
||||
h := chatAttentionHub(t, "ses_r")
|
||||
notes, recorder := newNoteCapture(t, nil)
|
||||
h.notes = notes
|
||||
resolved := protocol.ChatForm{ID: "que_old", SessionID: "ses_r", Kind: "question", Title: "Old?", Resolved: true}
|
||||
cancelled := protocol.ChatForm{ID: "que_gone", SessionID: "ses_r", Kind: "question", Title: "Gone?", Cancelled: true}
|
||||
h.emit(protocol.ChatEventFrame{SessionID: "ses_r", Kind: protocol.ChatEventForm, Form: &resolved})
|
||||
@@ -196,14 +177,15 @@ func TestChatResolvedOrReplayedFormsDoNotNotify(t *testing.T) {
|
||||
h.emit(protocol.ChatEventFrame{SessionID: "ses_r", Kind: protocol.ChatEventText, MessageID: "msg_1", Part: &protocol.ChatPart{ID: "p1"}, Delta: "done"})
|
||||
h.emit(protocol.ChatEventFrame{SessionID: "ses_r", Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded}})
|
||||
h.attentionWG.Wait()
|
||||
if got := captured(); len(got) != 0 {
|
||||
if got := recorder.recorded(); len(got) != 0 {
|
||||
t.Fatalf("unexpected deliveries %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatPermissionNotifiesOnce(t *testing.T) {
|
||||
captured := newNtfyCapture(t) // No web origin: no browser action.
|
||||
h := chatAttentionHub(t, "ses_p")
|
||||
notes, recorder := newNoteCapture(t, nil)
|
||||
h.notes = notes
|
||||
frame := protocol.ChatPermissionFrame{SessionID: "ses_p", RequestID: "per_1", Action: "bash", Message: "Run rm -rf build?"}
|
||||
h.pushPermission("ses_p", frame)
|
||||
h.pushPermission("ses_p", frame)
|
||||
@@ -215,40 +197,31 @@ func TestChatPermissionNotifiesOnce(t *testing.T) {
|
||||
resolved := protocol.ChatPermissionFrame{SessionID: "ses_p", RequestID: "per_2", Message: "Already answered", Resolved: true}
|
||||
h.pushPermission("ses_p", resolved)
|
||||
h.attentionWG.Wait()
|
||||
got := captured()
|
||||
want := ntfyCapture{Title: "Chat needs your approval", Message: "Run rm -rf build?", Click: "pipelines://chat/ses_p"}
|
||||
if len(got) != 1 || got[0] != want {
|
||||
t.Fatalf("deliveries = %+v, want one %+v", got, want)
|
||||
got := recorder.recorded()
|
||||
if len(got) != 1 || got[0].Title != "Chat needs your approval" || got[0].Message != "Run rm -rf build?" || got[0].Click != "pipelines://chat/ses_p" {
|
||||
t.Fatalf("deliveries = %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatAttentionNeedsNtfyConfiguration(t *testing.T) {
|
||||
t.Setenv("PIPELINES_NTFY_URL", "")
|
||||
calls := 0
|
||||
original := chatAttentionPublish
|
||||
chatAttentionPublish = func(context.Context, string, string, string, string, []byte, string, ...stage.NtfyView) (stage.Values, error) {
|
||||
calls++
|
||||
return nil, nil
|
||||
}
|
||||
defer func() { chatAttentionPublish = original }()
|
||||
// Without the app notification stream a chat attention item has nowhere to
|
||||
// go, so it is not even claimed.
|
||||
func TestChatAttentionNeedsTheAppStream(t *testing.T) {
|
||||
h := chatAttentionHub(t, "ses_n")
|
||||
form := protocol.ChatForm{ID: "que_1", SessionID: "ses_n", Kind: "question", Title: "Ready?"}
|
||||
h.emit(protocol.ChatEventFrame{SessionID: "ses_n", Kind: protocol.ChatEventForm, Form: &form})
|
||||
h.pushPermission("ses_n", protocol.ChatPermissionFrame{SessionID: "ses_n", RequestID: "per_1", Message: "Allow?"})
|
||||
h.attentionWG.Wait()
|
||||
if calls != 0 {
|
||||
t.Fatalf("published %d without ntfy configuration", calls)
|
||||
if len(h.attentionSeen) != 0 {
|
||||
t.Fatalf("claimed without a notification stream: %v", h.attentionSeen)
|
||||
}
|
||||
}
|
||||
|
||||
// A failing ntfy endpoint never blocks or fails chat handling.
|
||||
// A failing notification store never blocks or fails chat handling.
|
||||
func TestChatAttentionFailureIsOnlyLogged(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "down", http.StatusServiceUnavailable)
|
||||
}))
|
||||
defer srv.Close()
|
||||
t.Setenv("PIPELINES_NTFY_URL", srv.URL+"/pipelines")
|
||||
h := chatAttentionHub(t, "ses_f")
|
||||
notes, recorder := newNoteCapture(t, nil)
|
||||
recorder.fail = true
|
||||
h.notes = notes
|
||||
form := protocol.ChatForm{ID: "que_1", SessionID: "ses_f", Kind: "question", Title: "Ready?"}
|
||||
h.emit(protocol.ChatEventFrame{SessionID: "ses_f", Kind: protocol.ChatEventForm, Form: &form})
|
||||
h.attentionWG.Wait()
|
||||
|
||||
Reference in New Issue
Block a user