Files
grafana/pkg/services/ngalert/api/tooling/definitions/alertmanager_validation.go
T
Matthew Jacobson 9e933882ed Alerting: Webhook Improvements - Templateable Payloads (#103818)
* Template editor syntax highlighting when preview is json-like

* Add new template editor language examples, snippets, and functions

* Use updated NewTemplate function

* Add new fields to webhook notifier

- CustomPayload
- ExtraHeaders

* Documentation

* Update grafana/alerting to in-progress PR (needs updating after merge)

* Fix integration test

* Remove docs reference to .Extra template context

No longer exists, was part of a previous iteration

* make update-workspace

* Update grafana/alerting to actual merged commit
2025-04-11 12:22:51 -04:00

58 lines
1.3 KiB
Go

package definitions
import (
"fmt"
"regexp"
"strings"
"github.com/grafana/alerting/templates"
"gopkg.in/yaml.v3"
)
func (t *NotificationTemplate) Validate() error {
if t.Name == "" {
return fmt.Errorf("template must have a name")
}
if t.Template == "" {
return fmt.Errorf("template must have content")
}
content := strings.TrimSpace(t.Template)
found, err := regexp.MatchString(`\{\{\s*define`, content)
if err != nil {
return fmt.Errorf("failed to match regex: %w", err)
}
if !found {
lines := strings.Split(content, "\n")
for i, s := range lines {
lines[i] = " " + s
}
content = strings.Join(lines, "\n")
content = fmt.Sprintf("{{ define \"%s\" }}\n%s\n{{ end }}", t.Name, content)
}
t.Template = content
// Validate template contents. We try to stick as close to what will actually happen when the templates are parsed
// by the alertmanager as possible.
tmpl, err := templates.NewTemplate()
if err != nil {
return fmt.Errorf("failed to create template: %w", err)
}
if err := tmpl.Parse(strings.NewReader(t.Template)); err != nil {
return fmt.Errorf("invalid template: %w", err)
}
return nil
}
func (mt *MuteTimeInterval) Validate() error {
s, err := yaml.Marshal(mt.MuteTimeInterval)
if err != nil {
return err
}
if err = yaml.Unmarshal(s, &(mt.MuteTimeInterval)); err != nil {
return err
}
return nil
}