From 8a03c4a2fc3f24580506cb60b6beff9847947842 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Sep 19 2020 12:14:09 +0000 Subject: Move the email sending code from the toddlers to the notify module This is for two reasons: a) keep the toddler shorter and simpler/easier to read and b) be consistent with the other toddlers. Signed-off-by: Pierre-Yves Chibon --- diff --git a/tests/plugins/test_check_email_overrides.py b/tests/plugins/test_check_email_overrides.py index 5354e2e..c90e3c1 100644 --- a/tests/plugins/test_check_email_overrides.py +++ b/tests/plugins/test_check_email_overrides.py @@ -86,7 +86,7 @@ class TestCheckEmailOverridesToddler: @patch("toddlers.utils.fedora_account.set_fas", new=Mock(return_value=True)) @patch("toddlers.utils.bugzilla_system.set_bz", new=Mock(return_value=True)) - @patch("toddlers.plugins.check_email_overrides.send_email") + @patch("toddlers.utils.notify.send_email") @patch("toddlers.utils.fedora_account.get_user_by_email") @patch("toddlers.utils.bugzilla_system.get_user") def test_process_no_fas(self, get_user, get_user_by_email, send_email, toddler): @@ -126,7 +126,7 @@ class TestCheckEmailOverridesToddler: @patch("toddlers.utils.fedora_account.set_fas", new=Mock(return_value=True)) @patch("toddlers.utils.bugzilla_system.set_bz", new=Mock(return_value=True)) - @patch("toddlers.plugins.check_email_overrides.send_email") + @patch("toddlers.utils.notify.send_email") @patch("toddlers.utils.fedora_account.get_user_by_email") @patch("toddlers.utils.bugzilla_system.get_user") def test_process_no_bz(self, get_user, get_user_by_email, send_email, toddler): diff --git a/tests/plugins/test_clean_retired_packages.py b/tests/plugins/test_clean_retired_packages.py index 7383253..a3febf9 100644 --- a/tests/plugins/test_clean_retired_packages.py +++ b/tests/plugins/test_clean_retired_packages.py @@ -69,7 +69,7 @@ class TestCleanRetiredPackagesToddler: ): assert toddler.process(config=config, message=msg) is None - @patch("toddlers.plugins.clean_retired_packages.send_email") + @patch("toddlers.utils.notify.send_email") def test_process(self, send_email, toddler, caplog): pdc_branches = MagicMock() pdc_branches.json.return_value = { diff --git a/toddlers/plugins/check_email_overrides.py b/toddlers/plugins/check_email_overrides.py index ca99900..24b17c4 100644 --- a/toddlers/plugins/check_email_overrides.py +++ b/toddlers/plugins/check_email_overrides.py @@ -17,8 +17,8 @@ import toml from toddlers.base import ToddlerBase from toddlers.utils import bugzilla_system from toddlers.utils import fedora_account +from toddlers.utils import notify from toddlers.utils.misc import merge_dicts -from toddlers.utils.notify import send_email from toddlers.utils.requests import make_session @@ -93,28 +93,12 @@ class CheckEmailOverrides(ToddlerBase): if logs: _log.info("- %s", "\n- ".join(logs)) logs_text = "\n- ".join(logs) - message = f"""Dear Admin, - -In the email_overrides.toml file located at: -{url} - -We have identified the following issues: -- {logs_text} - -Overrides having no FAS account can be simply removed. - -Overrides having no bugzilla account needs to be notified to the person. - - -Have a wonderful day and see you (maybe?) at the next run! - -""" - send_email( + notify.notify_admins_on_invalid_entry_in_email_override( to_addresses=[config["admin_email"]], from_address=config["admin_email"], - subject="Invalid entries in email_overrides.toml", - content=message, mail_server=config["mail_server"], + url=url, + logs_text=logs_text, ) diff --git a/toddlers/plugins/clean_retired_packages.py b/toddlers/plugins/clean_retired_packages.py index 80aa1a9..2cfc56e 100644 --- a/toddlers/plugins/clean_retired_packages.py +++ b/toddlers/plugins/clean_retired_packages.py @@ -13,7 +13,7 @@ import collections import logging from toddlers.base import ToddlerBase -from toddlers.utils.notify import send_email +from toddlers.utils import notify from toddlers.utils.requests import make_session @@ -141,24 +141,11 @@ class CleanRetiredPackages(ToddlerBase): if logs: logs_text = "\n- ".join(logs) - message = f"""Dear Admin, - -The clean_retired_packages toddler just ran and adjusted some retired packages -for recording purposes, here is what it did: -- {logs_text} - -In case someone asks, you can come back to this email but there isn't really -anything else to do with it. - -Have a wonderful day and see you (maybe?) at the next run! - -""" - send_email( + notify.notify_admins_on_retired_packages_cleanup( to_addresses=[config["admin_email"]], from_address=config["admin_email"], - subject="Toddlers cleaned up some retired packages", - content=message, mail_server=config["mail_server"], + logs_text=logs_text, ) def orphan_package(self, namespace, name, current_poc): diff --git a/toddlers/utils/notify.py b/toddlers/utils/notify.py index 9057448..3b21445 100644 --- a/toddlers/utils/notify.py +++ b/toddlers/utils/notify.py @@ -139,6 +139,30 @@ Please, be so kind and see if you can do something with them. Here is the list o ) +def notify_admins_on_retired_packages_cleanup( + to_addresses, from_address, mail_server, logs_text +): + message = f"""Dear Admin, + +The clean_retired_packages toddler just ran and adjusted some retired packages +for recording purposes, here is what it did: +- {logs_text} + +In case someone asks, you can come back to this email but there isn't really +anything else to do with it. + +Have a wonderful day and see you (maybe?) at the next run! + +""" + send_email( + to_addresses=to_addresses, + from_address=from_address, + subject="Toddlers cleaned up some retired packages", + content=message, + mail_server=mail_server, + ) + + def notify_packager_distgit_sync_error( mail_server, admin_email, user_email, cc_address ): @@ -195,6 +219,34 @@ The Fedora admins. ) +def notify_admins_on_invalid_entry_in_email_override( + to_addresses, from_address, mail_server, url, logs_text +): + message = f"""Dear Admin, + +In the email_overrides.toml file located at: +{url} + +We have identified the following issues: +- {logs_text} + +Overrides having no FAS account can be simply removed. + +Overrides having no bugzilla account needs to be notified to the person. + + +Have a wonderful day and see you (maybe?) at the next run! + +""" + send_email( + to_addresses=to_addresses, + from_address=from_address, + subject="Invalid entries in email_overrides.toml", + content=message, + mail_server=mail_server, + ) + + def send_email( to_addresses, from_address, subject, content, mail_server, cc_address=None ):