Report: add methods to attach file/payload
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Benjamin Renard 2022-05-27 19:59:47 +02:00
parent 5a7a46355c
commit e8de509346

View file

@ -33,6 +33,8 @@ class Report(ConfigurableObject): # pylint: disable=useless-object-inheritance
def __init__(self, email_client=None, initialize=True, **kwargs):
super().__init__(**kwargs)
self.email_client = email_client
self._attachment_files = []
self._attachment_payloads = []
if initialize:
self.initialize()
@ -84,6 +86,14 @@ class Report(ConfigurableObject): # pylint: disable=useless-object-inheritance
""" Read the report content """
return "".join(self.content)
def add_attachment_file(self, filepath):
""" Add attachment file """
self._attachment_files.append(filepath)
def add_attachment_payload(self, payload):
""" Add attachment payload """
self._attachment_payloads.append(payload)
def send(self, subject=None, rcpt_to=None, email_client=None, just_try=False):
""" Send report using an EmailClient """
if rcpt_to is None:
@ -102,7 +112,10 @@ class Report(ConfigurableObject): # pylint: disable=useless-object-inheritance
if not content:
log.debug('Report is empty, do not send it')
return True
msg = email_client.forge_message(rcpt_to, subject=subject, text_body=content)
msg = email_client.forge_message(
rcpt_to, subject=subject, text_body=content,
attachment_files=self._attachment_files,
attachment_payloads=self._attachment_payloads)
if email_client.send(rcpt_to, msg=msg, just_try=just_try):
log.debug('Report sent to %s', rcpt_to)
return True