def list_build_sources_for_namespace(self, namespace): def repo_view(repo): return { "name": repo.name, "full_name": repo.full_name, "description": repo.description or "", "last_updated": timegm(repo.pushed_at.utctimetuple()) if repo.pushed_at else 0, "url": repo.html_url, "has_admin_permissions": True, "private": repo.private, } gh_client = self._get_client() usr = gh_client.get_user() if namespace == usr.login: repos = [repo_view(repo) for repo in usr.get_repos(type="owner", sort="updated")] return BuildTriggerHandler.build_sources_response(repos) try: org = gh_client.get_organization(namespace) if org is None: return [] except GithubException: return [] repos = [repo_view(repo) for repo in org.get_repos(type="member")] return BuildTriggerHandler.build_sources_response(repos)
def list_build_sources_for_namespace(self, namespace): def repo_view(repo): return { 'name': repo.name, 'full_name': repo.full_name, 'description': repo.description or '', 'last_updated': timegm(repo.pushed_at.utctimetuple()) if repo.pushed_at else 0, 'url': repo.html_url, 'has_admin_permissions': repo.permissions.admin, 'private': repo.private, } gh_client = self._get_client() usr = gh_client.get_user() if namespace == usr.login: repos = [repo_view(repo) for repo in usr.get_repos(type='owner', sort='updated')] return BuildTriggerHandler.build_sources_response(repos) try: org = gh_client.get_organization(namespace) if org is None: return [] except GithubException: return [] repos = [repo_view(repo) for repo in org.get_repos(type='member')] return BuildTriggerHandler.build_sources_response(repos)
def list_build_sources_for_namespace(self, namespace): def repo_view(repo): last_modified = dateutil.parser.parse(repo['utc_last_updated']) return { 'name': repo['slug'], 'full_name': '%s/%s' % (repo['owner'], repo['slug']), 'description': repo['description'] or '', 'last_updated': timegm(last_modified.utctimetuple()), 'url': 'https://bitbucket.org/%s/%s' % (repo['owner'], repo['slug']), 'has_admin_permissions': repo['read_only'] is False, 'private': repo['is_private'], } bitbucket_client = self._get_authorized_client() (result, data, err_msg) = bitbucket_client.get_visible_repositories() if not result: raise RepositoryReadException('Could not read repository list: ' + err_msg) repos = [ repo_view(repo) for repo in data if repo['owner'] == namespace ] return BuildTriggerHandler.build_sources_response(repos)
def list_build_sources_for_namespace(self, namespace): def repo_view(repo): last_modified = dateutil.parser.parse(repo["utc_last_updated"]) return { "name": repo["slug"], "full_name": "%s/%s" % (repo["owner"], repo["slug"]), "description": repo["description"] or "", "last_updated": timegm(last_modified.utctimetuple()), "url": "https://bitbucket.org/%s/%s" % (repo["owner"], repo["slug"]), "has_admin_permissions": repo["read_only"] is False, "private": repo["is_private"], } bitbucket_client = self._get_authorized_client() (result, data, err_msg) = bitbucket_client.get_visible_repositories() if not result: raise RepositoryReadException("Could not read repository list: " + err_msg) repos = [ repo_view(repo) for repo in data if repo["owner"] == namespace ] return BuildTriggerHandler.build_sources_response(repos)
def list_build_sources_for_namespace(self, namespace_id): if not namespace_id: return [] def repo_view(repo): # Because *anything* can be None in GitLab API! permissions = repo.attributes.get("permissions") or {} group_access = permissions.get("group_access") or {} project_access = permissions.get("project_access") or {} missing_group_access = permissions.get("group_access") is None missing_project_access = permissions.get("project_access") is None access_level = max( group_access.get("access_level") or 0, project_access.get("access_level") or 0 ) has_admin_permission = _ACCESS_LEVEL_MAP.get(access_level, ("", False))[1] if missing_group_access or missing_project_access: # Default to has permission if we cannot check the permissions. This will allow our users # to select the repository and then GitLab's own checks will ensure that the webhook is # added only if allowed. # TODO: Do we want to display this differently in the UI? has_admin_permission = True view = { "name": repo.attributes["path"], "full_name": repo.attributes["path_with_namespace"], "description": repo.attributes.get("description") or "", "url": repo.attributes.get("web_url"), "has_admin_permissions": has_admin_permission, "private": repo.attributes.get("visibility") == "private", } if repo.attributes.get("last_activity_at"): try: last_modified = dateutil.parser.parse(repo.attributes["last_activity_at"]) view["last_updated"] = timegm(last_modified.utctimetuple()) except ValueError: logger.exception( "Gitlab gave us an invalid last_activity_at: %s", last_modified ) return view gl_client = self._get_authorized_client() try: gl_namespace = gl_client.namespaces.get(namespace_id) except gitlab.GitlabGetError: return [] namespace_obj = self._get_namespace(gl_client, gl_namespace, lazy=True) repositories = _paginated_iterator(namespace_obj.projects.list, RepositoryReadException) try: return BuildTriggerHandler.build_sources_response( [repo_view(repo) for repo in repositories] ) except gitlab.GitlabGetError: return []