Skip to content

Commit

Permalink
Merge pull request #2404 from chaoss/invalid-keys-fix
Browse files Browse the repository at this point in the history
No valid Github API keys fix
  • Loading branch information
IsaacMilarky authored May 11, 2023
2 parents 2b745b2 + 5cca96d commit 96d8712
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
9 changes: 4 additions & 5 deletions augur/api/view/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -56,19 +56,18 @@ 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

# matches /{org}/ or /{org} or {org}/ or {org}
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:
Expand Down
15 changes: 12 additions & 3 deletions augur/application/db/models/augur_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

Expand Down
8 changes: 8 additions & 0 deletions augur/tasks/github/util/github_api_key_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 96d8712

Please sign in to comment.