Notify on run end only for runs marked Notify me
Auto runs no longer alert on failure or success; only notify_mode on does, unless the run_finished setting (now 'Notify when any run ends', default off) opts into every run end. Waiting still notifies auto and on runs. The setting is stored under a new key so a value saved under the old meaning is ignored.
This commit is contained in:
@@ -334,12 +334,20 @@ func (r *runner) notifyRun(id int64, name, status string, sequence int, currentS
|
||||
return
|
||||
}
|
||||
event := runAttentionEvent(status)
|
||||
if status == "succeeded" && r.notifyMode(id) == "on" {
|
||||
if status == "succeeded" {
|
||||
event = "succeeded"
|
||||
}
|
||||
if event == "" {
|
||||
return
|
||||
}
|
||||
// A run's end notifies only runs marked "on", unless the operator opted
|
||||
// into every run end (run_finished); waiting notifies any non-off run.
|
||||
if event != protocol.NotifyWaiting && r.notifyMode(id) != "on" {
|
||||
settings, err := r.store.NotificationSettings(r.ctx)
|
||||
if err != nil || !settings.RunFinished {
|
||||
return
|
||||
}
|
||||
}
|
||||
if r.attentionAlready(id, sequence, event) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -331,7 +331,10 @@ func TestRunAttentionNotificationDelivery(t *testing.T) {
|
||||
r.notifyRun(7, "release", protocol.RunStatusWaiting, 1, "approve", "")
|
||||
// A run that stays waiting is never republished, on a later call or a re-request.
|
||||
r.notifyRun(7, "release", protocol.RunStatusWaiting, 1, "approve", "")
|
||||
// A failed run publishes once, with its diagnostic.
|
||||
// An auto run's failure is silent: only runs marked "on" announce their end.
|
||||
r.notifyRun(8, "release", "failed", 2, "deploy", "provider returned 500")
|
||||
// A failed run marked "on" publishes once, with its diagnostic.
|
||||
r.setNotifyMode(7, "on")
|
||||
r.notifyRun(7, "release", "failed", 2, "deploy", "provider returned 500")
|
||||
|
||||
first := <-deliveries
|
||||
|
||||
@@ -42,6 +42,8 @@ func TestRunNotificationPublisherFailureRetriesAfterReopen(t *testing.T) {
|
||||
t.Setenv("PIPELINES_NTFY_TOKEN", "")
|
||||
t.Setenv("PIPELINES_NTFY_TOKEN_FILE", "")
|
||||
r := &runner{ctx: ctx, store: s}
|
||||
// The run was started with "Notify me", so its failure is announced.
|
||||
r.setNotifyMode(7, "on")
|
||||
// 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)
|
||||
@@ -60,6 +62,7 @@ func TestRunNotificationPublisherFailureRetriesAfterReopen(t *testing.T) {
|
||||
}
|
||||
defer s.Close()
|
||||
r = &runner{ctx: ctx, store: s}
|
||||
r.setNotifyMode(7, "on")
|
||||
r.deliverRunNotifications(time.Now().Add(time.Minute))
|
||||
r.notifyRun(7, "test", "failed", 1, "stage", "")
|
||||
got, err = s.RunNotifications(ctx, 7)
|
||||
@@ -250,14 +253,17 @@ func TestRunNotificationModesAndFinalStageDedupe(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
mode string
|
||||
hook bool
|
||||
// quiet turns the global run_finished setting off (it defaults on).
|
||||
quiet bool
|
||||
want int32
|
||||
// any turns the global run_finished setting on (it defaults off), so
|
||||
// every run end notifies, not only runs marked "on".
|
||||
any bool
|
||||
want int32
|
||||
}{
|
||||
{"on", false, false, 1}, {"auto", false, false, 1}, {"auto", false, true, 0}, {"on", false, true, 1}, {"off", false, false, 0},
|
||||
{"on", false, false, 1}, {"auto", false, false, 0}, {"auto", false, true, 1}, {"on", false, true, 1}, {"off", false, false, 0}, {"off", false, true, 0},
|
||||
// A stage hook is the pipeline's own opt-in: it still publishes for an
|
||||
// auto run, and never doubles up with the run alert.
|
||||
{"on", true, false, 1}, {"auto", true, false, 1}, {"auto", true, true, 1}, {"off", true, false, 0},
|
||||
} {
|
||||
t.Run(tc.mode+map[bool]string{true: "-hook", false: "-no-hook"}[tc.hook]+map[bool]string{true: "-quiet", false: ""}[tc.quiet], func(t *testing.T) {
|
||||
t.Run(tc.mode+map[bool]string{true: "-hook", false: "-no-hook"}[tc.hook]+map[bool]string{true: "-any", false: ""}[tc.any], func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
s, err := pipeline.Open(filepath.Join(t.TempDir(), "modes.db"))
|
||||
@@ -265,8 +271,8 @@ func TestRunNotificationModesAndFinalStageDedupe(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer s.Close()
|
||||
if tc.quiet {
|
||||
if _, err := s.UpdateNotificationSettings(ctx, map[string]any{protocol.NotifyRunFinished: false}); err != nil {
|
||||
if tc.any {
|
||||
if _, err := s.UpdateNotificationSettings(ctx, map[string]any{protocol.NotifyRunFinished: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -295,7 +301,7 @@ func TestRunNotificationModesAndFinalStageDedupe(t *testing.T) {
|
||||
if err != nil || detail.Status != "succeeded" || detail.NotifyMode != tc.mode || calls.Load()-before != tc.want {
|
||||
t.Fatalf("mode contract: %+v calls=%d %v", detail, calls.Load()-before, err)
|
||||
}
|
||||
if tc.want == 1 && !(tc.hook && tc.quiet) && (len(detail.Notifications) != 1 || (detail.Notifications[0].Status != "sent" && detail.Notifications[0].Status != "covered")) {
|
||||
if tc.want == 1 && !(tc.hook && tc.mode == "auto" && !tc.any) && (len(detail.Notifications) != 1 || (detail.Notifications[0].Status != "sent" && detail.Notifications[0].Status != "covered")) {
|
||||
t.Fatalf("missing final audit: %+v", detail.Notifications)
|
||||
}
|
||||
cancel()
|
||||
@@ -328,14 +334,16 @@ func TestRunNotificationAttentionModesAndRetryBound(t *testing.T) {
|
||||
r.notifyRun(id, "test", status, sequence, "stage", "")
|
||||
}
|
||||
}
|
||||
if calls.Load() != 4 {
|
||||
t.Fatalf("attention modes: %d calls, want 4", calls.Load())
|
||||
// auto waiting, on waiting and on failed; an auto run's failure is silent
|
||||
// because run_finished defaults off.
|
||||
if calls.Load() != 3 {
|
||||
t.Fatalf("attention modes: %d calls, want 3", calls.Load())
|
||||
}
|
||||
r.deliverRunNotifications(time.Now().Add(time.Minute))
|
||||
r.deliverRunNotifications(time.Now().Add(2 * time.Minute))
|
||||
r.deliverRunNotifications(time.Now().Add(3 * time.Minute))
|
||||
if calls.Load() != 12 {
|
||||
t.Fatalf("retry bound: %d calls, want 12", calls.Load())
|
||||
if calls.Load() != 9 {
|
||||
t.Fatalf("retry bound: %d calls, want 9", calls.Load())
|
||||
}
|
||||
for _, id := range []int64{1, 2, 3} {
|
||||
got, err := s.RunNotifications(ctx, id)
|
||||
|
||||
Reference in New Issue
Block a user