Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions backend/analytics_server/mhq/api/integrations.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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": [
{
Expand Down Expand Up @@ -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(
Expand All @@ -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 [
{
Expand Down
19 changes: 11 additions & 8 deletions backend/analytics_server/mhq/exapi/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
@@ -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

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