def get_organization_repositories(organization_name): github_organization = get_github_organization_instance() try: github_organization.load(str(organization_name)) if github_organization.get_organization_installation_id() is not None: cla.log.debug('GitHub Organization ID: {}'.format(github_organization.get_organization_installation_id())) try: installation = GitHubInstallation(github_organization.get_organization_installation_id()) except Exception as e: msg = ('Unable to load repositories from organization: {} ({}) due to GitHub ' 'installation permission problem or other issue, error: {} - returning error response'. format(organization_name, github_organization.get_organization_installation_id(), e)) cla.log.warn(msg) return {'errors': {'organization_name': organization_name, 'error': msg}} if installation.repos: repos = [] for repo in installation.repos: repos.append(repo.full_name) return repos else: cla.log.debug('No repositories found for Github installation id: {}'. format(github_organization.get_organization_installation_id())) return [] except DoesNotExist as err: cla.log.warning('organization name {} does not exist, error: {}'.format(organization_name, err)) return {'errors': {'organization_name': organization_name, 'error': str(err)}}
def get_organization_repositories(organization_name): github_organization = get_github_organization_instance() try: github_organization.load(str(organization_name)) if github_organization.get_organization_installation_id() is not None: cla.log.debug('GitHub Organization ID: {}'.format( github_organization.get_organization_installation_id())) installation = GitHubInstallation( github_organization.get_organization_installation_id()) if installation.repos: repos = [] for repo in installation.repos: repos.append(repo.full_name) return repos else: cla.log.debug( 'No repositories found for Github installation id: {}'. format(github_organization. get_organization_installation_id())) return [] except DoesNotExist as err: cla.log.warning( 'organization name {} does not exist, error: {}'.format( organization_name, err)) return {'errors': {'organization_name': str(err)}}
def get_github_repositories_by_org(project): """ Gets organization with the project_id specified and all its repositories from Github API :param project: The Project object :type project: Project :return: [] of organizations and its repositories [{ 'organization_name': .. ... 'repositories': [{ 'repository_github_id': '' 'repository_name': '' 'repository_type': '' 'repository_url': '' }] }] :rtype: array """ organization_dicts = [] # Get all organizations connected to this project cla.log.info("Retrieving GH organization details using ID: {}".format(project.get_project_external_id)) github_organizations = GitHubOrg().get_organization_by_sfid(project.get_project_external_id()) cla.log.info("Retrieved {} GH organizations using ID: {}".format( len(github_organizations), project.get_project_external_id)) # Iterate over each organization for github_organization in github_organizations: installation_id = github_organization.get_organization_installation_id() # Verify installation_id exist if installation_id is not None: try: installation = GitHubInstallation(installation_id) # Prepare organization in dict organization_dict = github_organization.to_dict() organization_dict['repositories'] = [] # Get repositories from Github API github_repos = installation.repos cla.log.info("Retrieved {} repositories using GH installation id: {}".format( github_repos, installation_id)) if github_repos is not None: for repo in github_repos: # Convert repository entities from lib to a dict. repo_dict = { 'repository_github_id': repo.id, 'repository_name': repo.full_name, 'repository_type': 'github', 'repository_url': repo.html_url } # Add repository to organization repositories list organization_dict['repositories'].append(repo_dict) # Add organization dict to list organization_dicts.append(organization_dict) except Exception as e: cla.log.warning('Error connecting to Github to fetch repository details, error: {}'.format(e)) return organization_dicts
def get_github_repositories_by_org(project): """ Gets organization with the project_id specified and all its repositories from Github API :param project: The Project object :type project: Project :return: [] of organizations and its repositories [{ 'organization_name': .. ... 'repositories': [{ 'repository_github_id': '' 'repository_name': '' 'repository_type': '' 'repository_url': '' }] }] :rtype: array """ organization_dicts = [] # Get all organizations connected to this project github_organizations = GitHubOrg().get_organization_by_sfid( project.get_project_external_id()) # Iterate over each organization for github_organization in github_organizations: installation_id = github_organization.get_organization_installation_id( ) # Verify installation_id exist if installation_id is not None: installation = GitHubInstallation(installation_id) # Prepare organization in dict organization_dict = github_organization.to_dict() organization_dict['repositories'] = [] # Get repositories from Github API github_repos = installation.repos if github_repos is not None: for repo in github_repos: # Convert repository entities from lib to a dict. repo_dict = { 'repository_github_id': repo.id, 'repository_name': repo.full_name, 'repository_type': 'github', 'repository_url': repo.html_url } # Add repository to organization repositories list organization_dict['repositories'].append(repo_dict) # Add organization dict to list organization_dicts.append(organization_dict) return organization_dicts
def get_organization_repositories(organization_name): github_organization = get_github_organization_instance() try: github_organization.load(str(organization_name)) if github_organization.get_organization_installation_id() is not None: print('GitHub Organization ID: {}'.format( github_organization.get_organization_installation_id())) installation = GitHubInstallation( github_organization.get_organization_installation_id()) if installation.repos: repos = [] for repo in installation.repos: repos.append(repo.full_name) return repos else: return [] except DoesNotExist as err: return {'errors': {'organization_name': str(err)}}
def get_github_repositories_by_org(project): """ Gets organization with the project_id specified and all its repositories from Github API :param project: The Project object :type project: Project :return: [] of organizations and its repositories [{ 'organization_name': .. ... 'repositories': [{ 'repository_github_id': '' 'repository_name': '' 'repository_type': '' 'repository_url': '' 'enabled': '' }] }] :rtype: array """ organization_dicts = [] # Get all organizations connected to this project cla.log.info( f'Retrieving GH organization details using ID: {project.get_project_external_id()}' ) github_organizations = GitHubOrg().get_organization_by_sfid( project.get_project_external_id()) cla.log.info( f'Retrieved {len(github_organizations)} ' f'GH organizations using ID: {project.get_project_external_id()}') repository_instance = cla.utils.get_repository_instance() # Iterate over each organization for github_organization in github_organizations: installation_id = github_organization.get_organization_installation_id( ) # Verify installation_id exist if installation_id is not None: try: installation = GitHubInstallation(installation_id) # Prepare organization in dict organization_dict = github_organization.to_dict() organization_dict['repositories'] = [] # Get repositories from Github API github_repos = installation.repos cla.log.info( f'Retrieved {github_repos} repositories using GH installation id: {installation_id}' ) if github_repos is not None: for repo in github_repos: # enabled flag checks whether repo has been added or removed from org enabled = False record_repo = repository_instance.get_repository_by_external_id( repo.id, "github") if record_repo: enabled = record_repo.get_enabled() # Convert repository entities from lib to a dict. repo_dict = { 'repository_github_id': repo.id, 'repository_name': repo.full_name, 'repository_type': 'github', 'repository_url': repo.html_url, 'enabled': enabled, } # Add repository to organization repositories list organization_dict['repositories'].append(repo_dict) # Add organization dict to list organization_dicts.append(organization_dict) except Exception as e: cla.log.warning( 'Error connecting to Github to fetch repository details, error: {}' .format(e)) return organization_dicts
def get_github_integration_client(installation_id): """ GitHub App integration client used for authenticated client actions through an installed app. """ return GitHubInstallation(installation_id).api_object