Compare commits

..

10 Commits

Author SHA1 Message Date
T'rahk Rokym
c9b07c12a0 Last fixes 2025-04-21 16:31:39 +02:00
T'rahk Rokym
fd84f7fe15 Doc fix 2025-04-21 16:12:00 +02:00
T'rahk Rokym
92d8c699eb Ignore closed issues 2025-04-21 16:10:58 +02:00
T'rahk Rokym
9cc3283399 Move logger statements to debug 2025-04-21 15:44:15 +02:00
T'rahk Rokym
401c093b74 Remove template hooks 2025-04-21 15:26:00 +02:00
T'rahk Rokym
b3534f4f44 Basic documentation 2025-04-21 13:16:21 +02:00
T'rahk Rokym
f88249c8fc Enable caching 2025-04-21 12:35:55 +02:00
T'rahk Rokym
ec34d7fd29 Properly translates GitHub attributes to GitLab 2025-04-21 12:35:03 +02:00
T'rahk Rokym
cd9d985732 Upgrades
- Error handling per hook
- Fix github redirect
- Better code for github pagination
2025-04-21 12:30:32 +02:00
T'rahk Rokym
1c1e219037 Basic implementation of app hooks
Still need to remove the examples and add tests
2025-04-21 01:12:40 +02:00
21 changed files with 318 additions and 49 deletions

View File

@@ -24,14 +24,20 @@ exclude: |
)
repos:
# Code Upgrades
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.11.4
hooks:
# Run the linter, and only the linter
- id: ruff
- repo: https://github.com/adamchainz/django-upgrade
rev: 1.25.0
rev: 1.24.0
hooks:
- id: django-upgrade
args: [--target-version=5.2]
- repo: https://github.com/asottile/pyupgrade
rev: v3.20.0
args: [--target-version=5.1]
- repo: https://github.com/asottile/pyupgrade # Ruff doesnt get everything.
rev: v3.19.1
hooks:
- id: pyupgrade
args: [--py310-plus]
@@ -53,7 +59,7 @@ repos:
- id: detect-private-key
- id: check-case-conflict
# Python checks
# - id: check-docstring-first
#
- id: debug-statements
# - id: requirements-txt-fixer
- id: fix-encoding-pragma
@@ -71,22 +77,21 @@ repos:
hooks:
- id: editorconfig-checker
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.45.0
rev: v0.44.0
hooks:
- id: markdownlint
language: node
args:
- --disable=MD013
# Infrastructure
- repo: https://github.com/tox-dev/pyproject-fmt
rev: v2.6.0
rev: v2.5.1
hooks:
- id: pyproject-fmt
args:
- --indent=4
additional_dependencies:
- tox==4.26.0 # https://github.com/tox-dev/tox/releases/latest
- tox==4.24.1 # https://github.com/tox-dev/tox/releases/latest
- repo: https://github.com/tox-dev/tox-ini-fmt
rev: 1.5.0
hooks:

View File

@@ -5,7 +5,7 @@ manage online service access.
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
__version__ = '5.0.0a3'
__title__ = 'AllianceAuth'
__version__ = '5.0.0a1'
__title__ = 'Alliance Auth'
__url__ = 'https://gitlab.com/allianceauth/allianceauth'
NAME = f'{__title__} v{__version__}'

View File

