package controlplane import ( "fmt" "net/url" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/alert" ) const TaskPolicyKind = "task-policy" type taskPolicy struct { DiffCapLines int `json:"diff_cap_lines"` NotifyWebhook string `json:"notify_webhook"` NotifyMinSeverity string `json:"notify_min_severity"` NotifySecret config.SecretRef `json:"notify_secret"` } func taskPolicyDefinition() Definition { return Definition{Kind: TaskPolicyKind, Title: "Task policy", ApplyMode: "live", Document: taskPolicy{}, Seed: func(cfg config.Config) any { return taskPolicyOf(cfg) }, Validate: func(input []byte) error { return validateAs(input, func(doc taskPolicy) error { if doc.DiffCapLines < 0 { return fmt.Errorf("diff_cap_lines must not be negative") } if doc.NotifyWebhook != "" { parsed, err := url.Parse(doc.NotifyWebhook) if err != nil || parsed.Host == "" || (parsed.Scheme != "http" && parsed.Scheme != "https") { return fmt.Errorf("notify_webhook must be an HTTP or HTTPS URL") } } if !alert.Severity(doc.NotifyMinSeverity).Valid() && doc.NotifyMinSeverity != "" { return fmt.Errorf("notify_min_severity must be info, warning or action") } if (doc.NotifySecret.Engine == "") != (doc.NotifySecret.Key == "") { return fmt.Errorf("notify_secret needs both an engine and a key") } return nil }) }} } func taskPolicyOf(cfg config.Config) taskPolicy { return taskPolicy{ DiffCapLines: cfg.DiffCap(), NotifyWebhook: cfg.Notify.Webhook, NotifyMinSeverity: cfg.Notify.MinSeverity, NotifySecret: cfg.Notify.Secret, } }