From 39aebaf10dac41262ea7d0d391b976d6479a0bc8 Mon Sep 17 00:00:00 2001 From: Jakub Kadlcik Date: Thu, 25 Apr 2024 19:56:04 +0200 Subject: [PATCH] frontend: make EOL chroot emails shorter Fix #594 --- frontend/coprs_frontend/coprs/mail.py | 22 +++++++---- frontend/coprs_frontend/tests/test_mail.py | 43 ++++++++++++++++++---- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/frontend/coprs_frontend/coprs/mail.py b/frontend/coprs_frontend/coprs/mail.py index 3f65f9d24..38dd56317 100644 --- a/frontend/coprs_frontend/coprs/mail.py +++ b/frontend/coprs_frontend/coprs/mail.py @@ -1,3 +1,4 @@ +from itertools import groupby, batched import flask import platform from smtplib import SMTP @@ -104,14 +105,21 @@ def __init__(self, copr_chroots): if not copr_chroots: raise AttributeError("No outdated chroots to notify about") - for chroot in copr_chroots: + # We need to sort the chroots first because `groupby` groups only + # consecutive keys + groups = groupby(sorted(copr_chroots, key=lambda x: x.copr.id), lambda x: x.copr) + for copr, chroots in groups: + block = "Project: {0}\n".format(copr.full_name) + block += "Remaining time:\n" + for days, chroots in groupby(chroots, lambda x: x.delete_after_days): + block += " {0} days:\n".format(days) + for chroots in batched(chroots, 4): + block += " {0}\n".format(" ".join([x.name for x in chroots])) + url = helpers.fix_protocol_for_frontend( - helpers.copr_url('coprs_ns.copr_repositories', chroot.copr, _external=True)) - self.text += ( - "Project: {0}\n" - "Chroot: {1}\n" - "Remaining: {2} days\n" - "{3}\n\n".format(chroot.copr.full_name, chroot.name, chroot.delete_after_days, url)) + helpers.copr_url('coprs_ns.copr_repositories', copr, _external=True)) + block += "{0}\n\n".format(url) + self.text += block def filter_allowlisted_recipients(recipients): diff --git a/frontend/coprs_frontend/tests/test_mail.py b/frontend/coprs_frontend/tests/test_mail.py index 410ca6a2e..16ef2bb90 100644 --- a/frontend/coprs_frontend/tests/test_mail.py +++ b/frontend/coprs_frontend/tests/test_mail.py @@ -1,8 +1,8 @@ import pytest import datetime from coprs.mail import PermissionRequestMessage, PermissionChangeMessage, LegalFlagMessage, OutdatedChrootMessage, filter_allowlisted_recipients +from coprs import app, models from tests.coprs_test_case import CoprsTestCase -from coprs import app class TestMail(CoprsTestCase): @@ -51,9 +51,24 @@ def test_legal_flag_message(self, f_users, f_coprs, f_db): "Reported by user2 ") def test_outdated_chroot_message(self, f_users, f_coprs, f_mock_chroots, f_db): - chroots = [self.c1.copr_chroots[0], self.c2.copr_chroots[0]] + chroots = self.c1.copr_chroots + self.c2.copr_chroots + self.c3.copr_chroots + + # Create more chroots within one project to later make sure line + # wrapping works as expected + for i in range(30, 38): + mc = models.MockChroot(os_release="fedora", os_version=i, + arch="x86_64", is_active=True) + mc.distgit_branch = models.DistGitBranch.query.first() + cc = models.CoprChroot() + cc.mock_chroot = mc + cc.copr = self.c2 + chroots.append(cc) + + now = datetime.datetime.now() for chroot in chroots: - chroot.delete_after = datetime.datetime.now() + datetime.timedelta(days=7 + 1) # 7 days = 6d, 23h, 59m, ... + # 7 days = 6d, 23h, 59m, ... + chroot.delete_after = now + datetime.timedelta(days=7 + 1) + chroots[3].delete_after = now + datetime.timedelta(days=5 + 1) app.config["SERVER_NAME"] = "localhost" app.config["DELETE_EOL_CHROOTS_AFTER"] = 123 @@ -71,14 +86,26 @@ def test_outdated_chroot_message(self, f_users, f_coprs, f_mock_chroots, f_db): "Please, visit the projects settings if you want to extend the time.\n\n" "Project: user1/foocopr\n" - "Chroot: fedora-18-x86_64\n" - "Remaining: 7 days\n" + "Remaining time:\n" + " 7 days:\n" + " fedora-18-x86_64\n" "https://localhost/coprs/user1/foocopr/repositories/\n\n" "Project: user2/foocopr\n" - "Chroot: fedora-17-x86_64\n" - "Remaining: 7 days\n" - "https://localhost/coprs/user2/foocopr/repositories/\n\n") + "Remaining time:\n" + " 7 days:\n" + " fedora-17-x86_64 fedora-17-i386 fedora-30-x86_64 fedora-31-x86_64\n" + " fedora-32-x86_64 fedora-33-x86_64 fedora-34-x86_64 fedora-35-x86_64\n" + " fedora-36-x86_64 fedora-37-x86_64\n" + "https://localhost/coprs/user2/foocopr/repositories/\n\n" + + "Project: user2/barcopr\n" + "Remaining time:\n" + " 5 days:\n" + " fedora-18-x86_64\n" + " 7 days:\n" + " fedora-rawhide-i386\n" + "https://localhost/coprs/user2/barcopr/repositories/\n\n") def test_outdated_chroot_message_empty_chroots(self): with pytest.raises(AttributeError) as ex: