-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhReload.py
More file actions
100 lines (75 loc) · 3.07 KB
/
hReload.py
File metadata and controls
100 lines (75 loc) · 3.07 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
import os
import requests
import subprocess
import signal
import time
from dotenv import dotenv_values
import sys
import sched
# Uses the GitHub API to extract the hash of the latest commit on a specific branch
# Requires STAGING_USER, STAGING_REPO, and STAGING_BRANCH
def get_commit_hash(user, repo, br, token):
url = f"https://api.github.com/repos/{user}/{repo}/branches/{br}"
headers = {"Accept": "application/vnd.github.v3+json",
"User-Agent": "Frappe",
"Authorization": f"Bearer {token}"}
response = requests.get(url, headers=headers)
if (response.status_code == 200):
# Received OK HTTP code
lHash = response.json()["commit"]["sha"]
msg = response.json()["commit"]["commit"]["message"]
return str(lHash)
else:
print(f"Failed to retrieve latest commit hash. Code: {response.status_code}")
print(response.text)
return None
# Opens a new process with thr same python interpreter for the discord bot
def startBot():
pHandle = subprocess.Popen(args=["python", "main.py"], executable=sys.executable)
return pHandle
env_vars = dotenv_values(".env")
user = env_vars["STAGING_USER"]
repo = env_vars["STAGING_REPO"]
branch = env_vars["STAGING_BRANCH"]
token = env_vars["PERSONAL_GITHUB_TOKEN"]
# Current latest commit
shaPr = get_commit_hash(user, repo, branch, token)
# pull latest commit
subprocess.call(["git", "remote", "add", "origin", f"git@github.com:{user}/{repo}"], stdout=subprocess.PIPE)
subprocess.call(["git", "fetch", "origin"], stdout=subprocess.PIPE)
subprocess.call(["git", "switch", branch], stdout=subprocess.PIPE)
subprocess.call(["git", "reset", "--hard"], stdout=subprocess.PIPE)
# Start the bot
pHandle = startBot()
# Runs every COMMIT_CHECK_INTERVAL seconds and checks if a new commit has been pushed to the STAGING_BRANCH
# If so, it kills the bot process and restarts it
def check_for_new_commit(scheduler, pHandle, shaPr):
print("oh boy! time to check github!")
commit = get_commit_hash(user, repo, branch, token)
sha = commit
# new commit
if (shaPr != sha):
print(f"new commit on {branch}: {commit[1]} - reloading bot")
pHandle.send_signal(signal.SIGTERM)
pHandle.wait()
## Fetch the commit
subprocess.call(["git", "pull"], stdout=subprocess.PIPE)
pHandle = startBot()
shaPr = sha
else:
print("no new commit D:")
scheduler.enter(float(env_vars["COMMIT_CHECK_INTERVAL"]), 1, check_for_new_commit, (scheduler, pHandle, shaPr,))
# Setup scheduler and schedule the commit check
my_scheduler = sched.scheduler(time.time, time.sleep)
my_scheduler.enter(float(env_vars["COMMIT_CHECK_INTERVAL"]), 1, check_for_new_commit, (my_scheduler, pHandle, shaPr))
my_scheduler.run()
# while (True):
# this code does not work and i am not sure why rn
# could be useful later
# bot is frozen
# if (pHandle.poll() is not None):
# print("bot dripped too hard, melting")
# pHandle.send_signal(signal.SIGTERM)
# pHandle.wait()
# pHandle = startBot()
# Check github every 60 seconds