Skip to content

auto-load internal library webhook events based on async/sync view #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/django_github_app/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 0 additions & 2 deletions src/django_github_app/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
23 changes: 23 additions & 0 deletions src/django_github_app/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
22 changes: 0 additions & 22 deletions tests/test_apps.py

This file was deleted.

1 change: 0 additions & 1 deletion tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
("NAME", ""),
("PRIVATE_KEY", ""),
("WEBHOOK_SECRET", ""),
("WEBHOOK_TYPE", "async"),
],
)
def test_default_settings(setting, default_setting):
Expand Down
43 changes: 43 additions & 0 deletions tests/test_routing.py
Original file line number Diff line number Diff line change
@@ -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
Loading