Ticket #175: customMailNotifier-v2.diff

File customMailNotifier-v2.diff, 5.5 KB (added by bhearsum, 4 years ago)

mail notifier custom messages without Deferred support.

  • buildbot/status/mail.py

    diff -rN -u old-upstream-buildbot-mirror/buildbot/status/mail.py new-upstream-buildbot-mirror/buildbot/status/mail.py
    old new  
    6363                 addLogs=False, relayhost="localhost", 
    6464                 subject="buildbot %(result)s in %(projectName)s on %(builder)s", 
    6565                 lookup=None, extraRecipients=[], 
    66                  sendToInterestedUsers=True): 
     66                 sendToInterestedUsers=True, 
     67                 messageGenerator=None): 
    6768        """ 
    6869        @type  fromaddr: string 
    6970        @param fromaddr: the email address to be used in the 'From' header. 
     
    127128                          example, lookup='twistedmatrix.com' will allow mail 
    128129                          to be sent to all developers whose SVN usernames 
    129130                          match their twistedmatrix.com account names. 
     131        @type  messageGenerator: callable 
     132        @param messageGenerator: Normally the message sent with a MailNotifier 
     133                                 is built with MailNotifier.buildMessageBody. 
     134                                 You may optionally use an alternative function. 
     135                                 The signature of it should be as follows: 
     136                                  func(builderName, mailNotifierMode, 
     137                                       results, IBuildStatus, IStatus) 
     138                                 where results is one of (SUCCESS, WARNINGS, 
     139                                 FAILURE, SKIPPED, EXCEPTION). It should return 
     140                                 a string that will be used as the body of the 
     141                                 message sent by MailNotifier. 
    130142        """ 
    131143 
    132144        base.StatusReceiverMultiService.__init__(self) 
     
    151163        self.lookup = lookup 
    152164        self.watched = [] 
    153165        self.status = None 
     166        assert messageGenerator is None or callable(messageGenerator) 
     167        self.messageGenerator = messageGenerator 
    154168 
    155169        # you should either limit on builders or categories, not both 
    156170        if self.builders != None and self.categories != None: 
     
    216230        # rearrange this. 
    217231        return self.buildMessage(name, build, results) 
    218232 
    219     def buildMessage(self, name, build, results): 
     233    def buildMessageBody(self, name, mode, results, build, masterStatus): 
    220234        projectName = self.status.getProjectName() 
    221235        text = "" 
    222         if self.mode == "all": 
     236        if mode == "all": 
    223237            text += "The Buildbot has finished a build" 
    224         elif self.mode == "failing": 
     238        elif mode == "failing": 
    225239            text += "The Buildbot has detected a failed build" 
    226240        elif self.mode == "passing": 
    227241            text += "The Buildbot has detected a passing build" 
    228242        else: 
    229243            text += "The Buildbot has detected a new failure" 
    230244        text += " of %s on %s.\n" % (name, projectName) 
    231         buildurl = self.status.getURLForThing(build) 
     245        buildurl = masterStatus.getURLForThing(build) 
    232246        if buildurl: 
    233247            text += "Full details are available at:\n %s\n" % buildurl 
    234248        text += "\n" 
    235249 
    236         url = self.status.getBuildbotURL() 
     250        url = masterStatus.getBuildbotURL() 
    237251        if url: 
    238252            text += "Buildbot URL: %s\n\n" % urllib.quote(url, '/:') 
    239253 
    240254        text += "Buildslave for this Build: %s\n\n" % build.getSlavename() 
    241255        text += "Build Reason: %s\n" % build.getReason() 
    242256 
    243         patch = None 
    244257        ss = build.getSourceStamp() 
    245258        if ss is None: 
    246259            source = "unavailable" 
     
    254267                source += "HEAD" 
    255268            if ss.patch is not None: 
    256269                source += " (plus patch)" 
    257                 patch = ss.patch 
    258270        text += "Build Source Stamp: %s\n" % source 
    259271 
    260272        text += "Blamelist: %s\n" % ",".join(build.getResponsibleUsers()) 
     
    270282 
    271283        if results == SUCCESS: 
    272284            text += "Build succeeded!\n" 
    273             res = "success" 
    274285        elif results == WARNINGS: 
    275286            text += "Build Had Warnings%s\n" % t 
    276             res = "warnings" 
    277287        else: 
    278288            text += "BUILD FAILED%s\n" % t 
    279             res = "failure" 
    280  
    281         if self.addLogs and build.getLogs(): 
    282             text += "Logs are attached.\n" 
    283289 
    284290        # TODO: it would be nice to provide a URL for the specific build 
    285291        # here. That involves some coordination with html.Waterfall . 
     
    293299        text += " -The Buildbot\n" 
    294300        text += "\n" 
    295301 
     302        return text 
     303 
     304    def buildMessage(self, name, build, results): 
     305        projectName = self.status.getProjectName() 
     306 
     307        text = "" 
     308        if self.messageGenerator: 
     309            text = self.messageGenerator(name, self.mode, results, build, 
     310                                         self.status) 
     311        else: 
     312            text = self.buildMessageBody(name, self.mode, results, build, 
     313                                         self.status) 
     314 
     315        if self.addLogs and build.getLogs(): 
     316            text += "P.S. Logs are attached" 
     317 
     318        patch = None 
     319        ss = build.getSourceStamp() 
     320        if ss and ss.patch: 
     321            patch = ss.patch 
     322 
     323        res = "" 
     324        if results == SUCCESS: 
     325            res = "success" 
     326        elif results == WARNINGS: 
     327            res = "warnings" 
     328        else: 
     329            res = "failure" 
     330 
    296331        haveAttachments = False 
    297332        if patch or self.addLogs: 
    298333            haveAttachments = True