From 12b90b22ddc53d13786032a7a9481ca34438461d Mon Sep 17 00:00:00 2001 From: Nils Philippsen Date: Fri, 22 Nov 2019 16:57:43 +0100 Subject: [PATCH] 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 --- .../default-config-files/configuration.toml | 1 + distgit_bugzilla_sync/script.py | 46 +++++++++---------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/distgit_bugzilla_sync/default-config-files/configuration.toml b/distgit_bugzilla_sync/default-config-files/configuration.toml index 4649b51..8ce9d16 100644 --- a/distgit_bugzilla_sync/default-config-files/configuration.toml +++ b/distgit_bugzilla_sync/default-config-files/configuration.toml @@ -30,6 +30,7 @@ Reassigning to the new maintainer of this component. [email] send_mails = false + smtp_host = "bastion" from = "accounts@fedoraproject.org" notify_admins = [ "root@localhost.localdomain", diff --git a/distgit_bugzilla_sync/script.py b/distgit_bugzilla_sync/script.py index 712f6a8..2f0d154 100644 --- a/distgit_bugzilla_sync/script.py +++ b/distgit_bugzilla_sync/script.py @@ -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',