We want to report on "changes" triggered by a timer. Since those aren't really changes, they are represented by a class called NullChange which carries as much of the interface for a Change object as possible (not much) so that they can be enqueued in pipelines. If a NullChange is reportable, then pretty much any kind of change should be considered reportable. So remove the flag that indicates that NullChanges and Refs are not reportable. Only attempt to format a change report if there is an action defined for that pipeline (in case the default formatting process attempts to access a change attribute that is inappropriate). Remove checks that try to avoid formatting or sending a report based on attributes of the change (which are no longer relevant). Add a test for using a timer trigger with an smtp reporter. Add validation of the attributes that an smtp reporter can use in the layout file. Allow the operator to configure a Subject for smtp reports. Change-Id: Icd067a7600c2922a318b9ade11b3946df4e53065
72 lines
2.5 KiB
Python
72 lines
2.5 KiB
Python
# Copyright 2013 Rackspace Australia
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
import logging
|
|
import smtplib
|
|
|
|
from email.mime.text import MIMEText
|
|
|
|
|
|
class Reporter(object):
|
|
"""Sends off reports to emails via SMTP."""
|
|
|
|
name = 'smtp'
|
|
log = logging.getLogger("zuul.reporter.smtp.Reporter")
|
|
|
|
def __init__(self, smtp_default_from, smtp_default_to,
|
|
smtp_server='localhost', smtp_port=25):
|
|
"""Set up the reporter.
|
|
|
|
Takes parameters for the smtp server.
|
|
"""
|
|
self.smtp_server = smtp_server
|
|
self.smtp_port = smtp_port
|
|
self.smtp_default_from = smtp_default_from
|
|
self.smtp_default_to = smtp_default_to
|
|
|
|
def report(self, change, message, params):
|
|
"""Send the compiled report message via smtp."""
|
|
self.log.debug("Report change %s, params %s, message: %s" %
|
|
(change, params, message))
|
|
|
|
# Create a text/plain email message
|
|
from_email = params['from']\
|
|
if 'from' in params else self.smtp_default_from
|
|
to_email = params['to']\
|
|
if 'to' in params else self.smtp_default_to
|
|
msg = MIMEText(message)
|
|
if 'subject' in params:
|
|
subject = params['subject'].format(change=change)
|
|
else:
|
|
subject = "Report for change %s" % change
|
|
msg['Subject'] = subject
|
|
msg['From'] = from_email
|
|
msg['To'] = to_email
|
|
|
|
try:
|
|
s = smtplib.SMTP(self.smtp_server, self.smtp_port)
|
|
s.sendmail(from_email, to_email.split(','), msg.as_string())
|
|
s.quit()
|
|
except:
|
|
return "Could not send email via SMTP"
|
|
return
|
|
|
|
def getSubmitAllowNeeds(self, params):
|
|
"""Get a list of code review labels that are allowed to be
|
|
"needed" in the submit records for a change, with respect
|
|
to this queue. In other words, the list of review labels
|
|
this reporter itself is likely to set before submitting.
|
|
"""
|
|
return []
|