github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/notify/email.go (about)

     1  package notify
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/evergreen-ci/evergreen/util"
     7  	"github.com/mongodb/grip"
     8  )
     9  
    10  // Interface around an email that may be sent as a notification. Provides
    11  // getters for body, subject, and change info, and functions to determine
    12  // if this email should be skipped.
    13  type Email interface {
    14  	GetBody() string
    15  	GetSubject() string
    16  	// for system-related failures, we only want to notify the admins
    17  	// regardless of who the default recipient is
    18  	IsLikelySystemFailure() bool
    19  	GetRecipients(defaultRecipient string) []string
    20  	GetChangeInfo() []ChangeInfo
    21  	// Should you skip sending this email given the provided variants to skip
    22  	ShouldSkip(skipVariants []string) bool
    23  }
    24  
    25  // "Base class" for structs that implement the Email interface. Stores the
    26  // basics that any email should have.
    27  type EmailBase struct {
    28  	Body       string
    29  	Subject    string
    30  	ChangeInfo []ChangeInfo
    31  }
    32  
    33  func (self *EmailBase) GetBody() string {
    34  	return self.Body
    35  }
    36  
    37  func (self *EmailBase) GetSubject() string {
    38  	return self.Subject
    39  }
    40  
    41  func (self *EmailBase) GetChangeInfo() []ChangeInfo {
    42  	return self.ChangeInfo
    43  }
    44  
    45  // Implements Email interface for build-specific emails. Defines skip logic for
    46  // emails specific to build
    47  type BuildEmail struct {
    48  	EmailBase
    49  	Trigger TriggeredBuildNotification
    50  }
    51  
    52  func (self *BuildEmail) ShouldSkip(skipVariants []string) bool {
    53  	buildVariant := self.Trigger.Current.BuildVariant
    54  	if util.SliceContains(skipVariants, buildVariant) {
    55  		grip.Debugf("Skipping buildvariant %s '%s' notification: '%s'",
    56  			buildVariant, self.Trigger.Key.NotificationName, self.Subject)
    57  		return true
    58  	}
    59  	return false
    60  }
    61  
    62  func (self *BuildEmail) IsLikelySystemFailure() bool {
    63  	return false
    64  }
    65  
    66  func (self *BuildEmail) GetRecipients(defaultRecipient string) []string {
    67  	return []string{defaultRecipient}
    68  }
    69  
    70  // Implements Email interface for task-specific emails. Defines skip logic for
    71  // emails specific to tasks
    72  type TaskEmail struct {
    73  	EmailBase
    74  	Trigger TriggeredTaskNotification
    75  }
    76  
    77  func (self *TaskEmail) ShouldSkip(skipVariants []string) bool {
    78  	// skip the buildvariant notification if necessary
    79  	buildVariant := self.Trigger.Current.BuildVariant
    80  	if util.SliceContains(skipVariants, buildVariant) {
    81  		grip.Debugf("Skipping buildvariant %s '%s' notification: '%s'",
    82  			buildVariant, self.Trigger.Key.NotificationName, self.Subject)
    83  		return true
    84  	}
    85  	return false
    86  }
    87  
    88  func (self *TaskEmail) IsLikelySystemFailure() bool {
    89  	return strings.Contains(self.GetSubject(), UnresponsiveMessage)
    90  }
    91  
    92  func (self *TaskEmail) GetRecipients(defaultRecipient string) []string {
    93  	return []string{defaultRecipient}
    94  }