honor configuration when sending emails

- Move the send_email() function into DistgitBugzillaSync as a method in
  order to access the configuration environment easily.
- Make whether or not emails are sent use the email.send_mails
  configuration option rather than the name of the configuration
  environment.
- Don't hard code the SMTP host.

Signed-off-by: Nils Philippsen <nils@redhat.com>
This commit is contained in:
Nils Philippsen 2019-11-22 16:57:43 +01:00
parent 2b7712f826
commit 12b90b22dd
2 changed files with 23 additions and 24 deletions

View file

@ -446,28 +446,6 @@ class BugzillaProxy:
raise
def send_email(from_address, to_address, subject, message, cc_address=None):
'''Send an email if there's an error.
This will be replaced by sending messages to a log later.
'''
if env == 'staging':
# Send no email in staging...
pass
else:
msg = EmailMessage()
msg.add_header('To', ','.join(to_address))
msg.add_header('From', from_address)
msg.add_header('Subject', subject)
if cc_address is not None:
msg.add_header('Cc', ','.join(cc_address))
to_address += cc_address
msg.set_payload(message)
smtp = smtplib.SMTP('bastion')
smtp.sendmail(from_address, to_address, msg.as_string())
smtp.quit()
def _get_pdc_branches(session, repo):
"""
Gets the branches on a project. This function is used for mapping.
@ -520,6 +498,26 @@ class ScriptExecError(RuntimeError):
class DistgitBugzillaSync:
def send_email(self, from_address, to_address, subject, message, cc_address=None):
'''Send an email if there's an error.
This will be replaced by sending messages to a log later.
'''
if not self.env['email']['send_mails']:
return
msg = EmailMessage()
msg.add_header('To', ','.join(to_address))
msg.add_header('From', from_address)
msg.add_header('Subject', subject)
if cc_address is not None:
msg.add_header('Cc', ','.join(cc_address))
to_address += cc_address
msg.set_payload(message)
smtp = smtplib.SMTP(self.env['email']['smtp_host'])
smtp.sendmail(from_address, to_address, msg.as_string())
smtp.quit()
def notify_users(self, errors):
''' Browse the list of errors and when we can retrieve the email
address, use it to notify the user about the issue.
@ -562,7 +560,7 @@ class DistgitBugzillaSync:
notify_user = False
if notify_user:
send_email(
self.send_email(
self.env['email']['from'],
[user_email],
subject='Please fix your bugzilla.redhat.com account',
@ -876,7 +874,7 @@ class DistgitBugzillaSync:
print('[DEBUG]', '\n'.join(errors))
else:
self.notify_users(errors)
send_email(
self.send_email(
self.env['email']['from'],
self.env['email']['notify_admins'],
'Errors while syncing bugzilla with the PackageDB',