fix(notify): resolve web links for transactional outbox deliveries

This commit is contained in:
2026-10-21 13:16:20 +01:00
parent eabe5d64dd
commit 184ce78b65
4 changed files with 72 additions and 6 deletions
+6
View File
@@ -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()
@@ -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()