Exemple #1
0
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,
    )]
Exemple #2
0
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'],
    )]
Exemple #3
0
    def before_page_load(self, node, user):
        """

        :param Node node:
        :param User user:
        :return str: Alert message
        """
        messages = []

        # Quit if not contributor
        if not node.is_contributor(user):
            return messages

        # Quit if not configured
        if self.user is None or self.repo is None:
            return messages

        # Quit if no user authorization
        if self.user_settings is None:
            return messages

        connect = GitLabClient(external_account=self.external_account)

        try:
            repo = connect.repo(self.repo_id)
        except (ApiError, GitLabError):
            return
        except gitlab.exceptions.GitlabError as exc:
            if exc.response_code == 403 and 'must accept the Terms of Service' in exc.error_message:
                return [(
                    'Your gitlab account does not have proper authentication. Ensure you have agreed to Gitlab\'s '
                    'current Terms of Service by disabling and re-enabling your account.'
                )]
            else:
                raise exc

        # GitLab has visibility types: public, private, internal.
        node_permissions = 'public' if node.is_public else 'private'
        if repo.visibility != node_permissions:
            message = (
                'Warning: This OSF {category} is {node_perm}, but the GitLab '
                'repo {user} / {repo} has {repo_perm} visibility.'.format(
                    category=markupsafe.escape(node.project_or_component),
                    node_perm=markupsafe.escape(node_permissions),
                    repo_perm=markupsafe.escape(repo.visibility),
                    user=markupsafe.escape(self.user),
                    repo=markupsafe.escape(self.repo),
                ))
            if repo.visibility == 'private':
                message += (
                    ' Users can view the contents of this private GitLab '
                    'repository through this public project.')
            else:
                message += (
                    ' The files in this GitLab repo can be viewed on GitLab '
                    '<u><a href="{url}">here</a></u>.').format(
                        url=repo.http_url_to_repo)
            messages.append(message)
            return messages
    def before_page_load(self, node, user):
        """

        :param Node node:
        :param User user:
        :return str: Alert message
        """
        messages = []

        # Quit if not contributor
        if not node.is_contributor(user):
            return messages

        # Quit if not configured
        if self.user is None or self.repo is None:
            return messages

        # Quit if no user authorization
        if self.user_settings is None:
            return messages

        connect = GitLabClient(external_account=self.external_account)

        try:
            repo = connect.repo(self.repo_id)
        except (ApiError, GitLabError):
            return
        except gitlab.exceptions.GitlabError as exc:
            if exc.response_code == 403 and 'must accept the Terms of Service' in exc.error_message:
                return [('Your gitlab account does not have proper authentication. Ensure you have agreed to Gitlab\'s '
                         'current Terms of Service by disabling and re-enabling your account.')]
            else:
                raise exc

        # GitLab has visibility types: public, private, internal.
        node_permissions = 'public' if node.is_public else 'private'
        if repo.visibility != node_permissions:
            message = (
                'Warning: This OSF {category} is {node_perm}, but the GitLab '
                'repo {user} / {repo} has {repo_perm} visibility.'.format(
                    category=markupsafe.escape(node.project_or_component),
                    node_perm=markupsafe.escape(node_permissions),
                    repo_perm=markupsafe.escape(repo.visibility),
                    user=markupsafe.escape(self.user),
                    repo=markupsafe.escape(self.repo),
                )
            )
            if repo.visibility == 'private':
                message += (
                    ' Users can view the contents of this private GitLab '
                    'repository through this public project.'
                )
            else:
                message += (
                    ' The files in this GitLab repo can be viewed on GitLab '
                    '<u><a href="{url}">here</a></u>.'
                ).format(url=repo.http_url_to_repo)
            messages.append(message)
            return messages
