diff --git a/CHANGELOG b/CHANGELOG index fdd544a08..4b0c6e4ba 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -23,6 +23,7 @@ When using `mass_background_update`, added `ignore_locked` attribute which will Add `date` option for schedules Add `trakt`, `omdb_metascore`, `omdb_tomatoes` ratings sources for mass operations. Add `trakt` ratings source for mass episode operations. +Added GitHub token validation during config validation. # Docs Added "getting started" page diff --git a/PART b/PART index 8b1378917..7ed6ff82d 100644 --- a/PART +++ b/PART @@ -1 +1 @@ - +5 diff --git a/kometa.py b/kometa.py index 2ccd44945..b67a46774 100644 --- a/kometa.py +++ b/kometa.py @@ -205,14 +205,12 @@ def get_env(env_str, default, arg_bool=False, arg_int=False): print(f"Configuration File ('config.yml') has been downloaded from GitHub (Branch: '{git_branch}') and saved as '{config_path}'. Please update this file with your API keys and other required settings.") sys.exit(1) else: - print(f"Config Error: Unable to download the configuration file from GitHub (URL: {github_url}'). Please save it as '{config_path}' before running Kometa again.") - sys.exit(1) + raise requests.RequestException except requests.RequestException as e: print(f"Config Error: Unable to download the configuration file from GitHub (URL: {github_url}'). Please save it as '{config_path}' before running Kometa again.") sys.exit(1) - logger = MyLogger("Kometa", default_dir, run_args["width"], run_args["divider"][0], run_args["ignore-ghost"], run_args["tests"] or run_args["debug"], run_args["trace"], run_args["log-requests"]) diff --git a/modules/config.py b/modules/config.py index 0b11fb83b..687205cc9 100644 --- a/modules/config.py +++ b/modules/config.py @@ -548,6 +548,7 @@ def check_for_attribute(data, attribute, parent=None, test_list=None, translatio self.Cache = Cache(self.config_path, self.general["cache_expiration"]) else: self.Cache = None + self.GitHub = GitHub(self.Requests, { "token": check_for_attribute(self.data, "token", parent="github", default_is_none=True) }) diff --git a/modules/github.py b/modules/github.py index 768b0fc0a..0a14c2d61 100644 --- a/modules/github.py +++ b/modules/github.py @@ -13,8 +13,19 @@ class GitHub: def __init__(self, requests, params): self.requests = requests self.token = params["token"] - logger.secret(self.token) - self.headers = {"Authorization": f"token {self.token}"} if self.token else None + self.headers = None + if self.token: + logger.separator() + logger.info("Connecting to GitHub...") + logger.secret(self.token) + self.headers = {"Authorization": f"token {self.token}"} + try: + response = self._requests("https://api.github.com/user", err_msg="The GitHub token specified could not be validated. Please verify that the token is correct.") + logger.info(f"GitHub token validated successfully. Authenticated as {response['login']}") + except Failed as e: + self.token = None + self.headers = None + logger.error(e) self.images_raw_url = f"{raw_url}/Kometa-Team/Image-Sets/master/sets/" self.translation_url = f"{raw_url}/Kometa-Team/Translations/master/defaults/" self._configs_url = None @@ -29,14 +40,13 @@ def _requests(self, url, err_msg=None, params=None, yaml=False): return self.requests.get_yaml(url, headers=self.headers, params=params) response = self.requests.get(url, headers=self.headers, params=params) if response.status_code >= 400: - logger.stacktrace() - logger.error(response.reason) - raise Failed(f"Git Error: {err_msg}") + raise Failed(f"GitHub Error: {err_msg} Response: {response.status_code} - {response.reason} ") try: return response.json() except ValueError: + logger.stacktrace() logger.error(str(response.content)) - raise + raise Failed("GitHub JSON Unpack Error") def get_top_tree(self, repo): if not str(repo).startswith("/"):