- Error handling per hook
- Fix github redirect
- Better code for github pagination
This commit is contained in:
T'rahk Rokym 2025-04-21 12:25:53 +02:00
parent 1c1e219037
commit cd9d985732
2 changed files with 26 additions and 24 deletions

View File

@ -63,7 +63,7 @@
<span class="badge bg-danger me-2">{% translate "Closed" %}</span>
{% endif %}
<span class="badge bg-info me-2">{{ notif.app_name }}</span>
<a href="{{ notif.web_url }}{{ notif.url }}" target="_blank">#{{ notif.iid }}{{ notif.number }} {{ notif.title }}</a>
<a href="{{ notif.web_url }}{{ notif.html_url }}" target="_blank">#{{ notif.iid }}{{ notif.number }} {{ notif.title }}</a>
</li>
{% endfor %}
</ul>

View File

@ -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