forked from facesea/banshee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.go
43 lines (37 loc) · 1006 Bytes
/
webhook.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
package notifier
import (
"bytes"
"encoding/json"
"net/http"
"github.com/eleme/banshee/alerter"
"github.com/eleme/banshee/models"
)
// WebHook notifier
type WebHook struct{}
// Event is the webhook payload
type Event struct {
ID string `json:"id"`
Comment string `json:"comment"`
Metric *models.Metric `json:"metric"`
Rule *models.Rule `json:"rule"`
Project *models.Project `json:"project"`
Team *models.Team `json:"team"`
}
// Notify event
func (w *WebHook) Notify(hook models.WebHook, ew *models.EventWrapper) error {
evt := Event{}
evt.ID = ew.ID
evt.Comment = ew.RuleTranslatedComment
evt.Metric = ew.Metric
evt.Rule = ew.Rule
evt.Team = ew.Team
evt.Project = ew.Project
body, _ := json.Marshal(evt)
req, err := http.NewRequest("POST", hook.URL, bytes.NewReader(body))
req.Header.Add("Content-Type", "application/json")
_, err = http.DefaultClient.Do(req)
return err
}
func init() {
alerter.RegisterNotifier("webhook", new(WebHook))
}