Exemple #5
0
    def before_page_load(self, node, user):
        """

        :param Node node:
        :param User user:
        :return str: Alert message
        """
        messages = []

        # Quit if not contributor
        if not node.is_contributor(user):
            return messages

        # Quit if not configured
        if self.user is None or self.repo is None:
            return messages

        # Quit if no user authorization
        if self.user_settings is None:
            return messages

        connect = GitLabClient(external_account=self.external_account)

        try:
            repo = connect.repo(self.repo_id)
        except (ApiError, GitLabError):
            return

        # GitLab has visibility types: public, private, internal.
        node_permissions = 'public' if node.is_public else 'private'
        if repo.visibility != node_permissions:
            message = (
                'Warning: This OSF {category} is {node_perm}, but the GitLab '
                'repo {user} / {repo} has {repo_perm} visibility.'.format(
                    category=markupsafe.escape(node.project_or_component),
                    node_perm=markupsafe.escape(node_permissions),
                    repo_perm=markupsafe.escape(repo.visibility),
                    user=markupsafe.escape(self.user),
                    repo=markupsafe.escape(self.repo),
                )
            )
            if repo.visibility == 'private':
                message += (
                    ' Users can view the contents of this private GitLab '
                    'repository through this public project.'
                )
            else:
                message += (
                    ' The files in this GitLab repo can be viewed on GitLab '
                    '<u><a href="{url}">here</a></u>.'
                ).format(url=repo.http_url_to_repo)
            messages.append(message)
            return messages
Exemple #6
0
 def is_private(self):
     connection = GitLabClient(external_account=self.external_account)
     return connection.repo(self.repo_id).visibility == 'private'
Exemple #7
0
 def is_private(self):
     connection = GitLabClient(external_account=self.external_account)
     return connection.repo(self.repo_id).visibility == 'private'
Exemple #8
0
def gitlab_set_config(auth, **kwargs):
    node_settings = kwargs.get('node_addon', None)
    node = kwargs.get('node', None)
    user_settings = kwargs.get('user_addon', None)

    try:
        if not node:
            node = node_settings.owner
        if not user_settings:
            user_settings = node_settings.user_settings
    except AttributeError:
        raise HTTPError(http.BAD_REQUEST)

    # Parse request
    gitlab_user_name = request.json.get('gitlab_user', '')
    gitlab_repo_name = request.json.get('gitlab_repo', '')
    gitlab_repo_id = request.json.get('gitlab_repo_id', '')

    if not gitlab_user_name or not gitlab_repo_name or not gitlab_repo_id:
        raise HTTPError(http.BAD_REQUEST)

    # Verify that repo exists and that user can access
    connection = GitLabClient(external_account=node_settings.external_account)
    repo = connection.repo(gitlab_repo_id)
    if repo is None:
        if user_settings:
            message = (
                'Cannot access repo. Either the repo does not exist '
                'or your account does not have permission to view it.'
            )
        else:
            message = (
                'Cannot access repo.'
            )
        return {'message': message}, http.BAD_REQUEST

    changed = (
        gitlab_user_name != node_settings.user or
        gitlab_repo_name != node_settings.repo or
        gitlab_repo_id != node_settings.repo_id
    )

    # Update hooks
    if changed:

        # Delete existing hook, if any
        node_settings.delete_hook()

        # Update node settings
        node_settings.user = gitlab_user_name
        node_settings.repo = gitlab_repo_name
        node_settings.repo_id = gitlab_repo_id

        # Log repo select
        node.add_log(
            action='gitlab_repo_linked',
            params={
                'project': node.parent_id,
                'node': node._id,
                'gitlab': {
                    'user': gitlab_user_name,
                    'repo': gitlab_repo_name,
                    'repo_id': gitlab_repo_id,
                }
            },
            auth=auth,
        )

        # Add new hook
        if node_settings.user and node_settings.repo:
            node_settings.add_hook(save=False)

        node_settings.save()

    return {}
Exemple #9
0
 def is_private(self):
     connection = GitLabClient(external_account=self.external_account)
     return not connection.repo(repo_id=self.repo_id)['public']
