Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate GitHub Token #2430

Merged
merged 14 commits into from
Jan 8, 2025
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion PART
Original file line number Diff line number Diff line change
@@ -1 +1 @@

5
4 changes: 1 addition & 3 deletions kometa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

Expand Down
1 change: 1 addition & 0 deletions modules/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
22 changes: 16 additions & 6 deletions modules/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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("/"):
Expand Down
Loading