Skip to content

Commit e577e10

Browse files
authored
Merge pull request #3 from primetheus/feature/async-function-handling
Feature: handle async function hook calls for events
2 parents 7f37f91 + a3528aa commit e577e10

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

src/githubapp/core.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import hmac
66
import hashlib
77
import requests
8+
import inspect
89
from fastapi import FastAPI, APIRouter, Request, HTTPException, status
910
from fastapi.responses import JSONResponse
1011
from ghapi.all import GhApi
@@ -395,7 +396,7 @@ async def _handle_request(self, request: Request):
395396
if functions_to_call:
396397
for function in functions_to_call:
397398
try:
398-
result = function()
399+
result = await function() if inspect.iscoroutinefunction(function) else function()
399400
except Exception as e:
400401
raise HTTPException(
401402
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)
@@ -407,4 +408,4 @@ async def _handle_request(self, request: Request):
407408
)
408409
return JSONResponse(
409410
status_code=status.HTTP_200_OK, content={"status": "MISS", "calls": {}}
410-
)
411+
)

tests/test_core.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,37 @@ def test_handler():
421421
assert data["status"] == STATUS_FUNC_CALLED
422422
assert "test_handler" in data["calls"]
423423

424+
def test_handle_request_call_async_hook_function(self):
425+
app = FastAPI()
426+
github_app = GitHubApp(
427+
app,
428+
github_app_id=123,
429+
github_app_key=b"test_key",
430+
github_app_secret=False, # Disable signature verification for testing
431+
)
432+
github_app.init_app(app)
433+
434+
@github_app.on("issues.opened")
435+
async def async_test_handler():
436+
return "handled"
437+
438+
with TestClient(app) as client:
439+
response = client.post(
440+
"/",
441+
json={
442+
"action": "opened",
443+
"installation": {"id": 123},
444+
"issue": {"number": 1},
445+
},
446+
headers={
447+
"Content-Type": "application/json",
448+
"X-GitHub-Event": "issues",
449+
},
450+
)
451+
assert response.status_code == 200
452+
data = response.json()
453+
assert data["status"] == STATUS_FUNC_CALLED
454+
assert "async_test_handler" in data["calls"]
424455

425456
class TestGitHubAppWebhookSignatureVerification:
426457
def test_signature_verification_disabled(self):

0 commit comments

Comments
 (0)