From 72f55136485eb0855bc4256ab046cb48591cc0ab Mon Sep 17 00:00:00 2001 From: Esat Akyol Date: Mon, 19 Feb 2024 13:39:23 +0300 Subject: [PATCH] feat(opsgenie): Add support for setting note in Opsgenie notification Signed-off-by: Esat Akyol --- docs/services/opsgenie.md | 2 ++ pkg/services/opsgenie.go | 21 ++++++++++++++++++--- pkg/services/opsgenie_test.go | 22 ++++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/docs/services/opsgenie.md b/docs/services/opsgenie.md index afd342ad..2cc1ebff 100644 --- a/docs/services/opsgenie.md +++ b/docs/services/opsgenie.md @@ -23,6 +23,7 @@ To be able to send notifications with argocd-notifications you have to create an | `description` | True | `string` | Description field of the alert that is generally used to provide a detailed information about the alert. | `Hello from Argo CD!` | | `priority` | False | `string` | Priority level of the alert. Possible values are P1, P2, P3, P4 and P5. Default value is P3. | `P1` | | `alias` | False | `string` | Client-defined identifier of the alert, that is also the key element of Alert De-Duplication. | `Life is too short for no alias` | +| `note` | False | `string` | Additional note that will be added while creating the alert. | `Error from Argo CD!` | ```yaml apiVersion: v1 @@ -45,6 +46,7 @@ data: Sync Status: {{.app.status.sync.status}} priority: P1 alias: {{.app.metadata.name}} + note: Error from Argo CD! trigger.on-a-problem: | - description: Application has a problem. send: diff --git a/pkg/services/opsgenie.go b/pkg/services/opsgenie.go index 72eee328..e46b4b73 100644 --- a/pkg/services/opsgenie.go +++ b/pkg/services/opsgenie.go @@ -23,6 +23,7 @@ type OpsgenieNotification struct { Description string `json:"description"` Priority string `json:"priority,omitempty"` Alias string `json:"alias,omitempty"` + Note string `json:"note,omitempty"` } func (n *OpsgenieNotification) GetTemplater(name string, f texttemplate.FuncMap) (Templater, error) { @@ -34,6 +35,10 @@ func (n *OpsgenieNotification) GetTemplater(name string, f texttemplate.FuncMap) if err != nil { return nil, err } + note, err := texttemplate.New(name).Funcs(f).Parse(n.Note) + if err != nil { + return nil, err + } return func(notification *Notification, vars map[string]interface{}) error { if notification.Opsgenie == nil { notification.Opsgenie = &OpsgenieNotification{} @@ -48,6 +53,11 @@ func (n *OpsgenieNotification) GetTemplater(name string, f texttemplate.FuncMap) return err } notification.Opsgenie.Alias = aliasData.String() + var noteData bytes.Buffer + if err := note.Execute(¬eData, vars); err != nil { + return err + } + notification.Opsgenie.Note = noteData.String() return nil }, nil } @@ -73,9 +83,9 @@ func (s *opsgenieService) Send(notification Notification, dest Destination) erro httputil.NewTransport(s.opts.ApiUrl, false), log.WithField("service", "opsgenie")), }, }) - description := "" - priority := "" - alias := "" + + var description, priority, alias, note string + if notification.Opsgenie != nil { if notification.Opsgenie.Description == "" { return fmt.Errorf("Opsgenie notification description is missing") @@ -90,6 +100,10 @@ func (s *opsgenieService) Send(notification Notification, dest Destination) erro if notification.Opsgenie.Alias != "" { alias = notification.Opsgenie.Alias } + + if notification.Opsgenie.Note != "" { + note = notification.Opsgenie.Note + } } alertPriority := alert.Priority(priority) @@ -99,6 +113,7 @@ func (s *opsgenieService) Send(notification Notification, dest Destination) erro Description: description, Priority: alertPriority, Alias: alias, + Note: note, Responders: []alert.Responder{ { Type: "team", diff --git a/pkg/services/opsgenie_test.go b/pkg/services/opsgenie_test.go index 6f40304c..034aabe0 100644 --- a/pkg/services/opsgenie_test.go +++ b/pkg/services/opsgenie_test.go @@ -39,6 +39,7 @@ func TestOpsgenieNotification_GetTemplater(t *testing.T) { name := "testTemplate" descriptionTemplate := "Test Opsgenie alert: {{.foo}}" aliasTemplate := "Test alias: {{.foo}}" + noteTemplate := "Test note: {{.foo}}" f := texttemplate.FuncMap{} t.Run("ValidTemplate", func(t *testing.T) { @@ -46,6 +47,7 @@ func TestOpsgenieNotification_GetTemplater(t *testing.T) { notification := OpsgenieNotification{ Description: descriptionTemplate, Alias: aliasTemplate, + Note: noteTemplate, } // Call the GetTemplater method @@ -71,6 +73,9 @@ func TestOpsgenieNotification_GetTemplater(t *testing.T) { // Assert that the OpsgenieNotification's alias field was correctly updated assert.Equal(t, "Test alias: bar", mockNotification.Opsgenie.Alias) + + // Assert that the OpsgenieNotification's note field was correctly updated + assert.Equal(t, "Test note: bar", mockNotification.Opsgenie.Note) }) t.Run("InvalidTemplateDescription", func(t *testing.T) { @@ -98,6 +103,19 @@ func TestOpsgenieNotification_GetTemplater(t *testing.T) { // Assert that an error occurred during the call assert.Error(t, err) }) + + t.Run("InvalidTemplateNote", func(t *testing.T) { + // Create a new OpsgenieNotification instance with an invalid note template + notification := OpsgenieNotification{ + Note: "{{.invalid", // Invalid template syntax + } + + // Call the GetTemplater method with the invalid template + _, err := notification.GetTemplater(name, f) + + // Assert that an error occurred during the call + assert.Error(t, err) + }) } func TestOpsgenie_SendNotification_MissingAPIKey(t *testing.T) { @@ -116,6 +134,7 @@ func TestOpsgenie_SendNotification_MissingAPIKey(t *testing.T) { message := "Test message" descriptionTemplate := "Test Opsgenie alert: {{.foo}}" aliasTemplate := "Test alias: {{.foo}}" + noteTemplate := "Test note: {{.foo}}" // Create test notification with description notification := Notification{ @@ -123,6 +142,7 @@ func TestOpsgenie_SendNotification_MissingAPIKey(t *testing.T) { Opsgenie: &OpsgenieNotification{ Description: descriptionTemplate, Alias: aliasTemplate, + Note: noteTemplate, }, } @@ -267,6 +287,7 @@ func TestOpsgenie_SendNotification_WithAllFields(t *testing.T) { descriptionTemplate := "Test Opsgenie alert: {{.foo}}" aliasTemplate := "Test alias: {{.foo}}" priority := "P1" + noteTemplate := "Test note: {{.foo}}" // Create test notification with description and priority notification := Notification{ @@ -275,6 +296,7 @@ func TestOpsgenie_SendNotification_WithAllFields(t *testing.T) { Description: descriptionTemplate, Priority: priority, Alias: aliasTemplate, + Note: noteTemplate, }, }