diff --git a/backend/analytics_server/mhq/api/integrations.py b/backend/analytics_server/mhq/api/integrations.py index 39165cf49..7aadd3b38 100644 --- a/backend/analytics_server/mhq/api/integrations.py +++ b/backend/analytics_server/mhq/api/integrations.py @@ -1,4 +1,5 @@ -from flask import Blueprint +from flask import Blueprint, jsonify +from github import GithubException from voluptuous import Schema, Optional, Coerce, Range, All, Required from mhq.api.request_utils import queryschema @@ -22,10 +23,9 @@ def get_github_orgs(org_id: str): ) try: orgs = external_integrations_service.get_github_organizations() - except Exception as e: - return e, STATUS_TOO_MANY_REQUESTS + except GithubException as e: + return jsonify(e.data), e.status org_data_map = github_org_data_multi_thread_worker(orgs) - return { "orgs": [ { @@ -65,8 +65,8 @@ def get_repos(org_id: str, org_login: str, page_size: int, page: int): return external_integrations_service.get_github_org_repos( org_login, page_size, page - 1 ) - except Exception as e: - return e, STATUS_TOO_MANY_REQUESTS + except GithubException as e: + return jsonify(e.data), e.status @app.route( @@ -80,10 +80,12 @@ def get_prs_for_repo(org_id: str, gh_org_name: str, gh_org_repo_name: str): external_integrations_service = get_external_integrations_service( org_id, UserIdentityProvider.GITHUB ) - - workflows_list = external_integrations_service.get_repo_workflows( - gh_org_name, gh_org_repo_name - ) + try: + workflows_list = external_integrations_service.get_repo_workflows( + gh_org_name, gh_org_repo_name + ) + except GithubException as e: + return jsonify(e.data), e.status return [ { diff --git a/backend/analytics_server/mhq/exapi/github.py b/backend/analytics_server/mhq/exapi/github.py index bcd13d64f..92de6f78c 100644 --- a/backend/analytics_server/mhq/exapi/github.py +++ b/backend/analytics_server/mhq/exapi/github.py @@ -5,7 +5,7 @@ import requests from github import Github, UnknownObjectException -from github.GithubException import RateLimitExceededException +from github.GithubException import GithubException from github.Organization import Organization as GithubOrganization from github.PaginatedList import PaginatedList as GithubPaginatedList from github.PullRequest import PullRequest as GithubPullRequest @@ -41,14 +41,17 @@ def check_pat(self) -> bool: :raises HTTPError: If the request fails and status code is not 200 """ url = f"{self.base_url}/user" - response = requests.get(url, headers=self.headers) + try: + response = requests.get(url, headers=self.headers) + except GithubException as e: + raise Exception(f"Error in PAT validation, Error: {e.data}") return response.status_code == 200 def get_org_list(self) -> [GithubOrganization]: try: orgs = list(self._g.get_user().get_orgs()) - except RateLimitExceededException: - raise GithubRateLimitExceeded("GITHUB_API_LIMIT_EXCEEDED") + except GithubException as e: + raise e return orgs @@ -67,16 +70,16 @@ def get_repos_raw( ) -> [Dict]: try: repos = self.get_repos(org_login, per_page, page) - except RateLimitExceededException: - raise GithubRateLimitExceeded("GITHUB_API_LIMIT_EXCEEDED") + except GithubException as e: + raise e return [repo.__dict__["_rawData"] for repo in repos] def get_repo(self, org_login: str, repo_name: str) -> Optional[GithubRepository]: try: return self._g.get_repo(f"{org_login}/{repo_name}") - except UnknownObjectException: - return None + except GithubException as e: + raise e def get_repo_contributors(self, github_repo: GithubRepository) -> [Tuple[str, int]]: contributors = list(github_repo.get_contributors()) diff --git a/backend/analytics_server/mhq/service/external_integrations_service.py b/backend/analytics_server/mhq/service/external_integrations_service.py index 50280ef47..3a7970feb 100644 --- a/backend/analytics_server/mhq/service/external_integrations_service.py +++ b/backend/analytics_server/mhq/service/external_integrations_service.py @@ -1,6 +1,7 @@ +from github import GithubException from github.Organization import Organization as GithubOrganization -from mhq.exapi.github import GithubApiService, GithubRateLimitExceeded +from mhq.exapi.github import GithubApiService from mhq.store.models import UserIdentityProvider from mhq.store.repos.core import CoreRepoService @@ -22,27 +23,32 @@ def get_github_organizations(self): github_api_service = GithubApiService(self.access_token) try: orgs: [GithubOrganization] = github_api_service.get_org_list() - except GithubRateLimitExceeded as e: - raise Exception(e) + except GithubException as e: + raise e return orgs def get_github_org_repos(self, org_login: str, page_size: int, page: int): github_api_service = GithubApiService(self.access_token) try: return github_api_service.get_repos_raw(org_login, page_size, page) - except Exception as e: - raise Exception(e) + except GithubException as e: + raise e def get_repo_workflows(self, gh_org_name: str, gh_org_repo_name: str): github_api_service = GithubApiService(self.access_token) - workflows = github_api_service.get_repo_workflows(gh_org_name, gh_org_repo_name) - workflows_list = [] - for page in range(0, workflows.totalCount // PAGE_SIZE + 1, 1): - workflows = workflows.get_page(page) - if not workflows: - break - workflows_list += workflows - return workflows_list + try: + workflows = github_api_service.get_repo_workflows( + gh_org_name, gh_org_repo_name + ) + workflows_list = [] + for page in range(0, workflows.totalCount // PAGE_SIZE + 1, 1): + workflows = workflows.get_page(page) + if not workflows: + break + workflows_list += workflows + return workflows_list + except GithubException as e: + raise e def get_external_integrations_service(