(
request: Request,
webhook_data: tuple[dict, str] = Depends(_verify_github_webhook),
db: AsyncSession = Depends(get_db),
redis_client: Redis = Depends(get_redis_client),
queue: ArqRedis = Depends(get_queue),
)
| 435 | |
| 436 | @router.post("/webhook", name="github_webhook") |
| 437 | async def github_webhook( |
| 438 | request: Request, |
| 439 | webhook_data: tuple[dict, str] = Depends(_verify_github_webhook), |
| 440 | db: AsyncSession = Depends(get_db), |
| 441 | redis_client: Redis = Depends(get_redis_client), |
| 442 | queue: ArqRedis = Depends(get_queue), |
| 443 | ): |
| 444 | try: |
| 445 | data, event = webhook_data |
| 446 | |
| 447 | logger.info(f"Received GitHub webhook event: {event}") |
| 448 | |
| 449 | match event: |
| 450 | case "installation": |
| 451 | if data["action"] in ["deleted", "suspended", "unsuspended"]: |
| 452 | # App uninstalled or suspended |
| 453 | status = ( |
| 454 | "active" if data["action"] == "unsuspended" else data["action"] |
| 455 | ) |
| 456 | await db.execute( |
| 457 | update(GithubInstallation) |
| 458 | .where( |
| 459 | GithubInstallation.installation_id |
| 460 | == data["installation"]["id"] |
| 461 | ) |
| 462 | .values(status=status) |
| 463 | ) |
| 464 | await db.commit() |
| 465 | logger.info( |
| 466 | f"Installation {data['installation']['id']} for {data['installation']['account']['login']} is {data['action']}" |
| 467 | ) |
| 468 | |
| 469 | elif data["action"] == "created": |
| 470 | # App installed |
| 471 | installation_id = data["installation"]["id"] |
| 472 | github_service = get_github_service() |
| 473 | token_data = await github_service.get_installation_access_token( |
| 474 | installation_id |
| 475 | ) |
| 476 | installation = GithubInstallation( |
| 477 | installation_id=installation_id, |
| 478 | token=token_data["token"], |
| 479 | token_expires_at=datetime.fromisoformat( |
| 480 | token_data["expires_at"].replace("Z", "+00:00") |
| 481 | ), |
| 482 | ) |
| 483 | await db.merge(installation) |
| 484 | await db.commit() |
| 485 | logger.info( |
| 486 | f"Installation {installation_id} for {data['installation']['account']['login']} created" |
| 487 | ) |
| 488 | |
| 489 | case "installation_target": |
| 490 | if data["action"] == "renamed": |
| 491 | # Installation account is renamed (not used) |
| 492 | pass |
| 493 | |
| 494 | case "installation_repositories": |
nothing calls this directly
no test coverage detected