Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/brasse/buildbot
Browse files Browse the repository at this point in the history
* 'master' of git://github.com/brasse/buildbot:
  Fixed email creation bug in MailNotifier.
  Refactored to be able to test email creation.
  • Loading branch information
Dustin J. Mitchell committed Mar 13, 2010
2 parents db22729 + 5f9b2e7 commit 0fb4d4d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 23 deletions.
62 changes: 39 additions & 23 deletions buildbot/status/mail.py
Expand Up @@ -26,6 +26,8 @@

VALID_EMAIL = re.compile("[a-zA-Z0-9\.\_\%\-\+]+@[a-zA-Z0-9\.\_\%\-]+.[a-zA-Z]{2,6}")

ENCODING = 'utf8'

class Domain(util.ComparableMixin):
implements(interfaces.IEmailLookup)
compare_attrs = ["domain"]
Expand Down Expand Up @@ -401,54 +403,46 @@ def getCustomMesgData(self, mode, name, build, results, master_status):

return attrs

def buildMessage(self, name, build, results):
if self.customMesg:
# the customMesg stuff can be *huge*, so we prefer not to load it
attrs = self.getCustomMesgData(self.mode, name, build, results, self.master_status)
text, type = self.customMesg(attrs)
msgdict = { 'body' : text, 'type' : type }
else:
msgdict = self.messageFormatter(self.mode, name, build, results, self.master_status)

text = msgdict['body']
def createEmail(self, msgdict, builderName, projectName, results,
patch=None, logs=None):
text = msgdict['body'].encode(ENCODING)
type = msgdict['type']
if 'subject' in msgdict:
subject = msgdict['subject']
subject = msgdict['subject'].encode(ENCODING)
else:
subject = self.subject % { 'result': Results[results],
'projectName': self.master_status.getProjectName(),
'builder': name,
'projectName': projectName,
'builder': builderName,
}


assert type in ('plain', 'html'), "'%s' message type must be 'plain' or 'html'." % type

ss = build.getSourceStamp()
if (ss and ss.patch and self.addPatch) or self.addLogs:
if patch or logs:
m = MIMEMultipart()
m.attach(MIMEText(text, type))
m.attach(MIMEText(text, type, ENCODING))
else:
m = Message()
m.set_payload(text)
m.set_payload(text, ENCODING)
m.set_type("text/%s" % type)

m['Date'] = formatdate(localtime=True)
m['Subject'] = subject
m['From'] = self.fromaddr
# m['To'] is added later

if ss and ss.patch and self.addPatch:
patch = ss.patch
a = MIMEText(patch[1])
if patch:
a = MIMEText(patch[1].encode(ENCODING), _charset=ENCODING)
a.add_header('Content-Disposition', "attachment",
filename="source patch")
m.attach(a)
if self.addLogs:
for log in build.getLogs():
if logs:
for log in logs:
name = "%s.%s" % (log.getStep().getName(),
log.getName())
if self._shouldAttachLog(log.getName()) or self._shouldAttachLog(name):
a = MIMEText(log.getText())
a = MIMEText(log.getText().encode(ENCODING),
_charset=ENCODING)
a.add_header('Content-Disposition', "attachment",
filename=name)
m.attach(a)
Expand All @@ -465,6 +459,28 @@ def buildMessage(self, name, build, results):
continue
m[k] = properties.render(v)

return m

def buildMessage(self, name, build, results):
if self.customMesg:
# the customMesg stuff can be *huge*, so we prefer not to load it
attrs = self.getCustomMesgData(self.mode, name, build, results, self.master_status)
text, type = self.customMesg(attrs)
msgdict = { 'body' : text, 'type' : type }
else:
msgdict = self.messageFormatter(self.mode, name, build, results, self.master_status)

patch = None
ss = build.getSourceStamp()
if ss and ss.patch and self.addPatch:
patch == ss.patch
logs = None
if self.addLogs:
logs = build.getLogs()
twlog.err("LOG: %s" % str(logs))
m = self.createEmail(msgdict, name, self.master_status.getProjectName(),
results, patch, logs)

# now, who is this message going to?
dl = []
recipients = []
Expand Down
46 changes: 46 additions & 0 deletions buildbot/test/unit/test_status_mail_MailNotifier.py
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-

from twisted.trial import unittest

from buildbot.status.builder import SUCCESS
from buildbot.status.mail import MailNotifier

class FakeLog(object):
def __init__(self, text):
self.text = text

def getName(self):
return 'log-name'

def getStep(self):
class FakeStep():
def getName(self):
return 'step-name'
return FakeStep()

def getText(self):
return self.text

class TestMailNotifier(unittest.TestCase):
def test_createEmail_message_without_patch_and_log_contains_unicode(self):
msgdict = dict(body=u'Unicode body with non-ascii (åäö).',
type='plain')
mn = MailNotifier('from@example.org')
m = mn.createEmail(msgdict, u'builder-näme', u'project-näme', SUCCESS)
try:
print m.as_string()
except UnicodeEncodeError:
self.fail('Failed to call as_string() on email message.')

def test_createEmail_message_with_patch_and_log_contains_unicode(self):
msgdict = dict(body=u'Unicode body with non-ascii (åäö).',
type='plain')
patch = ['', u'åäö', '']
logs = [FakeLog(u'Unicode log with non-ascii (åäö).')]
mn = MailNotifier('from@example.org', addLogs=True)
m = mn.createEmail(msgdict, u'builder-näme', u'project-näme', SUCCESS,
patch, logs)
try:
m.as_string()
except UnicodeEncodeError:
self.fail('Failed to call as_string() on email message.')

0 comments on commit 0fb4d4d

Please sign in to comment.