Skip to content

Commit 6e2988c

Browse files
committed
added fast API to the backend and /github/hook.
1 parent 54958e3 commit 6e2988c

File tree

4 files changed

+105
-54
lines changed

4 files changed

+105
-54
lines changed

backend/app/core/events/enums.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ class PlatformType(str, Enum):
99

1010
class EventType(str, Enum):
1111
ISSUE_CREATED = "issue.created"
12+
ISSUE_CLOSED = "issue.closed"
1213
ISSUE_UPDATED = "issue.updated"
1314
ISSUE_COMMENTED = "issue.commented"
1415
PR_CREATED = "pr.created"
1516
PR_UPDATED = "pr.updated"
1617
PR_COMMENTED = "pr.commented"
18+
PR_MERGED = "pr.merged"
1719
PR_REVIEWED = "pr.reviewed"
1820

1921
MESSAGE_CREATED = "message.created"
@@ -25,4 +27,4 @@ class EventType(str, Enum):
2527
ONBOARDING_COMPLETED = "onboarding.completed"
2628
FAQ_REQUESTED = "faq.requested"
2729
KNOWLEDGE_UPDATED = "knowledge.updated"
28-
ANALYTICS_COLLECTED = "analytics.collected"
30+
ANALYTICS_COLLECTED = "analytics.collected"

backend/main.py

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,11 @@
1-
import asyncio
2-
import uuid
3-
import logging
4-
from .app.core.events.event_bus import EventBus
5-
from .app.core.events.enums import EventType, PlatformType
6-
from .app.core.events.base import BaseEvent
7-
from .app.core.handler.handler_registry import HandlerRegistry
1+
from fastapi import FastAPI
2+
from routes import router
3+
import uvicorn
84

9-
logging.basicConfig(level=logging.INFO)
5+
app = FastAPI()
106

11-
# Initialize Handler Registry and Event Bus
12-
handler_registry = HandlerRegistry()
13-
event_bus = EventBus(handler_registry)
14-
15-
# Sample Handler Function
16-
async def sample_handler(event: BaseEvent):
17-
logging.info(f"Handler received event: {event.event_type} with data: {event.raw_data}")
18-
19-
# Register Handlers
20-
def register_event_handlers():
21-
event_bus.register_handler(EventType.ISSUE_CREATED, sample_handler, PlatformType.GITHUB)
22-
event_bus.register_handler(EventType.PR_COMMENTED, sample_handler, PlatformType.GITHUB)
23-
event_bus.register_handler(EventType.ONBOARDING_COMPLETED, sample_handler, PlatformType.DISCORD)
24-
25-
# Create a test event
26-
def create_test_event(event_type: EventType, platform: PlatformType, raw_data: dict) -> BaseEvent:
27-
return BaseEvent(
28-
id=str(uuid.uuid4()),
29-
actor_id="1234",
30-
event_type=event_type,
31-
platform=platform,
32-
raw_data=raw_data
33-
)
34-
35-
# Test Event Dispatching
36-
async def test_event_dispatch(event_type: EventType, platform: PlatformType, raw_data: dict):
37-
event = create_test_event(event_type, platform, raw_data)
38-
await event_bus.dispatch(event)
39-
40-
# Main function to run all tests
41-
async def main():
42-
logging.info("Starting EventBus Tests...")
43-
register_event_handlers()
44-
45-
test_cases = [
46-
(EventType.ISSUE_CREATED, PlatformType.GITHUB, {"title": "Bug Report", "description": "Issue in production"}),
47-
(EventType.PR_COMMENTED, PlatformType.GITHUB, {"pr_id": 5678, "merged_by": "dev_user"}),
48-
(EventType.ONBOARDING_COMPLETED, PlatformType.DISCORD, {"channel": "general", "content": "Hello everyone!"})
49-
]
50-
51-
for event_type, platform, data in test_cases:
52-
await test_event_dispatch(event_type, platform, data)
53-
54-
logging.info("All EventBus Tests Completed.")
7+
# Include GitHub webhook routes
8+
app.include_router(router)
559

5610
if __name__ == "__main__":
57-
asyncio.run(main())
11+
uvicorn.run(app, host="127.0.0.1", port=8000)

backend/routes.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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"}

backend/temp/Perspective

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit cd9f19d40e9bdd25172c09d6093386778959690c

0 commit comments

Comments
 (0)