Files
pipelines/internal/pipeline/notification_settings.go
alan b109ab3e06 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.
2026-10-31 16:52:04 +00:00

87 lines
3.1 KiB
Go

package pipeline
import (
"context"
"database/sql"
"fmt"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// notificationSettingPrefix namespaces the global notification choices inside
// server_settings; each key stores "true" or "false". A missing row means the
// 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)
}
// readNotificationSettings reads the stored choices through q, which may be the
// database or an open transaction (run-transition enqueue reads inside its tx).
func readNotificationSettings(ctx context.Context, q settingsQuerier) (protocol.NotificationSettings, error) {
settings := protocol.DefaultNotificationSettings()
rows, err := q.QueryContext(ctx, `SELECT key,value FROM server_settings WHERE key LIKE 'notify.%'`)
if err != nil {
return settings, err
}
defer rows.Close()
for rows.Next() {
var key, value string
if err := rows.Scan(&key, &value); err != nil {
return settings, err
}
stored := key[len(notificationSettingPrefix):]
for _, wire := range protocol.NotificationSettingKeys {
if notificationStorageKey(wire) == stored {
*settings.Field(wire) = value == "true"
}
}
}
return settings, rows.Err()
}
// NotificationSettings returns the operator's global notification choices.
func (s *Store) NotificationSettings(ctx context.Context) (protocol.NotificationSettings, error) {
return readNotificationSettings(ctx, s.db)
}
// UpdateNotificationSettings applies a validated partial update atomically and
// returns the stored result. Unknown keys and non-boolean values are refused
// before anything is written.
func (s *Store) UpdateNotificationSettings(ctx context.Context, update map[string]any) (protocol.NotificationSettings, error) {
values, err := protocol.ValidateNotificationSettingsUpdate(update)
if err != nil {
return protocol.NotificationSettings{}, err
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return protocol.NotificationSettings{}, err
}
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+notificationStorageKey(key), fmt.Sprint(value)); err != nil {
return protocol.NotificationSettings{}, err
}
}
settings, err := readNotificationSettings(ctx, tx)
if err != nil {
return protocol.NotificationSettings{}, err
}
return settings, tx.Commit()
}