forked from evergreen-ci/evergreen
/
notify.go
825 lines (721 loc) · 26.6 KB
/
notify.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
package notify
import (
"fmt"
"github.com/10gen-labs/slogger/v1"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/build"
"github.com/evergreen-ci/evergreen/model/patch"
"github.com/evergreen-ci/evergreen/model/user"
"github.com/evergreen-ci/evergreen/model/version"
"github.com/evergreen-ci/evergreen/util"
"github.com/evergreen-ci/evergreen/web"
"gopkg.in/yaml.v2"
"io/ioutil"
"net/mail"
"path"
"path/filepath"
"strings"
"time"
)
const (
// number of times to try sending a notification
NumSmtpRetries = 3
// period to sleep between tries
SmtpSleepTime = time.Duration(1) * time.Second
// smtp port to connect to
SmtpPort = 25
// smtp relay host to connect to
SmtpServer = "localhost"
// Thus on each run of the notifier, we check if the last notification time
// (LNT) is within this window. If it is, then we use the retrieved LNT.
// If not, we use the current time
LNRWindow = 60 * time.Minute
// MCI ops notification prefaces
ProvisionFailurePreface = "[PROVISION-FAILURE]"
ProvisionTimeoutPreface = "[PROVISION-TIMEOUT]"
ProvisionLatePreface = "[PROVISION-LATE]"
// repotracker notification prefaces
RepotrackerFailurePreface = "[REPOTRACKER-FAILURE %v] on %v"
// branch name to use if the project reference is not found
UnknownProjectBranch = ""
)
var (
// notifications can be either build-level or task-level
// we might subsequently want to submit js test notifications
// within each task
buildType = "build"
taskType = "task"
// notification key types
buildFailureKey = "build_failure"
buildSuccessKey = "build_success"
buildSuccessToFailureKey = "build_success_to_failure"
buildCompletionKey = "build_completion"
taskFailureKey = "task_failure"
taskSuccessKey = "task_success"
taskSuccessToFailureKey = "task_success_to_failure"
taskCompletionKey = "task_completion"
taskFailureKeys = []string{taskFailureKey, taskSuccessToFailureKey}
buildFailureKeys = []string{buildFailureKey, buildSuccessToFailureKey}
allFailureKeys = []string{taskFailureKey, taskSuccessToFailureKey, buildFailureKey, buildSuccessToFailureKey}
// notification subjects
failureSubject = "failed"
completionSubject = "completed"
successSubject = "succeeded"
transitionSubject = "transitioned to failure"
// task/build status notification prefaces
mciSuccessPreface = "[MCI-SUCCESS %v]"
mciFailurePreface = "[MCI-FAILURE %v]"
mciCompletionPreface = "[MCI-COMPLETION %v]"
// patch notification prefaces
patchSuccessPreface = "[PATCH-SUCCESS %v]"
patchFailurePreface = "[PATCH-FAILURE %v]"
patchCompletionPreface = "[PATCH-COMPLETION %v]"
buildNotificationHandler = BuildNotificationHandler{buildType}
taskNotificationHandler = TaskNotificationHandler{taskType}
Handlers = map[string]NotificationHandler{
buildFailureKey: &BuildFailureHandler{buildNotificationHandler, buildFailureKey},
buildSuccessKey: &BuildSuccessHandler{buildNotificationHandler, buildSuccessKey},
buildCompletionKey: &BuildCompletionHandler{buildNotificationHandler, buildCompletionKey},
buildSuccessToFailureKey: &BuildSuccessToFailureHandler{buildNotificationHandler, buildSuccessToFailureKey},
taskFailureKey: &TaskFailureHandler{taskNotificationHandler, taskFailureKey},
taskSuccessKey: &TaskSuccessHandler{taskNotificationHandler, taskSuccessKey},
taskCompletionKey: &TaskCompletionHandler{taskNotificationHandler, taskCompletionKey},
taskSuccessToFailureKey: &TaskSuccessToFailureHandler{taskNotificationHandler, taskSuccessToFailureKey},
}
)
var (
// These are helpful for situations where
// a current task/build has completed but not the
// previous task/build it is to be compared with
// *Not Yet Implemented*
unprocessedBuilds = []string{}
unprocessedTasks = []string{}
// These help us to limit the tasks/builds we pull
// from the database on each run of the notifier
lastProjectNotificationTime = make(map[string]time.Time)
newProjectNotificationTime = make(map[string]time.Time)
// Simple map for optimization
cachedProjectRecords = make(map[string]interface{})
// Simple map for caching project refs
cachedProjectRef = make(map[string]*model.ProjectRef)
)
func ConstructMailer(notifyConfig evergreen.NotifyConfig) Mailer {
if notifyConfig.SMTP != nil {
return SmtpMailer{
notifyConfig.SMTP.From,
notifyConfig.SMTP.Server,
notifyConfig.SMTP.Port,
notifyConfig.SMTP.UseSSL,
notifyConfig.SMTP.Username,
notifyConfig.SMTP.Password,
}
} else {
return SmtpMailer{
Server: SmtpServer,
Port: SmtpPort,
UseSSL: false,
}
}
}
// This function is responsible for running the notifications pipeline
//
// ParseNotifications
// ↓↓
// ValidateNotifications
// ↓↓
// ProcessNotifications
// ↓↓
// SendNotifications
// ↓↓
// UpdateNotificationTimes
//
func Run(settings *evergreen.Settings) error {
// get the notifications
mciNotification, err := ParseNotifications(settings.ConfigDir)
if err != nil {
evergreen.Logger.Errorf(slogger.ERROR, "Error parsing notifications: %v", err)
return err
}
// validate the notifications
err = ValidateNotifications(settings.ConfigDir, mciNotification)
if err != nil {
evergreen.Logger.Errorf(slogger.ERROR, "Error validating notifications: %v", err)
return err
}
templateGlobals := map[string]interface{}{
"UIRoot": settings.Ui.Url,
}
ae, err := createEnvironment(settings, templateGlobals)
if err != nil {
return err
}
// process the notifications
emails, err := ProcessNotifications(ae, settings.ConfigDir, mciNotification, true)
if err != nil {
evergreen.Logger.Errorf(slogger.ERROR, "Error processing notifications: %v", err)
return err
}
// Remnants are outstanding build/task notifications that
// we couldn't process on a prior run of the notifier
// Currently, this can only happen if an administrator
// bumps up the priority of a build/task
// evergreen.Logger.Logf(slogger.INFO, "Remnant builds %v", len(unprocessedBuilds))
// evergreen.Logger.Logf(slogger.INFO, "Remnant tasks %v", len(unprocessedTasks))
// send the notifications
err = SendNotifications(settings, mciNotification, emails,
ConstructMailer(settings.Notify))
if err != nil {
evergreen.Logger.Errorf(slogger.ERROR, "Error sending notifications: %v", err)
return err
}
// update notification times
err = UpdateNotificationTimes()
if err != nil {
evergreen.Logger.Errorf(slogger.ERROR, "Error updating notification times: %v", err)
return err
}
return nil
}
// This function is responsible for reading the notifications file
func ParseNotifications(configName string) (*MCINotification, error) {
evergreen.Logger.Logf(slogger.INFO, "Parsing notifications...")
configRoot, err := evergreen.FindConfig(configName)
if err != nil {
return nil, err
}
notificationsFile := filepath.Join(configRoot, evergreen.NotificationsFile)
data, err := ioutil.ReadFile(notificationsFile)
if err != nil {
return nil, err
}
// unmarshal file contents into MCINotification struct
mciNotification := &MCINotification{}
err = yaml.Unmarshal(data, mciNotification)
if err != nil {
return nil, fmt.Errorf("Parse error unmarshalling notifications %v: %v", notificationsFile, err)
}
return mciNotification, nil
}
// This function is responsible for validating the notifications file
func ValidateNotifications(configName string, mciNotification *MCINotification) error {
evergreen.Logger.Logf(slogger.INFO, "Validating notifications...")
allNotifications := []string{}
projectNameToBuildVariants, err := findProjectBuildVariants(configName)
if err != nil {
return fmt.Errorf("Error loading project build variants: %v", err)
}
// Validate default notification recipients
for _, notification := range mciNotification.Notifications {
if notification.Project == "" {
return fmt.Errorf("Must specify a project for each notification - see %v", notification.Name)
}
buildVariants, ok := projectNameToBuildVariants[notification.Project]
if !ok {
return fmt.Errorf("Notifications validation failed: "+
"project `%v` not found", notification.Project)
}
// ensure all supplied build variants are valid
for _, buildVariant := range notification.SkipVariants {
if !util.SliceContains(buildVariants, buildVariant) {
return fmt.Errorf("Nonexistent buildvariant - ”%v” - specified for ”%v” notification", buildVariant, notification.Name)
}
}
allNotifications = append(allNotifications, notification.Name)
}
// Validate team name and addresses
for _, team := range mciNotification.Teams {
if team.Name == "" {
return fmt.Errorf("Each notification team must have a name")
}
for _, subscription := range team.Subscriptions {
for _, notification := range subscription.NotifyOn {
if !util.SliceContains(allNotifications, notification) {
return fmt.Errorf("Team ”%v” contains a non-existent subscription - %v", team.Name, notification)
}
}
for _, buildVariant := range subscription.SkipVariants {
buildVariants, ok := projectNameToBuildVariants[subscription.Project]
if !ok {
return fmt.Errorf("Teams validation failed: project `%v` not found", subscription.Project)
}
if !util.SliceContains(buildVariants, buildVariant) {
return fmt.Errorf("Nonexistent buildvariant - ”%v” - specified for team ”%v” ", buildVariant, team.Name)
}
}
}
}
// Validate patch notifications
for _, subscription := range mciNotification.PatchNotifications {
for _, notification := range subscription.NotifyOn {
if !util.SliceContains(allNotifications, notification) {
return fmt.Errorf("Nonexistent patch notification - ”%v” - specified", notification)
}
}
}
// validate the patch notification buildvariatns
for _, subscription := range mciNotification.PatchNotifications {
buildVariants, ok := projectNameToBuildVariants[subscription.Project]
if !ok {
return fmt.Errorf("Patch notification build variants validation failed: "+
"project `%v` not found", subscription.Project)
}
for _, buildVariant := range subscription.SkipVariants {
if !util.SliceContains(buildVariants, buildVariant) {
return fmt.Errorf("Nonexistent buildvariant - ”%v” - specified for patch notifications", buildVariant)
}
}
}
// all good!
return nil
}
// This function is responsible for all notifications processing
func ProcessNotifications(ae *web.App, configName string, mciNotification *MCINotification, updateTimes bool) (map[NotificationKey][]Email, error) {
// create MCI notifications
allNotificationsSlice := notificationsToStruct(mciNotification)
// get the last notification time for all projects
if updateTimes {
err := getLastProjectNotificationTime(allNotificationsSlice)
if err != nil {
return nil, err
}
}
evergreen.Logger.Logf(slogger.INFO, "Processing notifications...")
emails := make(map[NotificationKey][]Email)
for _, key := range allNotificationsSlice {
emailsForKey, err := Handlers[key.NotificationName].GetNotifications(ae, configName, &key)
if err != nil {
evergreen.Logger.Logf(slogger.INFO, "Error processing %v on %v: %v", key.NotificationName, key.Project, err)
continue
}
emails[key] = emailsForKey
}
return emails, nil
}
// This function is responsible for managing the sending triggered email notifications
func SendNotifications(settings *evergreen.Settings, mciNotification *MCINotification,
emails map[NotificationKey][]Email, mailer Mailer) (err error) {
evergreen.Logger.Logf(slogger.INFO, "Sending notifications...")
// parse all notifications, sending it to relevant recipients
for _, notification := range mciNotification.Notifications {
key := NotificationKey{
Project: notification.Project,
NotificationName: notification.Name,
NotificationType: getType(notification.Name),
NotificationRequester: evergreen.RepotrackerVersionRequester,
}
for _, recipient := range notification.Recipients {
// send all triggered notifications
for _, email := range emails[key] {
// determine if this notification should be skipped - based on the buildvariant
if email.ShouldSkip(notification.SkipVariants) {
continue
}
// send to individual subscriber, or the admin team if it's not their fault
recipients := []string{}
if settings.Notify.SMTP != nil {
recipients = settings.Notify.SMTP.AdminEmail
}
if !email.IsLikelySystemFailure() {
recipients = email.GetRecipients(recipient)
}
err = TrySendNotification(recipients, email.GetSubject(), email.GetBody(), mailer)
if err != nil {
evergreen.Logger.Errorf(slogger.ERROR, "Unable to send "+
"individual notification %#v: %v", key, err)
continue
}
}
}
}
// Send team subscribed notifications
for _, team := range mciNotification.Teams {
for _, subscription := range team.Subscriptions {
for _, name := range subscription.NotifyOn {
key := NotificationKey{
Project: subscription.Project,
NotificationName: name,
NotificationType: getType(name),
NotificationRequester: evergreen.RepotrackerVersionRequester,
}
// send all triggered notifications for this key
for _, email := range emails[key] {
// determine if this notification should be skipped - based on the buildvariant
if email.ShouldSkip(subscription.SkipVariants) {
continue
}
teamEmail := fmt.Sprintf("%v <%v>", team.Name, team.Address)
err = TrySendNotification([]string{teamEmail}, email.GetSubject(), email.GetBody(), mailer)
if err != nil {
evergreen.Logger.Errorf(slogger.ERROR, "Unable to send notification %#v: %v", key, err)
continue
}
}
}
}
}
// send patch notifications
for _, subscription := range mciNotification.PatchNotifications {
for _, notification := range subscription.NotifyOn {
key := NotificationKey{
Project: subscription.Project,
NotificationName: notification,
NotificationType: getType(notification),
NotificationRequester: evergreen.PatchVersionRequester,
}
for _, email := range emails[key] {
// determine if this notification should be skipped -
// based on the buildvariant
if email.ShouldSkip(subscription.SkipVariants) {
continue
}
// send to the appropriate patch requester
for _, changeInfo := range email.GetChangeInfo() {
// send notification to each member of the blamelist
patchRequester := fmt.Sprintf("%v <%v>", changeInfo.Author,
changeInfo.Email)
err = TrySendNotification([]string{patchRequester},
email.GetSubject(), email.GetBody(), mailer)
if err != nil {
evergreen.Logger.Errorf(slogger.ERROR, "Unable to send "+
"notification %#v: %v", key, err)
continue
}
}
}
}
}
return nil
}
// This stores the last time threshold after which
// we search for possible new notification events
func UpdateNotificationTimes() (err error) {
evergreen.Logger.Logf(slogger.INFO, "Updating notification times...")
for project, time := range newProjectNotificationTime {
evergreen.Logger.Logf(slogger.INFO, "Updating %v notification time...", project)
err = model.SetLastNotificationsEventTime(project, time)
if err != nil {
return err
}
}
return nil
}
//***********************************\/
// Notification Helper Functions \/
//***********************************\/
// Construct a map of project names to build variants for that project
func findProjectBuildVariants(configName string) (map[string][]string, error) {
projectNameToBuildVariants := make(map[string][]string)
allProjects, err := model.FindAllTrackedProjectRefs()
if err != nil {
return nil, err
}
for _, projectRef := range allProjects {
var buildVariants []string
var proj *model.Project
var err error
if projectRef.LocalConfig != "" {
proj, err = model.FindProject("", &projectRef)
if err != nil {
return nil, fmt.Errorf("unable to find project file: %v", err)
}
} else {
lastGood, err := version.FindOne(version.ByLastKnownGoodConfig(projectRef.Identifier))
if err != nil {
return nil, fmt.Errorf("unable to find last valid config: %v", err)
}
if lastGood == nil { // brand new project + no valid config yet, just return an empty map
return projectNameToBuildVariants, nil
}
proj = &model.Project{}
err = model.LoadProjectInto([]byte(lastGood.Config), projectRef.Identifier, proj)
if err != nil {
return nil, fmt.Errorf("Error loading project from version: %v", err)
}
}
for _, buildVariant := range proj.BuildVariants {
buildVariants = append(buildVariants, buildVariant.Name)
}
projectNameToBuildVariants[projectRef.Identifier] = buildVariants
}
return projectNameToBuildVariants, nil
}
// construct the change information
// struct from a given version struct
func constructChangeInfo(v *version.Version, notification *NotificationKey) (changeInfo *ChangeInfo) {
changeInfo = &ChangeInfo{}
switch notification.NotificationRequester {
case evergreen.RepotrackerVersionRequester:
changeInfo.Project = v.Identifier
changeInfo.Author = v.Author
changeInfo.Message = v.Message
changeInfo.Revision = v.Revision
changeInfo.Email = v.AuthorEmail
case evergreen.PatchVersionRequester:
// get the author and description from the patch request
patch, err := patch.FindOne(patch.ByVersion(v.Id))
if err != nil {
evergreen.Logger.Errorf(slogger.ERROR, "Error finding patch for version %v: %v", v.Id, err)
return
}
if patch == nil {
evergreen.Logger.Errorf(slogger.ERROR, "%v notification was unable to locate patch with version: %v", notification, v.Id)
return
}
// get the display name and email for this user
dbUser, err := user.FindOne(user.ById(patch.Author))
if err != nil {
evergreen.Logger.Errorf(slogger.ERROR, "Error finding user %v: %v",
patch.Author, err)
changeInfo.Author = patch.Author
changeInfo.Email = patch.Author
} else if dbUser == nil {
evergreen.Logger.Errorf(slogger.ERROR, "User %v not found", patch.Author)
changeInfo.Author = patch.Author
changeInfo.Email = patch.Author
} else {
changeInfo.Email = dbUser.Email()
changeInfo.Author = dbUser.DisplayName()
}
changeInfo.Project = patch.Project
changeInfo.Message = patch.Description
changeInfo.Revision = patch.Id.Hex()
}
return
}
// use mail's rfc2047 to encode any string
func encodeRFC2047(String string) string {
addr := mail.Address{String, ""}
return strings.Trim(addr.String(), " <>")
}
// get the display name for a build variant's
// task given the build variant name
func getDisplayName(buildVariant string) (displayName string) {
build, err := build.FindOne(build.ByVariant(buildVariant))
if err != nil || build == nil {
evergreen.Logger.Errorf(slogger.ERROR, "Error fetching buildvariant name: %v", err)
displayName = buildVariant
} else {
displayName = build.DisplayName
}
return
}
// get the failed task(s) for a given build
func getFailedTasks(current *build.Build, notificationName string) (failedTasks []build.TaskCache) {
if util.SliceContains(buildFailureKeys, notificationName) {
for _, task := range current.Tasks {
if task.Status == evergreen.TaskFailed {
failedTasks = append(failedTasks, task)
}
}
}
return
}
// get the specific failed test(s) for this task
func getFailedTests(current *model.Task, notificationName string) (failedTests []model.TestResult) {
if util.SliceContains(taskFailureKeys, notificationName) {
for _, test := range current.TestResults {
if test.Status == "fail" {
// get the base name for windows/non-windows paths
test.TestFile = path.Base(strings.Replace(test.TestFile, "\\", "/", -1))
failedTests = append(failedTests, test)
}
}
}
return
}
// gets the project ref project name corresponding to this identifier
func getProjectRef(identifier string) (projectRef *model.ProjectRef,
err error) {
if cachedProjectRef[identifier] == nil {
projectRef, err = model.FindOneProjectRef(identifier)
if err != nil {
return
}
cachedProjectRecords[identifier] = projectRef
return projectRef, nil
}
return cachedProjectRef[identifier], nil
}
// This gets the time threshold - events before which
// we searched for possible notification events
func getLastProjectNotificationTime(keys []NotificationKey) error {
for _, key := range keys {
lastNotificationTime, err := model.LastNotificationsEventTime(key.Project)
if err != nil {
return err
}
if lastNotificationTime.Before(time.Now().Add(-LNRWindow)) {
lastNotificationTime = time.Now()
}
lastProjectNotificationTime[key.Project] = lastNotificationTime
newProjectNotificationTime[key.Project] = time.Now()
}
return nil
}
// This is used to pull recently finished builds
func getRecentlyFinishedBuilds(notificationKey *NotificationKey) (builds []build.Build, err error) {
if cachedProjectRecords[notificationKey.String()] == nil {
builds, err = build.Find(build.ByFinishedAfter(lastProjectNotificationTime[notificationKey.Project], notificationKey.Project, notificationKey.NotificationRequester))
if err != nil {
return nil, err
}
cachedProjectRecords[notificationKey.String()] = builds
return builds, err
}
return cachedProjectRecords[notificationKey.String()].([]build.Build), nil
}
// This is used to pull recently finished tasks
func getRecentlyFinishedTasks(notificationKey *NotificationKey) (tasks []model.Task, err error) {
if cachedProjectRecords[notificationKey.String()] == nil {
tasks, err = model.RecentlyFinishedTasks(lastProjectNotificationTime[notificationKey.Project], notificationKey.Project, notificationKey.NotificationRequester)
if err != nil {
return nil, err
}
cachedProjectRecords[notificationKey.String()] = tasks
return tasks, err
}
return cachedProjectRecords[notificationKey.String()].([]model.Task), nil
}
// gets the type of notification - we support build/task level notification
func getType(notification string) (nkType string) {
nkType = taskType
if strings.Contains(notification, buildType) {
nkType = buildType
}
return
}
// creates/returns slice of 'relevant' NotificationKeys a
// notification is relevant if it has at least one recipient
func notificationsToStruct(mciNotification *MCINotification) (notifyOn []NotificationKey) {
// Get default notifications
for _, notification := range mciNotification.Notifications {
if len(notification.Recipients) != 0 {
// flag the notification as needed
key := NotificationKey{
Project: notification.Project,
NotificationName: notification.Name,
NotificationType: getType(notification.Name),
NotificationRequester: evergreen.RepotrackerVersionRequester,
}
// prevent duplicate notifications from being sent
if !util.SliceContains(notifyOn, key) {
notifyOn = append(notifyOn, key)
}
}
}
// Get team notifications
for _, team := range mciNotification.Teams {
for _, subscription := range team.Subscriptions {
for _, name := range subscription.NotifyOn {
key := NotificationKey{
Project: subscription.Project,
NotificationName: name,
NotificationType: getType(name),
NotificationRequester: evergreen.RepotrackerVersionRequester,
}
// prevent duplicate notifications from being sent
if !util.SliceContains(notifyOn, key) {
notifyOn = append(notifyOn, key)
}
}
}
}
// Get patch notifications
for _, subscription := range mciNotification.PatchNotifications {
for _, notification := range subscription.NotifyOn {
key := NotificationKey{
Project: subscription.Project,
NotificationName: notification,
NotificationType: getType(notification),
NotificationRequester: evergreen.PatchVersionRequester,
}
// prevent duplicate notifications from being sent
if !util.SliceContains(notifyOn, key) {
notifyOn = append(notifyOn, key)
}
}
}
return
}
// NotifyAdmins is a helper method to send a notification to the MCI admin team
func NotifyAdmins(subject, message string, settings *evergreen.Settings) error {
if settings.Notify.SMTP != nil {
return TrySendNotification(settings.Notify.SMTP.AdminEmail,
subject, message, ConstructMailer(settings.Notify))
}
return evergreen.Logger.Errorf(slogger.ERROR, "Cannot notify admins: admin_email not set")
}
// String method for notification key
func (nk NotificationKey) String() string {
return fmt.Sprintf("%v-%v-%v", nk.Project, nk.NotificationType, nk.NotificationRequester)
}
// Helper function to send notifications
func TrySendNotification(recipients []string, subject, body string, mailer Mailer) (err error) {
// evergreen.Logger.Logf(slogger.DEBUG, "address: %v subject: %v body: %v", recipients, subject, body)
// return nil
_, err = util.RetryArithmeticBackoff(func() error {
err = mailer.SendMail(recipients, subject, body)
if err != nil {
evergreen.Logger.Errorf(slogger.ERROR, "Error sending notification: %v", err)
return util.RetriableError{err}
}
return nil
}, NumSmtpRetries, SmtpSleepTime)
return err
}
// Helper function to send notification to a given user
func TrySendNotificationToUser(userId string, subject, body string, mailer Mailer) (err error) {
dbUser, err := user.FindOne(user.ById(userId))
if err != nil {
return fmt.Errorf("Error finding user %v: %v", userId, err)
} else if dbUser == nil {
return fmt.Errorf("User %v not found", userId)
} else {
return TrySendNotification([]string{dbUser.Email()}, subject, body, mailer)
}
}
//***************************\/
// Notification Structs \/
//***************************\/
// stores supported notifications
type Notification struct {
Name string `yaml:"name"`
Project string `yaml:"project"`
Recipients []string `yaml:"recipients"`
SkipVariants []string `yaml:"skip_variants"`
}
// stores notifications subscription for a team
type Subscription struct {
Project string `yaml:"project"`
SkipVariants []string `yaml:"skip_variants"`
NotifyOn []string `yaml:"notify_on"`
}
// stores 10gen team information
type Team struct {
Name string `yaml:"name"`
Address string `yaml:"address"`
Subscriptions []Subscription `yaml:"subscriptions"`
}
// store notifications file
type MCINotification struct {
Notifications []Notification `yaml:"notifications"`
Teams []Team `yaml:"teams"`
PatchNotifications []Subscription `yaml:"patch_notifications"`
}
// stores high level notifications key
type NotificationKey struct {
Project string
NotificationName string
NotificationType string
NotificationRequester string
}
// stores information pertaining to a repository's changes
type ChangeInfo struct {
Revision string
Author string
Email string
Pushtime string
Project string
Message string
}