SharathReddy commited on
Commit
fa27a43
·
verified ·
1 Parent(s): df83c02

uploaded app.py and requirements.txt

Browse files
Files changed (2) hide show
  1. app.py +90 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import hmac
3
+ import hashlib
4
+ from fastapi import FastAPI, Request, HTTPException, status
5
+ from dotenv import load_dotenv
6
+
7
+ # Load environment variables from .env file
8
+ load_dotenv()
9
+
10
+ # --- Configuration ---
11
+ # It's crucial to load the secret from the environment for security
12
+ GITHUB_WEBHOOK_SECRET = os.getenv("GITHUB_WEBHOOK_SECRET")
13
+
14
+ # --- FastAPI App Initialization ---
15
+ app = FastAPI()
16
+
17
+ # --- Helper Functions ---
18
+ async def verify_signature(request: Request):
19
+ """Verify that the request is from GitHub."""
20
+ if not GITHUB_WEBHOOK_SECRET:
21
+ # This is a server-side configuration error.
22
+ raise HTTPException(
23
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
24
+ detail="Webhook secret not configured."
25
+ )
26
+
27
+ # Get the signature from the headers
28
+ signature_header = request.headers.get("X-Hub-Signature-256")
29
+ if not signature_header:
30
+ raise HTTPException(
31
+ status_code=status.HTTP_400_BAD_REQUEST,
32
+ detail="X-Hub-Signature-256 header is missing."
33
+ )
34
+
35
+ # The signature is in the format "sha256=..."
36
+ sha_name, signature = signature_header.split("=")
37
+ if sha_name != "sha256":
38
+ raise HTTPException(
39
+ status_code=status.HTTP_501_NOT_IMPLEMENTED,
40
+ detail="Only sha256 signatures are supported."
41
+ )
42
+
43
+ # Get the raw request body
44
+ body = await request.body()
45
+
46
+ # Create our own signature
47
+ mac = hmac.new(
48
+ GITHUB_WEBHOOK_SECRET.encode("utf-8"),
49
+ msg=body,
50
+ digestmod=hashlib.sha256
51
+ )
52
+
53
+ # Compare the signatures
54
+ if not hmac.compare_digest(mac.hexdigest(), signature):
55
+ raise HTTPException(
56
+ status_code=status.HTTP_400_BAD_REQUEST,
57
+ detail="Invalid signature."
58
+ )
59
+
60
+ # --- API Endpoints ---
61
+ @app.get("/")
62
+ def read_root():
63
+ """A simple root endpoint to confirm the server is running."""
64
+ return {"message": "Docu-Pilot server is alive!"}
65
+
66
+
67
+ @app.post("/api/github/webhook")
68
+ async def github_webhook(request: Request):
69
+ """
70
+ This endpoint receives webhook events from GitHub.
71
+ It first verifies the signature to ensure the request is authentic.
72
+ """
73
+ await verify_signature(request)
74
+
75
+ # You can get the event type from the headers
76
+ event_type = request.headers.get("X-GitHub-Event")
77
+ payload = await request.json()
78
+
79
+ if event_type == "push":
80
+ # In future steps, we will process the push event here.
81
+ # For now, we'll just log it.
82
+ pusher = payload.get("pusher", {}).get("name")
83
+ repo_name = payload.get("repository", {}).get("full_name")
84
+ print(f"Received a push event from {pusher} on repo {repo_name}")
85
+
86
+ elif event_type == "ping":
87
+ # GitHub sends a ping event when the webhook is first set up.
88
+ print("Received ping event from GitHub!")
89
+
90
+ return {"status": "ok"}
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ fastapi
2
+ uvicorn[standard]
3
+ python-dotenv