Skip to content

Commit ace7871

Browse files
Merge branch 'main' into update-automemlimit-version
2 parents bf4bd3f + 6e7d493 commit ace7871

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

notify/jira/jira.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ func (n *Notifier) prepareIssueRequestBody(ctx context.Context, logger *slog.Log
124124
if err != nil {
125125
return issue{}, fmt.Errorf("summary template: %w", err)
126126
}
127+
project, err := tmplTextFunc(n.conf.Project)
128+
if err != nil {
129+
return issue{}, fmt.Errorf("project template: %w", err)
130+
}
131+
issueType, err := tmplTextFunc(n.conf.IssueType)
132+
if err != nil {
133+
return issue{}, fmt.Errorf("issue_type template: %w", err)
134+
}
127135

128136
// Recursively convert any maps to map[string]interface{}, filtering out all non-string keys, so the json encoder
129137
// doesn't blow up when marshaling JIRA requests.
@@ -138,8 +146,8 @@ func (n *Notifier) prepareIssueRequestBody(ctx context.Context, logger *slog.Log
138146
}
139147

140148
requestBody := issue{Fields: &issueFields{
141-
Project: &issueProject{Key: n.conf.Project},
142-
Issuetype: &idNameValue{Name: n.conf.IssueType},
149+
Project: &issueProject{Key: project},
150+
Issuetype: &idNameValue{Name: issueType},
143151
Summary: summary,
144152
Labels: make([]string, 0, len(n.conf.Labels)+1),
145153
Fields: fieldsWithStringKeys,

notify/jira/jira_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,24 @@ func TestJiraTemplating(t *testing.T) {
9595
},
9696
retry: false,
9797
},
98+
{
99+
title: "template project",
100+
cfg: &config.JiraConfig{
101+
Project: `{{ .CommonLabels.lbl1 }}`,
102+
Summary: `{{ template "jira.default.summary" . }}`,
103+
Description: `{{ template "jira.default.description" . }}`,
104+
},
105+
retry: false,
106+
},
107+
{
108+
title: "template issue type",
109+
cfg: &config.JiraConfig{
110+
IssueType: `{{ .CommonLabels.lbl1 }}`,
111+
Summary: `{{ template "jira.default.summary" . }}`,
112+
Description: `{{ template "jira.default.description" . }}`,
113+
},
114+
retry: false,
115+
},
98116
{
99117
title: "summary with templating errors",
100118
cfg: &config.JiraConfig{
@@ -208,6 +226,51 @@ func TestJiraNotify(t *testing.T) {
208226
customFieldAssetFn: func(t *testing.T, issue map[string]any) {},
209227
errMsg: "",
210228
},
229+
{
230+
title: "create new issue with template project and issue type",
231+
cfg: &config.JiraConfig{
232+
Summary: `{{ template "jira.default.summary" . }}`,
233+
Description: `{{ template "jira.default.description" . }}`,
234+
IssueType: "{{ .CommonLabels.issue_type }}",
235+
Project: "{{ .CommonLabels.project }}",
236+
Priority: `{{ template "jira.default.priority" . }}`,
237+
Labels: []string{"alertmanager", "{{ .GroupLabels.alertname }}"},
238+
ReopenDuration: model.Duration(1 * time.Hour),
239+
ReopenTransition: "REOPEN",
240+
ResolveTransition: "CLOSE",
241+
WontFixResolution: "WONTFIX",
242+
},
243+
alert: &types.Alert{
244+
Alert: model.Alert{
245+
Labels: model.LabelSet{
246+
"alertname": "test",
247+
"instance": "vm1",
248+
"severity": "critical",
249+
"project": "MONITORING",
250+
"issue_type": "MINOR",
251+
},
252+
StartsAt: time.Now(),
253+
EndsAt: time.Now().Add(time.Hour),
254+
},
255+
},
256+
searchResponse: issueSearchResult{
257+
Total: 0,
258+
Issues: []issue{},
259+
},
260+
issue: issue{
261+
Key: "",
262+
Fields: &issueFields{
263+
Summary: "[FIRING:1] test (vm1 MINOR MONITORING critical)",
264+
Description: "\n\n# Alerts Firing:\n\nLabels:\n - alertname = test\n - instance = vm1\n - issue_type = MINOR\n - project = MONITORING\n - severity = critical\n\nAnnotations:\n\nSource: \n\n\n\n\n",
265+
Issuetype: &idNameValue{Name: "MINOR"},
266+
Labels: []string{"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}", "alertmanager", "test"},
267+
Project: &issueProject{Key: "MONITORING"},
268+
Priority: &idNameValue{Name: "High"},
269+
},
270+
},
271+
customFieldAssetFn: func(t *testing.T, issue map[string]any) {},
272+
errMsg: "",
273+
},
211274
{
212275
title: "create new issue with custom field and too long summary",
213276
cfg: &config.JiraConfig{

0 commit comments

Comments
 (0)