This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
slack.go
78 lines (68 loc) · 1.59 KB
/
slack.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
package history
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"regexp"
"strings"
"github.com/pkg/errors"
)
func NewSlackEventWriter(d Doer, webhookURL, username string, matchExprs ...string) *Slack {
var re []*regexp.Regexp
for _, expr := range matchExprs {
re = append(re, regexp.MustCompile(expr))
}
return &Slack{
d: d,
webhookURL: webhookURL,
username: username,
re: re,
}
}
type Slack struct {
d Doer
webhookURL string
username string
re []*regexp.Regexp
}
func (s *Slack) LogEvent(namespace, service, msg string) error {
text := fmt.Sprintf("%s/%s: %s", namespace, service, msg)
if !s.match(text) {
return nil
}
buf := &bytes.Buffer{}
if err := json.NewEncoder(buf).Encode(map[string]string{
"username": s.username,
"text": text,
}); err != nil {
return errors.Wrap(err, "encoding Slack POST request")
}
req, err := http.NewRequest("POST", s.webhookURL, buf)
if err != nil {
return errors.Wrap(err, "constructing Slack HTTP request")
}
resp, err := s.d.Do(req)
if err != nil {
return errors.Wrap(err, "executing HTTP POST to Slack")
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
body, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 1024*1024))
return fmt.Errorf("%s from Slack (%s)", resp.Status, strings.TrimSpace(string(body)))
}
return nil
}
func (s *Slack) match(text string) bool {
for _, re := range s.re {
if re.MatchString(text) {
return true
}
}
return false
}
// Doer is satisfied by *http.Client.
type Doer interface {
Do(*http.Request) (*http.Response, error)
}