|
| 1 | +import asyncio |
| 2 | +import uuid |
| 3 | +import logging |
| 4 | +from fastapi import APIRouter, Request, HTTPException |
| 5 | +from app.core.events.event_bus import EventBus |
| 6 | +from app.core.events.enums import EventType, PlatformType |
| 7 | +from app.core.events.base import BaseEvent |
| 8 | +from app.core.handler.handler_registry import HandlerRegistry |
| 9 | +from pydantic import BaseModel |
| 10 | + |
| 11 | +router = APIRouter() |
| 12 | + |
| 13 | +class RepoRequest(BaseModel): |
| 14 | + repo_url: str |
| 15 | + |
| 16 | +logging.basicConfig(level=logging.INFO) |
| 17 | +handler_registry = HandlerRegistry() |
| 18 | +event_bus = EventBus(handler_registry) |
| 19 | + |
| 20 | +# Sample handler function to process events |
| 21 | +async def sample_handler(event: BaseEvent): |
| 22 | + logging.info(f"Handler received event: {event.event_type} with data: {event.raw_data}") |
| 23 | + |
| 24 | +# Register all the event handlers for issues and pull requests |
| 25 | +def register_event_handlers(): |
| 26 | + # Issue events |
| 27 | + event_bus.register_handler(EventType.ISSUE_CREATED, sample_handler, PlatformType.GITHUB) |
| 28 | + event_bus.register_handler(EventType.ISSUE_CLOSED, sample_handler, PlatformType.GITHUB) |
| 29 | + event_bus.register_handler(EventType.ISSUE_UPDATED, sample_handler, PlatformType.GITHUB) |
| 30 | + event_bus.register_handler(EventType.ISSUE_COMMENTED, sample_handler, PlatformType.GITHUB) |
| 31 | + # Pull request events |
| 32 | + event_bus.register_handler(EventType.PR_CREATED, sample_handler, PlatformType.GITHUB) |
| 33 | + event_bus.register_handler(EventType.PR_UPDATED, sample_handler, PlatformType.GITHUB) |
| 34 | + event_bus.register_handler(EventType.PR_COMMENTED, sample_handler, PlatformType.GITHUB) |
| 35 | + event_bus.register_handler(EventType.PR_MERGED, sample_handler, PlatformType.GITHUB) |
| 36 | + |
| 37 | +@router.post("/github/webhook") |
| 38 | +async def github_webhook(request: Request): |
| 39 | + payload = await request.json() |
| 40 | + event_header = request.headers.get("X-GitHub-Event") |
| 41 | + logging.info(f"Received GitHub event: {event_header}") |
| 42 | + |
| 43 | + event_type = None |
| 44 | + |
| 45 | + # Handle issue events |
| 46 | + if event_header == "issues": |
| 47 | + action = payload.get("action") |
| 48 | + if action == "opened": |
| 49 | + event_type = EventType.ISSUE_CREATED |
| 50 | + elif action == "closed": |
| 51 | + event_type = EventType.ISSUE_CLOSED |
| 52 | + elif action == "edited": |
| 53 | + event_type = EventType.ISSUE_UPDATED |
| 54 | + |
| 55 | + # Handle issue comment events |
| 56 | + elif event_header == "issue_comment": |
| 57 | + action = payload.get("action") |
| 58 | + if action == "created": |
| 59 | + event_type = EventType.ISSUE_COMMENTED |
| 60 | + |
| 61 | + # Handle pull request events |
| 62 | + elif event_header == "pull_request": |
| 63 | + action = payload.get("action") |
| 64 | + if action == "opened": |
| 65 | + event_type = EventType.PR_CREATED |
| 66 | + elif action == "edited": |
| 67 | + event_type = EventType.PR_UPDATED |
| 68 | + elif action == "closed": |
| 69 | + # Determine if the PR was merged or simply closed |
| 70 | + if payload.get("pull_request", {}).get("merged"): |
| 71 | + event_type = EventType.PR_MERGED |
| 72 | + else: |
| 73 | + logging.info("Pull request closed without merge; no event dispatched.") |
| 74 | + |
| 75 | + # Handle pull request comment events |
| 76 | + elif event_header in ["pull_request_review_comment", "pull_request_comment"]: |
| 77 | + action = payload.get("action") |
| 78 | + if action == "created": |
| 79 | + event_type = EventType.PR_COMMENTED |
| 80 | + |
| 81 | + # Dispatch the event if we have a matching type |
| 82 | + if event_type: |
| 83 | + event = BaseEvent( |
| 84 | + id=str(uuid.uuid4()), |
| 85 | + actor_id=str(payload.get("sender", {}).get("id", "unknown")), |
| 86 | + event_type=event_type, |
| 87 | + platform=PlatformType.GITHUB, |
| 88 | + raw_data=payload |
| 89 | + ) |
| 90 | + await event_bus.dispatch(event) |
| 91 | + else: |
| 92 | + logging.info(f"No matching event type for header: {event_header} with action: {payload.get('action')}") |
| 93 | + |
| 94 | + return {"status": "ok"} |
0 commit comments