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:
@@ -13,6 +13,19 @@ import (
|
||||
// default from protocol.DefaultNotificationSettings.
|
||||
const notificationSettingPrefix = "notify."
|
||||
|
||||
// notificationStorageKeys maps a wire key to a different stored key when its
|
||||
// meaning changed. run_finished used to mean "auto runs announce success"
|
||||
// (default on); it now means "every run end notifies" (default off), so it is
|
||||
// stored under a new key and any value saved under the old meaning is ignored.
|
||||
var notificationStorageKeys = map[string]string{protocol.NotifyRunFinished: "any_run_ended"}
|
||||
|
||||
func notificationStorageKey(key string) string {
|
||||
if stored, ok := notificationStorageKeys[key]; ok {
|
||||
return stored
|
||||
}
|
||||
return key
|
||||
}
|
||||
|
||||
type settingsQuerier interface {
|
||||
QueryContext(context.Context, string, ...any) (*sql.Rows, error)
|
||||
}
|
||||
@@ -31,8 +44,11 @@ func readNotificationSettings(ctx context.Context, q settingsQuerier) (protocol.
|
||||
if err := rows.Scan(&key, &value); err != nil {
|
||||
return settings, err
|
||||
}
|
||||
if field := settings.Field(key[len(notificationSettingPrefix):]); field != nil {
|
||||
*field = value == "true"
|
||||
stored := key[len(notificationSettingPrefix):]
|
||||
for _, wire := range protocol.NotificationSettingKeys {
|
||||
if notificationStorageKey(wire) == stored {
|
||||
*settings.Field(wire) = value == "true"
|
||||
}
|
||||
}
|
||||
}
|
||||
return settings, rows.Err()
|
||||
@@ -58,7 +74,7 @@ func (s *Store) UpdateNotificationSettings(ctx context.Context, update map[strin
|
||||
defer tx.Rollback()
|
||||
for key, value := range values {
|
||||
if _, err := tx.ExecContext(ctx, `INSERT INTO server_settings(key,value) VALUES(?,?)
|
||||
ON CONFLICT(key) DO UPDATE SET value=excluded.value`, notificationSettingPrefix+key, fmt.Sprint(value)); err != nil {
|
||||
ON CONFLICT(key) DO UPDATE SET value=excluded.value`, notificationSettingPrefix+notificationStorageKey(key), fmt.Sprint(value)); err != nil {
|
||||
return protocol.NotificationSettings{}, err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,16 +22,16 @@ func TestNotificationSettingsDefaultPersistAndPartialUpdate(t *testing.T) {
|
||||
if err != nil || got != protocol.DefaultNotificationSettings() {
|
||||
t.Fatalf("defaults = %+v, %v", got, err)
|
||||
}
|
||||
if !got.RunFinished || !got.ChatNeedsYou || got.ChatReplyFinished {
|
||||
if got.RunFinished || !got.ChatNeedsYou || got.ChatReplyFinished {
|
||||
t.Fatalf("default values changed: %+v", got)
|
||||
}
|
||||
got, err = s.UpdateNotificationSettings(ctx, map[string]any{protocol.NotifyChatReplyFinished: true})
|
||||
want := protocol.NotificationSettings{RunFinished: true, ChatNeedsYou: true, ChatReplyFinished: true}
|
||||
want := protocol.NotificationSettings{RunFinished: false, ChatNeedsYou: true, ChatReplyFinished: true}
|
||||
if err != nil || got != want {
|
||||
t.Fatalf("partial update = %+v, %v", got, err)
|
||||
}
|
||||
got, err = s.UpdateNotificationSettings(ctx, map[string]any{protocol.NotifyRunFinished: false, protocol.NotifyChatNeedsYou: false})
|
||||
want = protocol.NotificationSettings{RunFinished: false, ChatNeedsYou: false, ChatReplyFinished: true}
|
||||
got, err = s.UpdateNotificationSettings(ctx, map[string]any{protocol.NotifyRunFinished: true, protocol.NotifyChatNeedsYou: false})
|
||||
want = protocol.NotificationSettings{RunFinished: true, ChatNeedsYou: false, ChatReplyFinished: true}
|
||||
if err != nil || got != want {
|
||||
t.Fatalf("second update = %+v, %v", got, err)
|
||||
}
|
||||
@@ -78,56 +78,67 @@ func TestNotificationSettingsRejectUnknownOrInvalid(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A run reaching succeeded enqueues its generic alert according to its notify
|
||||
// mode and, for auto, the run_finished setting read in the same transaction.
|
||||
func TestSucceededRunEnqueueFollowsRunFinishedSetting(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
mode string
|
||||
runFinished bool
|
||||
want int
|
||||
}{
|
||||
{"auto", true, 1}, {"auto", false, 0}, {"on", true, 1}, {"on", false, 1}, {"off", true, 0}, {"off", false, 0},
|
||||
} {
|
||||
t.Run(tc.mode+map[bool]string{true: "-enabled", false: "-disabled"}[tc.runFinished], func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, id := notificationTransitionRun(t, filepath.Join(t.TempDir(), "succeeded.db"), tc.mode)
|
||||
defer s.Close()
|
||||
if _, err := s.UpdateNotificationSettings(ctx, map[string]any{protocol.NotifyRunFinished: tc.runFinished}); err != nil {
|
||||
t.Fatal(err)
|
||||
// A run's end (succeeded or failed) enqueues its generic alert only for a run
|
||||
// marked "on", or for "auto" when run_finished (default off) opts into every
|
||||
// run end. Waiting notifies auto and on runs regardless; "off" never notifies.
|
||||
func TestRunEndEnqueueFollowsNotifyModeAndRunFinished(t *testing.T) {
|
||||
for _, status := range []string{"succeeded", "failed", "waiting"} {
|
||||
for _, tc := range []struct {
|
||||
mode string
|
||||
runFinished bool
|
||||
want int
|
||||
}{
|
||||
{"auto", false, 0}, {"auto", true, 1}, {"on", false, 1}, {"on", true, 1}, {"off", false, 0}, {"off", true, 0},
|
||||
} {
|
||||
want := tc.want
|
||||
if status == "waiting" && tc.mode == "auto" {
|
||||
want = 1
|
||||
}
|
||||
if err := notificationTransition(ctx, s, id, "succeeded"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := s.RunNotifications(ctx, id)
|
||||
if err != nil || len(got) != tc.want {
|
||||
t.Fatalf("notifications = %+v, %v; want %d", got, err, tc.want)
|
||||
}
|
||||
if tc.want == 1 {
|
||||
n, err := s.ClaimRunNotification(ctx, time.Now().Unix())
|
||||
if err != nil || n == nil || n.Event != "succeeded" || n.Title != "Run succeeded" || n.Click != "pipelines://run/"+strconv.FormatInt(id, 10) {
|
||||
t.Fatalf("claimed = %+v, %v", n, err)
|
||||
t.Run(status+"-"+tc.mode+map[bool]string{true: "-any", false: "-marked"}[tc.runFinished], func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, id := notificationTransitionRun(t, filepath.Join(t.TempDir(), "end.db"), tc.mode)
|
||||
defer s.Close()
|
||||
if tc.runFinished {
|
||||
if _, err := s.UpdateNotificationSettings(ctx, map[string]any{protocol.NotifyRunFinished: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
if err := notificationTransition(ctx, s, id, status); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := s.RunNotifications(ctx, id)
|
||||
if err != nil || len(got) != want {
|
||||
t.Fatalf("notifications = %+v, %v; want %d", got, err, want)
|
||||
}
|
||||
if want == 1 {
|
||||
title := map[string]string{"succeeded": "Run succeeded", "failed": "Run failed", "waiting": "Run needs your decision"}[status]
|
||||
n, err := s.ClaimRunNotification(ctx, time.Now().Unix())
|
||||
if err != nil || n == nil || n.Event != status || n.Title != title || n.Click != "pipelines://run/"+strconv.FormatInt(id, 10) {
|
||||
t.Fatalf("claimed = %+v, %v", n, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Waiting and failed alerts do not depend on run_finished.
|
||||
func TestRunFinishedSettingLeavesAttentionAlone(t *testing.T) {
|
||||
for _, status := range []string{"waiting", "failed"} {
|
||||
t.Run(status, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, id := notificationTransitionRun(t, filepath.Join(t.TempDir(), "attention.db"), "auto")
|
||||
defer s.Close()
|
||||
if _, err := s.UpdateNotificationSettings(ctx, map[string]any{protocol.NotifyRunFinished: false}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := notificationTransition(ctx, s, id, status); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := s.RunNotifications(ctx, id); err != nil || len(got) != 1 || got[0].Event != status {
|
||||
t.Fatalf("notifications = %+v, %v", got, err)
|
||||
}
|
||||
})
|
||||
// run_finished was stored as "notify.run_finished" when it meant "auto runs
|
||||
// announce success" (default on). That stale value must not opt the operator
|
||||
// into every run end under the new meaning.
|
||||
func TestLegacyRunFinishedValueIsIgnored(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, id := notificationTransitionRun(t, filepath.Join(t.TempDir(), "legacy.db"), "auto")
|
||||
defer s.Close()
|
||||
if _, err := s.db.ExecContext(ctx, `INSERT INTO server_settings(key,value) VALUES('notify.run_finished','true')`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := s.NotificationSettings(ctx); err != nil || got.RunFinished {
|
||||
t.Fatalf("legacy value honoured: %+v, %v", got, err)
|
||||
}
|
||||
if err := notificationTransition(ctx, s, id, "failed"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := s.RunNotifications(ctx, id); err != nil || len(got) != 0 {
|
||||
t.Fatalf("notifications = %+v, %v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,9 +144,10 @@ func TestRunNotificationTransitionModesAndCoverage(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// run_finished defaults on, so an auto run announces success too.
|
||||
// run_finished defaults off, so an auto run notifies only
|
||||
// while waiting; its end is silent unless marked "on".
|
||||
want := 1
|
||||
if mode == "off" {
|
||||
if mode == "off" || (mode == "auto" && status != "waiting") {
|
||||
want = 0
|
||||
}
|
||||
if len(ns) != want {
|
||||
|
||||
@@ -42,12 +42,9 @@ JOIN pipeline_versions v ON v.pipeline_id=r.pipeline_id AND v.version=r.version
|
||||
case "waiting":
|
||||
title = "Run needs your decision"
|
||||
body = "waiting for a human decision"
|
||||
case "failed":
|
||||
title = "Run failed"
|
||||
body = "run failed"
|
||||
case "succeeded":
|
||||
// "on" always announces success; any other (auto/legacy) mode follows
|
||||
// the operator's run_finished choice, read in this transaction.
|
||||
case "failed", "succeeded":
|
||||
// A run's end notifies only for runs marked "on", unless the operator
|
||||
// opted into every run end via run_finished, read in this transaction.
|
||||
if mode != "on" {
|
||||
settings, err := readNotificationSettings(ctx, tx)
|
||||
if err != nil {
|
||||
@@ -57,8 +54,8 @@ JOIN pipeline_versions v ON v.pipeline_id=r.pipeline_id AND v.version=r.version
|
||||
return nil
|
||||
}
|
||||
}
|
||||
title = "Run succeeded"
|
||||
body = "run succeeded"
|
||||
title = "Run " + status
|
||||
body = "run " + status
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -27,8 +27,9 @@ const (
|
||||
|
||||
// NotificationSettings is the typed form of the global notification choices.
|
||||
type NotificationSettings struct {
|
||||
// RunFinished notifies when a run with notify mode "auto" succeeds. Runs
|
||||
// started with notifications "on" always notify and "off" never do.
|
||||
// RunFinished makes every run end (success or failure) notify, not only
|
||||
// runs started with notify mode "on". Off by default: an "auto" run then
|
||||
// notifies only when it waits for a human decision, and "off" never does.
|
||||
RunFinished bool
|
||||
// ChatNeedsYou notifies when a chat asks a question or requests approval.
|
||||
ChatNeedsYou bool
|
||||
@@ -41,7 +42,7 @@ var NotificationSettingKeys = []string{NotifyRunFinished, NotifyChatNeedsYou, No
|
||||
|
||||
// DefaultNotificationSettings are the values before the operator changes any.
|
||||
func DefaultNotificationSettings() NotificationSettings {
|
||||
return NotificationSettings{RunFinished: true, ChatNeedsYou: true, ChatReplyFinished: false}
|
||||
return NotificationSettings{RunFinished: false, ChatNeedsYou: true, ChatReplyFinished: false}
|
||||
}
|
||||
|
||||
// Map returns the wire form with every key present.
|
||||
|
||||
@@ -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