Skip to content

Commit

Permalink
Merge pull request #2457 from benallard/messageFormatter
Browse files Browse the repository at this point in the history
Message formater improvements
  • Loading branch information
tardyp committed Oct 25, 2016
2 parents 7320f02 + a6c2010 commit b12616e
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 168 deletions.
6 changes: 4 additions & 2 deletions master/buildbot/reporters/mail.py
Expand Up @@ -242,7 +242,8 @@ def buildsetComplete(self, key, msg):
self.master, bsid,
wantProperties=self.messageFormatter.wantProperties,
wantSteps=self.messageFormatter.wantSteps,
wantPreviousBuild=self.wantPreviousBuild())
wantPreviousBuild=self.wantPreviousBuild(),
wantLogs=self.messageFormatter.wantLogs)

builds = res['builds']
buildset = res['buildset']
Expand All @@ -262,7 +263,8 @@ def buildComplete(self, key, build):
self.master, buildset, [build],
wantProperties=self.messageFormatter.wantProperties,
wantSteps=self.messageFormatter.wantSteps,
wantPreviousBuild=self.wantPreviousBuild())
wantPreviousBuild=self.wantPreviousBuild(),
wantLogs=self.messageFormatter.wantLogs)
# only include builds for which isMailNeeded returns true
if self.isMailNeeded(build):
self.buildMessage(
Expand Down
84 changes: 66 additions & 18 deletions master/buildbot/reporters/message.py
@@ -1,7 +1,23 @@
# This file is part of Buildbot. Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members

import os

import jinja2

from buildbot import config
from buildbot.process.results import CANCELLED
from buildbot.process.results import EXCEPTION
from buildbot.process.results import FAILURE
Expand All @@ -12,25 +28,54 @@


class MessageFormatter(object):
template_name = 'default_mail.txt'
template_filename = 'default_mail.txt'
template_type = 'plain'
wantProperties = True
wantSteps = False

def __init__(self, template_name=None, template_dir=None, template_type=None):
def __init__(self, template_dir=None,
template_filename=None, template=None, template_name=None,
subject_filename=None, subject=None,
template_type=None, ctx=None,
wantProperties=True, wantSteps=False, wantLogs=False):

if template_dir is None:
template_dir = os.path.join(os.path.dirname(__file__), "templates")
if template_name is not None:
config.warnDeprecated('0.9.1', "template_name is deprecated, use template_filename")
template_filename = template_name

loader = jinja2.FileSystemLoader(template_dir)
self.env = jinja2.Environment(
loader=loader, undefined=jinja2.StrictUndefined)
self.body_template = self.getTemplate(template_filename, template_dir, template)
self.subject_template = None
if subject_filename or subject:
self.subject_template = self.getTemplate(subject_filename, template_dir, subject)

if template_name is not None:
self.template_name = template_name
if template_type is not None:
self.template_type = template_type

if ctx is None:
ctx = {}

self.ctx = ctx
self.wantProperties = wantProperties
self.wantSteps = wantSteps
self.wantLogs = wantLogs

def getTemplate(self, filename, dirname, content):
if content and (filename or dirname):
config.error("Only one of template or template path can be given")

if content:
return jinja2.Template(content)

if dirname is None:
dirname = os.path.join(os.path.dirname(__file__), "templates")

loader = jinja2.FileSystemLoader(dirname)
env = jinja2.Environment(
loader=loader, undefined=jinja2.StrictUndefined)

if filename is None:
filename = self.template_filename

return env.get_template(filename)

def getDetectedStatus(self, mode, results, previous_results):

if results == FAILURE:
Expand All @@ -40,7 +85,7 @@ def getDetectedStatus(self, mode, results, previous_results):
else:
text = "failed build"
elif results == WARNINGS:
text = "The Buildbot has detected a problem in the build"
text = "problem in the build"
elif results == SUCCESS:
if "change" in mode and previous_results is not None and previous_results != results:
text = "restored build"
Expand Down Expand Up @@ -109,13 +154,12 @@ def messageSummary(self, build, results):
return text

def __call__(self, mode, buildername, buildset, build, master, previous_results, blamelist):
"""Generate a buildbot mail message and return a tuple of message text
and type."""
"""Generate a buildbot mail message and return a dictionary
containing the message body, type and subject."""
ss_list = buildset['sourcestamps']
results = build['results']

tpl = self.env.get_template(self.template_name)
cxt = dict(results=build['results'],
ctx = dict(results=build['results'],
mode=mode,
buildername=buildername,
workername=build['properties'].get(
Expand All @@ -133,5 +177,9 @@ def __call__(self, mode, buildername, buildset, build, master, previous_results,
summary=self.messageSummary(build, results),
sourcestamps=self.messageSourceStamps(ss_list)
)
contents = tpl.render(cxt)
return {'body': contents, 'type': self.template_type}
ctx.update(self.ctx)
body = self.body_template.render(ctx)
email = {'body': body, 'type': self.template_type}
if self.subject_template is not None:
email['subject'] = self.subject_template.render(ctx)
return email
14 changes: 14 additions & 0 deletions master/buildbot/test/unit/test_reporters_message.py
Expand Up @@ -87,6 +87,20 @@ def test_message_success(self):
Sincerely,
-The Buildbot'''))
self.assertTrue('subject' not in res)

@defer.inlineCallbacks
def test_inline_template(self):
self.message = message.MessageFormatter(template="URL: {{ build_url }} -- {{ summary }}")
res = yield self.doOneTest(SUCCESS, SUCCESS)
self.assertEqual(res['type'], "plain")
self.assertEqual(res['body'], "URL: http://localhost:8080/#builders/80/builds/1 -- Build succeeded!")

@defer.inlineCallbacks
def test_inline_subject(self):
self.message = message.MessageFormatter(subject="subject")
res = yield self.doOneTest(SUCCESS, SUCCESS)
self.assertEqual(res['subject'], "subject")

@defer.inlineCallbacks
def test_message_failure(self):
Expand Down

0 comments on commit b12616e

Please sign in to comment.