From cd9d985732f767cf5dc718e704cd63cee31b2ba8 Mon Sep 17 00:00:00 2001 From: T'rahk Rokym Date: Mon, 21 Apr 2025 12:25:53 +0200 Subject: [PATCH] Upgrades - Error handling per hook - Fix github redirect - Better code for github pagination --- .../allianceauth/admin-status/overview.html | 2 +- allianceauth/templatetags/admin_status.py | 48 ++++++++++--------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/allianceauth/templates/allianceauth/admin-status/overview.html b/allianceauth/templates/allianceauth/admin-status/overview.html index 482c1cc8..eff882d3 100644 --- a/allianceauth/templates/allianceauth/admin-status/overview.html +++ b/allianceauth/templates/allianceauth/admin-status/overview.html @@ -63,7 +63,7 @@ {% translate "Closed" %} {% endif %} {{ notif.app_name }} - #{{ notif.iid }}{{ notif.number }} {{ notif.title }} + #{{ notif.iid }}{{ notif.number }} {{ notif.title }} {% endfor %} diff --git a/allianceauth/templatetags/admin_status.py b/allianceauth/templatetags/admin_status.py index d576d474..989a820e 100644 --- a/allianceauth/templatetags/admin_status.py +++ b/allianceauth/templatetags/admin_status.py @@ -88,7 +88,7 @@ class AppAnnouncementHook: @hooks.register("app_announcement_hook") def test_hook(): - return AppAnnouncementHook("test GitHub app", "r0kym/allianceauth-example-plugin", RepositoryKind.GITLAB) + return AppAnnouncementHook("test GitLab app", "r0kym/allianceauth-example-plugin", RepositoryKind.GITLAB) @hooks.register("app_announcement_hook") def test_hook_2(): @@ -141,10 +141,20 @@ def _current_notifications() -> dict: _fetch_notification_issues_from_gitlab, NOTIFICATION_CACHE_TIME ) - app_notifications = [] - hooks = get_hooks("app_announcement_hook") - items = [fn() for fn in hooks] - for hook in items: + except requests.HTTPError: + logger.warning('Error while getting gitlab notifications', exc_info=True) + top_notifications = [] + else: + if notifications: + top_notifications = notifications[:5] + else: + top_notifications = [] + + app_notifications = [] + hooks = [fn() for fn in get_hooks("app_announcement_hook")] + for hook in hooks: + logger.info(hook) + try: app_notifications.extend(hook.get_announcement_list()) """ app_notifications.extend(cache.get_or_set( @@ -153,20 +163,13 @@ def _current_notifications() -> dict: NOTIFICATION_CACHE_TIME, )) """ - except requests.HTTPError: - logger.warning('Error while getting gitlab notifications', exc_info=True) - top_notifications = [] - application_notifications = [] - else: - if notifications: - top_notifications = notifications[:5] - else: - top_notifications = [] + except requests.HTTPError: + logger.warning("Error when getting %s notifications", hook, exc_info=True) - if app_notifications: - application_notifications = app_notifications[:10] - else: - application_notifications = [] + if app_notifications: + application_notifications = app_notifications[:10] + else: + application_notifications = [] response = { 'notifications': top_notifications, @@ -282,7 +285,6 @@ def _fetch_list_from_gitlab(url: str, max_pages: int = MAX_PAGES) -> list: def _fetch_list_from_github(url: str, max_pages: int = MAX_PAGES) -> list: """returns a list from the GitHub API. Supports paging""" - # TODO actual paging result = [] for page in range(1, max_pages+1): @@ -308,9 +310,9 @@ def _fetch_list_from_github(url: str, max_pages: int = MAX_PAGES) -> list: result += request.json() - if 'link' in request.headers and 'rel=\"next\"' in request.headers['link']: - continue - - break + # https://docs.github.com/en/rest/using-the-rest-api/using-pagination-in-the-rest-api?apiVersion=2022-11-28 + # See Example creating a pagination metho + if not ('link' in request.headers and 'rel=\"next\"' in request.headers['link']): + break return result