Skip to content

Commit 1864d6c

Browse files
add unit tests for templates within customfields. add new template jira.host to default.tmpl
Signed-off-by: Holger Waschke <[email protected]>
1 parent 08a3da0 commit 1864d6c

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

asset/assets_vfsdata.go

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

notify/jira/jira_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ func TestJiraTemplating(t *testing.T) {
9393
cfg: &config.JiraConfig{
9494
Summary: `{{ template "jira.default.summary" . }}`,
9595
Description: `{{ template "jira.default.description" . }}`,
96+
Fields: map[string]any{
97+
"customfield_14400": `{{ template "jira.host" . }}`,
98+
},
9699
},
97100
retry: false,
98101
},
@@ -228,6 +231,7 @@ func TestJiraNotify(t *testing.T) {
228231
"customfield_10006": []map[any]any{{"value": "red"}, {"value": "blue"}, {"value": "green"}},
229232
"customfield_10007": []map[any]any{{"value": "red"}, {"value": "blue"}, {"value": 0}},
230233
"customfield_10008": []map[any]any{{"value": 0}, {"value": 1}, {"value": 2}},
234+
"customfield_14400": `{{ template "jira.host" . }}`,
231235
},
232236
ReopenDuration: model.Duration(1 * time.Hour),
233237
ReopenTransition: "REOPEN",
@@ -239,6 +243,7 @@ func TestJiraNotify(t *testing.T) {
239243
Labels: model.LabelSet{
240244
"alertname": "test",
241245
"instance": "vm1",
246+
"hostname": "host1.exmple.com",
242247
},
243248
StartsAt: time.Now(),
244249
EndsAt: time.Now().Add(time.Hour),
@@ -252,14 +257,15 @@ func TestJiraNotify(t *testing.T) {
252257
Key: "",
253258
Fields: &issueFields{
254259
Summary: strings.Repeat("A", maxSummaryLenRunes-1) + "…",
255-
Description: "\n\n# Alerts Firing:\n\nLabels:\n - alertname = test\n - instance = vm1\n\nAnnotations:\n\nSource: \n\n\n\n\n",
260+
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",
256261
Issuetype: &idNameValue{Name: "Incident"},
257262
Labels: []string{"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}", "alertmanager", "test"},
258263
Project: &issueProject{Key: "OPS"},
259264
},
260265
},
261266
customFieldAssetFn: func(t *testing.T, issue map[string]any) {
262267
require.Equal(t, "value", issue["customfield_10001"])
268+
require.Equal(t, "host1.exmple.com", issue["customfield_14400"])
263269
require.Equal(t, float64(0), issue["customfield_10002"])
264270
require.Equal(t, []any{float64(0)}, issue["customfield_10003"])
265271
require.Equal(t, map[string]any{"value": "red"}, issue["customfield_10004"])

template/default.tmpl

+8
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,11 @@ Alerts Resolved:
197197
{{- end -}}
198198
{{- $priority -}}
199199
{{- end -}}
200+
201+
{{ define "jira.host" }}
202+
{{- with index .Alerts 0 -}}
203+
{{- if .Labels.hostname -}}
204+
{{- .Labels.hostname -}}
205+
{{- end -}}
206+
{{- end -}}
207+
{{ end }}

0 commit comments

Comments
 (0)