Exemple #1
0
 def test_permissions_not_head(self, mock_has_auth):
     mock_has_auth.return_value = True
     connection = github_mock
     mock_branch = mock.NonCallableMock()
     mock_branch.commit.sha = '67890'
     sha = '12345'
     assert_false(check_permissions(self.node_settings, self.consolidated_auth, connection, mock_branch, sha=sha))
Exemple #2
0
 def test_permissions_no_auth(self):
     # project is set to private right now
     connection = github_mock
     non_authenticated_user = UserFactory()
     non_authenticated_auth = Auth(user=non_authenticated_user)
     branch = 'master'
     assert_false(check_permissions(self.node_settings, non_authenticated_auth, connection, branch))
Exemple #3
0
 def test_permissions_not_head(self, mock_has_auth):
     github_mock = self.github
     mock_has_auth.return_value = True
     connection = github_mock
     mock_branch = mock.NonCallableMock()
     mock_branch.commit.sha = '67890'
     sha = '12345'
     assert_false(check_permissions(self.node_settings, self.consolidated_auth, connection, mock_branch, sha=sha))
Exemple #4
0
 def test_permissions_no_auth(self):
     github_mock = self.github
     # project is set to private right now
     connection = github_mock
     non_authenticated_user = UserFactory()
     non_authenticated_auth = Auth(user=non_authenticated_user)
     branch = 'master'
     assert_false(check_permissions(self.node_settings, non_authenticated_auth, connection, branch))
Exemple #5
0
 def test_permissions(self, mock_has_auth):
     github_mock = self.github
     mock_has_auth.return_value = True
     connection = github_mock
     self.node_settings.owner.is_registration = True
     assert_false(
         check_permissions(self.node_settings, self.consolidated_auth,
                           connection, 'master'))
Exemple #6
0
 def test_permissions_no_access(self, mock_repo, mock_has_auth):
     mock_has_auth.return_value = True
     connection = github_mock
     branch = 'master'
     mock_repository = mock.NonCallableMock()
     mock_repository.user = '******'
     mock_repository.repo = 'mock-repo'
     mock_repository.to_json.return_value = {'user': '******',
                                             'repo': 'mock-repo',
                                             'permissions': {
                                                 'push': False, # this is key
                                                 },
                                            }
     mock_repo.return_value = mock_repository
     assert_false(check_permissions(self.node_settings, self.consolidated_auth, connection, branch, repo=mock_repository))
Exemple #7
0
 def test_permissions_no_access(self, mock_repo, mock_has_auth):
     github_mock = self.github
     mock_has_auth.return_value = True
     connection = github_mock
     branch = 'master'
     mock_repository = mock.NonCallableMock()
     mock_repository.user = '******'
     mock_repository.repo = 'mock-repo'
     mock_repository.to_json.return_value = {
         'user': '******',
         'repo': 'mock-repo',
         'permissions': {
             'push': False,  # this is key
         },
     }
     mock_repo.return_value = mock_repository
     assert_false(check_permissions(self.node_settings, self.consolidated_auth, connection, branch, repo=mock_repository))
Exemple #8
0
def github_hgrid_data_contents(**kwargs):
    """Return a repo's file tree as a dict formatted for HGrid.

    """
    auth = kwargs['auth']
    node = kwargs['node'] or kwargs['project']
    node_addon = kwargs['node_addon']
    path = kwargs.get('path', '')

    connection = GitHub.from_settings(node_addon.user_settings)
    # The requested branch and sha
    req_branch, req_sha = request.args.get('branch'), request.args.get('sha')
    # The actual branch and sha to use, given the addon settings
    branch, sha, branches = get_refs(node_addon,
                                     req_branch,
                                     req_sha,
                                     connection=connection)
    # Get file tree
    try:
        contents = connection.contents(
            user=node_addon.user,
            repo=node_addon.repo,
            path=path,
            ref=sha or branch,
        )
    except ApiError:
        raise HTTPError(http.NOT_FOUND)

    can_edit = check_permissions(node_addon, auth, connection, branch, sha)

    if contents:
        hgrid_tree = to_hgrid(
            contents,
            node_url=node.url,
            node_api_url=node.api_url,
            branch=branch,
            sha=sha,
            can_edit=can_edit,
            parent=path,
            max_size=node_addon.config.max_file_size,
            accepted_files=node_addon.config.accept_extensions)
    else:
        hgrid_tree = []
    return hgrid_tree
Exemple #9
0
def github_hgrid_data_contents(**kwargs):
    """Return a repo's file tree as a dict formatted for HGrid.

    """
    auth = kwargs['auth']
    node = kwargs['node'] or kwargs['project']
    node_addon = kwargs['node_addon']
    path = kwargs.get('path', '')

    connection = GitHub.from_settings(node_addon.user_settings)
    # The requested branch and sha
    req_branch, req_sha = request.args.get('branch'), request.args.get('sha')
    # The actual branch and sha to use, given the addon settings
    branch, sha, branches = get_refs(
        node_addon, req_branch, req_sha, connection=connection
    )
    # Get file tree
    try:
        contents = connection.contents(
            user=node_addon.user, repo=node_addon.repo, path=path,
            ref=sha or branch,
        )
    except ApiError:
        raise HTTPError(http.NOT_FOUND)

    can_edit = check_permissions(node_addon, auth, connection, branch, sha)

    if contents:
        hgrid_tree = to_hgrid(
            contents, node_url=node.url, node_api_url=node.api_url,
            branch=branch, sha=sha, can_edit=can_edit, parent=path,
            max_size=node_addon.config.max_file_size,
            accepted_files=node_addon.config.accept_extensions
        )
    else:
        hgrid_tree = []
    return hgrid_tree
