|
| 1 | +from fastapi.routing import APIRouter |
| 2 | +from ..core.models import GitHubPayload, TelexWebhookPayload |
| 3 | +from ..config.config import settings |
| 4 | +from ..utils.telex_utils import send_payload_to_telex |
| 5 | +from fastapi.responses import JSONResponse |
| 6 | +from fastapi import status, HTTPException |
| 7 | +import json |
| 8 | + |
| 9 | + |
| 10 | +router = APIRouter(prefix="/github") |
| 11 | + |
| 12 | + |
| 13 | +@router.post("/{telex_channel_id}", status_code=status.HTTP_200_OK) |
| 14 | +async def github_webhook(telex_channel_id: str, payload: GitHubPayload): |
| 15 | + """Endpoint to receive GitHub webhook events and forward the commits to Telex""" |
| 16 | + # Payload in format required by Telex channels' webhooks |
| 17 | + telex_payload = TelexWebhookPayload( |
| 18 | + event_name="pushed_commits", |
| 19 | + message=str(payload.commits), |
| 20 | + status="success", |
| 21 | + username=payload.pusher["name"], |
| 22 | + ).model_dump_json() |
| 23 | + |
| 24 | + # Telex channel webhook URL |
| 25 | + telex_url = f"{settings.telex_webhook_url}/{telex_channel_id}" |
| 26 | + |
| 27 | + try: |
| 28 | + # Send payload to Telex |
| 29 | + response = await send_payload_to_telex(telex_payload, telex_url) |
| 30 | + response_data = json.loads(response.decode().strip()) |
| 31 | + except Exception as e: |
| 32 | + raise HTTPException( |
| 33 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 34 | + detail=f"Telex payload sending failed: {str(e)}", |
| 35 | + ) |
| 36 | + |
| 37 | + return JSONResponse(content={"data": response_data}) |
0 commit comments