-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
76 lines (62 loc) · 1.96 KB
/
main.py
File metadata and controls
76 lines (62 loc) · 1.96 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
from dotenv import load_dotenv
load_dotenv()
from fastapi import FastAPI, HTTPException, Header
from db import get_connection
from models import PRMergedEvent, ReviewSubmittedEvent
app = FastAPI()
API_KEY = os.environ["LEADERBOARD_API_KEY"]
def verify_auth(auth: str | None):
if auth != f"Bearer {API_KEY}":
raise HTTPException(status_code=401, detail="unauthorized")
@app.post("/events/pr-merged")
def pr_merged(
event: PRMergedEvent,
authorization: str | None = Header(default=None),
):
verify_auth(authorization)
with get_connection() as conn:
with conn.cursor() as cur:
cur.execute(
"""
insert into pr_events
(repo, pr_number, author, additions, deletions, merged_at)
values
(%s, %s, %s, %s, %s, %s)
""",
(
event.repo,
event.pr_number,
event.author,
event.additions,
event.deletions,
event.merged_at,
),
)
return {"status": "ok"}
@app.post("/events/review-submitted")
def review_submitted(
event: ReviewSubmittedEvent,
authorization: str | None = Header(default=None),
):
verify_auth(authorization)
if event.review_state == "commented":
return {"status": "ignored"}
with get_connection() as conn:
with conn.cursor() as cur:
cur.execute(
"""
insert into review_events
(repo, pr_number, reviewer, review_state, submitted_at)
values
(%s, %s, %s, %s, %s)
""",
(
event.repo,
event.pr_number,
event.reviewer,
event.review_state,
event.submitted_at,
),
)
return {"status": "ok"}