diff --git a/pkg/services/opsgenie.go b/pkg/services/opsgenie.go index e053c0ef..72eee328 100644 --- a/pkg/services/opsgenie.go +++ b/pkg/services/opsgenie.go @@ -22,6 +22,7 @@ type OpsgenieOptions struct { type OpsgenieNotification struct { Description string `json:"description"` Priority string `json:"priority,omitempty"` + Alias string `json:"alias,omitempty"` } func (n *OpsgenieNotification) GetTemplater(name string, f texttemplate.FuncMap) (Templater, error) { @@ -29,6 +30,10 @@ func (n *OpsgenieNotification) GetTemplater(name string, f texttemplate.FuncMap) if err != nil { return nil, err } + alias, err := texttemplate.New(name).Funcs(f).Parse(n.Alias) + if err != nil { + return nil, err + } return func(notification *Notification, vars map[string]interface{}) error { if notification.Opsgenie == nil { notification.Opsgenie = &OpsgenieNotification{} @@ -38,6 +43,11 @@ func (n *OpsgenieNotification) GetTemplater(name string, f texttemplate.FuncMap) return err } notification.Opsgenie.Description = descData.String() + var aliasData bytes.Buffer + if err := alias.Execute(&aliasData, vars); err != nil { + return err + } + notification.Opsgenie.Alias = aliasData.String() return nil }, nil } @@ -65,6 +75,7 @@ func (s *opsgenieService) Send(notification Notification, dest Destination) erro }) description := "" priority := "" + alias := "" if notification.Opsgenie != nil { if notification.Opsgenie.Description == "" { return fmt.Errorf("Opsgenie notification description is missing") @@ -75,6 +86,10 @@ func (s *opsgenieService) Send(notification Notification, dest Destination) erro if notification.Opsgenie.Priority != "" { priority = notification.Opsgenie.Priority } + + if notification.Opsgenie.Alias != "" { + alias = notification.Opsgenie.Alias + } } alertPriority := alert.Priority(priority) @@ -83,6 +98,7 @@ func (s *opsgenieService) Send(notification Notification, dest Destination) erro Message: notification.Message, Description: description, Priority: alertPriority, + Alias: alias, Responders: []alert.Responder{ { Type: "team", diff --git a/pkg/services/opsgenie_test.go b/pkg/services/opsgenie_test.go index a19badf8..34410d4d 100644 --- a/pkg/services/opsgenie_test.go +++ b/pkg/services/opsgenie_test.go @@ -38,12 +38,14 @@ func TestOpsgenieNotification_GetTemplater(t *testing.T) { // Prepare test data name := "testTemplate" descriptionTemplate := "Test Opsgenie alert: {{.foo}}" + aliasTemplate := "Test alias: {{.foo}}" f := texttemplate.FuncMap{} t.Run("ValidTemplate", func(t *testing.T) { // Create a new OpsgenieNotification instance notification := OpsgenieNotification{ Description: descriptionTemplate, + Alias: aliasTemplate, } // Call the GetTemplater method @@ -66,12 +68,16 @@ 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 alias field was correctly updated + assert.Equal(t, "Test alias: bar", mockNotification.Opsgenie.Alias) }) t.Run("InvalidTemplate", func(t *testing.T) { // Create a new OpsgenieNotification instance with an invalid description template notification := OpsgenieNotification{ Description: "{{.invalid", // Invalid template syntax + Alias: "{{.invalid", } // Call the GetTemplater method with the invalid template @@ -101,12 +107,14 @@ func TestOpsgenie_SendNotification_MissingAPIKey(t *testing.T) { recipient := "testRecipient" message := "Test message" descriptionTemplate := "Test Opsgenie alert: {{.foo}}" + aliasTemplate := "Test alias: {{.foo}}" // Create test notification with description notification := Notification{ Message: message, Opsgenie: &OpsgenieNotification{ Description: descriptionTemplate, + Alias: aliasTemplate, }, } @@ -117,7 +125,7 @@ func TestOpsgenie_SendNotification_MissingAPIKey(t *testing.T) { assert.Error(t, err) assert.Contains(t, err.Error(), "No API key configured for recipient") } -func TestOpsgenie_SendNotification_MissingDescriptionAndPriority(t *testing.T) { +func TestOpsgenie_SendNotification_WithMessageOnly(t *testing.T) { // Create a mock HTTP server server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) @@ -227,3 +235,44 @@ func TestOpsgenie_SendNotification_WithDescriptionAndPriority(t *testing.T) { // Assert the result for description and priority present assert.NoError(t, err) // Expect no error } + +func TestOpsgenie_SendNotification_WithAllFields(t *testing.T) { + // Create a mock HTTP server + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + })) + defer server.Close() + + // Replace the HTTP client in the Opsgenie service with a mock client + mockClient := &http.Client{ + Transport: &http.Transport{}, + } + service := NewOpsgenieServiceWithClient(OpsgenieOptions{ + ApiUrl: server.URL, + ApiKeys: map[string]string{ + "testRecipient": "testApiKey", + }}, mockClient) + + // Prepare test data + recipient := "testRecipient" + message := "Test message" + descriptionTemplate := "Test Opsgenie alert: {{.foo}}" + aliasTemplate := "Test alias: {{.foo}}" + priority := "P1" + + // Create test notification with description and priority + notification := Notification{ + Message: message, + Opsgenie: &OpsgenieNotification{ + Description: descriptionTemplate, + Priority: priority, + Alias: aliasTemplate, + }, + } + + // Execute the service method with description and priority + err := service.Send(notification, Destination{Recipient: recipient, Service: "opsgenie"}) + + // Assert the result for description and priority present + assert.NoError(t, err) // Expect no error +}