-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook_server.py
More file actions
73 lines (60 loc) · 2.28 KB
/
webhook_server.py
File metadata and controls
73 lines (60 loc) · 2.28 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
#!/usr/bin/env python3
"""
Simple webhook server for GitHub Actions events.
Stores events in a JSON file that the MCP server can read.
"""
import json
from datetime import datetime
from pathlib import Path
from aiohttp import web
# Load environment variables from .env file
try:
from dotenv import load_dotenv
from pathlib import Path
# Get the directory where this script is located
script_dir = Path(__file__).parent
env_file = script_dir / ".env"
load_dotenv(env_file)
print(f"🔑 Webhook Server: Environment loaded from {env_file}")
except ImportError:
# python-dotenv not installed, that's fine
print("⚠️ Webhook Server: python-dotenv not available")
pass
# File to store events
EVENTS_FILE = Path(__file__).parent / "github_events.json"
async def handle_webhook(request):
"""Handle incoming GitHub webhook"""
try:
data = await request.json()
# Create event record
event = {
"timestamp": datetime.utcnow().isoformat(),
"event_type": request.headers.get("X-GitHub-Event", "unknown"),
"action": data.get("action"),
"workflow_run": data.get("workflow_run"),
"check_run": data.get("check_run"),
"repository": data.get("repository", {}).get("full_name"),
"sender": data.get("sender", {}).get("login")
}
# Load existing events
events = []
if EVENTS_FILE.exists():
with open(EVENTS_FILE, 'r') as f:
events = json.load(f)
# Add new event and keep last 100
events.append(event)
events = events[-100:]
# Save events
with open(EVENTS_FILE, 'w') as f:
json.dump(events, f, indent=2)
return web.json_response({"status": "received"})
except Exception as e:
return web.json_response({"error": str(e)}, status=400)
# Create app and add route
app = web.Application()
app.router.add_post('/webhook/github', handle_webhook)
if __name__ == '__main__':
print("🚀 Starting webhook server on http://localhost:8080")
print("📝 Events will be saved to:", EVENTS_FILE)
print("🔗 Webhook URL: http://localhost:8080/webhook/github")
web.run_app(app, host='localhost', port=8080)