diff --git a/augur/api/view/api.py b/augur/api/view/api.py index 084ee4bce..e0fd36d6f 100644 --- a/augur/api/view/api.py +++ b/augur/api/view/api.py @@ -40,14 +40,14 @@ def av_add_user_repo(): # matches https://github.com/{org}/ or htts://github.com/{org} if Repo.parse_github_org_url(url): - added = current_user.add_org(group, url) + added = current_user.add_org(group, url)[0] if added: added_orgs += 1 # matches https://github.com/{org}/{repo}/ or htts://github.com/{org}/{repo} elif Repo.parse_github_repo_url(url)[0]: print("Adding repo") - added = current_user.add_repo(group, url) + added = current_user.add_repo(group, url)[0] if added: print("Repo added") added_repos += 1 @@ -56,7 +56,7 @@ def av_add_user_repo(): elif (match := re.match(r'^\/?([a-zA-Z0-9_-]+)\/([a-zA-Z0-9_-]+)\/?$', url)): org, repo = match.groups() repo_url = f"https://github.com/{org}/{repo}/" - added = current_user.add_repo(group, repo_url) + added = current_user.add_repo(group, repo_url)[0] if added: added_repos += 1 @@ -64,11 +64,10 @@ def av_add_user_repo(): elif (match := re.match(r'^\/?([a-zA-Z0-9_-]+)\/?$', url)): org = match.group(1) org_url = f"https://github.com/{org}/" - added = current_user.add_org(group, org_url) + added = current_user.add_org(group, org_url)[0] if added: added_orgs += 1 - if not added_orgs and not added_repos: flash(f"Unable to add any repos or orgs") else: diff --git a/augur/application/db/models/augur_operations.py b/augur/application/db/models/augur_operations.py index e65363ffb..8e60a4aaf 100644 --- a/augur/application/db/models/augur_operations.py +++ b/augur/application/db/models/augur_operations.py @@ -429,9 +429,14 @@ def remove_group(self, group_name): def add_repo(self, group_name, repo_url): from augur.tasks.github.util.github_task_session import GithubTaskSession + from augur.tasks.github.util.github_api_key_handler import NoValidKeysError with GithubTaskSession(logger) as session: - result = UserRepo.add(session, repo_url, self.user_id, group_name) + try: + with GithubTaskSession(logger) as session: + result = UserRepo.add(session, repo_url, self.user_id, group_name) + except NoValidKeysError: + return False, {"status": "No valid keys"} return result @@ -445,9 +450,13 @@ def remove_repo(self, group_name, repo_id): def add_org(self, group_name, org_url): from augur.tasks.github.util.github_task_session import GithubTaskSession + from augur.tasks.github.util.github_api_key_handler import NoValidKeysError - with GithubTaskSession(logger) as session: - result = UserRepo.add_org_repos(session, org_url, self.user_id, group_name) + try: + with GithubTaskSession(logger) as session: + result = UserRepo.add_org_repos(session, org_url, self.user_id, group_name) + except NoValidKeysError: + return False, {"status": "No valid keys"} return result diff --git a/augur/tasks/github/util/github_api_key_handler.py b/augur/tasks/github/util/github_api_key_handler.py index 86055d7a7..2406ecef0 100644 --- a/augur/tasks/github/util/github_api_key_handler.py +++ b/augur/tasks/github/util/github_api_key_handler.py @@ -8,6 +8,11 @@ from augur.application.db.session import DatabaseSession from augur.application.config import AugurConfig + +class NoValidKeysError(Exception): + pass + + class GithubApiKeyHandler(): """Handles Github API key retrieval from the database and redis @@ -122,6 +127,9 @@ def get_api_keys(self) -> List[str]: # add all the keys to redis self.redis_key_list.extend(valid_keys) + if not valid_keys: + raise NoValidKeysError("No valid github api keys found in the config or worker oauth table") + return valid_keys def is_bad_api_key(self, client: httpx.Client, oauth_key: str) -> bool: