Skip to content

Commit

Permalink
feat(opsgenie): Add support for setting note in Opsgenie notification (
Browse files Browse the repository at this point in the history
…#271)

Signed-off-by: Esat Akyol <[email protected]>
  • Loading branch information
esatakyol authored Feb 19, 2024
1 parent 743f877 commit 7a06976
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/services/opsgenie.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
21 changes: 18 additions & 3 deletions pkg/services/opsgenie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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{}
Expand All @@ -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(&noteData, vars); err != nil {
return err
}
notification.Opsgenie.Note = noteData.String()
return nil
}, nil
}
Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -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",
Expand Down
22 changes: 22 additions & 0 deletions pkg/services/opsgenie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ 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) {
// Create a new OpsgenieNotification instance
notification := OpsgenieNotification{
Description: descriptionTemplate,
Alias: aliasTemplate,
Note: noteTemplate,
}

// Call the GetTemplater method
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -116,13 +134,15 @@ 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{
Message: message,
Opsgenie: &OpsgenieNotification{
Description: descriptionTemplate,
Alias: aliasTemplate,
Note: noteTemplate,
},
}

Expand Down Expand Up @@ -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{
Expand All @@ -275,6 +296,7 @@ func TestOpsgenie_SendNotification_WithAllFields(t *testing.T) {
Description: descriptionTemplate,
Priority: priority,
Alias: aliasTemplate,
Note: noteTemplate,
},
}

Expand Down

0 comments on commit 7a06976

Please sign in to comment.