Skip to content

Commit

Permalink
add priority support for opsgenie
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Tang <[email protected]>
  • Loading branch information
patrick-tang committed Aug 22, 2024
1 parent 261728a commit 492241c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/services/opsgenie.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"net/http"
"strings"
texttemplate "text/template"

"github.com/opsgenie/opsgenie-go-sdk-v2/alert"
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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")

Check warning on line 103 in pkg/services/opsgenie.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/opsgenie.go#L103

Added line #L103 was not covered by tests
}

description = notification.Opsgenie.Description
Expand Down
33 changes: 33 additions & 0 deletions pkg/services/opsgenie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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,
}
Expand All @@ -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)

Expand All @@ -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{
Expand Down Expand Up @@ -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}}"

Expand All @@ -141,6 +173,7 @@ func TestOpsgenie_SendNotification_MissingAPIKey(t *testing.T) {
Message: message,
Opsgenie: &OpsgenieNotification{
Description: descriptionTemplate,
Priority: priority,
Alias: aliasTemplate,
Note: noteTemplate,
},
Expand Down

0 comments on commit 492241c

Please sign in to comment.