Report: add methods to attach file/payload
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
5a7a46355c
commit
e8de509346
1 changed files with 14 additions and 1 deletions
|
@ -33,6 +33,8 @@ class Report(ConfigurableObject): # pylint: disable=useless-object-inheritance
|
||||||
def __init__(self, email_client=None, initialize=True, **kwargs):
|
def __init__(self, email_client=None, initialize=True, **kwargs):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
self.email_client = email_client
|
self.email_client = email_client
|
||||||
|
self._attachment_files = []
|
||||||
|
self._attachment_payloads = []
|
||||||
|
|
||||||
if initialize:
|
if initialize:
|
||||||
self.initialize()
|
self.initialize()
|
||||||
|
@ -84,6 +86,14 @@ class Report(ConfigurableObject): # pylint: disable=useless-object-inheritance
|
||||||
""" Read the report content """
|
""" Read the report content """
|
||||||
return "".join(self.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):
|
def send(self, subject=None, rcpt_to=None, email_client=None, just_try=False):
|
||||||
""" Send report using an EmailClient """
|
""" Send report using an EmailClient """
|
||||||
if rcpt_to is None:
|
if rcpt_to is None:
|
||||||
|
@ -102,7 +112,10 @@ class Report(ConfigurableObject): # pylint: disable=useless-object-inheritance
|
||||||
if not content:
|
if not content:
|
||||||
log.debug('Report is empty, do not send it')
|
log.debug('Report is empty, do not send it')
|
||||||
return True
|
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):
|
if email_client.send(rcpt_to, msg=msg, just_try=just_try):
|
||||||
log.debug('Report sent to %s', rcpt_to)
|
log.debug('Report sent to %s', rcpt_to)
|
||||||
return True
|
return True
|
||||||
|
|
Loading…
Reference in a new issue