@@ -36,6 +36,10 @@ import (
3636 "github.com/prometheus/alertmanager/types"
3737)
3838
39+ func jiraStringDescription (v string ) * jiraDescription {
40+ return & jiraDescription {StringDescription : stringPtr (v )}
41+ }
42+
3943func stringPtr (v string ) * string {
4044 return & v
4145}
@@ -504,7 +508,7 @@ func TestJiraNotify(t *testing.T) {
504508 Key : "" ,
505509 Fields : & issueFields {
506510 Summary : stringPtr ("[FIRING:1] test (vm1 critical)" ),
507- Description : stringPtr ("\n \n # Alerts Firing:\n \n Labels:\n - alertname = test\n - instance = vm1\n - severity = critical\n \n Annotations:\n \n Source: \n \n \n \n \n " ),
511+ Description : jiraStringDescription ("\n \n # Alerts Firing:\n \n Labels:\n - alertname = test\n - instance = vm1\n - severity = critical\n \n Annotations:\n \n Source: \n \n \n \n \n " ),
508512 Issuetype : & idNameValue {Name : "Incident" },
509513 Labels : []string {"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}" , "alertmanager" , "test" },
510514 Project : & issueProject {Key : "OPS" },
@@ -553,7 +557,7 @@ func TestJiraNotify(t *testing.T) {
553557 Key : "MONITORING-1" ,
554558 Fields : & issueFields {
555559 Summary : stringPtr ("Original Summary" ),
556- Description : stringPtr ("Original Description" ),
560+ Description : jiraStringDescription ("Original Description" ),
557561 Status : & issueStatus {
558562 Name : "Open" ,
559563 StatusCategory : struct {
@@ -619,7 +623,7 @@ func TestJiraNotify(t *testing.T) {
619623 Key : "" ,
620624 Fields : & issueFields {
621625 Summary : stringPtr ("[FIRING:1] test (vm1 MINOR MONITORING critical)" ),
622- Description : stringPtr ("\n \n # Alerts Firing:\n \n Labels:\n - alertname = test\n - instance = vm1\n - issue_type = MINOR\n - project = MONITORING\n - severity = critical\n \n Annotations:\n \n Source: \n \n \n \n \n " ),
626+ Description : jiraStringDescription ("\n \n # Alerts Firing:\n \n Labels:\n - alertname = test\n - instance = vm1\n - issue_type = MINOR\n - project = MONITORING\n - severity = critical\n \n Annotations:\n \n Source: \n \n \n \n \n " ),
623627 Issuetype : & idNameValue {Name : "MINOR" },
624628 Labels : []string {"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}" , "alertmanager" , "test" },
625629 Project : & issueProject {Key : "MONITORING" },
@@ -671,7 +675,7 @@ func TestJiraNotify(t *testing.T) {
671675 Key : "" ,
672676 Fields : & issueFields {
673677 Summary : stringPtr (strings .Repeat ("A" , maxSummaryLenRunes - 1 ) + "…" ),
674- Description : stringPtr ("\n \n # Alerts Firing:\n \n Labels:\n - alertname = test\n - instance = vm1\n \n Annotations:\n \n Source: \n \n \n \n \n " ),
678+ Description : jiraStringDescription ("\n \n # Alerts Firing:\n \n Labels:\n - alertname = test\n - instance = vm1\n \n Annotations:\n \n Source: \n \n \n \n \n " ),
675679 Issuetype : & idNameValue {Name : "Incident" },
676680 Labels : []string {"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}" , "alertmanager" , "test" },
677681 Project : & issueProject {Key : "OPS" },
@@ -734,7 +738,7 @@ func TestJiraNotify(t *testing.T) {
734738 Key : "" ,
735739 Fields : & issueFields {
736740 Summary : stringPtr ("[FIRING:1] test (vm1)" ),
737- Description : stringPtr ("\n \n # Alerts Firing:\n \n Labels:\n - alertname = test\n - instance = vm1\n \n Annotations:\n \n Source: \n \n \n \n \n " ),
741+ Description : jiraStringDescription ("\n \n # Alerts Firing:\n \n Labels:\n - alertname = test\n - instance = vm1\n \n Annotations:\n \n Source: \n \n \n \n \n " ),
738742 Issuetype : & idNameValue {Name : "Incident" },
739743 Labels : []string {"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}" , "alertmanager" , "test" },
740744 Project : & issueProject {Key : "OPS" },
@@ -789,7 +793,7 @@ func TestJiraNotify(t *testing.T) {
789793 Key : "" ,
790794 Fields : & issueFields {
791795 Summary : stringPtr ("[RESOLVED] test (vm1)" ),
792- Description : stringPtr ("\n \n \n # Alerts Resolved:\n \n Labels:\n - alertname = test\n - instance = vm1\n \n Annotations:\n \n Source: \n \n \n \n " ),
796+ Description : jiraStringDescription ("\n \n \n # Alerts Resolved:\n \n Labels:\n - alertname = test\n - instance = vm1\n \n Annotations:\n \n Source: \n \n \n \n " ),
793797 Issuetype : & idNameValue {Name : "Incident" },
794798 Labels : []string {"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}" , "alertmanager" , "test" },
795799 Project : & issueProject {Key : "OPS" },
@@ -843,7 +847,7 @@ func TestJiraNotify(t *testing.T) {
843847 Key : "" ,
844848 Fields : & issueFields {
845849 Summary : stringPtr ("[FIRING:1] test (vm1)" ),
846- Description : stringPtr ("\n \n # Alerts Firing:\n \n Labels:\n - alertname = test\n - instance = vm1\n \n Annotations:\n \n Source: \n \n \n \n \n " ),
850+ Description : jiraStringDescription ("\n \n # Alerts Firing:\n \n Labels:\n - alertname = test\n - instance = vm1\n \n Annotations:\n \n Source: \n \n \n \n \n " ),
847851 Issuetype : & idNameValue {Name : "Incident" },
848852 Labels : []string {"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}" , "alertmanager" , "test" },
849853 Project : & issueProject {Key : "OPS" },
@@ -1236,3 +1240,83 @@ func TestJiraPriority(t *testing.T) {
12361240 })
12371241 }
12381242}
1243+
1244+ func TestPrepareIssueRequestBodyAPIv3DescriptionValidation (t * testing.T ) {
1245+ for _ , tc := range []struct {
1246+ name string
1247+ descriptionTemplate string
1248+ expectErrSubstring string
1249+ }{
1250+ {
1251+ name : "valid JSON description" ,
1252+ descriptionTemplate : `{"type":"doc","version":1,"content":[{"type":"paragraph","content":[{"type":"text","text":"hello"}]}]}` ,
1253+ },
1254+ {
1255+ name : "invalid JSON description" ,
1256+ descriptionTemplate : `not-json` ,
1257+ expectErrSubstring : "invalid JSON for API v3" ,
1258+ },
1259+ } {
1260+ t .Run (tc .name , func (t * testing.T ) {
1261+ cfg := & config.JiraConfig {
1262+ Summary : config.JiraFieldConfig {Template : `{{ template "jira.default.summary" . }}` },
1263+ Description : config.JiraFieldConfig {Template : tc .descriptionTemplate },
1264+ IssueType : "Incident" ,
1265+ Project : "OPS" ,
1266+ Labels : []string {"alertmanager" },
1267+ Priority : `{{ template "jira.default.priority" . }}` ,
1268+ APIURL : & config.URL {
1269+ URL : & url.URL {
1270+ Scheme : "https" ,
1271+ Host : "example.atlassian.net" ,
1272+ Path : "/rest/api/3" ,
1273+ },
1274+ },
1275+ HTTPConfig : & commoncfg.HTTPClientConfig {},
1276+ }
1277+
1278+ notifier , err := New (cfg , test .CreateTmpl (t ), promslog .NewNopLogger ())
1279+ require .NoError (t , err )
1280+
1281+ alert := & types.Alert {
1282+ Alert : model.Alert {
1283+ Labels : model.LabelSet {
1284+ "alertname" : "test" ,
1285+ "instance" : "vm1" ,
1286+ "severity" : "critical" ,
1287+ },
1288+ StartsAt : time .Now (),
1289+ EndsAt : time .Now ().Add (time .Hour ),
1290+ },
1291+ }
1292+
1293+ ctx := context .Background ()
1294+ groupID := "1"
1295+ ctx = notify .WithGroupKey (ctx , groupID )
1296+ ctx = notify .WithGroupLabels (ctx , alert .Labels )
1297+
1298+ alerts := []* types.Alert {alert }
1299+ logger := notifier .logger .With ("group_key" , groupID )
1300+ data := notify .GetTemplateData (ctx , notifier .tmpl , alerts , logger )
1301+
1302+ var tmplErr error
1303+ tmplText := notify .TmplText (notifier .tmpl , data , & tmplErr )
1304+ tmplTextFunc := func (tmpl string ) (string , error ) {
1305+ return tmplText (tmpl ), tmplErr
1306+ }
1307+
1308+ issue , err := notifier .prepareIssueRequestBody (ctx , logger , groupID , tmplTextFunc )
1309+ if tc .expectErrSubstring != "" {
1310+ require .Error (t , err )
1311+ require .ErrorContains (t , err , tc .expectErrSubstring )
1312+ return
1313+ }
1314+
1315+ require .NoError (t , err )
1316+ require .NotNil (t , issue .Fields )
1317+
1318+ require .NotNil (t , issue .Fields .Description )
1319+ require .JSONEq (t , tc .descriptionTemplate , string (issue .Fields .Description .RawJSONDescription ))
1320+ })
1321+ }
1322+ }
0 commit comments