def list_build_subdirs(self): config = self.config gl_client = self._get_authorized_client() new_build_source = config["build_source"] gl_project = gl_client.projects.get(new_build_source) if not gl_project: msg = "Unable to find GitLab repository for source: %s" % new_build_source raise RepositoryReadException(msg) repo_branches = gl_project.branches.list() if not repo_branches: msg = "Unable to find GitLab branches for source: %s" % new_build_source raise RepositoryReadException(msg) branches = [branch.attributes["name"] for branch in repo_branches] branches = find_matching_branches(config, branches) branches = branches or [gl_project.attributes["default_branch"] or "master"] repo_tree = gl_project.repository_tree(ref=branches[0]) if not repo_tree: msg = "Unable to find GitLab repository tree for source: %s" % new_build_source raise RepositoryReadException(msg) return [node["name"] for node in repo_tree if self.filename_is_dockerfile(node["name"])]
def load_dockerfile_contents(self): config = self.config gh_client = self._get_client() source = config["build_source"] try: repo = gh_client.get_repo(source) except GithubException as ghe: message = ghe.data.get("message", "Unable to list contents of repository: %s" % source) raise RepositoryReadException(message) path = self.get_dockerfile_path() if not path: return None try: file_info = repo.get_contents(path) # TypeError is needed because directory inputs cause a TypeError except (GithubException, TypeError) as ghe: logger.error("got error from trying to find github file %s" % ghe) return None if file_info is None: return None if isinstance(file_info, list): return None content = file_info.content if file_info.encoding == "base64": content = base64.b64decode(content) return content
def list_build_subdirs(self): config = self.config gh_client = self._get_client() source = config["build_source"] try: repo = gh_client.get_repo(source) # Find the first matching branch. repo_branches = self.list_field_values("branch_name") or [] branches = find_matching_branches(config, repo_branches) branches = branches or [repo.default_branch or "master"] default_commit = repo.get_branch(branches[0]).commit commit_tree = repo.get_git_tree(default_commit.sha, recursive=True) return [ elem.path for elem in commit_tree.tree if ( elem.type == "blob" and self.filename_is_dockerfile(os.path.basename(elem.path)) ) ] except GithubException as ghe: message = ghe.data.get("message", "Unable to list contents of repository: %s" % source) if message == "Branch not found": raise EmptyRepositoryException() raise RepositoryReadException(message)
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_source_namespaces(self): 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) namespaces = {} for repo in data: owner = repo['owner'] if owner in namespaces: namespaces[owner]['score'] = namespaces[owner]['score'] + 1 else: namespaces[owner] = { 'personal': owner == self.config.get('nickname', self.config.get('username')), 'id': owner, 'title': owner, 'avatar_url': repo['logo'], 'url': 'https://bitbucket.org/%s' % (owner), 'score': 1, } return BuildTriggerHandler.build_namespaces_response(namespaces)
def list_build_source_namespaces(self): gl_client = self._get_authorized_client() current_user = gl_client.user if not current_user: raise RepositoryReadException("Unable to get current user") namespaces = {} for namespace in _paginated_iterator(gl_client.namespaces.list, RepositoryReadException): namespace_id = namespace.get_id() if namespace_id in namespaces: namespaces[namespace_id][ "score"] = namespaces[namespace_id]["score"] + 1 else: owner = namespace.attributes["name"] namespaces[namespace_id] = { "personal": namespace.attributes["kind"] == "user", "id": str(namespace_id), "title": namespace.attributes["name"], "avatar_url": namespace.attributes.get("avatar_url"), "score": 1, "url": namespace.attributes.get("web_url") or "", } return BuildTriggerHandler.build_namespaces_response(namespaces)
def list_build_source_namespaces(self): gl_client = self._get_authorized_client() current_user = gl_client.user if not current_user: raise RepositoryReadException('Unable to get current user') namespaces = {} for namespace in _paginated_iterator(gl_client.namespaces.list, RepositoryReadException): namespace_id = namespace.get_id() if namespace_id in namespaces: namespaces[namespace_id][ 'score'] = namespaces[namespace_id]['score'] + 1 else: owner = namespace.attributes['name'] namespaces[namespace_id] = { 'personal': namespace.attributes['kind'] == 'user', 'id': str(namespace_id), 'title': namespace.attributes['name'], 'avatar_url': namespace.attributes.get('avatar_url'), 'score': 1, 'url': namespace.attributes.get('web_url') or '', } return BuildTriggerHandler.build_namespaces_response(namespaces)
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_source_namespaces(self): 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) namespaces = {} for repo in data: owner = repo["owner"] if owner in namespaces: namespaces[owner]["score"] = namespaces[owner]["score"] + 1 else: namespaces[owner] = { "personal": owner == self.config.get("nickname", self.config.get("username")), "id": owner, "title": owner, "avatar_url": repo["logo"], "url": "https://bitbucket.org/%s" % (owner), "score": 1, } return BuildTriggerHandler.build_namespaces_response(namespaces)
def list_build_subdirs(self): config = self.config repository = self._get_repository_client() # Find the first matching branch. repo_branches = self.list_field_values('branch_name') or [] branches = find_matching_branches(config, repo_branches) if not branches: branches = [self._get_default_branch(repository)] (result, data, err_msg) = repository.get_path_contents('', revision=branches[0]) if not result: raise RepositoryReadException(err_msg) files = set([f['path'] for f in data['files']]) return [ "/" + file_path for file_path in files if self.filename_is_dockerfile(os.path.basename(file_path)) ]