Skip to content

Commit

Permalink
MessageFormatter: Allow to give the template inline
Browse files Browse the repository at this point in the history
  • Loading branch information
Benoît Allard committed Oct 25, 2016
1 parent eb6ee84 commit e821c63
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
26 changes: 16 additions & 10 deletions master/buildbot/reporters/message.py
Expand Up @@ -17,7 +17,7 @@

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 @@ -33,19 +33,25 @@ class MessageFormatter(object):
wantProperties = True
wantSteps = False

def __init__(self, template_name=None, template_dir=None, template_type=None):
def __init__(self, template_name=None, template_dir=None, template=None, template_type=None):

if (template is not None) and ((template_name is not None) or (template_dir is not None)):
config.error("Only one of template or template path can be given")

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

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

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

self.template = env.get_template(self.template_name)
self.template = env.get_template(self.template_name)
else:
self.template = jinja2.Template(template)

if template_type is not None:
self.template_type = template_type
Expand Down
7 changes: 7 additions & 0 deletions master/buildbot/test/unit/test_reporters_message.py
Expand Up @@ -88,6 +88,13 @@ def test_message_success(self):
Sincerely,
-The Buildbot'''))

@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_message_failure(self):
res = yield self.doOneTest(SUCCESS, FAILURE)
Expand Down

0 comments on commit e821c63

Please sign in to comment.