Skip to content

Commit

Permalink
feat: added notifier and notify command
Browse files Browse the repository at this point in the history
  • Loading branch information
c0rydoras committed Jul 17, 2023
1 parent 9d70627 commit ada338e
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 291 deletions.
13 changes: 13 additions & 0 deletions api/outdated/outdated/management/commands/notify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.core.management.base import BaseCommand

from outdated.outdated.models import Project
from outdated.outdated.notifier import send


class Command(BaseCommand):
help = "Sends email notification to maintainers when project outdated/warning"

def handle(self, *args, **options):
for project in Project.objects.all():
send(project)
print("Finished")
16 changes: 16 additions & 0 deletions api/outdated/outdated/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ def __str__(self):
def version(self):
return f"{self.release_version.version}.{self.patch_version}"

@property
def end_of_life(self):
return self.release_version.end_of_life

@property
def dependency(self):
return self.release_version.dependency

@property
def status(self):
return self.release_version.status

@property
def is_EOL(self):
return self.status == STATUS_OPTIONS["outdated"]


class Project(UUIDModel):
name = models.CharField(max_length=100, db_index=True)
Expand Down
52 changes: 52 additions & 0 deletions api/outdated/outdated/notifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from datetime import date

from django.core.mail import EmailMessage

from outdated.outdated import models


def _get_day_difference(version: models.Version) -> str:
values = [(version.end_of_life, "in"), (date.today(), "since")]
if version.status == "OUTDATED":
values = values[::-1]
return f"{values[0][1]} {(values[0][0] - values[1][0]).days} days"


def send(project: models.Project):
if not project.maintainers or project.status in ["UP_TO_DATE", "UNDEFINED"]:
return

is_outdated = project.status == "OUTDATED"

to = [project.maintainers.get(is_primary=True).user.email]
cc = [
maintainer.user.email
for maintainer in project.maintainers.filter(is_primary=False)
if is_outdated
]

subject = (
"Your project is out of date!"
if is_outdated
else "Your project will be out of date soon!"
)

project_info = f"Project: {project.name}\nRepo: {project.repo}\n"
message = (
"Your project has some dependencies that have reached their EOL. This means they are no longer stable or supported and may have security vulnerabilities or compatibility problems. You should update them to the next LTS version immediately to fix these issues."
if is_outdated
else "Your project has some dependencies that will reach their EOL soon. You should consider updating them as soon as possible to avoid potential issues."
)

dependency_lines = [
# make it so current LTS version alighs with itself on all occasions
f"{str(vd.dependency) + ':': <26s} current LTS version {str(vd.version)+',': <9s} EOL {_get_day_difference(vd)}"
for vd in project.versioned_dependencies.all()
if vd.status in ["OUTDATED", "WARNING"]
]

body = f"{project_info}{message}\nHere is a list of the dependencies:\n\n"
body += "\n".join(dependency_lines)

email_message = EmailMessage(subject, body, to=to, cc=cc)
return email_message.send()
4 changes: 2 additions & 2 deletions api/outdated/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ def default(default_dev=env.NOTSET, default_prod=env.NOTSET):
SERVER_EMAIL = env.str("SERVER_EMAIL", EMAIL_HOST_USER)
EMAIL_HOST_PASSWORD = env.str("EMAIL_HOST_PASSWORD", "")
EMAIL_USE_TLS = env.bool("EMAIL_USE_TLS", False)

from_name = env.str("MAILING_FROM_NAME", "outdated")
from_mail = env.str("MAILING_FROM_MAIL", "[email protected]")
MAILING = {"from_email": from_mail, "from_name": from_name}
MAILING_SENDER = f"{from_name} <{from_mail}>"
DEFAULT_FROM_EMAIL = f"{from_name} <{from_mail}>"

# Syncproject settings
RELEVANT_DEPENDENCIES = [
Expand Down
Loading

0 comments on commit ada338e

Please sign in to comment.