"""Webhook endpoints""" from fastapi import APIRouter, Depends, Request, Header, BackgroundTasks from sqlalchemy.ext.asyncio import AsyncSession from typing import Optional from app.database import get_db from app.schemas.webhook import GiteaWebhook, GitHubWebhook, BitbucketWebhook from app.webhooks import handle_gitea_webhook, handle_github_webhook, handle_bitbucket_webhook from app.agents import ReviewerAgent router = APIRouter() async def start_review_task(review_id: int, pr_number: int, repository_id: int): """Background task to start review""" from app.database import async_session_maker async with async_session_maker() as db: agent = ReviewerAgent(db) await agent.run_review(review_id, pr_number, repository_id) @router.post("/gitea/{repository_id}") async def gitea_webhook( repository_id: int, request: Request, background_tasks: BackgroundTasks, x_gitea_signature: Optional[str] = Header(None), db: AsyncSession = Depends(get_db) ): """Handle Gitea webhook""" raw_payload = await request.body() webhook_data = GiteaWebhook(**await request.json()) result = await handle_gitea_webhook( webhook_data=webhook_data, signature=x_gitea_signature or "", raw_payload=raw_payload, db=db ) # Start review in background if created if "review_id" in result: background_tasks.add_task( start_review_task, result["review_id"], webhook_data.number, repository_id ) return result @router.post("/github/{repository_id}") async def github_webhook( repository_id: int, request: Request, background_tasks: BackgroundTasks, x_hub_signature_256: Optional[str] = Header(None), db: AsyncSession = Depends(get_db) ): """Handle GitHub webhook""" raw_payload = await request.body() webhook_data = GitHubWebhook(**await request.json()) result = await handle_github_webhook( webhook_data=webhook_data, signature=x_hub_signature_256 or "", raw_payload=raw_payload, db=db ) # Start review in background if created if "review_id" in result: background_tasks.add_task( start_review_task, result["review_id"], webhook_data.number, repository_id ) return result @router.post("/bitbucket/{repository_id}") async def bitbucket_webhook( repository_id: int, request: Request, background_tasks: BackgroundTasks, db: AsyncSession = Depends(get_db) ): """Handle Bitbucket webhook""" webhook_data = BitbucketWebhook(**await request.json()) result = await handle_bitbucket_webhook( webhook_data=webhook_data, db=db ) # Start review in background if created if "review_id" in result: background_tasks.add_task( start_review_task, result["review_id"], webhook_data.pullrequest.id, repository_id ) return result