From 4ed0a49a44975e5ac2dc59d59aadf2f1f19c410e Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Tue, 6 Apr 2021 05:51:50 +0300 Subject: [PATCH] add ability to skip email report generation for empty update list --- CONFIGURING.md | 1 + package/etc/ahriman.ini | 1 + src/ahriman/core/report/email.py | 4 ++++ tests/ahriman/core/report/test_email.py | 24 ++++++++++++++++++++++++ tests/testresources/core/ahriman.ini | 1 + 5 files changed, 31 insertions(+) diff --git a/CONFIGURING.md b/CONFIGURING.md index e0421ccd..8838af47 100644 --- a/CONFIGURING.md +++ b/CONFIGURING.md @@ -56,6 +56,7 @@ Group name must refer to architecture, e.g. it should be `email:x86_64` for x86_ * `homepage` - link to homepage, string, optional. * `host` - SMTP host for sending emails, string, required. * `link_path` - prefix for HTML links, string, required. +* `no_empty_report` - skip report generation for empty packages list, boolean, optional, default `yes`. * `password` - SMTP password to authenticate, string, optional. * `port` - SMTP port for sending emails, int, required. * `receivers` - SMTP receiver addresses, space separated list of strings, required. diff --git a/package/etc/ahriman.ini b/package/etc/ahriman.ini index 1d60e650..43bcc797 100644 --- a/package/etc/ahriman.ini +++ b/package/etc/ahriman.ini @@ -26,6 +26,7 @@ target = target = [email] +no_empty_report = yes template_path = /usr/share/ahriman/repo-index.jinja2 ssl = disabled diff --git a/src/ahriman/core/report/email.py b/src/ahriman/core/report/email.py index af42e0ad..1fe1da38 100644 --- a/src/ahriman/core/report/email.py +++ b/src/ahriman/core/report/email.py @@ -36,6 +36,7 @@ class Email(Report, JinjaTemplate): """ email report generator :ivar host: SMTP host to connect + :ivar no_empty_report: skip empty report generation :ivar password: password to authenticate via SMTP :ivar port: SMTP port to connect :ivar receivers: list of receivers emails @@ -55,6 +56,7 @@ class Email(Report, JinjaTemplate): # base smtp settings self.host = configuration.get("email", "host") + self.no_empty_report = configuration.getboolean("email", "no_empty_report", fallback=True) self.password = configuration.get("email", "password", fallback=None) self.port = configuration.getint("email", "port") self.receivers = configuration.getlist("email", "receivers") @@ -96,6 +98,8 @@ class Email(Report, JinjaTemplate): :param packages: list of packages to generate report :param built_packages: list of packages which has just been built """ + if self.no_empty_report and not built_packages: + return text = self.make_html(built_packages, False) attachments = {"index.html": self.make_html(packages, True)} self._send(text, attachments) diff --git a/tests/ahriman/core/report/test_email.py b/tests/ahriman/core/report/test_email.py index 26d883c7..ed5f7905 100644 --- a/tests/ahriman/core/report/test_email.py +++ b/tests/ahriman/core/report/test_email.py @@ -103,3 +103,27 @@ def test_generate_with_built(configuration: Configuration, package_ahriman: Pack report = Email("x86_64", configuration) report.generate([package_ahriman], [package_ahriman]) send_mock.assert_called_once() + + +def test_generate_no_empty(configuration: Configuration, package_ahriman: Package, mocker: MockerFixture) -> None: + """ + must not generate report with built packages if no_empty_report is set + """ + configuration.set("email", "no_empty_report", "yes") + send_mock = mocker.patch("ahriman.core.report.email.Email._send") + + report = Email("x86_64", configuration) + report.generate([package_ahriman], []) + send_mock.assert_not_called() + + +def test_generate_no_empty_with_built(configuration: Configuration, package_ahriman: Package, mocker: MockerFixture) -> None: + """ + must generate report with built packages if no_empty_report is set + """ + configuration.set("email", "no_empty_report", "yes") + send_mock = mocker.patch("ahriman.core.report.email.Email._send") + + report = Email("x86_64", configuration) + report.generate([package_ahriman], [package_ahriman]) + send_mock.assert_called_once() diff --git a/tests/testresources/core/ahriman.ini b/tests/testresources/core/ahriman.ini index e80977a6..0268af71 100644 --- a/tests/testresources/core/ahriman.ini +++ b/tests/testresources/core/ahriman.ini @@ -28,6 +28,7 @@ target = [email] host = 0.0.0.0 link_path = +no_empty_report = no port = 587 receivers = mail@example.com sender = mail@example.com