@@ -1,7 +1,6 @@
import logging
import os
from bravado.client import SwaggerClient
from bravado.exception import HTTPError, HTTPNotFound, HTTPUnprocessableEntity
from jsonschema.exceptions import RefResolutionError
@@ -9,7 +8,7 @@ from django.conf import settings
from esi.clients import esi_client_factory
from allianceauth import __version__, __title__, __url__
from allianceauth import __version__
from allianceauth.utils.django import StartupCommand
SWAGGER_SPEC_PATH = os.path.join(os.path.dirname(
@@ -176,11 +175,7 @@ class EveProvider:
class EveSwaggerProvider(EveProvider):
def __init__(self, token=None, adapter=None) -> None:
self._token = token
self.adapter = adapter or self
self._faction_list = None # what are the odds this will change? could cache forever!
def __init__(self, token=None, adapter=None):
if settings.DEBUG or StartupCommand().is_management_command:
self._client = None
logger.info('ESI client will be loaded on-demand')
@@ -188,10 +183,9 @@ class EveSwaggerProvider(EveProvider):
logger.info('Loading ESI client')
try:
self._client = esi_client_factory(
token=self._token,
ua_appname=__title__,
ua_version=__version__,
ua_url=__url__,
token=token,
spec_file=SWAGGER_SPEC_PATH,
app_info_text=f"allianceauth v{__version__}"
)
except (HTTPError, RefResolutionError):
logger.exception(
@@ -200,14 +194,15 @@ class EveSwaggerProvider(EveProvider):
)
self._client = None
self._token = token
self.adapter = adapter or self
self._faction_list = None # what are the odds this will change? could cache forever!
@property
def client(self) -> SwaggerClient:
def client(self):
if self._client is None:
self._client = esi_client_factory(
token=self._token,
ua_appname=__title__,
ua_version=__version__,
ua_url=__url__,
token=self._token, spec_file=SWAGGER_SPEC_PATH, app_info_text=("allianceauth v" + __version__)
)
return self._client

File diff suppressed because one or more lines are too long

View File

@@ -676,6 +676,16 @@ class TestEveSwaggerProvider(TestCase):
self.assertTrue(mock_esi_client_factory.called)
self.assertIsNotNone(my_provider._client)
@patch(MODULE_PATH + '.SWAGGER_SPEC_PATH', SWAGGER_OLD_SPEC_PATH)
@patch(MODULE_PATH + '.settings.DEBUG', False)
@patch('socket.socket')
def test_create_client_on_normal_startup_w_old_swagger_spec(
self, mock_socket
):
mock_socket.side_effect = Exception('Network blocked for testing')
my_provider = EveSwaggerProvider()
self.assertIsNone(my_provider._client)
@patch(MODULE_PATH + '.settings.DEBUG', True)
@patch(MODULE_PATH + '.esi_client_factory')
def test_dont_create_client_on_debug_startup(self, mock_esi_client_factory):
@@ -712,6 +722,6 @@ class TestEveSwaggerProvider(TestCase):
my_provider = EveSwaggerProvider()
my_client = my_provider.client
operation = my_client.Universe.get_universe_factions()
self.assertIn(
'AllianceAuth/1.0.0 (dummy@example.net; +https://gitlab.com/allianceauth/allianceauth)', operation.future.request.headers['User-Agent']
self.assertEqual(
operation.future.request.headers['User-Agent'], 'allianceauth v1.0.0 dummy@example.net'
)

View File

@@ -57,9 +57,9 @@ CELERYBEAT_SCHEDULE = {
'task': 'esi.tasks.cleanup_callbackredirect',
'schedule': crontab(minute='0', hour='*/4'),
},
'esi_cleanup_token_subset': { # 1/48th * 1hr = 48Hr/2Day Refresh Cycles.
'task': 'esi.tasks.cleanup_token_subset',
'schedule': crontab(minute="0", hour="*/1"),
'esi_cleanup_token': {
'task': 'esi.tasks.cleanup_token',
'schedule': crontab(minute='0', hour='0'),
},
'run_model_update': {
'task': 'allianceauth.eveonline.tasks.run_model_update',

View File

@@ -8,6 +8,7 @@ from django.utils.functional import cached_property
from allianceauth.hooks import get_hooks
from allianceauth.menu.hooks import MenuItemHook
from allianceauth.templatetags.admin_status import AppAnnouncementHook
from .models import NameFormatConfig
@@ -145,6 +146,16 @@ class MenuItemHook(MenuItemHook):
def __init_subclass__(cls) -> None:
return super().__init_subclass__()
class AppAnnouncementHook(AppAnnouncementHook):
"""
AppAnnouncementHook shim to allianceauth.templatetags.admin_status
:param AppAnnouncementHook: _description_
:type AppAnnouncementHook: _type_
"""
def __init_subclass__(cls) -> None:
return super().__init_subclass__()
class UrlHook:
"""A hook for registering the URLs of a Django app.

View File

@@ -2,7 +2,7 @@ import os
from esi.clients import EsiClientProvider
from allianceauth import __version__, __title__, __url__
from allianceauth import __version__
SWAGGER_SPEC = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'swagger.json')
@@ -12,8 +12,8 @@ get_killmails_killmail_id_killmail_hash
get_universe_types_type_id
"""
esi = EsiClientProvider(
ua_appname=__title__,
ua_version=__version__,
ua_url=__url__,
spec_file=SWAGGER_SPEC,
app_info_text=("allianceauth v" + __version__)
)

File diff suppressed because one or more lines are too long

View File

@@ -46,6 +46,30 @@
</div>
{% endif %}
{% if application_notifications %}
<div id="aa-dashboard-panel-admin-application-notifications" class="col-12 mb-3">
<div class="card">
<div class="card-body">
{% translate "Application Notifications" as widget_title %}
{% include "framework/dashboard/widget-title.html" with title=widget_title %}
<div>
<ul class="list-group">
{% for notif in application_notifications %}
<li class="list-group-item">
<span class="badge bg-success me-2">{% translate "Open" %}</span>
<span class="badge bg-info me-2">{{ notif.app_name }}</span>
<a href="{{ notif.web_url }}" target="_blank">#{{ notif.iid }} {{ notif.title }}</a>
</li>
{% endfor %}
</ul>
{# TODO maybe add some disclaimer that those are managed by application devs? #}
</div>
</div>
</div>
</div>
{% endif %}
<div class="col-12 mb-3">
<div class="card">
<div class="card-body row">

View File

@@ -1,4 +1,7 @@
import logging
from dataclasses import dataclass
from enum import Enum
from urllib.parse import quote_plus
import requests
from packaging.version import InvalidVersion, Version as Pep440Version
@@ -11,6 +14,7 @@ from allianceauth import __version__
from allianceauth.authentication.task_statistics.counters import (
dashboard_results,
)
from allianceauth.hooks import get_hooks
register = template.Library()
@@ -32,6 +36,72 @@ GITLAB_AUTH_ANNOUNCEMENT_ISSUES_URL = (
logger = logging.getLogger(__name__)
@dataclass
class AppAnnouncementHook:
"""
A hook for an application to send GitHub/GitLab issues as announcements on the dashboard
Args:
- app_name: The name of your application
- repository_namespace: The namespace of the remote repository of your application source code.
It should look like `<username>/<application_name>`.
- repository_kind: Enumeration to determine if your repository is a GitHub or GitLab repository.
- label: The label applied to issues that should be seen as announcements, case-sensitive.
Default value: `announcement`
"""
class RepositoryKind(Enum):
"""Simple enumeration to determine which api should be called to access issues"""
GITLAB = "gitlab"
GITHUB = "github"
app_name: str
repository_namespace: str
repository_kind: RepositoryKind
label: str = "announcement"
def get_announcement_list(self) -> list:
"""
Checks the application repository to find issues with the `Announcement` tag and return their title and link to
be displayed.
"""
match self.repository_kind:
case AppAnnouncementHook.RepositoryKind.GITHUB:
announcement_list = self._get_github_announcement_list()
case AppAnnouncementHook.RepositoryKind.GITLAB:
announcement_list = self._get_gitlab_announcement_list()
case _:
return []
for announcement in announcement_list:
announcement["app_name"] = self.app_name
return announcement_list
def _get_github_announcement_list(self) -> list:
"""
Return the issue list for a GitHub repository
Will filter if the `pull_request` attribute is present
"""
raw_list = _fetch_list_from_github(
f"https://api.github.com/repos/{self.repository_namespace}/issues"
f"?labels={self.label}"
)
# Translates GitHub attributes to GitLab and filters out pull requests
clean_list = []
for element in raw_list:
if not element.get("pull_request"):
element["web_url"] = element["html_url"]
element["iid"] = element["number"]
clean_list.append(element)
return clean_list
def _get_gitlab_announcement_list(self) -> list:
"""Return the issues list for a GitLab repository"""
return _fetch_list_from_gitlab(
f"https://gitlab.com/api/v4/projects/{quote_plus(self.repository_namespace)}/issues"
f"?labels={self.label}&state=opened")
@register.simple_tag()
def decimal_widthratio(this_value, max_value, max_width) -> str:
@@ -89,8 +159,28 @@ def _current_notifications() -> dict:
else:
top_notifications = []
app_notifications = []
hooks = [fn() for fn in get_hooks("app_announcement_hook")]
for hook in hooks:
logger.debug(hook)
try:
app_notifications.extend(cache.get_or_set(
f"{hook.app_name}_notification_issues",
hook.get_announcement_list,
NOTIFICATION_CACHE_TIME,
))
except requests.HTTPError:
logger.warning("Error when getting %s notifications", hook, exc_info=True)
if app_notifications:
logger.debug(app_notifications)
application_notifications = app_notifications[:10]
else:
application_notifications = []
response = {
'notifications': top_notifications,
'application_notifications': application_notifications,
}
return response
@@ -199,3 +289,38 @@ def _fetch_list_from_gitlab(url: str, max_pages: int = MAX_PAGES) -> list:
break
return result
def _fetch_list_from_github(url: str, max_pages: int = MAX_PAGES) -> list:
"""returns a list from the GitHub API. Supports paging"""
result = []
for page in range(1, max_pages+1):
try:
request = requests.get(
url,
params={'page': page},
headers={
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28"
}
)
request.raise_for_status()
except requests.exceptions.RequestException as e:
error_str = str(e)
logger.warning(
f'Unable to fetch from GitHub API. Error: {error_str}',
exc_info=True,
)
return result
result += request.json()
logger.debug(request.json())
# 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 method
if not ('link' in request.headers and 'rel=\"next\"' in request.headers['link']):
break
return result

View File

@@ -1,7 +1,7 @@
PROTOCOL=https://
AUTH_SUBDOMAIN=%AUTH_SUBDOMAIN%
DOMAIN=%DOMAIN%
AA_DOCKER_TAG=registry.gitlab.com/allianceauth/allianceauth/auth:v5.0.0a3
AA_DOCKER_TAG=registry.gitlab.com/allianceauth/allianceauth/auth:v5.0.0a1
# Nginx Proxy Manager
PROXY_HTTP_PORT=80

View File

@@ -1,5 +1,5 @@
FROM python:3.12-slim
ARG AUTH_VERSION=v5.0.0a3
ARG AUTH_VERSION=v5.0.0a1
ARG AUTH_PACKAGE=allianceauth==${AUTH_VERSION}
ENV AUTH_USER=allianceauth
ENV AUTH_GROUP=allianceauth

View File

@@ -0,0 +1,52 @@
# Announcement Hooks
This hook allows the issues opened on your application repository to be displayed on the alliance auth front page to
administrators.
![app_announcement_hook_example](img/app_announcement_hook_example.png)
To register an AppAnnouncementHook class, you would do the following:
```python
from allianceauth import hooks
from allianceauth.services.hooks import AppAnnouncementHook
@hooks.register('app_announcement_hook')
def announcement_hook():
return AppAnnouncementHook("Your app name", "USERNAME/REPOSITORY_NAME", AppAnnouncementHook.RepositoryKind.GITLAB)
```
```{eval-rst}
.. autoclass:: allianceauth.services.hooks.AppAnnouncementHook
:members: __init__
:undoc-members:
```
## Parameters
### app_name
The name of your application.
### repository_namespace
Here you should enter the namespace of your repository.
The structure stays the same for both GitHub and GitLab repositories. \
A repository with the url `https://gitlab.com/username/appname` will have a namespace of `username/appname`.
### repository_kind
This variable is an enumeration of the class `AppAnnouncemementHook.RepositoryKind`
It is mandatory to specify this variable so alliance auth contacts the correct API when fetching your repository issues.
```{eval-rst}
.. autoclass:: allianceauth.services.hooks.AppAnnouncementHook.RepositoryKind
:members: GITLAB, GITHUB
:undoc-members:
```
### label
The label that will determine if issues should be seen as an announcement.
This value is case-sensitive and the default value is `"announcement"`.

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

View File

@@ -8,6 +8,7 @@ This section describes how to extend **Alliance Auth** with custom apps, service
integrating-services
menu-hooks
url-hooks
app-announcement-hooks
logging
custom-themes
aa-framework

View File

@@ -266,14 +266,14 @@ Every Alliance Auth installation will come with a couple of special celery relat
Celery-once is a celery extension "that allows you to prevent multiple execution and queuing of celery tasks". What that means is that you can ensure that only one instance of a celery task runs at any given time. This can be useful, for example, if you do not want multiple instances of your task to talk to the same external service at the same time.
We use a custom backend for celery_once in Alliance Auth defined in [allianceauth.services.tasks](https://gitlab.com/allianceauth/allianceauth/-/blob/master/allianceauth/services/tasks.py#L14)
We use a custom backend for celery_once in Alliance Auth defined [here](https://gitlab.com/allianceauth/allianceauth/-/blob/master/allianceauth/services/tasks.py#L14)
You can import it for use like so:
```python
from allianceauth.services.tasks import QueueOnce
```
An example of Alliance Auth's use within the `@sharedtask` decorator, can be seen in [allianceauth.services.modules.discord.tasks](https://gitlab.com/allianceauth/allianceauth/-/blob/master/allianceauth/services/modules/discord/tasks.py#L62) in the discord module
An example of Alliance Auth's use within the `@sharedtask` decorator, can be seen [here](https://gitlab.com/allianceauth/allianceauth/-/blob/master/allianceauth/services/modules/discord/tasks.py#L62) in the discord module
You can use it like so:
```python

View File

@@ -17,7 +17,7 @@ If at any point `docker compose` does not work, but `docker-compose` does, you h
1. run `bash <(curl -s https://gitlab.com/allianceauth/allianceauth/-/raw/master/docker/scripts/download.sh)`. This will download all the files you need to install Alliance Auth and place them in a directory named `aa-docker`. Feel free to rename/move this folder.
1. run `./scripts/prepare-env.sh` to set up your environment
1. (optional) Change `PROTOCOL` to `http://` if not using SSL in `.env`
1. run `docker compose --env-file=.env up -d` (NOTE: if this command hangs, follow the instructions on [How to Setup Entropy](https://www.digitalocean.com/community/tutorials/how-to-setup-additional-entropy-for-cloud-servers-using-haveged))
1. run `docker compose --env-file=.env up -d` (NOTE: if this command hangs, follow the instructions [here](https://www.digitalocean.com/community/tutorials/how-to-setup-additional-entropy-for-cloud-servers-using-haveged))
1. run `docker compose exec allianceauth_gunicorn bash` to open up a terminal inside an auth container
1. run `auth migrate`
1. run `auth collectstatic`

View File

@@ -69,7 +69,7 @@ Whatever you decide to use, remember it because we'll need it when configuring y
##### Number of workers
By default, Gunicorn will spawn only one worker. The number you set this to will depend on your own server environment, how many visitors you have etc. Gunicorn suggests `(2 x $num_cores) + 1` for the number of workers. So, for example, if you have 2 cores, you want 2 x 2 + 1 = 5 workers. See [How Mnay Workers](https://docs.gunicorn.org/en/stable/design.html#how-many-workers) for the official discussion on this topic.
By default, Gunicorn will spawn only one worker. The number you set this to will depend on your own server environment, how many visitors you have etc. Gunicorn suggests `(2 x $num_cores) + 1` for the number of workers. So, for example, if you have 2 cores, you want 2 x 2 + 1 = 5 workers. See [here](https://docs.gunicorn.org/en/stable/design.html#how-many-workers) for the official discussion on this topic.
Change it by adding `--workers=5` to the command.

View File

@@ -6,7 +6,7 @@
The default installation will have 3 workers configured for Gunicorn. This will be fine on most systems, but if your system as more than one core than you might want to increase the number of workers to get better response times. Note that more workers will also need more RAM though.
The number you set this to will depend on your own server environment, how many visitors you have etc. Gunicorn suggests `(2 x $num_cores) + 1` for the number of workers. So for example, if you have 2 cores, you want 2 x 2 + 1 = 5 workers. See [How Many Workers](https://docs.gunicorn.org/en/stable/design.html#how-many-workers) for the official discussion on this topic.
The number you set this to will depend on your own server environment, how many visitors you have etc. Gunicorn suggests `(2 x $num_cores) + 1` for the number of workers. So for example, if you have 2 cores, you want 2 x 2 + 1 = 5 workers. See [here](https://docs.gunicorn.org/en/stable/design.html#how-many-workers) for the official discussion on this topic.
::::{tabs}
:::{group-tab} Ubuntu 2204, 2404

View File

@@ -20,7 +20,7 @@ classifiers = [
"Environment :: Web Environment",
"Framework :: Celery",
"Framework :: Django",
"Framework :: Django :: 5.2",
"Framework :: Django :: 5.1",
"Intended Audience :: Developers",
"License :: OSI Approved :: GNU General Public License v2 (GPLv2)",
"Operating System :: POSIX :: Linux",
@@ -42,13 +42,13 @@ dependencies = [
"beautifulsoup4",
"celery>=5.5,<6",
"celery-once>=3.0.1",
"django>=5.2,<6",
"django>=5.1,<5.2",
"django-bootstrap-form",
"django-bootstrap5>=23.3",
"django-celery-beat>=2.8",
"django-esi>=7.0.0b1",
"django-celery-beat>=2.7",
"django-esi>=5",
"django-redis>=5.4",
"django-registration>=5.2,<6",
"django-registration>=5.1,<6",
"django-solo",
"django-sortedm2m",
"django-sri",
@@ -87,6 +87,50 @@ scripts.allianceauth = "allianceauth.bin.allianceauth:main"
[tool.flit.module]
name = "allianceauth"
[tool.ruff]
line-length = 119
format.line-ending = "lf"
lint.select = [
"B", # flake8-bugbear
"C", # pylint convention
# "D", # pydocstyle, Want to turn these on, but our docstrings are lightly used
"D300", # use triple double-quotes in docstrings PEP 257
"DJ", # flake8-django
"DOC", # pylintdoc
"E", # pycodestyle error
"F", # pyflakes (flake8 base)
"G010", # logging-warn, Warn on using logging.warn
"I", # isort
"PGH005", # pygrep-hooks, python-check-mock-method
"RUF100", # basically yesqa
"UP", # pyupgrade, will target requires-python
"W", # pycodestyle Warning
]
lint.ignore = [
"E501", # Line too long, WIP across repo.
]
lint.per-file-ignores = { "*local.py" = [ "F405", "F403" ] }
lint.isort.combine-as-imports = true # profile=django
lint.isort.section-order = [
"future",
"standard-library",
"third-party",
"DJANGO",
"ESI",
"first-party",
"local-folder",
]
lint.isort.sections."DJANGO" = [
"django",
"django_redis",
"django_registration",
]
lint.isort.sections."ESI" = [
"esi",
]
[tool.isort]
profile = "django"
sections = [