From e6da36db94abfaeaf4df76850575286ef79c5228 Mon Sep 17 00:00:00 2001 From: Hoang Hong Quan Date: Sat, 30 Aug 2025 14:21:10 +0700 Subject: [PATCH] Refactor GitHub commit fetching logic --- Scripts/github.py | 44 +++++++++++++++++++++++++++++--------------- updater.py | 7 ++----- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/Scripts/github.py b/Scripts/github.py index b050b8d..330d1e7 100644 --- a/Scripts/github.py +++ b/Scripts/github.py @@ -1,33 +1,47 @@ from Scripts import resource_fetcher from Scripts import utils import random +import json class Github: def __init__(self): self.utils = utils.Utils() self.fetcher = resource_fetcher.ResourceFetcher() + + def extract_payload(self, response): + 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_latest_commit(self, owner, repo, branch="main"): - url = "https://github.com/{}/{}/commits/{}".format(owner, repo, branch) + 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) if not response: raise ValueError("Failed to fetch commit information from GitHub.") - for line in response.splitlines(): - 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] + payload = self.extract_payload(response) - return { - "message": message, - "sha": sha - } - - return None + if not "commitGroups" in payload: + raise ValueError("Cannot find commit information for repository {} on branch {}.".format(repo, branch)) + + return payload def get_latest_release(self, owner, repo): url = "https://github.com/{}/{}/releases".format(owner, repo) diff --git a/updater.py b/updater.py index 81b7704..dd9f2af 100644 --- a/updater.py +++ b/updater.py @@ -34,14 +34,11 @@ class Updater: def get_latest_sha_version(self): print("Fetching latest version from GitHub...") try: - latest_commit = self.github.get_latest_commit("lzhoang2801", "OpCore-Simplify") - if latest_commit and latest_commit.get("sha"): - return latest_commit.get("sha") + commits = self.github.get_commits("lzhoang2801", "OpCore-Simplify") + return commits["commitGroups"][0]["commits"][0]["oid"] except Exception as e: print("Error fetching latest SHA version: {}".format(str(e))) - return None - print("Could not fetch latest commit information from GitHub.") return None def download_update(self):