diff --git a/docs/notes/notification-web-links.md b/docs/notes/notification-web-links.md index 8832be1f..6e168d8f 100644 --- a/docs/notes/notification-web-links.md +++ b/docs/notes/notification-web-links.md @@ -18,7 +18,14 @@ service-environment retention on both renderers. The isolated real proxy test checks unauthenticated gating, wrong-password rejection and return to the exact run after login. No production notification was sent to test this fix. -Code is not deployed/configured on the live server: the operator's phone-facing -web URL is still needed, and a backend update must wait for safe deployment. -Dummy run #22 completed with its old `pipelines://run/22` notification acknowledged -by ntfy; that verifies publishing, not reception or correct web navigation. +The operator supplied `https://alans-macbook-pro.tail6bf4dc.ts.net:8443/pipelines`; +the configured origin excludes the pipeline-list route. Deployment was authorized. +The first live check (#23) exposed a missing transactional-outbox path: the store +had already enqueued a legacy link before the server callback, so an insert-only +callback did not change it. Dispatch now resolves that placeholder to the +configured web link (also after restart), and the completed receipt saves the +actual target used. Added real FinishRun/outbox and failed-publication/reopen +regressions. Previously sent alerts are not modified or resent. + +Dummy runs #22 and #23 completed with their old Android-link notifications +acknowledged by ntfy. Those verify publishing, not correct web navigation. diff --git a/internal/pipeline/run_notifications.go b/internal/pipeline/run_notifications.go index 086395ea..fc3cb9d4 100644 --- a/internal/pipeline/run_notifications.go +++ b/internal/pipeline/run_notifications.go @@ -167,8 +167,8 @@ func (s *Store) FinishRunNotification(ctx context.Context, n RunNotification, no status = "failed" } } - _, err := s.db.ExecContext(ctx, `UPDATE run_notifications SET status=?,diagnostic=?,message_id=?,next_attempt=? -WHERE run_id=? AND sequence=? AND event=? AND channel=? AND status='sending' AND attempts=?`, status, diagnostic, messageID, now+int64(n.Attempts*5), n.RunID, n.Sequence, n.Event, n.Channel, n.Attempts) + _, err := s.db.ExecContext(ctx, `UPDATE run_notifications SET status=?,diagnostic=?,message_id=?,next_attempt=?,click=? +WHERE run_id=? AND sequence=? AND event=? AND channel=? AND status='sending' AND attempts=?`, status, diagnostic, messageID, now+int64(n.Attempts*5), n.Click, n.RunID, n.Sequence, n.Event, n.Channel, n.Attempts) return err } diff --git a/internal/server/notify.go b/internal/server/notify.go index 42233cee..662f3dd6 100644 --- a/internal/server/notify.go +++ b/internal/server/notify.go @@ -386,6 +386,12 @@ func (r *runner) deliverRunNotificationBatch(now time.Time, limit int) { if n == nil { return } + // State transitions enqueue their intent inside the store transaction, + // before notifyRun can supply a server-configured URL. Resolve that + // legacy placeholder here too, including pending intents after restart. + if os.Getenv("PIPELINES_WEB_PUBLIC_URL") != "" && n.Click == fmt.Sprintf("pipelines://run/%d", n.RunID) { + n.Click = runAttentionClick(n.RunID) + } ctx, cancel := context.WithTimeout(r.ctx, 30*time.Second) result, publishErr := stage.PublishNtfy(ctx, n.Channel, n.Title, n.Body, "", nil, n.Click) cancel() diff --git a/internal/server/run_notification_outbox_test.go b/internal/server/run_notification_outbox_test.go index d4f1fa1b..95840e2c 100644 --- a/internal/server/run_notification_outbox_test.go +++ b/internal/server/run_notification_outbox_test.go @@ -3,6 +3,7 @@ package server import ( "context" "encoding/json" + "fmt" "net/http" "net/http/httptest" "path/filepath" @@ -18,6 +19,7 @@ import ( ) func TestRunNotificationPublisherFailureRetriesAfterReopen(t *testing.T) { + t.Setenv("PIPELINES_WEB_PUBLIC_URL", "https://pipelines.example.com") ctx := context.Background() path := filepath.Join(t.TempDir(), "retry.db") s, err := pipeline.Open(path) @@ -26,6 +28,9 @@ func TestRunNotificationPublisherFailureRetriesAfterReopen(t *testing.T) { } var calls atomic.Int32 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if got := r.Header.Get("Click"); got != "https://pipelines.example.com/runs/7" { + t.Errorf("wrong web retry link: %q", got) + } if calls.Add(1) == 1 { http.Error(w, "transient", http.StatusServiceUnavailable) return @@ -37,6 +42,10 @@ func TestRunNotificationPublisherFailureRetriesAfterReopen(t *testing.T) { t.Setenv("PIPELINES_NTFY_TOKEN", "") t.Setenv("PIPELINES_NTFY_TOKEN_FILE", "") r := &runner{ctx: ctx, store: s} + // Emulate a durable transition intent saved before the runner callback. + if err := s.EnqueueRunNotification(ctx, pipeline.RunNotification{RunID: 7, Sequence: 1, Event: "failed", Channel: "system", Title: "Run failed", Body: "Run 7", Click: "pipelines://run/7"}); err != nil { + t.Fatal(err) + } r.notifyRun(7, "test", "failed", 1, "stage", "") got, err := s.RunNotifications(ctx, 7) if err != nil || len(got) != 1 || got[0].Status != "pending" || got[0].Attempts != 1 || got[0].Diagnostic == "" { @@ -59,6 +68,50 @@ func TestRunNotificationPublisherFailureRetriesAfterReopen(t *testing.T) { } } +func TestTransactionalSuccessNotificationUsesWebLink(t *testing.T) { + ctx := context.Background() + s, err := pipeline.Open(filepath.Join(t.TempDir(), "web-success.db")) + if err != nil { + t.Fatal(err) + } + defer s.Close() + if _, err := s.Submit(ctx, "test", pipeline.Template([]string{"one"}, true)); err != nil { + t.Fatal(err) + } + items, _ := s.List(ctx) + d, err := s.Get(ctx, items[0].ID, 1) + if err != nil { + t.Fatal(err) + } + id, _, err := s.CreateRunTitled(ctx, d, "web-success", t.TempDir(), false, nil, nil, pipeline.RunOverrides{NotifyMode: "on"}, summarise.Title{}) + if err != nil { + t.Fatal(err) + } + if err := s.FinishRun(ctx, id, "succeeded", "success", "", nil); err != nil { + t.Fatal(err) + } + var calls atomic.Int32 + endpoint := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + calls.Add(1) + if got, want := req.Header.Get("Click"), "https://pipelines.example.com/runs/"+fmt.Sprint(id); got != want { + t.Errorf("click = %q, want %q", got, want) + } + _ = json.NewEncoder(w).Encode(map[string]string{"id": "web-success", "event": "message"}) + })) + defer endpoint.Close() + t.Setenv("PIPELINES_WEB_PUBLIC_URL", "https://pipelines.example.com") + t.Setenv("PIPELINES_NTFY_URL", endpoint.URL+"/fake") + t.Setenv("PIPELINES_NTFY_TOKEN", "") + t.Setenv("PIPELINES_NTFY_TOKEN_FILE", "") + r := &runner{ctx: ctx, store: s} + r.notifyRun(id, "test", "succeeded", 0, "", "") + r.deliverRunNotifications(time.Now()) + got, err := s.RunNotifications(ctx, id) + if err != nil || len(got) != 1 || got[0].Status != "sent" || calls.Load() != 1 { + t.Fatalf("notification: %+v calls=%d err=%v", got, calls.Load(), err) + } +} + func TestFinishRunPersistenceFailureDoesNotAnnounceSuccess(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel()