-
Notifications
You must be signed in to change notification settings - Fork 2
/
notifier.go
168 lines (143 loc) · 3.71 KB
/
notifier.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
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"bytes"
"encoding/json"
"log"
"reflect"
"text/template"
"github.com/bittersweet/notifilter-receive/notifiers"
"github.com/jmoiron/sqlx/types"
)
// Notifier is a db-backed struct that contains everything that is necessary to
// check incoming events (rules) and what to do when those rules are matched.
type Notifier struct {
ID int `db:"id"`
Application string `db:"application"`
EventName string `db:"event_name"`
Template string `db:"template"`
Rules types.JSONText `db:"rules"`
NotificationType string `db:"notification_type"`
Target string `db:"target"`
}
func (n *Notifier) newNotifier() notifiers.MessageNotifier {
switch n.NotificationType {
case "email":
return ¬ifiers.EmailNotifier{}
case "slack":
return ¬ifiers.SlackNotifier{
HookURL: C.SlackHookURL,
}
}
return ¬ifiers.SlackNotifier{
HookURL: C.SlackHookURL,
}
}
func (n *Notifier) getRules() []*rule {
rules := []*rule{}
n.Rules.Unmarshal(&rules)
return rules
}
func (n *Notifier) checkRules(e *Event) bool {
rules := n.getRules()
for _, rule := range rules {
if !rule.Met(e) {
e.log("[NOTIFY] rule not met -- Key: %s, Type: %s, Setting %s, Value %s, Received Value %v", rule.Key, rule.Type, rule.Setting, rule.Value, e.dataToMap()[rule.Key])
e.log("[NOTIFY] Stopping notification of id: %d, rules not met", n.ID)
return false
}
}
return true
}
func isset(a map[string]interface{}, key string) bool {
if _, ok := a[key]; ok {
return true
}
return false
}
func present(str interface{}) bool {
switch t := str.(type) {
case nil:
return false
case string:
return t != ""
case bool:
return t == true
}
// If it was not nil, a blank string or false, we can assume it's present.
return true
}
func decodeJSON(str string) map[string]interface{} {
var parsed map[string]interface{}
err := json.Unmarshal([]byte(str), &parsed)
if err != nil {
log.Fatal("json.Unmarshal decodeJson", err)
}
return parsed
}
func eq(x, y interface{}) bool {
normalize := func(v interface{}) interface{} {
vv := reflect.ValueOf(v)
switch vv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return vv.Int()
case reflect.Float32, reflect.Float64:
return vv.Float()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return vv.Uint()
default:
return v
}
}
x = normalize(x)
y = normalize(y)
return reflect.DeepEqual(x, y)
}
var funcMap = template.FuncMap{
"isset": isset,
"present": present,
"eq": eq,
"decodeJSON": decodeJSON,
}
func (n *Notifier) renderTemplate(e *Event) ([]byte, error) {
var err error
var doc bytes.Buffer
t := template.New("notificationTemplate")
t.Funcs(funcMap)
t, err = t.Parse(n.Template)
if err != nil {
return []byte(""), err
}
err = t.Execute(&doc, e.dataToMap())
if err != nil {
return []byte(""), err
}
return doc.Bytes(), nil
}
func (n *Notifier) notify(e *Event, mn notifiers.MessageNotifier) {
nt := n.NotificationType
e.log("[NOTIFY] Notifying notifier id: %d type: %s", n.ID, nt)
if !n.checkRules(e) {
return
}
message, err := n.renderTemplate(e)
if err != nil {
e.log("[NOTIFY] renderTemplate failed:", err)
}
mn.SendMessage(n.Target, n.EventName, message)
e.log("[NOTIFY] Notifying notifier id: %d done", n.ID)
}
func renderTemplate(tmpl string, e *Event) ([]byte, error) {
var err error
var doc bytes.Buffer
t := template.New("notificationTemplate")
t.Funcs(funcMap)
t, err = t.Parse(tmpl)
if err != nil {
return []byte(""), err
}
err = t.Execute(&doc, e.dataToMap())
if err != nil {
return []byte(""), err
}
return doc.Bytes(), nil
}