[50] Validate GitHub Token (#2430)

pull/2436/head
YozoraXCII 7 days ago committed by GitHub Action
parent b1e9e86bc2
commit 344a6e32d9

@ -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

@ -1 +1 @@
2.1.0-build49
2.1.0-build50

@ -205,14 +205,12 @@ elif not os.path.exists(os.path.join(default_dir, "config.yml")):
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"])

@ -548,6 +548,7 @@ class ConfigFile:
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)
})

@ -13,8 +13,19 @@ class GitHub:
def __init__(self, requests, params):
self.requests = requests
self.token = params["token"]
self.headers = None
if self.token:
logger.separator()
logger.info("Connecting to GitHub...")
logger.secret(self.token)
self.headers = {"Authorization": f"token {self.token}"} if self.token else None
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 @@ class GitHub:
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("/"):

Loading…
Cancel
Save