-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
112 lines (95 loc) · 3.55 KB
/
utils.py
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
108
109
110
111
112
import os
import subprocess
import hashlib
import hmac
from fastapi import HTTPException
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
def verify_signature(
payload_body: bytes, secret_token: str | None, signature_header: str
):
"""
Verify that the payload was sent from GitHub by validating SHA256.
Args:
payload_body: original request body to verify (request.body())
secret_token: GitHub app webhook token (WEBHOOK_SECRET)
signature_header: header received from GitHub (x-hub-signature-256)
"""
if not secret_token:
raise HTTPException(status_code=403, detail="Secret token is missing!")
if not signature_header:
raise HTTPException(
status_code=403, detail="x-hub-signature-256 header is missing!"
)
hash_object = hmac.new(
secret_token.encode("utf-8"), msg=payload_body, digestmod=hashlib.sha256
)
expected_signature = "sha256=" + hash_object.hexdigest()
if not hmac.compare_digest(expected_signature, signature_header):
raise HTTPException(status_code=403, detail="Request signatures didn't match!")
def run_build(app, build_dir: str = "/build", services_dir: str = "/services"):
# Git Clone
result_git = subprocess.run(
[
"git",
"clone",
"-j8",
"--recurse-submodules",
"--remote-submodules",
"[email protected]:Clubs-Council-IIITH/services.git",
services_dir,
],
capture_output=True,
text=True,
)
# print(result_git)
returncode = result_git.returncode
stderr = result_git.stderr
if returncode != 0 and "already exists" in stderr:
curr_dir = os.getcwd()
os.chdir(services_dir)
subprocess.run(
["git", "pull", "origin", "master"], capture_output=True, text=True
)
result_pull = subprocess.run(
["git", "submodule", "update", "--recursive"],
capture_output=True,
text=True,
)
os.chdir(curr_dir)
# print(result_pull)
returncode = result_pull.returncode
stderr = result_pull.stderr
if returncode == 0:
# Run the mkdocs build command
result = subprocess.run(
["mkdocs", "build", "-d", build_dir+"_temp"], capture_output=True, text=True
)
# print("Build Result: ", result)
if result.returncode == 0:
# Move the new temp to build
os.makedirs(build_dir, exist_ok=True)
os.system(f"mv {build_dir}_temp/* {build_dir}/")
os.system(f"rm -rf {build_dir}_temp")
# If the build is successful, remount the static files
app.router.routes = [
route
for route in app.router.routes
if not (hasattr(route, "name") and route.name == "static")
]
app.mount("/", StaticFiles(directory=build_dir, html=True), name="static")
return {"message": "Update Successful!!"}
else:
# If the build fails, return the error
print("Build failed: ", result.stderr)
return JSONResponse(
content={"error": "Build failed", "details": result.stderr},
status_code=500,
)
else:
# If the git clone fails, return the error
print("Git clone failed: ", stderr)
return JSONResponse(
content={"error": "Git clone failed", "details": stderr},
status_code=500,
)