-
Notifications
You must be signed in to change notification settings - Fork 7
/
slack.go
409 lines (377 loc) · 12.4 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
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
Releases bool
BuildStatus bool
ReleaseManagerError 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) {
if token == "" {
log.Infof("slack: skipping: no token, so no slack notification")
return &Client{
muteOptions: MuteOptions{
Flux: true,
Kubernetes: true,
Policy: true,
ReleaseProcessed: true,
Releases: true,
BuildStatus: true,
ReleaseManagerError: true,
},
}, nil
}
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) {
if token == "" {
log.Infof("slack: skipping: no token, so no slack notification")
return &Client{
muteOptions: MuteOptions{
Flux: true,
Kubernetes: true,
Policy: true,
ReleaseProcessed: true,
Releases: true,
BuildStatus: true,
ReleaseManagerError: true,
},
}, nil
}
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) {
if c.muteOptions.BuildStatus {
return "", "", nil
}
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) {
if c.muteOptions.BuildStatus {
return "", "", nil
}
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 {
if c.muteOptions.Releases {
return nil
}
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
}
func (c *Client) NotifyReleaseManagerError(ctx context.Context, msgType, service, environment, branch, namespace, actorEmail string, inputErr error) error {
if c.muteOptions.ReleaseManagerError {
return nil
}
userID, err := c.getIdByEmail(ctx, actorEmail)
if err != nil {
// If user id somehow couldn't be found, post the message to #squad-nasa
log.With("actorEmail", actorEmail).Infof("slack: skipping: no user id found, so no slack notification")
return nil
}
asUser := slack.MsgOptionAsUser(true)
attachments := slack.MsgOptionAttachments(slack.Attachment{
Title: fmt.Sprintf(":boom: Release Manager failed :x:"),
Color: MsgColorRed,
Text: generateSlackMessage(msgType, service, environment, branch, namespace, inputErr),
MarkdownIn: []string{"text", "fields"},
})
_, _, err = c.client.PostMessageContext(ctx, userID, asUser, attachments)
if err != nil {
return err
}
return nil
}
func generateSlackMessage(msgType, service, environment, branch, namespace string, err error) string {
switch {
case msgType == "promote" && service != "" && environment != "" && branch != "":
return fmt.Sprintf("Failed promoting %s #%s in %s. Validate the options you used and try promoting again.\nError: %s", service, branch, environment, err)
case msgType == "release.branch" && service != "" && environment != "" && branch != "":
return fmt.Sprintf("Failed releasing %s #%s to %s. Validate the options you used and try releasing again.\nError: %s", service, branch, environment, err)
default:
return fmt.Sprintf("Failed handling event in release manager for:\nEvent: %s\nService: %s\nEnvironment: %s\nBranch: %s\nNamespace: %s\nError: %s", msgType, service, environment, branch, namespace, err)
}
}