Exemple #10
0
def gitlab_set_config(auth, **kwargs):
    node_settings = kwargs.get('node_addon', None)
    node = kwargs.get('node', None)
    user_settings = kwargs.get('user_addon', None)

    try:
        if not node:
            node = node_settings.owner
        if not user_settings:
            user_settings = node_settings.user_settings
    except AttributeError:
        raise HTTPError(http.BAD_REQUEST)

    # Parse request
    gitlab_user_name = request.json.get('gitlab_user', '')
    gitlab_repo_name = request.json.get('gitlab_repo', '')
    gitlab_repo_id = request.json.get('gitlab_repo_id', '')

    if not gitlab_user_name or not gitlab_repo_name or not gitlab_repo_id:
        raise HTTPError(http.BAD_REQUEST)

    # Verify that repo exists and that user can access
    connection = GitLabClient(external_account=node_settings.external_account)
    repo = connection.repo(gitlab_repo_id)
    if repo is None:
        if user_settings:
            message = ('Cannot access repo. Either the repo does not exist '
                       'or your account does not have permission to view it.')
        else:
            message = ('Cannot access repo.')
        return {'message': message}, http.BAD_REQUEST

    changed = (gitlab_user_name != node_settings.user
               or gitlab_repo_name != node_settings.repo
               or gitlab_repo_id != node_settings.repo_id)

    # Update hooks
    if changed:

        # Delete existing hook, if any
        node_settings.delete_hook()

        # Update node settings
        node_settings.user = gitlab_user_name
        node_settings.repo = gitlab_repo_name
        node_settings.repo_id = gitlab_repo_id

        # Log repo select
        node.add_log(
            action='gitlab_repo_linked',
            params={
                'project': node.parent_id,
                'node': node._id,
                'gitlab': {
                    'user': gitlab_user_name,
                    'repo': gitlab_repo_name,
                    'repo_id': gitlab_repo_id,
                }
            },
            auth=auth,
        )

        # Add new hook
        if node_settings.user and node_settings.repo:
            node_settings.add_hook(save=False)

        node_settings.save()

    return {}
Exemple #11
0
 def is_private(self):
     connection = GitLabClient(external_account=self.external_account)
     return not connection.repo(repo_id=self.repo_id)['public']
Exemple #12
0
def gitlab_set_config(auth, **kwargs):
    node_settings = kwargs.get('node_addon', None)
    node = kwargs.get('node', None)
    user_settings = kwargs.get('user_addon', None)

    try:
        if not node:
            node = node_settings.owner
        if not user_settings:
            user_settings = node_settings.user_settings
    except AttributeError:
        raise HTTPError(http_status.HTTP_400_BAD_REQUEST)

    # Parse request
    gitlab_user_name = request.json.get('gitlab_user', '')
    gitlab_repo_name = request.json.get('gitlab_repo', '')
    gitlab_repo_id = request.json.get('gitlab_repo_id', '')

    if not gitlab_user_name or not gitlab_repo_name or not gitlab_repo_id:
        raise HTTPError(http_status.HTTP_400_BAD_REQUEST)

    # Verify that repo exists and that user can access
    connection = GitLabClient(external_account=node_settings.external_account)

    try:
        repo = connection.repo(gitlab_repo_id)
    except gitlab.exceptions.GitlabError as exc:
        if exc.response_code == 403 and 'must accept the Terms of Service' in exc.error_message:
            return {
                'message':
                'Your gitlab account does not have proper authentication. Ensure you have agreed to Gitlab\'s '
                'current Terms of Service by disabling and re-enabling your account.'
            }, http_status.HTTP_400_BAD_REQUEST

    if repo is None:
        if user_settings:
            message = ('Cannot access repo. Either the repo does not exist '
                       'or your account does not have permission to view it.')
        else:
            message = ('Cannot access repo.')
        return {'message': message}, http_status.HTTP_400_BAD_REQUEST

    changed = (gitlab_user_name != node_settings.user
               or gitlab_repo_name != node_settings.repo
               or gitlab_repo_id != node_settings.repo_id)

    # Update hooks
    if changed:

        # Delete existing hook, if any
        node_settings.delete_hook()

        # Update node settings
        node_settings.user = gitlab_user_name
        node_settings.repo = gitlab_repo_name
        node_settings.repo_id = gitlab_repo_id

        # Log repo select
        node.add_log(
            action='gitlab_repo_linked',
            params={
                'project': node.parent_id,
                'node': node._id,
                'gitlab': {
                    'user': gitlab_user_name,
                    'repo': gitlab_repo_name,
                    'repo_id': gitlab_repo_id,
                }
            },
            auth=auth,
        )

        # Add new hook
        if node_settings.user and node_settings.repo:
            node_settings.add_hook(save=False)

        node_settings.save()

    return {}