Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add priority support for opsgenie #329

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions 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
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
Loading