def gitlab_hgrid_data(node_settings, auth, **kwargs): # Quit if no repo linked if not node_settings.complete: return connection = GitLabClient(external_account=node_settings.external_account) # Initialize repo here in the event that it is set in the privacy check # below. This potentially saves an API call in _check_permissions, below. repo = None # Quit if privacy mismatch and not contributor node = node_settings.owner if node.is_public or node.is_contributor(auth.user): try: repo = connection.repo(node_settings.repo_id) except NotFoundError: logger.error('Could not access GitLab repo') return None try: branch, sha, branches = get_refs(node_settings, branch=kwargs.get('branch'), sha=kwargs.get('sha'), connection=connection) except (NotFoundError, GitLabError): logger.error('GitLab repo not found') return if branch is not None: ref = ref_to_params(branch, sha) can_edit = check_permissions(node_settings, auth, connection, branch, sha, repo=repo) else: ref = '' can_edit = False permissions = { 'edit': can_edit, 'view': True, 'private': node_settings.is_private } urls = { 'upload': node_settings.owner.api_url + 'gitlab/file/' + ref, 'fetch': node_settings.owner.api_url + 'gitlab/hgrid/' + ref, 'branch': node_settings.owner.api_url + 'gitlab/hgrid/root/' + ref, 'zip': 'https://{0}/{1}/repository/archive.zip?branch={2}'.format(node_settings.external_account.oauth_secret, repo['path_with_namespace'], ref), 'repo': 'https://{0}/{1}/tree/{2}'.format(node_settings.external_account.oauth_secret, repo['path_with_namespace'], ref) } 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, repo['path_with_namespace'], urls=urls, permissions=permissions, branches=branch_names, private_key=kwargs.get('view_only', None), default_branch=repo['default_branch'], )]
def gitlab_hgrid_data(node_settings, auth, **kwargs): # Quit if no repo linked if not node_settings.complete: return connection = GitLabClient(external_account=node_settings.external_account) # Initialize repo here in the event that it is set in the privacy check # below. This potentially saves an API call in _check_permissions, below. repo = None # Quit if privacy mismatch and not contributor node = node_settings.owner if node.is_public or node.is_contributor(auth.user): try: repo = connection.repo(node_settings.repo_id) except NotFoundError: logger.error('Could not access GitLab repo') return None try: branch, sha, branches = get_refs(node_settings, branch=kwargs.get('branch'), sha=kwargs.get('sha'), connection=connection) except (NotFoundError, GitLabError): logger.error('GitLab repo not found') return if branch is not None: ref = ref_to_params(branch, sha) can_edit = check_permissions(node_settings, auth, connection, branch, sha, repo=repo) else: ref = '' can_edit = False permissions = { 'edit': can_edit, 'view': True, 'private': node_settings.is_private } urls = { 'upload': node_settings.owner.api_url + 'gitlab/file/' + ref, 'fetch': node_settings.owner.api_url + 'gitlab/hgrid/' + ref, 'branch': node_settings.owner.api_url + 'gitlab/hgrid/root/' + ref, 'zip': 'https://{0}/{1}/repository/archive.zip?branch={2}'.format(node_settings.external_account.oauth_secret, repo.path_with_namespace, ref), 'repo': 'https://{0}/{1}/tree/{2}'.format(node_settings.external_account.oauth_secret, repo.path_with_namespace, ref) } 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, repo.path_with_namespace, urls=urls, permissions=permissions, branches=branch_names, private_key=kwargs.get('view_only', None), default_branch=repo.default_branch, )]
def test_get_refs_branch(self, mock_repo, mock_branches): gitlab_mock = self.gitlab mock_repo.return_value = gitlab_mock.repo.return_value mock_branches.return_value = gitlab_mock.branches.return_value branch, sha, branches = utils.get_refs(self.node_settings, 'master') assert_equal(branch, 'master') assert_equal(sha, branches[0].commit['id']) assert_equal(branches, gitlab_mock.branches.return_value)
def test_get_refs_defaults(self, mock_repo, mock_branches): gitlab_mock = self.gitlab mock_repo.return_value = gitlab_mock.repo mock_branches.return_value = gitlab_mock.branches.return_value branch, sha, branches = utils.get_refs(self.node_settings) assert_equal(branch, gitlab_mock.repo.default_branch) assert_equal(sha, branches[0].commit['id']) # Get refs for default branch assert_equal(branches, gitlab_mock.branches.return_value)
def test_get_refs_branch(self, mock_repo, mock_branches): gitlab_mock = self.gitlab mock_repo.return_value = gitlab_mock.repo.return_value mock_branches.return_value = gitlab_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) assert_equal(branches, gitlab_mock.branches.return_value)
def test_get_refs_defaults(self, mock_repo, mock_branches): gitlab_mock = self.gitlab mock_repo.return_value = gitlab_mock.repo.return_value mock_branches.return_value = gitlab_mock.branches.return_value branch, sha, branches = utils.get_refs(self.node_settings) assert_equal(branch, gitlab_mock.repo.return_value['default_branch']) assert_equal(sha, self._get_sha_for_branch( branch=None)) # Get refs for default branch assert_equal(branches, gitlab_mock.branches.return_value)
def test_get_refs_branch(self, mock_repo, mock_branches): gitlab_mock = self.gitlab mock_repo.return_value = gitlab_mock.repo.return_value mock_branches.return_value = gitlab_mock.branches.return_value branch, sha, branches = utils.get_refs(self.node_settings, 'master') assert_equal(branch, 'master') assert_equal(sha, branches[0].commit['id']) assert_equal( branches, gitlab_mock.branches.return_value )
def test_get_refs_branch(self, mock_repo, mock_branches): gitlab_mock = self.gitlab mock_repo.return_value = gitlab_mock.repo.return_value mock_branches.return_value = gitlab_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) assert_equal( branches, gitlab_mock.branches.return_value )
def test_get_refs_defaults(self, mock_repo, mock_branches): gitlab_mock = self.gitlab mock_repo.return_value = gitlab_mock.repo mock_branches.return_value = gitlab_mock.branches.return_value branch, sha, branches = utils.get_refs(self.node_settings) assert_equal( branch, gitlab_mock.repo.default_branch ) assert_equal(sha, branches[0].commit['id']) # Get refs for default branch assert_equal( branches, gitlab_mock.branches.return_value )
def test_get_refs_defaults(self, mock_repo, mock_branches): gitlab_mock = self.gitlab mock_repo.return_value = gitlab_mock.repo.return_value mock_branches.return_value = gitlab_mock.branches.return_value branch, sha, branches = utils.get_refs(self.node_settings) assert_equal( branch, gitlab_mock.repo.return_value['default_branch'] ) assert_equal(sha, self._get_sha_for_branch(branch=None)) # Get refs for default branch assert_equal( branches, gitlab_mock.branches.return_value )
def test_get_refs_sha_no_branch(self): with assert_raises(HTTPError): utils.get_refs(self.node_settings, sha='12345')