Skip to content

Commit

Permalink
reporters: Remove support for loading message templates from files
Browse files Browse the repository at this point in the history
  • Loading branch information
p12tic committed Mar 8, 2021
1 parent 1abd15a commit 9251a99
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Removed deprecated support for ``template_filename``, ``template_dir`` and ``subject_filename`` configuration parameters of message formatters.
90 changes: 46 additions & 44 deletions master/buildbot/reporters/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@
# Copyright Buildbot Team Members


import os

import jinja2

from twisted.internet import defer

from buildbot import config
from buildbot import util
from buildbot.process.properties import Properties
from buildbot.process.results import CANCELLED
Expand All @@ -30,7 +27,6 @@
from buildbot.process.results import WARNINGS
from buildbot.process.results import statusToString
from buildbot.reporters import utils
from buildbot.warnings import warn_deprecated


def get_detected_status_text(mode, results, previous_results):
Expand Down Expand Up @@ -243,43 +239,43 @@ def render_message_subject(self, context):
return body


class MessageFormatterBaseJinja(MessageFormatterBase):
template_filename = 'default_mail.txt'
default_body_template = '''\
The Buildbot has detected a {{ status_detected }} on builder {{ buildername }} while building {{ projects }}.
Full details are available at:
{{ build_url }}
compare_attrs = ['body_template', 'subject_template', 'template_type']
Buildbot URL: {{ buildbot_url }}
def __init__(self, template_dir=None,
template_filename=None, template=None,
subject_filename=None, subject=None,
template_type=None, **kwargs):
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)
Worker for this Build: {{ workername }}
if template_type is not None:
self.template_type = template_type
Build Reason: {{ build['properties'].get('reason', ["<unknown>"])[0] }}
Blamelist: {{ ", ".join(blamelist) }}
super().__init__(**kwargs)
{{ summary }}
def getTemplate(self, filename, dirname, content):
if content and (filename or dirname):
config.error("Only one of template or template path can be given")
Sincerely,
-The Buildbot
''' # noqa pylint: disable=line-too-long

if content:
return jinja2.Template(content)

if dirname is None:
dirname = os.path.join(os.path.dirname(__file__), "templates")
class MessageFormatterBaseJinja(MessageFormatterBase):
compare_attrs = ['body_template', 'subject_template', 'template_type']
subject_template = None
template_type = 'plain'

loader = jinja2.FileSystemLoader(dirname)
env = jinja2.Environment(
loader=loader, undefined=jinja2.StrictUndefined)
def __init__(self, template=None, subject=None, template_type=None, **kwargs):
if template is None:
template = default_body_template

if filename is None:
filename = self.template_filename
self.body_template = jinja2.Template(template)

if subject is not None:
self.subject_template = jinja2.Template(subject)

if template_type is not None:
self.template_type = template_type

return env.get_template(filename)
super().__init__(**kwargs)

def buildAdditionalContext(self, master, ctx):
pass
Expand All @@ -294,29 +290,35 @@ def render_message_subject(self, context):


class MessageFormatter(MessageFormatterBaseJinja):
template_filename = 'default_mail.txt'

compare_attrs = ['wantProperties', 'wantSteps', 'wantLogs']

def __init__(self, **kwargs):
if 'template_filename' in kwargs:
warn_deprecated('2.10.0',
"template_filename is deprecated, supply the template as text")
if 'subject_filename' in kwargs:
warn_deprecated('2.10.0',
"subject_filename is deprecated, supply the template as text")
super().__init__(**kwargs)

@defer.inlineCallbacks
def format_message_for_build(self, mode, buildername, build, master, blamelist):
ctx = create_context_for_build(mode, buildername, build, master, blamelist)
msgdict = yield self.render_message_dict(master, ctx)
return msgdict


default_missing_template = '''\
The Buildbot working for '{{buildbot_title}}' has noticed that the worker named {{worker.name}} went away.
It last disconnected at {{worker.last_connection}}.
{% if 'admin' in worker['workerinfo'] %}
The admin on record (as reported by WORKER:info/admin) was {{worker.workerinfo.admin}}.
{% endif %}
Sincerely,
-The Buildbot
''' # noqa pylint: disable=line-too-long


class MessageFormatterMissingWorker(MessageFormatterBaseJinja):
template_filename = 'missing_mail.txt'

def __init__(self, template=None, **kwargs):
if template is None:
template = default_missing_template
super().__init__(template=template, **kwargs)

@defer.inlineCallbacks
def formatMessageForMissingWorker(self, master, worker):
ctx = create_context_for_worker(master, worker)
Expand Down
15 changes: 0 additions & 15 deletions master/buildbot/reporters/templates/default_mail.txt

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions master/buildbot/reporters/templates/missing_mail.txt

This file was deleted.

This file was deleted.

15 changes: 1 addition & 14 deletions master/docs/manual/configuration/report_generators/formatter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,15 @@ It formats a message using the Jinja2_ templating language and picks the templat

The constructor of the class takes the following arguments:

``template_dir``
The directory that is used to look for the various templates.
This argument is deprecated, please supply message templates as ``template`` directly.

``template_filename``
This is the name of the file in the ``template_dir`` directory that will be used to generate the body of the mail.
It defaults to ``default_mail.txt``.
This argument is deprecated, please supply message templates as ``template`` directly.

``template``
If this parameter is set, this parameter indicates the content of the template used to generate the body of the mail as string.

``template_type``
This indicates the type of the generated template.
Use either 'plain' (the default) or 'html'.

``subject_filename``
This is the name of the file in the ``template_dir`` directory that contains the content of the subject of the mail.
This argument is deprecated, please supply message templates as ``subject`` directly.

``subject``
Alternatively, this is the content of the subject of the mail as string.
The content of the subject of the mail as string.

``ctx``
This is an extension of the standard context that will be given to the templates.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,15 @@ It formats a message using the Jinja2_ templating language and picks the templat

The constructor to that class takes the same arguments as MessageFormatter, minus ``wantLogs``, ``wantProperties``, ``wantSteps``.

``template_dir``
The directory that is used to look for the various templates.

``template_filename``
This is the name of the file in the ``template_dir`` directory that will be used to generate the body of the mail.
It defaults to ``default_mail.txt``.

``template``
If this parameter is set, this parameter indicates the content of the template used to generate the body of the mail as string.
The content of the template used to generate the body of the mail as string.

``template_type``
This indicates the type of the generated template.
Use either 'plain' (the default) or 'html'.

``subject_filename``
This is the name of the file in the ``template_dir`` directory that contains the content of the subject of the mail.

``subject``
Alternatively, this is the content of the subject of the mail as string.
The content of the subject of the mail as string.


``ctx``
Expand Down

0 comments on commit 9251a99

Please sign in to comment.