From c90ecf4ebe9506450029c424ad770fde5ffb6763 Mon Sep 17 00:00:00 2001 From: "Jens L." Date: Sun, 8 Sep 2024 01:35:07 +0200 Subject: [PATCH] admin: refactor update check (#11272) * admin: refactor update check Signed-off-by: Jens Langhammer * fix tests Signed-off-by: Jens Langhammer --------- Signed-off-by: Jens Langhammer --- authentik/admin/tasks.py | 20 +++++++++++--------- authentik/admin/tests/test_tasks.py | 5 +++-- web/src/components/ak-event-info.ts | 16 +++++++--------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/authentik/admin/tasks.py b/authentik/admin/tasks.py index 8bafb3bef4..8d38adb2cc 100644 --- a/authentik/admin/tasks.py +++ b/authentik/admin/tasks.py @@ -1,10 +1,8 @@ """authentik admin tasks""" -import re - from django.core.cache import cache -from django.core.validators import URLValidator from django.db import DatabaseError, InternalError, ProgrammingError +from django.utils.translation import gettext_lazy as _ from packaging.version import parse from requests import RequestException from structlog.stdlib import get_logger @@ -21,8 +19,6 @@ LOGGER = get_logger() VERSION_NULL = "0.0.0" VERSION_CACHE_KEY = "authentik_latest_version" VERSION_CACHE_TIMEOUT = 8 * 60 * 60 # 8 hours -# Chop of the first ^ because we want to search the entire string -URL_FINDER = URLValidator.regex.pattern[1:] LOCAL_VERSION = parse(__version__) @@ -78,10 +74,16 @@ def update_latest_version(self: SystemTask): context__new_version=upstream_version, ).exists(): return - event_dict = {"new_version": upstream_version} - if match := re.search(URL_FINDER, data.get("stable", {}).get("changelog", "")): - event_dict["message"] = f"Changelog: {match.group()}" - Event.new(EventAction.UPDATE_AVAILABLE, **event_dict).save() + Event.new( + EventAction.UPDATE_AVAILABLE, + message=_( + "New version {version} available!".format( + version=upstream_version, + ) + ), + new_version=upstream_version, + changelog=data.get("stable", {}).get("changelog_url"), + ).save() except (RequestException, IndexError) as exc: cache.set(VERSION_CACHE_KEY, VERSION_NULL, VERSION_CACHE_TIMEOUT) self.set_error(exc) diff --git a/authentik/admin/tests/test_tasks.py b/authentik/admin/tests/test_tasks.py index 0ea3457f61..b88d16aec9 100644 --- a/authentik/admin/tests/test_tasks.py +++ b/authentik/admin/tests/test_tasks.py @@ -17,6 +17,7 @@ RESPONSE_VALID = { "stable": { "version": "99999999.9999999", "changelog": "See https://goauthentik.io/test", + "changelog_url": "https://goauthentik.io/test", "reason": "bugfix", }, } @@ -35,7 +36,7 @@ class TestAdminTasks(TestCase): Event.objects.filter( action=EventAction.UPDATE_AVAILABLE, context__new_version="99999999.9999999", - context__message="Changelog: https://goauthentik.io/test", + context__message="New version 99999999.9999999 available!", ).exists() ) # test that a consecutive check doesn't create a duplicate event @@ -45,7 +46,7 @@ class TestAdminTasks(TestCase): Event.objects.filter( action=EventAction.UPDATE_AVAILABLE, context__new_version="99999999.9999999", - context__message="Changelog: https://goauthentik.io/test", + context__message="New version 99999999.9999999 available!", ) ), 1, diff --git a/web/src/components/ak-event-info.ts b/web/src/components/ak-event-info.ts index 41faf4f577..0eb3396c89 100644 --- a/web/src/components/ak-event-info.ts +++ b/web/src/components/ak-event-info.ts @@ -513,16 +513,14 @@ ${JSON.stringify(value.new_value, null, 4)}${msg("New version available")} - - ${this.event.context.new_version} - `; - // Action types which typically don't record any extra context. - // If context is not empty, we fall to the default response. + `; } renderLogin() {