Refactor GitHub commit fetching logic

This commit is contained in:
Hoang Hong Quan
2025-08-30 14:21:10 +07:00
parent 50e3226209
commit e6da36db94
2 changed files with 31 additions and 20 deletions
+28 -14
View File
@@ -1,33 +1,47 @@
from Scripts import resource_fetcher from Scripts import resource_fetcher
from Scripts import utils from Scripts import utils
import random import random
import json
class Github: class Github:
def __init__(self): def __init__(self):
self.utils = utils.Utils() self.utils = utils.Utils()
self.fetcher = resource_fetcher.ResourceFetcher() self.fetcher = resource_fetcher.ResourceFetcher()
def get_latest_commit(self, owner, repo, branch="main"): def extract_payload(self, response):
url = "https://github.com/{}/{}/commits/{}".format(owner, repo, branch) for line in response.splitlines():
if "type=\"application/json\"" in line:
payload = line.split(">", 1)[1].split("<", 1)[0]
try:
payload = json.loads(payload)
payload = payload["payload"]
except:
continue
return payload
return None
def get_commits(self, owner, repo, branch="main", start_commit=None, after=-1):
if after > -1 and not start_commit:
start_commit = self.get_commits(owner, repo, branch)["currentCommit"]["oid"]
if after < 0:
url = "https://github.com/{}/{}/commits/{}".format(owner, repo, branch)
else:
url = "https://github.com/{}/{}/commits/{}?after={}+{}".format(owner, repo, branch, start_commit, after)
response = self.fetcher.fetch_and_parse_content(url) response = self.fetcher.fetch_and_parse_content(url)
if not response: if not response:
raise ValueError("Failed to fetch commit information from GitHub.") raise ValueError("Failed to fetch commit information from GitHub.")
for line in response.splitlines(): payload = self.extract_payload(response)
if "href=\"" in line and "/commit/" in line and "title=\"" in line:
sha = line.split("href=\"", 1)[1].split("\"", 1)[0].split("/commit/")[-1]
try:
message = line.split("title=\"", 1)[1].split("\"", 1)[0]
except:
message = line.split(sha)[1].split(">", 1)[1].split("<")[0]
return { if not "commitGroups" in payload:
"message": message, raise ValueError("Cannot find commit information for repository {} on branch {}.".format(repo, branch))
"sha": sha
}
return None return payload
def get_latest_release(self, owner, repo): def get_latest_release(self, owner, repo):
url = "https://github.com/{}/{}/releases".format(owner, repo) url = "https://github.com/{}/{}/releases".format(owner, repo)
+2 -5
View File
@@ -34,14 +34,11 @@ class Updater:
def get_latest_sha_version(self): def get_latest_sha_version(self):
print("Fetching latest version from GitHub...") print("Fetching latest version from GitHub...")
try: try:
latest_commit = self.github.get_latest_commit("lzhoang2801", "OpCore-Simplify") commits = self.github.get_commits("lzhoang2801", "OpCore-Simplify")
if latest_commit and latest_commit.get("sha"): return commits["commitGroups"][0]["commits"][0]["oid"]
return latest_commit.get("sha")
except Exception as e: except Exception as e:
print("Error fetching latest SHA version: {}".format(str(e))) print("Error fetching latest SHA version: {}".format(str(e)))
return None
print("Could not fetch latest commit information from GitHub.")
return None return None
def download_update(self): def download_update(self):