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

feat(opsgenie): Add support for setting note in Opsgenie notification #271

Merged
merged 1 commit into from
Feb 19, 2024
Merged
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
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 @@
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 @@
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 @@
return err
}
notification.Opsgenie.Alias = aliasData.String()
var noteData bytes.Buffer
if err := note.Execute(&noteData, vars); err != nil {
return err
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/services/opsgenie.go#L58-L59

Added lines #L58 - L59 were not covered by tests
notification.Opsgenie.Note = noteData.String()
return nil
}, nil
}
Expand All @@ -73,9 +83,9 @@
httputil.NewTransport(s.opts.ApiUrl, false), log.WithField("service", "opsgenie")),
},
})
description := ""
priority := ""
alias := ""

var description, priority, alias, note string

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

View check run for this annotation

Codecov / codecov/patch

pkg/services/opsgenie.go#L86-L88

Added lines #L86 - L88 were not covered by tests
if notification.Opsgenie != nil {
if notification.Opsgenie.Description == "" {
return fmt.Errorf("Opsgenie notification description is missing")
Expand All @@ -90,6 +100,10 @@
if notification.Opsgenie.Alias != "" {
alias = notification.Opsgenie.Alias
}

if notification.Opsgenie.Note != "" {
note = notification.Opsgenie.Note
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/services/opsgenie.go#L104-L106

Added lines #L104 - L106 were not covered by tests
}

alertPriority := alert.Priority(priority)
Expand All @@ -99,6 +113,7 @@
Description: description,
Priority: alertPriority,
Alias: alias,
Note: note,

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

View check run for this annotation

Codecov / codecov/patch

pkg/services/opsgenie.go#L116

Added line #L116 was not covered by tests
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
Loading