Skip to content

Commit 6e1cbe9

Browse files
add note about importing in app ready
1 parent bdc5542 commit 6e1cbe9

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,10 @@ The library is async-only at the moment (following gidgethub), with sync support
102102
103103
django-github-app provides a router-based system for handling GitHub webhook events, built on top of [gidgethub](https://github.com/gidgethub/gidgethub). The router matches incoming webhooks to your handler functions based on the event type and optional action.
104104
105-
Each handler receives two key arguments:
106-
107-
- `event`: A `gidgethub.sansio.Event` containing the webhook payload
108-
- `gh`: A GitHub API client for making API calls
109-
110-
Here's an example:
105+
To start handling GitHub webhooks, create your event handlers in a new file (e.g. `events.py`) within your Django app:
111106
112107
```python
108+
# your_app/events.py
113109
from django_github_app.routing import Router
114110
115111
gh = Router()
@@ -145,6 +141,25 @@ async def welcome_new_issue(event, gh, *args, **kwargs):
145141
146142
In this example, we automatically label issues based on their title and post a welcome comment on newly opened issues. The router ensures each webhook is directed to the appropriate handler based on the event type and action.
147143
144+
Each handler receives two arguments:
145+
146+
- `event`: A `gidgethub.sansio.Event` containing the webhook payload
147+
- `gh`: A GitHub API client for making API calls
148+
149+
To activate your webhook handlers, import them in your app's `AppConfig.ready()` method, similar to how Django signals are registered:
150+
151+
```python
152+
# your_app/apps.py
153+
from django.apps import AppConfig
154+
155+
class YourAppConfig(AppConfig):
156+
default_auto_field = 'django.db.models.BigAutoField'
157+
name = 'your_app'
158+
159+
def ready(self):
160+
from . import events # noqa: F401
161+
```
162+
148163
> [!NOTE]
149164
> Handlers must be async functions as django-github-app uses gidgethub for webhook event routing which only supports async operations. Sync support is planned to better integrate with Django projects that don't use async.
150165

0 commit comments

Comments
 (0)