forked from ashmeet07/webhook-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
107 lines (73 loc) · 2.52 KB
/
Copy pathapp.py
File metadata and controls
107 lines (73 loc) · 2.52 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from flask import Flask, request, jsonify, render_template
from pymongo import MongoClient
from datetime import datetime
import os
app = Flask(__name__)
client = MongoClient(os.environ.get("MONGO_STRING"))
db = client["github"]
collection = db["event"]
@app.route("/")
def home():
return render_template("index.html")
@app.route("/webhook", methods=["POST"])
def webhook():
payload = request.json
event_type = request.headers.get("X-GitHub-Event")
data = {}
if event_type == "push":
data["request_id"] = payload["after"]
data["author"] = payload["pusher"]["name"]
data["action"] = "PUSH"
data["from_branch"] = None
data["to_branch"] = payload["ref"].split("/")[-1]
data["timestamp"] = datetime.utcnow()
elif event_type == "pull_request":
pr = payload["pull_request"]
data["request_id"] = str(pr["id"])
data["author"] = pr["user"]["login"]
data["action"] = "PULL_REQUEST"
data["from_branch"] = pr["head"]["ref"]
data["to_branch"] = pr["base"]["ref"]
data["timestamp"] = datetime.utcnow()
if payload["action"] == "closed" and pr["merged"]:
data["action"] = "MERGE"
else:
return jsonify({"msg": "ignored"}), 200
collection.insert_one(data)
return jsonify({"msg": "stored"}), 200
@app.route("/event", methods=["POST"])
def store_event():
payload = request.json
required_fields = ["request_id", "author", "action", "to_branch"]
for field in required_fields:
if field not in payload:
return jsonify({"error": f"{field} missing"}), 400
data = {
"request_id": payload["request_id"],
"author": payload["author"],
"action": payload["action"],
"from_branch": payload.get("from_branch"),
"to_branch": payload["to_branch"],
"timestamp": datetime.utcnow()
}
collection.insert_one(data)
return jsonify({"msg": "event stored"}), 200
@app.route("/events", methods=["GET"])
def events():
page = int(request.args.get("page", 1))
limit = 10
skip = (page - 1) * limit
results = list(
collection.find()
.sort("timestamp", -1)
.skip(skip)
.limit(limit)
)
for r in results:
r["_id"] = str(r["_id"])
r["timestamp"] = r["timestamp"].strftime("%d %b %Y • %I:%M %p UTC")
return jsonify(results)
# ✅ CRITICAL FOR RENDER 🚀
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host="0.0.0.0", port=port)