-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.py
More file actions
35 lines (31 loc) · 1.15 KB
/
Copy pathgithub.py
File metadata and controls
35 lines (31 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from fastapi import APIRouter, Depends
from github import Github # pip install PyGithub
router = APIRouter()
@router.get("/callback")
async def github_oauth_callback(code: str):
# Exchange code for access token with GitHub
token = exchange_code_for_token(code)
g = Github(token)
user = g.get_user()
# Save user + token to DB, return JWT to frontend
return {"username": user.login, "avatar": user.avatar_url}
@router.get("/activity/{username}")
async def get_activity(username: str, token: str):
g = Github(token)
user = g.get_user(username)
events = []
for event in user.get_events()[:20]:
events.append({
"type": event.type, # PushEvent, PullRequestEvent, etc.
"repo": event.repo.name,
"created_at": str(event.created_at),
"payload": event.payload
})
return events
@router.post("/webhook")
async def github_webhook(payload: dict):
# GitHub sends this automatically on every push/PR
# Parse and store as activity, link to tasks
event_type = payload.get("action")
# e.g. link commit message "#TASK-12" to that task
return {"received": True}