From c82978ec2618aec57d90f418a1f4badc747e4b04 Mon Sep 17 00:00:00 2001 From: Hoang Hong Quan Date: Sun, 27 Oct 2024 01:31:01 +0700 Subject: [PATCH] Update functions to retrieve information from GitHub API --- Scripts/gathering_files.py | 8 ++++++-- Scripts/github.py | 36 +++++++++++++++++++++++++++++------- updater.py | 2 +- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/Scripts/gathering_files.py b/Scripts/gathering_files.py index e59ce8e..8865e90 100644 --- a/Scripts/gathering_files.py +++ b/Scripts/gathering_files.py @@ -63,7 +63,9 @@ class gatheringFiles: }) else: if self.github.check_ratelimit(): - add_product_to_download_urls(self.github.get_latest_release(kext.github_repo.get("owner"), kext.github_repo.get("repo"))) + latest_release = self.github.get_latest_release(kext.github_repo.get("owner"), kext.github_repo.get("repo")) or {} + + add_product_to_download_urls(latest_release.get("assets")) add_product_to_download_urls({ "product_name": "OpenCorePkg", @@ -234,7 +236,9 @@ class gatheringFiles: if not isinstance(download_history, list): download_history = [] - for product in self.github.get_latest_release("lzhoang2801", "Hardware-Sniffer"): + latest_release = self.github.get_latest_release("lzhoang2801", "Hardware-Sniffer") or {} + + for product in latest_release.get("assets"): if product.get("product_name") == "Hardware-Sniffer-CLI": hardware_sniffer_cli = product diff --git a/Scripts/github.py b/Scripts/github.py index cdd8ea8..1199775 100644 --- a/Scripts/github.py +++ b/Scripts/github.py @@ -24,12 +24,23 @@ class Github: response = self.fetcher.fetch_and_parse_content(url, "json") - return response[0] + try: + latest_commit = response[0].get("commit") + except: + return + + if not isinstance(latest_commit, dict) or not latest_commit.get("tree"): + return + + return { + "message": latest_commit.get("message").split("\n")[0], + "sha": latest_commit.get("tree").get("sha") + } def get_latest_artifact(self, owner, repo): results = [] - url = "https://api.github.com/repos/{}/{}/actions/artifacts?per_page=1".format(owner, repo) + url = "https://api.github.com/repos/{}/{}/actions/artifacts".format(owner, repo) response = self.fetcher.fetch_and_parse_content(url, "json") @@ -44,11 +55,19 @@ class Github: return results def get_latest_release(self, owner, repo): - results = [] - - url = "https://api.github.com/repos/{}/{}/releases?per_page=1".format(owner, repo) + url = "https://api.github.com/repos/{}/{}/releases".format(owner, repo) response = self.fetcher.fetch_and_parse_content(url, "json") + + try: + latest_release = response[0] + except: + return + + if not isinstance(latest_release, dict): + return + + assets = [] for asset in response[0].get("assets"): asset_id = asset.get("id") @@ -56,13 +75,16 @@ class Github: asset_name = self.extract_asset_name(asset.get("name")) if "tlwm" in download_url or ("tlwm" not in download_url and "DEBUG" not in download_url.upper()): - results.append({ + assets.append({ "product_name": asset_name, "id": asset_id, "url": download_url }) - return results + return { + "describe": latest_release.get("body"), + "assets": assets + } def extract_asset_name(self, file_name): end_idx = len(file_name) diff --git a/updater.py b/updater.py index c6cba01..c695575 100644 --- a/updater.py +++ b/updater.py @@ -29,7 +29,7 @@ class Updater: return current_sha_version.decode() def get_latest_sha_version(self): - latest_commit = self.github.get_latest_commit("lzhoang2801", "OpCore-Simplify") + latest_commit = self.github.get_latest_commit("lzhoang2801", "OpCore-Simplify") or {} return latest_commit.get("sha") or "0506fb67111d5c5230bf59b826c318ea4251dfc4"