Skip to content

Commit 4efbe7f

Browse files
committed
WIP: adding tests
Signed-off-by: Andre Marcelo-Tanner <drechin@gmail.com>
1 parent 9c4926d commit 4efbe7f

File tree

2 files changed

+90
-57
lines changed

2 files changed

+90
-57
lines changed

pkg/services/datadog.go

+16-57
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,41 @@ package services
22

33
import (
44
"bytes"
5-
"context"
5+
"fmt"
66
texttemplate "text/template"
7-
8-
slackutil "github.com/argoproj/notifications-engine/pkg/util/slack"
97
)
108

119
type DatadogNotification struct {
12-
Attachments string `json:"attachments,omitempty"`
13-
Blocks string `json:"blocks,omitempty"`
14-
GroupingKey string `json:"groupingKey"`
15-
NotifyBroadcast bool `json:"notifyBroadcast"`
16-
DeliveryPolicy slackutil.DeliveryPolicy `json:"deliveryPolicy"`
10+
AlertType string `json:"alertType,omitempty"`
11+
Tags string `json:"tags,omitempty"`
1712
}
1813

1914
func (n *DatadogNotification) GetTemplater(name string, f texttemplate.FuncMap) (Templater, error) {
20-
slackAttachments, err := texttemplate.New(name).Funcs(f).Parse(n.Attachments)
21-
if err != nil {
22-
return nil, err
23-
}
24-
slackBlocks, err := texttemplate.New(name).Funcs(f).Parse(n.Blocks)
25-
if err != nil {
26-
return nil, err
27-
}
28-
groupingKey, err := texttemplate.New(name).Funcs(f).Parse(n.GroupingKey)
15+
tags, err := texttemplate.New(name).Funcs(f).Parse(n.Tags)
2916
if err != nil {
3017
return nil, err
3118
}
3219

3320
return func(notification *Notification, vars map[string]interface{}) error {
34-
if notification.Slack == nil {
35-
notification.Slack = &SlackNotification{}
21+
if notification.Datadog == nil {
22+
notification.Datadog = &DatadogNotification{}
3623
}
37-
var slackAttachmentsData bytes.Buffer
38-
if err := slackAttachments.Execute(&slackAttachmentsData, vars); err != nil {
39-
return err
40-
}
41-
notification.Slack.Attachments = slackAttachmentsData.String()
4224

43-
var slackBlocksData bytes.Buffer
44-
if err := slackBlocks.Execute(&slackBlocksData, vars); err != nil {
45-
return err
46-
}
47-
notification.Slack.Blocks = slackBlocksData.String()
25+
notification.Datadog.AlertType = n.AlertType
4826

49-
var groupingKeyData bytes.Buffer
50-
if err := groupingKey.Execute(&groupingKeyData, vars); err != nil {
27+
var tagsData bytes.Buffer
28+
if err := tags.Execute(&tagsData, vars); err != nil {
5129
return err
5230
}
53-
notification.Slack.GroupingKey = groupingKeyData.String()
31+
notification.Datadog.Tags = tagsData.String()
5432

55-
notification.Slack.NotifyBroadcast = n.NotifyBroadcast
56-
notification.Slack.DeliveryPolicy = n.DeliveryPolicy
5733
return nil
5834
}, nil
5935
}
6036

6137
type DatadogOptions struct {
62-
Username string `json:"username"`
63-
Icon string `json:"icon"`
64-
Token string `json:"token"`
65-
SigningSecret string `json:"signingSecret"`
66-
Channels []string `json:"channels"`
67-
InsecureSkipVerify bool `json:"insecureSkipVerify"`
68-
ApiURL string `json:"apiURL"`
69-
DisableUnfurl bool `json:"disableUnfurl"`
38+
DDApiKey string `json:"ddApiKey"`
39+
DDAppKey string `json:"ddAppKey"`
7040
}
7141

7242
type datadogService struct {
@@ -78,19 +48,8 @@ func NewDatadogService(opts DatadogOptions) NotificationService {
7848
}
7949

8050
func (s *datadogService) Send(notification Notification, dest Destination) error {
81-
slackNotification, msgOptions, err := buildMessageOptions(notification, dest, s.opts)
82-
if err != nil {
83-
return err
84-
}
85-
return slackutil.NewThreadedClient(
86-
newSlackClient(s.opts),
87-
slackState,
88-
).SendMessage(
89-
context.TODO(),
90-
dest.Recipient,
91-
slackNotification.GroupingKey,
92-
slackNotification.NotifyBroadcast,
93-
slackNotification.DeliveryPolicy,
94-
msgOptions,
95-
)
51+
// print notificaiton.Datadog
52+
fmt.Printf("%+v", notification.Datadog)
53+
54+
return nil
9655
}

pkg/services/datadog_test.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package services
2+
3+
import (
4+
"testing"
5+
"text/template"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestGetTemplater_Datadog(t *testing.T) {
11+
n := Notification{
12+
Datadog: &DatadogNotification{
13+
AlertType: "info",
14+
Tags: "{{.context.argocdUrl}}",
15+
},
16+
}
17+
templater, err := n.GetTemplater("", template.FuncMap{})
18+
19+
if !assert.NoError(t, err) {
20+
return
21+
}
22+
23+
var notification Notification
24+
err = templater(&notification, map[string]interface{}{
25+
"context": map[string]interface{}{
26+
"argocdUrl": "https://example.com",
27+
"state": "success",
28+
},
29+
"app": map[string]interface{}{
30+
"metadata": map[string]interface{}{
31+
"name": "argocd-notifications",
32+
},
33+
"spec": map[string]interface{}{
34+
"source": map[string]interface{}{
35+
"repoURL": "https://github.com/argoproj-labs/argocd-notifications.git",
36+
},
37+
},
38+
"status": map[string]interface{}{
39+
"operationState": map[string]interface{}{
40+
"syncResult": map[string]interface{}{
41+
"revision": "0123456789",
42+
},
43+
},
44+
},
45+
},
46+
})
47+
48+
if !assert.NoError(t, err) {
49+
return
50+
}
51+
52+
assert.Equal(t, "https://github.com/argoproj-labs/argocd-notifications.git", notification.GitHub.repoURL)
53+
assert.Equal(t, "0123456789", notification.GitHub.revision)
54+
assert.Equal(t, "success", notification.GitHub.Status.State)
55+
assert.Equal(t, "continuous-delivery/argocd-notifications", notification.GitHub.Status.Label)
56+
assert.Equal(t, "https://example.com/applications/argocd-notifications", notification.GitHub.Status.TargetURL)
57+
}
58+
59+
/*
60+
func TestSend_DataDogService_BadURL(t *testing.T) {
61+
e := datadogService{}.Send(
62+
Notification{
63+
GitHub: &GitHubNotification{
64+
repoURL: "hello",
65+
},
66+
},
67+
Destination{
68+
Service: "",
69+
Recipient: "",
70+
},
71+
)
72+
assert.ErrorContains(t, e, "does not have a `/`")
73+
}
74+
*/

0 commit comments

Comments
 (0)