-
Notifications
You must be signed in to change notification settings - Fork 1
/
notifiers.go
156 lines (140 loc) · 3.77 KB
/
notifiers.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
package main
import (
"bytes"
"fmt"
"text/template"
"github.com/golang/glog"
"github.com/wm/go-flowdock/flowdock"
)
const (
DefaultNotifierName = "default"
DefaultSuccessFromAddress = "build+ok@flowdock.com"
DefaultFailureFromAddress = "build+fail@flowdock.com"
DefaultFromAddress = "openshift@example.org"
DefaultFromName = "OpenShift"
DefaultSource = "OpenShift"
DefaultSubjectTemplate = "{{.ObjectType}} {{.Namespace}}/{{.Name}} {{.Status}}"
DefaultContentTemplate = `<h3>{{.ObjectType}} {{.Namespace}}/{{.Name}}</h3>
<dl>
<dt>Status</dt>
<dd>{{.Status}}</dd>
<dt>Start Time</dt>
<dd>{{.ObjectStartTime}}</dd>
<dt>End Time</dt>
<dd>{{.ObjectEndTime}}</dd>
<dt>Duration</dt>
<dd>{{.ObjectDuration}}</dd>
<dt>Input</dt>
<dd>{{.Input}}</dd>
<dt>Output</dt>
<dd>{{.Output}}</dd>
<dt>Node</dt>
<dd>{{.NodeName}}</dd>
<dt>Logs</dt>
<dd><pre>{{.Logs}}</pre></dd>
<dt>Events</dt>
<dd>
<pre>
{{range .Events}}
{{.}}
{{end}}
</pre>
</dd>
<dt>Link</dt>
<dd>{{.Url}}</dd>
</dl>`
)
type FlowdockNotifier struct {
Config FlowdockNotifierConfig
Channel chan Event
FlowdockClient *flowdock.Client
SubjectTemplate *template.Template
ContentTemplate *template.Template
TagsTemplates []*template.Template
}
func NewFlowdockNotifier(config FlowdockNotifierConfig) (*FlowdockNotifier, error) {
subjectTemplate, err := template.New("subject").Parse(config.SubjectTemplate)
if err != nil {
return nil, err
}
contentTemplate, err := template.New("content").Parse(config.ContentTemplate)
if err != nil {
return nil, err
}
tagsTemplates := []*template.Template{}
for i, tagTmpl := range config.Tags {
tmpl, err := template.New(fmt.Sprintf("tag-%d", i)).Parse(tagTmpl)
if err != nil {
return nil, err
}
tagsTemplates = append(tagsTemplates, tmpl)
}
notifier := &FlowdockNotifier{
Config: config,
Channel: make(chan Event),
FlowdockClient: flowdock.NewClient(nil),
SubjectTemplate: subjectTemplate,
ContentTemplate: contentTemplate,
TagsTemplates: tagsTemplates,
}
return notifier, nil
}
func (notifier *FlowdockNotifier) Run() {
for {
event, open := <-notifier.Channel
if !open {
glog.Errorf("Flowdock Channel has been closed!")
break
}
if err := notifier.sendNotification(event); err != nil {
glog.Errorf("Failed to send an inbox message to Flowdock: %v", err)
}
}
}
func (notifier *FlowdockNotifier) sendNotification(event Event) error {
subject, err := executeTemplate(notifier.SubjectTemplate, event)
if err != nil {
return err
}
content, err := executeTemplate(notifier.ContentTemplate, event)
if err != nil {
return err
}
tags := []string{}
for _, tagTmpl := range notifier.TagsTemplates {
tag, err := executeTemplate(tagTmpl, event)
if err != nil {
glog.Warningf("Ignoring tag template: %v", err)
continue
}
tags = append(tags, tag)
}
fromAddress := notifier.Config.FromAddress
switch {
case event.IsSuccess():
fromAddress = DefaultSuccessFromAddress
case event.IsFailure():
fromAddress = DefaultFailureFromAddress
}
glog.V(2).Infof("Sending an inbox message to Flowdock...")
_, resp, err := notifier.FlowdockClient.Inbox.Create(notifier.Config.Token, &flowdock.InboxCreateOptions{
Source: notifier.Config.Source,
Project: event.Namespace(),
FromAddress: fromAddress,
FromName: notifier.Config.FromName,
Subject: subject,
Content: content,
Tags: tags,
Link: event.Url(),
})
if err != nil {
return err
}
glog.V(2).Infof("Successfully sent an inbox message to Flowdock. Response is: %+v", resp)
return nil
}
func executeTemplate(tmpl *template.Template, event Event) (string, error) {
buffer := &bytes.Buffer{}
err := tmpl.Execute(buffer, event)
return buffer.String(), err
}