From 178cd80cbb2ff37ce7e81aac954a1b94abc86bbb Mon Sep 17 00:00:00 2001 From: Patrick Tang Date: Wed, 21 Aug 2024 19:33:49 -0400 Subject: [PATCH] add priority support for opsgenie Signed-off-by: Patrick Tang --- pkg/services/opsgenie.go | 14 +++++++++++++- pkg/services/opsgenie_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/pkg/services/opsgenie.go b/pkg/services/opsgenie.go index e46b4b73..9f22fb8b 100644 --- a/pkg/services/opsgenie.go +++ b/pkg/services/opsgenie.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "net/http" + "strings" texttemplate "text/template" "github.com/opsgenie/opsgenie-go-sdk-v2/alert" @@ -39,10 +40,18 @@ func (n *OpsgenieNotification) GetTemplater(name string, f texttemplate.FuncMap) if err != nil { return nil, err } + + priority := strings.ToUpper(n.Priority) + + if err := alert.ValidatePriority(alert.Priority(priority)); err != nil { + return nil, err + } + return func(notification *Notification, vars map[string]interface{}) error { if notification.Opsgenie == nil { notification.Opsgenie = &OpsgenieNotification{} } + var descData bytes.Buffer if err := desc.Execute(&descData, vars); err != nil { return err @@ -58,6 +67,9 @@ func (n *OpsgenieNotification) GetTemplater(name string, f texttemplate.FuncMap) return err } notification.Opsgenie.Note = noteData.String() + + notification.Opsgenie.Priority = priority + return nil }, nil } @@ -88,7 +100,7 @@ func (s *opsgenieService) Send(notification Notification, dest Destination) erro if notification.Opsgenie != nil { if notification.Opsgenie.Description == "" { - return fmt.Errorf("Opsgenie notification description is missing") + return fmt.Errorf("opsgenie notification description is missing") } description = notification.Opsgenie.Description diff --git a/pkg/services/opsgenie_test.go b/pkg/services/opsgenie_test.go index 034aabe0..216ad108 100644 --- a/pkg/services/opsgenie_test.go +++ b/pkg/services/opsgenie_test.go @@ -38,6 +38,7 @@ func TestOpsgenieNotification_GetTemplater(t *testing.T) { // Prepare test data name := "testTemplate" descriptionTemplate := "Test Opsgenie alert: {{.foo}}" + priority := "P1" aliasTemplate := "Test alias: {{.foo}}" noteTemplate := "Test note: {{.foo}}" f := texttemplate.FuncMap{} @@ -46,6 +47,7 @@ func TestOpsgenieNotification_GetTemplater(t *testing.T) { // Create a new OpsgenieNotification instance notification := OpsgenieNotification{ Description: descriptionTemplate, + Priority: priority, Alias: aliasTemplate, Note: noteTemplate, } @@ -71,6 +73,9 @@ func TestOpsgenieNotification_GetTemplater(t *testing.T) { // Assert that the OpsgenieNotification's description field was correctly updated assert.Equal(t, "Test Opsgenie alert: bar", mockNotification.Opsgenie.Description) + // Assert that the OpsgenieNotification's priority field was correctly updated + assert.Equal(t, "P1", mockNotification.Opsgenie.Priority) + // Assert that the OpsgenieNotification's alias field was correctly updated assert.Equal(t, "Test alias: bar", mockNotification.Opsgenie.Alias) @@ -91,6 +96,32 @@ func TestOpsgenieNotification_GetTemplater(t *testing.T) { assert.Error(t, err) }) + t.Run("InvalidPriorityP0", func(t *testing.T) { + // Create a new OpsgenieNotification instance with an invalid priority value + notification := OpsgenieNotification{ + Priority: "P0", // Invalid priority value + } + + // 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) + }) + + t.Run("InvalidPriorityP8", func(t *testing.T) { + // Create a new OpsgenieNotification instance with an invalid priority value + notification := OpsgenieNotification{ + Priority: "P8", // Invalid priority value + } + + // 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) + }) + t.Run("InvalidTemplateAlias", func(t *testing.T) { // Create a new OpsgenieNotification instance with an invalid alias template notification := OpsgenieNotification{ @@ -133,6 +164,7 @@ func TestOpsgenie_SendNotification_MissingAPIKey(t *testing.T) { recipient := "testRecipient" message := "Test message" descriptionTemplate := "Test Opsgenie alert: {{.foo}}" + priority := "P1" aliasTemplate := "Test alias: {{.foo}}" noteTemplate := "Test note: {{.foo}}" @@ -141,6 +173,7 @@ func TestOpsgenie_SendNotification_MissingAPIKey(t *testing.T) { Message: message, Opsgenie: &OpsgenieNotification{ Description: descriptionTemplate, + Priority: priority, Alias: aliasTemplate, Note: noteTemplate, },