This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
services.go
155 lines (137 loc) · 3.89 KB
/
services.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package services
import (
"bytes"
"encoding/json"
"fmt"
"strings"
texttemplate "text/template"
"github.com/ghodss/yaml"
)
type Notification struct {
Message string `json:"message,omitempty"`
Email *EmailNotification `json:"email,omitempty"`
Slack *SlackNotification `json:"slack,omitempty"`
Webhook WebhookNotifications `json:"webhook,omitempty"`
Opsgenie *OpsgenieNotification `json:"opsgenie,omitempty"`
}
// Destination holds notification destination details
type Destination struct {
Service string `json:"service"`
Recipient string `json:"recipient"`
}
func (n *Notification) GetTemplater(name string, f texttemplate.FuncMap) (Templater, error) {
var sources []TemplaterSource
if n.Slack != nil {
sources = append(sources, n.Slack)
}
if n.Email != nil {
sources = append(sources, n.Email)
}
if n.Webhook != nil {
sources = append(sources, n.Webhook)
}
if n.Opsgenie != nil {
sources = append(sources, n.Opsgenie)
}
return n.getTemplater(name, f, sources)
}
//go:generate mockgen -destination=./mocks/mocks.go -package=mocks github.com/argoproj-labs/argocd-notifications/pkg/services NotificationService
// NotificationService defines notification service interface
type NotificationService interface {
Send(notification Notification, dest Destination) error
}
func NewService(serviceType string, optsData []byte) (NotificationService, error) {
switch serviceType {
case "email":
var opts EmailOptions
if err := yaml.Unmarshal(optsData, &opts); err != nil {
return nil, err
}
return NewEmailService(opts), nil
case "slack":
var opts SlackOptions
if err := yaml.Unmarshal(optsData, &opts); err != nil {
return nil, err
}
return NewSlackService(opts), nil
case "grafana":
var opts GrafanaOptions
if err := yaml.Unmarshal(optsData, &opts); err != nil {
return nil, err
}
return NewGrafanaService(opts), nil
case "opsgenie":
var opts OpsgenieOptions
if err := yaml.Unmarshal(optsData, &opts); err != nil {
return nil, err
}
return NewOpsgenieService(opts), nil
case "webhook":
var opts WebhookOptions
if err := yaml.Unmarshal(optsData, &opts); err != nil {
return nil, err
}
return NewWebhookService(opts), nil
case "telegram":
var opts TelegramOptions
if err := yaml.Unmarshal(optsData, &opts); err != nil {
return nil, err
}
return NewTelegramService(opts), nil
default:
return nil, fmt.Errorf("service type '%s' is not supported", serviceType)
}
}
func (n *Notification) Preview() string {
preview := ""
switch {
case n.Message != "":
preview = n.Message
default:
if yamlData, err := json.Marshal(n); err != nil {
preview = "failed to generate preview"
} else {
preview = string(yamlData)
}
}
preview = strings.Split(preview, "\n")[0]
if len(preview) > 100 {
preview = preview[:99] + "..."
}
return preview
}
func (n *Notification) getTemplater(name string, f texttemplate.FuncMap, sources []TemplaterSource) (Templater, error) {
message, err := texttemplate.New(name).Funcs(f).Parse(n.Message)
if err != nil {
return nil, err
}
templaters := []Templater{func(notification *Notification, vars map[string]interface{}) error {
var messageData bytes.Buffer
if err := message.Execute(&messageData, vars); err != nil {
return err
}
if val := messageData.String(); val != "" {
notification.Message = messageData.String()
}
return nil
}}
for _, src := range sources {
t, err := src.GetTemplater(name, f)
if err != nil {
return nil, err
}
templaters = append(templaters, t)
}
return func(notification *Notification, vars map[string]interface{}) error {
for _, t := range templaters {
if err := t(notification, vars); err != nil {
return err
}
}
return nil
}, nil
}
type Templater func(notification *Notification, vars map[string]interface{}) error
type TemplaterSource interface {
GetTemplater(name string, f texttemplate.FuncMap) (Templater, error)
}