def bitbucket_hgrid_data(node_settings, auth, **kwargs): # Quit if no repo linked if not node_settings.complete: return connection = BitbucketClient(access_token=node_settings.external_account.oauth_key) node = node_settings.owner if node.is_public and not node.is_contributor(auth.user): repo = connection.repo(node_settings.user, node_settings.repo) if not repo: # TODO: Add warning message logger.error('Could not access Bitbucket repo') return None try: branch, sha, branches = get_refs( node_settings, branch=kwargs.get('branch'), sha=kwargs.get('sha'), connection=connection, ) except (NotFoundError, Exception): # TODO: Show an alert or change Bitbucket configuration? logger.error('Bitbucket repo not found') return ref = None if branch is None else ref_to_params(branch, sha) name_tpl = '{user}/{repo}'.format( user=node_settings.user, repo=node_settings.repo ) permissions = { 'edit': False, 'view': True, 'private': node_settings.is_private } urls = { 'upload': None, 'fetch': node_settings.owner.api_url + 'bitbucket/hgrid/' + (ref or ''), 'branch': node_settings.owner.api_url + 'bitbucket/hgrid/root/', 'zip': node_settings.owner.api_url + 'bitbucket/zipball/' + (ref or ''), 'repo': 'https://bitbucket.com/{0}/{1}/branch/'.format(node_settings.user, node_settings.repo) } branch_names = [each['name'] for each in branches] if not branch_names: branch_names = [branch] # if repo un-init-ed then still add default branch to list of branches return [rubeus.build_addon_root( node_settings, name_tpl, urls=urls, permissions=permissions, branches=branch_names, defaultBranch=branch, private_key=kwargs.get('view_only', None), )]
def test_get_refs_branch(self, mock_account, mock_default_branch, mock_repo, mock_branches): bitbucket_mock = self.bitbucket mock_account.return_value = mock.Mock() mock_default_branch.return_value = bitbucket_mock.repo_default_branch.return_value mock_repo.return_value = bitbucket_mock.repo.return_value mock_branches.return_value = bitbucket_mock.branches.return_value branch, sha, branches = utils.get_refs(self.node_settings, 'master') assert_equal(branch, 'master') branch_sha = self._get_sha_for_branch('master') assert_equal(sha, branch_sha) expected_branches = [ {'name': x['name'], 'sha': x['target']['hash']} for x in bitbucket_mock.branches.return_value ] assert_equal(branches, expected_branches)
def test_get_refs_sha_no_branch(self, mock_account): with assert_raises(HTTPError): utils.get_refs(self.node_settings, sha='12345')
def bitbucket_hgrid_data(node_settings, auth, **kwargs): # Quit if no repo linked if not node_settings.complete: return connection = BitbucketClient( access_token=node_settings.external_account.oauth_key) node = node_settings.owner if node.is_public and not node.is_contributor(auth.user): repo = connection.repo(node_settings.user, node_settings.repo) if not repo: # TODO: Add warning message logger.error('Could not access Bitbucket repo') return None try: branch, sha, branches = get_refs( node_settings, branch=kwargs.get('branch'), sha=kwargs.get('sha'), connection=connection, ) except (NotFoundError, Exception): # TODO: Show an alert or change Bitbucket configuration? logger.error('Bitbucket repo not found') return ref = None if branch is None else ref_to_params(branch, sha) name_tpl = '{user}/{repo}'.format(user=node_settings.user, repo=node_settings.repo) permissions = { 'edit': False, 'view': True, 'private': node_settings.is_private } urls = { 'upload': None, 'fetch': node_settings.owner.api_url + 'bitbucket/hgrid/' + (ref or ''), 'branch': node_settings.owner.api_url + 'bitbucket/hgrid/root/', 'zip': node_settings.owner.api_url + 'bitbucket/zipball/' + (ref or ''), 'repo': 'https://bitbucket.com/{0}/{1}/branch/'.format(node_settings.user, node_settings.repo) } branch_names = [each['name'] for each in branches] if not branch_names: branch_names = [ branch ] # if repo un-init-ed then still add default branch to list of branches return [ rubeus.build_addon_root( node_settings, name_tpl, urls=urls, permissions=permissions, branches=branch_names, defaultBranch=branch, private_key=kwargs.get('view_only', None), ) ]