@@ -318,6 +318,8 @@ func TestPrepareSearchRequest(t *testing.T) {
318318}
319319
320320func TestJiraTemplating (t * testing.T ) {
321+ var capturedBody map [string ]any
322+
321323 srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
322324 switch r .URL .Path {
323325 case "/search" :
@@ -326,10 +328,10 @@ func TestJiraTemplating(t *testing.T) {
326328 default :
327329 dec := json .NewDecoder (r .Body )
328330 out := make (map [string ]any )
329- err := dec .Decode (& out )
330- if err != nil {
331+ if err := dec .Decode (& out ); err != nil {
331332 panic (err )
332333 }
334+ capturedBody = out
333335 }
334336 }))
335337 defer srv .Close ()
@@ -339,16 +341,23 @@ func TestJiraTemplating(t *testing.T) {
339341 title string
340342 cfg * config.JiraConfig
341343
342- retry bool
343- errMsg string
344+ retry bool
345+ errMsg string
346+ expectedFieldKey string
347+ expectedFieldValue any
344348 }{
345349 {
346- title : "full-blown message" ,
350+ title : "full-blown message with templated custom field " ,
347351 cfg : & config.JiraConfig {
348352 Summary : `{{ template "jira.default.summary" . }}` ,
349353 Description : `{{ template "jira.default.description" . }}` ,
354+ Fields : map [string ]any {
355+ "customfield_14400" : `{{ template "jira.host" . }}` ,
356+ },
350357 },
351- retry : false ,
358+ retry : false ,
359+ expectedFieldKey : "customfield_14400" ,
360+ expectedFieldValue : "host1.example.com" ,
352361 },
353362 {
354363 title : "template project" ,
@@ -396,19 +405,32 @@ func TestJiraTemplating(t *testing.T) {
396405 tc := tc
397406
398407 t .Run (tc .title , func (t * testing.T ) {
408+ capturedBody = nil
409+
399410 tc .cfg .APIURL = & config.URL {URL : u }
400411 tc .cfg .HTTPConfig = & commoncfg.HTTPClientConfig {}
401412 pd , err := New (tc .cfg , test .CreateTmpl (t ), promslog .NewNopLogger ())
402413 require .NoError (t , err )
403414
415+ // Add the jira.host template just for this test
416+ if tc .expectedFieldKey == "customfield_14400" {
417+ err = pd .tmpl .Parse (strings .NewReader (`{{ define "jira.host" }}{{ .CommonLabels.hostname }}{{ end }}` ))
418+ require .NoError (t , err )
419+ }
420+
404421 ctx := context .Background ()
405422 ctx = notify .WithGroupKey (ctx , "1" )
423+ ctx = notify .WithGroupLabels (ctx , model.LabelSet {
424+ "lbl1" : "val1" ,
425+ "hostname" : "host1.example.com" ,
426+ })
406427
407428 ok , err := pd .Notify (ctx , []* types.Alert {
408429 {
409430 Alert : model.Alert {
410431 Labels : model.LabelSet {
411- "lbl1" : "val1" ,
432+ "lbl1" : "val1" ,
433+ "hostname" : "host1.example.com" ,
412434 },
413435 StartsAt : time .Now (),
414436 EndsAt : time .Now ().Add (time .Hour ),
@@ -422,6 +444,14 @@ func TestJiraTemplating(t *testing.T) {
422444 require .Contains (t , err .Error (), tc .errMsg )
423445 }
424446 require .Equal (t , tc .retry , ok )
447+
448+ // Verify that custom fields were templated correctly
449+ if tc .expectedFieldKey != "" {
450+ require .NotNil (t , capturedBody , "expected request body" )
451+ fields , ok := capturedBody ["fields" ].(map [string ]any )
452+ require .True (t , ok , "fields should be a map" )
453+ require .Equal (t , tc .expectedFieldValue , fields [tc .expectedFieldKey ])
454+ }
425455 })
426456 }
427457}
0 commit comments