Exemple #10
0
def github_hgrid_data(node_settings, auth, **kwargs):

    # Quit if no repo linked
    if not node_settings.complete:
        return

    connection = GitHubClient(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 and not node.is_contributor(auth.user):
        try:
            repo = connection.repo(node_settings.user, node_settings.repo)
        except NotFoundError:
            # TODO: Test me @jmcarp
            # TODO: Add warning message
            logger.error('Could not access GitHub repo')
            return None
        if repo.private:
            return None

    try:
        branch, sha, branches = get_refs(
            node_settings,
            branch=kwargs.get('branch'),
            sha=kwargs.get('sha'),
            connection=connection,
        )
    except (NotFoundError, GitHubError):
        # TODO: Show an alert or change GitHub configuration?
        logger.error('GitHub 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 = None
        can_edit = False

    name_tpl = '{user}/{repo}'.format(
        user=node_settings.user, repo=node_settings.repo
    )

    permissions = {
        'edit': can_edit,
        'view': True,
        'private': node_settings.is_private
    }
    urls = {
        'upload': node_settings.owner.api_url + 'github/file/' + (ref or ''),
        'fetch': node_settings.owner.api_url + 'github/hgrid/' + (ref or ''),
        'branch': node_settings.owner.api_url + 'github/hgrid/root/',
        'zip': node_settings.owner.api_url + 'github/zipball/' + (ref or ''),
        'repo': "https://github.com/{0}/{1}/tree/{2}".format(node_settings.user, node_settings.repo, branch)
    }

    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,
    )]
Exemple #11
0
 def test_permissions(self, mock_has_auth):
     github_mock = self.github
     mock_has_auth.return_value = True
     connection = github_mock
     self.node_settings.owner.is_registration = True
     assert_false(check_permissions(self.node_settings, self.consolidated_auth, connection, 'master'))
Exemple #12
0
def github_hgrid_data(node_settings, auth, **kwargs):

    # Quit if no repo linked
    if not node_settings.complete:
        return

    connection = GitHub.from_settings(node_settings.user_settings)

    # 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 and not node.is_contributor(auth.user):
        try:
            repo = connection.repo(node_settings.user, node_settings.repo)
        except NotFoundError:
            # TODO: Test me @jmcarp
            # TODO: Add warning message
            logger.error("Could not access GitHub repo")
            return None
        if repo.private:
            return None

    try:
        branch, sha, branches = get_refs(
            node_settings, branch=kwargs.get("branch"), sha=kwargs.get("sha"), connection=connection
        )
    except (NotFoundError, GitHubError):
        # TODO: Show an alert or change GitHub configuration?
        logger.error("GitHub 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 = None
        can_edit = False

    name_tpl = "{user}/{repo}".format(user=node_settings.user, repo=node_settings.repo)

    permissions = {"edit": can_edit, "view": True}
    urls = {
        "upload": node_settings.owner.api_url + "github/file/" + (ref or ""),
        "fetch": node_settings.owner.api_url + "github/hgrid/" + (ref or ""),
        "branch": node_settings.owner.api_url + "github/hgrid/root/",
        "zip": node_settings.owner.api_url + "github/zipball/" + (ref or ""),
        "repo": github_repo_url(owner=node_settings.user, repo=node_settings.repo, branch=branch),
    }

    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
        )
    ]
Exemple #13
0
def github_hgrid_data(node_settings, auth, **kwargs):

    # Quit if no repo linked
    if not node_settings.complete:
        return

    connection = GitHub.from_settings(node_settings.user_settings)

    # 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 and not node.is_contributor(auth.user):
        try:
            repo = connection.repo(node_settings.user, node_settings.repo)
        except NotFoundError:
            # TODO: Test me @jmcarp
            # TODO: Add warning message
            logger.error('Could not access GitHub repo')
            return None
        if repo.private:
            return None

    try:
        branch, sha, branches = get_refs(
            node_settings,
            branch=kwargs.get('branch'),
            sha=kwargs.get('sha'),
            connection=connection,
        )
    except NotFoundError:
        # TODO: Show an alert or change GitHub configuration?
        logger.error('GitHub 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,
        )
        name_append = github_branch_widget(branches, owner=node_settings.user,
            repo=node_settings.repo, branch=branch, sha=sha)
    else:

        ref = None
        can_edit = False
        name_append = None

    name_tpl = '{user}/{repo}'.format(
        user=node_settings.user, repo=node_settings.repo
    )

    permissions = {
        'edit': can_edit,
        'view': True
    }
    urls = {
        'upload': node_settings.owner.api_url + 'github/file/' + (ref or ''),
        'fetch': node_settings.owner.api_url + 'github/hgrid/' + (ref or ''),
        'branch': node_settings.owner.api_url + 'github/hgrid/root/',
        'zip': node_settings.owner.api_url + 'github/zipball/' + (ref or ''),
        'repo': github_repo_url(owner=node_settings.user, repo=node_settings.repo, branch=branch)
    }
    buttons = [
        rubeus.build_addon_button('<i class="icon-download-alt"></i>', 'githubDownloadZip', "Download Zip"),
        rubeus.build_addon_button('<i class="icon-external-link"></i>', 'githubVisitRepo', "Visit Repository"),
    ]

    return [rubeus.build_addon_root(
        node_settings,
        name_tpl,
        urls=urls,
        permissions=permissions,
        extra=name_append,
        buttons=buttons,
    )]