Skip to content

Commit

Permalink
add unit tests for templates within customfields. add new template ji…
Browse files Browse the repository at this point in the history
…ra.host to default.tmpl

Signed-off-by: Holger Waschke <[email protected]>
  • Loading branch information
holger-waschke committed Oct 7, 2024
1 parent 08a3da0 commit 1864d6c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
5 changes: 2 additions & 3 deletions asset/assets_vfsdata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion notify/jira/jira_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ func TestJiraTemplating(t *testing.T) {
cfg: &config.JiraConfig{
Summary: `{{ template "jira.default.summary" . }}`,
Description: `{{ template "jira.default.description" . }}`,
Fields: map[string]any{
"customfield_14400": `{{ template "jira.host" . }}`,
},
},
retry: false,
},
Expand Down Expand Up @@ -228,6 +231,7 @@ func TestJiraNotify(t *testing.T) {
"customfield_10006": []map[any]any{{"value": "red"}, {"value": "blue"}, {"value": "green"}},
"customfield_10007": []map[any]any{{"value": "red"}, {"value": "blue"}, {"value": 0}},
"customfield_10008": []map[any]any{{"value": 0}, {"value": 1}, {"value": 2}},
"customfield_14400": `{{ template "jira.host" . }}`,
},
ReopenDuration: model.Duration(1 * time.Hour),
ReopenTransition: "REOPEN",
Expand All @@ -239,6 +243,7 @@ func TestJiraNotify(t *testing.T) {
Labels: model.LabelSet{
"alertname": "test",
"instance": "vm1",
"hostname": "host1.exmple.com",
},
StartsAt: time.Now(),
EndsAt: time.Now().Add(time.Hour),
Expand All @@ -252,14 +257,15 @@ func TestJiraNotify(t *testing.T) {
Key: "",
Fields: &issueFields{
Summary: strings.Repeat("A", maxSummaryLenRunes-1) + "…",
Description: "\n\n# Alerts Firing:\n\nLabels:\n - alertname = test\n - instance = vm1\n\nAnnotations:\n\nSource: \n\n\n\n\n",
Description: "\n\n# Alerts Firing:\n\nLabels:\n - alertname = test\n - hostname = host1.exmple.com\n - instance = vm1\n\nAnnotations:\n\nSource: \n\n\n\n\n",
Issuetype: &idNameValue{Name: "Incident"},
Labels: []string{"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}", "alertmanager", "test"},
Project: &issueProject{Key: "OPS"},
},
},
customFieldAssetFn: func(t *testing.T, issue map[string]any) {
require.Equal(t, "value", issue["customfield_10001"])
require.Equal(t, "host1.exmple.com", issue["customfield_14400"])
require.Equal(t, float64(0), issue["customfield_10002"])
require.Equal(t, []any{float64(0)}, issue["customfield_10003"])
require.Equal(t, map[string]any{"value": "red"}, issue["customfield_10004"])
Expand Down
8 changes: 8 additions & 0 deletions template/default.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,11 @@ Alerts Resolved:
{{- end -}}
{{- $priority -}}
{{- end -}}

{{ define "jira.host" }}
{{- with index .Alerts 0 -}}
{{- if .Labels.hostname -}}
{{- .Labels.hostname -}}
{{- end -}}
{{- end -}}
{{ end }}

0 comments on commit 1864d6c

Please sign in to comment.