-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify_slack.go
172 lines (143 loc) · 3.54 KB
/
notify_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
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
169
170
171
172
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/itchyny/gojq"
"github.com/kelseyhightower/envconfig"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/slack-go/slack"
)
type config struct {
LogLevel string `default:"info"`
LogFormat string `default:"json"`
DryRun bool
Users []string
Groups []string
SlackToken string `required:"true"`
}
type test struct {
Package, Test, Output string
}
var cfg config
func setupLogging() {
l, err := zerolog.ParseLevel(strings.ToLower(cfg.LogLevel))
if err != nil {
log.Err(err).Msg("")
return
}
zerolog.SetGlobalLevel(l)
if strings.ToLower(cfg.LogFormat) == "console" {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
}
func readStdIn() []interface{} {
//gojq expects []interface{} for arrays and map[]interface for single values
//that's why this is a bit more complicated then expected, maybe this could
//be simplified
var (
r []interface{}
v interface{}
)
d := json.NewDecoder(os.Stdin)
for {
if err := d.Decode(&v); err != nil {
break
}
r = append(r, v)
}
return r
}
func testsToReport(i []interface{}) []test {
q, err := gojq.Parse(".[] | select(.Action==\"fail\") | select(.Test != null) | {Test,Package}")
var r []test
if err != nil {
log.Err(err).Msg("can't parse tests queue")
}
iter := q.Run(i)
for {
c, ok := iter.Next()
if !ok {
break
}
t := fmt.Sprint(c.(map[string]interface{})["Test"])
p := fmt.Sprint(c.(map[string]interface{})["Package"])
o := testOutputsToReport(t, i)
r = append(r, test{Package: p, Test: t, Output: o})
}
return r
}
func testOutputsToReport(t string, i []interface{}) string {
q, err := gojq.Parse(fmt.Sprintf(".[] | select(.Action==\"output\") | select(.Test==\"%s\") | .Output ", t))
if err != nil {
log.Err(err).Msg("can't parse tests queue")
}
var r []string
iter := q.Run(i)
for {
c, ok := iter.Next()
if !ok {
break
}
r = append(r, fmt.Sprint(c))
}
return strings.Join(r, "")
}
func userIdsFromEmails(c *slack.Client, emails []string) []string {
var ids []string
for _, e := range emails {
user, err := c.GetUserByEmail(e)
if err != nil {
log.Err(err).Str("email", e).Msg("")
continue
}
log.Debug().Str("ID", user.ID).Str("Fullname", user.Profile.RealName).Str("Email", user.Profile.Email).Msg("")
ids = append(ids, user.ID)
}
return ids
}
func sendSlack(c *slack.Client, tests []test, usersGroups ...string) {
messageFields := make([]slack.AttachmentField, 0)
for _, t := range tests {
f := slack.AttachmentField{
Title: fmt.Sprintf("%s -> %s", t.Package, t.Test),
Value: fmt.Sprintf("```%s```", t.Output),
}
messageFields = append(messageFields, f)
}
redHex := "#ff0000"
attachment := slack.Attachment{
Color: redHex,
Fields: messageFields,
Text: "Tests have failed.",
}
o := slack.MsgOptionAttachments(attachment)
for _, id := range usersGroups {
if cfg.DryRun {
continue
}
if _, _, err := c.PostMessage(id, o); err != nil {
log.Err(err).Str("id", id).Msg("can't send to message")
}
}
}
func main() {
if err := envconfig.Process("notify", &cfg); err != nil {
log.Err(err).Msg("can't read config")
return
}
setupLogging()
log.Debug().Interface("config", cfg).Msg("")
records := readStdIn()
tests := testsToReport(records)
log.Debug().Interface("Tests", tests).Msg("")
if tests == nil {
log.Debug().Msg("nothing to do.")
return
}
c := slack.New(cfg.SlackToken)
userIds := userIdsFromEmails(c, cfg.Users)
sendSlack(c, tests, append(userIds, cfg.Groups...)...)
}