Skip to content

Commit

Permalink
frontend: make EOL chroot emails shorter
Browse files Browse the repository at this point in the history
Fix #594
  • Loading branch information
FrostyX authored and praiskup committed Apr 29, 2024
1 parent 1e21c06 commit 39aebaf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 15 deletions.
22 changes: 15 additions & 7 deletions frontend/coprs_frontend/coprs/mail.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from itertools import groupby, batched
import flask
import platform
from smtplib import SMTP
Expand Down Expand Up @@ -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):
Expand Down
43 changes: 35 additions & 8 deletions frontend/coprs_frontend/tests/test_mail.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -51,9 +51,24 @@ def test_legal_flag_message(self, f_users, f_coprs, f_db):
"Reported by user2 <[email protected]>")

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
Expand All @@ -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:
Expand Down

0 comments on commit 39aebaf

Please sign in to comment.