-
Notifications
You must be signed in to change notification settings - Fork 7
/
slack.go
328 lines (304 loc) · 9.7 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package slack
import (
"context"
"fmt"
"strings"
"github.com/lunarway/release-manager/internal/http"
"github.com/lunarway/release-manager/internal/log"
"github.com/nlopes/slack"
"github.com/pkg/errors"
)
type Client struct {
client *slack.Client
emailMappings map[string]string
muteOptions MuteOptions
}
type MuteOptions struct {
Flux bool
Kubernetes bool
Policy bool
ReleaseProcessed bool
}
var (
// ErrUnknownEmail indicates that an email not from the lunar.app domain
// is used and no email mapping exists.
ErrUnknownEmail = errors.New("not a lunarway email")
)
func NewClient(token string, emailMappings map[string]string) (*Client, error) {
log.Infof("slack: new client: initialized with emailMappings: %+v", emailMappings)
slackClient := slack.New(token)
client := Client{
client: slackClient,
emailMappings: emailMappings,
muteOptions: MuteOptions{},
}
return &client, nil
}
func NewMuteableClient(token string, emailMappings map[string]string, muteOptions MuteOptions) (*Client, error) {
log.Infof("slack: new client: initialized with emailMappings: %+v", emailMappings)
slackClient := slack.New(token)
client := Client{
client: slackClient,
emailMappings: emailMappings,
muteOptions: muteOptions,
}
return &client, nil
}
func (c *Client) getIdByEmail(ctx context.Context, email string) (string, error) {
if !strings.Contains(email, "@lunar.app") {
// check for fallback emails
lwEmail, ok := c.emailMappings[email]
if !ok {
log.WithContext(ctx).Errorf("%s is not a Lunar Way email and no mapping exist", email)
return "", ErrUnknownEmail
}
email = lwEmail
}
user, err := c.client.GetUserByEmailContext(ctx, email)
if err != nil {
return "", err
}
return user.ID, nil
}
func (c *Client) UpdateSlackBuildStatus(channel, title, titleLink, text, color, timestamp string) (string, string, error) {
asUser := slack.MsgOptionAsUser(true)
attachments := slack.MsgOptionAttachments(slack.Attachment{
Title: title,
TitleLink: titleLink,
Color: color,
Text: text,
MarkdownIn: []string{"text", "fields"},
})
respChannel, timestamp, _, err := c.client.UpdateMessage(channel, timestamp, asUser, attachments)
if err != nil {
return "", "", err
}
return respChannel, timestamp, nil
}
func (c *Client) PostSlackBuildStarted(email, title, titleLink, text, color string) (string, string, error) {
userID, err := c.getIdByEmail(context.Background(), email)
if err != nil {
return "", "", err
}
asUser := slack.MsgOptionAsUser(true)
attachments := slack.MsgOptionAttachments(slack.Attachment{
Title: title,
TitleLink: titleLink,
Color: color,
Text: text,
MarkdownIn: []string{"text", "fields"},
})
respChannel, timestamp, err := c.client.PostMessage(userID, asUser, attachments)
if err != nil {
return "", "", err
}
return respChannel, timestamp, err
}
type ReleaseOptions struct {
Service string
ArtifactID string
CommitSHA string
CommitLink string
CommitMessage string
CommitAuthor string
CommitAuthorEmail string
Releaser string
Environment string
}
func (c *Client) NotifySlackReleasesChannel(ctx context.Context, options ReleaseOptions) error {
asUser := slack.MsgOptionAsUser(true)
attachments := slack.MsgOptionAttachments(slack.Attachment{
Title: fmt.Sprintf("%s (%s)", options.Service, options.ArtifactID),
TitleLink: options.CommitLink,
Color: MsgColorGreen,
Text: fmt.Sprintf("*Author:* %s, *Releaser:* %s\n*Message:* _%s_", options.CommitAuthor, options.Releaser, options.CommitMessage),
MarkdownIn: []string{"text", "fields"},
})
_, _, err := c.client.PostMessageContext(ctx, fmt.Sprintf("#releases-%s", options.Environment), asUser, attachments)
if err != nil {
return err
}
return err
}
type BuildsOptions struct {
Service string
ArtifactID string
Branch string
CommitSHA string
CommitLink string
CommitMessage string
CommitAuthor string
CIJobURL string
Color string
}
func (c *Client) NotifySlackBuildsChannel(options BuildsOptions) error {
asUser := slack.MsgOptionAsUser(true)
attachments := slack.MsgOptionAttachments(slack.Attachment{
Title: fmt.Sprintf("%s (%s)", options.Service, options.ArtifactID),
TitleLink: options.CIJobURL,
Color: options.Color,
Text: fmt.Sprintf("*Author:* %s (<%s|%s>)\n*Message:* _%s_", options.CommitAuthor, options.CommitLink, options.CommitSHA[0:10], options.CommitMessage),
MarkdownIn: []string{"text", "fields"},
})
_, _, err := c.client.PostMessage("#builds", asUser, attachments)
if err != nil {
return err
}
return err
}
func (c *Client) NotifySlackPolicyFailed(ctx context.Context, email, title, errorMessage string) error {
if c.muteOptions.Policy {
return nil
}
userID, err := c.getIdByEmail(ctx, email)
if err != nil {
return err
}
asUser := slack.MsgOptionAsUser(true)
attachments := slack.MsgOptionAttachments(slack.Attachment{
Title: title,
Color: MsgColorRed,
Text: fmt.Sprintf("```%s```", errorMessage),
MarkdownIn: []string{"text", "fields"},
})
_, _, err = c.client.PostMessageContext(ctx, userID, asUser, attachments)
if err != nil {
return err
}
return nil
}
func (c *Client) NotifySlackPolicySucceeded(ctx context.Context, email, title, message string) error {
if c.muteOptions.Policy {
return nil
}
userID, err := c.getIdByEmail(ctx, email)
if err != nil {
return err
}
asUser := slack.MsgOptionAsUser(true)
attachments := slack.MsgOptionAttachments(slack.Attachment{
Title: title,
Color: MsgColorGreen,
Text: message,
MarkdownIn: []string{"text", "fields"},
})
_, _, err = c.client.PostMessageContext(ctx, userID, asUser, attachments)
if err != nil {
return err
}
return nil
}
func (c *Client) NotifyAuthorEventProcessed(ctx context.Context, options ReleaseOptions) error {
if c.muteOptions.ReleaseProcessed {
return nil
}
userID, err := c.getIdByEmail(ctx, options.CommitAuthorEmail)
if err != nil {
return err
}
asUser := slack.MsgOptionAsUser(true)
attachments := slack.MsgOptionAttachments(slack.Attachment{
Title: fmt.Sprintf(":rocket: Release Manager :white_check_mark:"),
Color: MsgColorGreen,
Text: fmt.Sprintf("Release for *%s* in %s processed\nArtifact: <%s|*%s*>", options.Service, options.Environment, options.CommitLink, options.ArtifactID),
MarkdownIn: []string{"text", "fields"},
})
_, _, err = c.client.PostMessageContext(ctx, userID, asUser, attachments)
if err != nil {
return err
}
return err
}
func (c *Client) NotifyFluxEventProcessed(ctx context.Context, artifactID, env, email, service string) error {
if c.muteOptions.Flux {
return nil
}
userID, err := c.getIdByEmail(ctx, email)
if err != nil {
return err
}
asUser := slack.MsgOptionAsUser(true)
attachments := slack.MsgOptionAttachments(slack.Attachment{
Title: fmt.Sprintf(":flux: Flux (%s) :white_check_mark:", env),
Color: MsgColorGreen,
Text: fmt.Sprintf("Rollout initiated for *%s*\nArtifact: %s", service, artifactID),
MarkdownIn: []string{"text", "fields"},
})
_, _, err = c.client.PostMessageContext(ctx, userID, asUser, attachments)
if err != nil {
return err
}
return err
}
func (c *Client) NotifyFluxErrorEvent(ctx context.Context, artifactID, env, email, service, errorMessage, errorPath string) error {
if c.muteOptions.Flux {
return nil
}
userID, err := c.getIdByEmail(ctx, email)
if err != nil {
return err
}
asUser := slack.MsgOptionAsUser(true)
attachments := slack.MsgOptionAttachments(slack.Attachment{
Title: fmt.Sprintf(":flux: Flux (%s) :no_entry:", env),
Color: MsgColorRed,
Text: fmt.Sprintf("Error detected for *%s*\nArtifact: %s\nPath: `%s`\n```%s```", service, artifactID, errorPath, errorMessage),
MarkdownIn: []string{"text", "fields"},
})
_, _, err = c.client.PostMessageContext(ctx, userID, asUser, attachments)
if err != nil {
return err
}
return err
}
func (c *Client) NotifyK8SDeployEvent(ctx context.Context, event *http.ReleaseEvent) error {
if c.muteOptions.Kubernetes {
return nil
}
userID, err := c.getIdByEmail(ctx, event.AuthorEmail)
if err != nil {
return err
}
asUser := slack.MsgOptionAsUser(true)
attachments := slack.MsgOptionAttachments(slack.Attachment{
Title: fmt.Sprintf(":kubernetes: k8s (%s) :white_check_mark:", event.Environment),
Color: MsgColorGreen,
Text: fmt.Sprintf("%s deployed\n%d/%d pods are running (%s)\nArtifact: *%s*", event.Name, event.AvailablePods, event.DesiredPods, event.ResourceType, event.ArtifactID),
MarkdownIn: []string{"text", "fields"},
})
_, _, err = c.client.PostMessageContext(ctx, userID, asUser, attachments)
if err != nil {
return err
}
return err
}
func (c *Client) NotifyK8SPodErrorEvent(ctx context.Context, event *http.PodErrorEvent) error {
if c.muteOptions.Kubernetes {
return nil
}
userID, err := c.getIdByEmail(ctx, event.AuthorEmail)
if err != nil {
return err
}
asUser := slack.MsgOptionAsUser(true)
var fields []slack.AttachmentField
for _, container := range event.Errors {
fields = append(fields, slack.AttachmentField{
Title: fmt.Sprintf("Container: %s (%s)", container.Name, container.Type),
Value: fmt.Sprintf("```%s```", container.ErrorMessage),
Short: false,
})
}
attachments := slack.MsgOptionAttachments(slack.Attachment{
Title: fmt.Sprintf(":kubernetes: k8s (%s) :no_entry:", event.Environment),
Text: fmt.Sprintf("Pod Error: %s\nArtifact: *%s*", event.PodName, event.ArtifactID),
Color: "#e24d42",
MarkdownIn: []string{"text", "fields"},
Fields: fields,
})
_, _, err = c.client.PostMessageContext(ctx, userID, asUser, attachments)
if err != nil {
return err
}
return err
}