forked from harness/gitness
-
Notifications
You must be signed in to change notification settings - Fork 3
/
notification.go
141 lines (121 loc) · 3.04 KB
/
notification.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
package notify
import (
"bytes"
"fmt"
"log"
"net/http"
"github.com/drone/drone/plugin/notify/email"
"github.com/drone/drone/plugin/notify/flowdock"
"github.com/drone/drone/plugin/notify/github"
"github.com/drone/drone/plugin/notify/irc"
"github.com/drone/drone/plugin/notify/katoim"
"github.com/drone/drone/plugin/notify/webhook"
"github.com/drone/drone/shared/model"
)
type Sender interface {
Send(context *model.Request) error
}
// Notification stores the configuration details
// for notifying a user, or group of users,
// when their Build has completed.
type Notification struct {
Email *email.Email `yaml:"email,omitempty"`
Webhook *webhook.Webhook `yaml:"webhook,omitempty"`
Hipchat *Hipchat `yaml:"hipchat,omitempty"`
Irc *irc.IRC `yaml:"irc,omitempty"`
Slack *Slack `yaml:"slack,omitempty"`
Gitter *Gitter `yaml:"gitter,omitempty"`
Flowdock *flowdock.Flowdock `yaml:"flowdock,omitempty"`
KatoIM *katoim.KatoIM `yaml:"katoim,omitempty"`
GitHub github.GitHub `yaml:"--"`
}
func (n *Notification) Send(context *model.Request) error {
// send email notifications
if n.Email != nil {
err := n.Email.Send(context)
if err != nil {
log.Println(err)
}
}
// send webhook notifications
if n.Webhook != nil {
err := n.Webhook.Send(context)
if err != nil {
log.Println(err)
}
}
// send hipchat notifications
if n.Hipchat != nil {
err := n.Hipchat.Send(context)
if err != nil {
log.Println(err)
}
}
// send irc notifications
if n.Irc != nil {
err := n.Irc.Send(context)
if err != nil {
log.Println(err)
}
}
// send slack notifications
if n.Slack != nil {
err := n.Slack.Send(context)
if err != nil {
log.Println(err)
}
}
// send gitter notifications
if n.Gitter != nil {
err := n.Gitter.Send(context)
if err != nil {
log.Println(err)
}
}
// send gitter notifications
if n.Flowdock != nil {
err := n.Flowdock.Send(context)
if err != nil {
log.Println(err)
}
}
// send kato-im notifications
if n.KatoIM != nil {
err := n.KatoIM.Send(context)
if err != nil {
log.Println(err)
}
}
// send email notifications
// TODO (bradrydzewski) need to improve this code
githubStatus := new(github.GitHub)
if err := githubStatus.Send(context); err != nil {
log.Println(err)
}
return nil
}
func getBuildUrl(context *model.Request) string {
return fmt.Sprintf("%s/%s/%s/%s/%s/%s", context.Host, context.Repo.Host, context.Repo.Owner, context.Repo.Name, context.Commit.Branch, context.Commit.Sha)
}
// helper fuction to sent HTTP Post requests
// with JSON data as the payload.
func sendJson(url string, payload []byte, headers map[string]string) error {
client := &http.Client{}
buf := bytes.NewBuffer(payload)
req, err := http.NewRequest("POST", url, buf)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
if headers != nil {
for k, v := range headers {
req.Header.Add(k, v)
}
}
resp, err := client.Do(req)
if err != nil {
return err
}
resp.Body.Close()
return nil
}