diff --git a/src/django_github_app/apps.py b/src/django_github_app/apps.py index 8e8bebb..d4716d2 100644 --- a/src/django_github_app/apps.py +++ b/src/django_github_app/apps.py @@ -13,9 +13,3 @@ class GitHubAppConfig(AppConfig): @override def ready(self): from . import checks # noqa: F401 - from .conf import app_settings - - if app_settings.WEBHOOK_TYPE == "async": - from .events import ahandlers # noqa: F401 - elif app_settings.WEBHOOK_TYPE == "sync": - from .events import handlers # noqa: F401 diff --git a/src/django_github_app/conf.py b/src/django_github_app/conf.py index 5e93489..7930e4d 100644 --- a/src/django_github_app/conf.py +++ b/src/django_github_app/conf.py @@ -3,7 +3,6 @@ from dataclasses import dataclass from pathlib import Path from typing import Any -from typing import Literal from django.conf import settings from django.utils.text import slugify @@ -22,7 +21,6 @@ class AppSettings: NAME: str = "" PRIVATE_KEY: str = "" WEBHOOK_SECRET: str = "" - WEBHOOK_TYPE: Literal["async", "sync"] = "async" @override def __getattribute__(self, __name: str) -> Any: diff --git a/src/django_github_app/routing.py b/src/django_github_app/routing.py index 8217b03..0263d22 100644 --- a/src/django_github_app/routing.py +++ b/src/django_github_app/routing.py @@ -19,13 +19,36 @@ class GitHubRouter(GidgetHubRouter): _routers: list[GidgetHubRouter] = [] + _library_handlers_loaded = False def __init__(self, *args) -> None: super().__init__(*args) GitHubRouter._routers.append(self) + @classmethod + def _reset(cls): + cls._routers = [] + cls._library_handlers_loaded = False + + @classmethod + def _load_library_handlers(cls): + if cls._library_handlers_loaded: + return + + from .checks import get_webhook_views + from .views import AsyncWebhookView + + views = get_webhook_views() + if views and issubclass(views[0], AsyncWebhookView): + from .events import ahandlers # noqa: F401 + else: + from .events import handlers # noqa: F401 + + cls._library_handlers_loaded = True + @classproperty def routers(cls): + cls._load_library_handlers() return list(cls._routers) def event(self, event_type: str, **kwargs: Any) -> Callable[[CB], CB]: diff --git a/tests/test_apps.py b/tests/test_apps.py deleted file mode 100644 index 2c81ce2..0000000 --- a/tests/test_apps.py +++ /dev/null @@ -1,22 +0,0 @@ -from __future__ import annotations - -import pytest - -from django_github_app.apps import GitHubAppConfig - - -class TestGitHubAppConfig: - @pytest.fixture - def app(self): - return GitHubAppConfig.create("django_github_app") - - @pytest.mark.parametrize( - "webhook_type", - [ - "async", - "sync", - ], - ) - def test_app_ready_urls(self, webhook_type, app, override_app_settings): - with override_app_settings(WEBHOOK_TYPE=webhook_type): - app.ready() diff --git a/tests/test_conf.py b/tests/test_conf.py index 121413b..2a8a93d 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -19,7 +19,6 @@ ("NAME", ""), ("PRIVATE_KEY", ""), ("WEBHOOK_SECRET", ""), - ("WEBHOOK_TYPE", "async"), ], ) def test_default_settings(setting, default_setting): diff --git a/tests/test_routing.py b/tests/test_routing.py new file mode 100644 index 0000000..2bf1597 --- /dev/null +++ b/tests/test_routing.py @@ -0,0 +1,43 @@ +from __future__ import annotations + +import pytest +from django.views.generic import View +from gidgethub import sansio + +from django_github_app.routing import GitHubRouter +from django_github_app.views import AsyncWebhookView +from django_github_app.views import SyncWebhookView + +from .utils import seq + + +@pytest.fixture +def test_router(): + GitHubRouter._reset() + router = GitHubRouter() + yield router + GitHubRouter._reset() + + +class TestGitHubRouter: + @pytest.mark.parametrize( + "urls", + [ + [SyncWebhookView], + [AsyncWebhookView], + [View], + [], + ], + ) + def test_library_handlers_loaded(self, urls, test_router, urlpatterns): + assert test_router._library_handlers_loaded is False + event = sansio.Event( + data={"action": "created"}, event="installation", delivery_id=seq.next() + ) + + with urlpatterns(urls): + test_router._load_library_handlers() + handlers = test_router.fetch(event) + + assert test_router._library_handlers_loaded is True + assert len(handlers) > 0