<commit_before># -*- coding: utf-8 -*-
import logging
from mako.template import Template

from framework import request
from framework.auth.decorators import Auth

from website.project.decorators import must_be_contributor_or_public
from website.project.decorators import must_have_addon

from ..api import GitHub, to_hgrid, ref_to_params
from .util import _get_refs, _check_permissions

logger = logging.getLogger(__name__)

github_branch_template = Template('''
    % if len(branches) > 1:
            <select class="github-branch-select">
                % for each in branches:
                    <option value="${each}" ${"selected" if each == branch else ""}>${each}</option>
                % endfor
            </select>
    % else:
        <span>${branch}</span>
    % endif
    % if sha:
        <a href="https://github.com/${owner}/${repo}/commit/${sha}" class="github-sha text-muted">${sha[:10]}</a>
    % endif
''')

def github_branch_widget(branches, owner, repo, branch, sha):
    """Render branch selection widget for GitHub add-on. Displayed in the
    name field of HGrid file trees.

    """
    rendered = github_branch_template.render(
        branches=[
            each['name']
            for each in branches
        ],
        branch=branch,
        sha=sha,
        owner=owner,
        repo=repo
    )
    return rendered


def github_hgrid_data(node_settings, auth, parent=None, contents=False, *args, **kwargs):

    # Quit if no repo linked
    if not node_settings.user or not node_settings.repo:
        return

    connection = GitHub.from_settings(node_settings.user_settings)

    branch, sha, branches = _get_refs(
        node_settings,
        branch=kwargs.get('branch'),
        sha=kwargs.get('sha'),
        connection=connection,
    )

    if branch is not None:
        ref = ref_to_params(branch, sha)
        can_edit = _check_permissions(
            node_settings, auth, connection, branch, sha
        )
        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 = ('GitHub: '
                '{user}/{repo}').format(user=node_settings.user,
                                                    repo=node_settings.repo)
    rv = {
        'addon': node_settings.config.short_name,
        'name': name_tpl,
        'extra': name_append,
        'kind': 'folder',
        'hasIcon': 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/',
        },
        'permissions': {
            'view': True,
            'edit': can_edit,
        },
        'accept': {
            'maxSize': node_settings.config.max_file_size,
            'extensions': node_settings.config.accept_extensions,
        }
    }

    return rv


@must_be_contributor_or_public
@must_have_addon('github', 'node')
def github_root_folder_public(*args, **kwargs):
    """View function returning the root container for a GitHub repo. In
    contrast to other add-ons, this is exposed via the API for GitHub to
    accommodate switching between branches and commits.

    """
    node_settings = kwargs['node_addon']
    auth = kwargs['auth']
    data = request.args.to_dict()
    parent = data.pop('parent', 'null')

    return github_hgrid_data(node_settings, auth=auth, parent=parent, contents=False, **data)<commit_msg>Updated to use Rubeus<commit_after># -*- coding: utf-8 -*-
import logging
from mako.template import Template

from framework import request
from framework.auth.decorators import Auth

from website.project.decorators import must_be_contributor_or_public
from website.project.decorators import must_have_addon

from ..api import GitHub, to_hgrid, ref_to_params
from .util import _get_refs, _check_permissions
from website.util import rubeus

logger = logging.getLogger(__name__)

github_branch_template = Template('''
    % if len(branches) > 1:
            <select class="github-branch-select">
                % for each in branches:
                    <option value="${each}" ${"selected" if each == branch else ""}>${each}</option>
                % endfor
            </select>
    % else:
        <span>${branch}</span>
    % endif
    % if sha:
        <a href="https://github.com/${owner}/${repo}/commit/${sha}" class="github-sha text-muted">${sha[:10]}</a>
    % endif
''')

def github_branch_widget(branches, owner, repo, branch, sha):
    """Render branch selection widget for GitHub add-on. Displayed in the
    name field of HGrid file trees.

    """
    rendered = github_branch_template.render(
        branches=[
            each['name']
            for each in branches
        ],
        branch=branch,
        sha=sha,
        owner=owner,
        repo=repo
    )
    return rendered


def github_hgrid_data(node_settings, auth, parent=None, contents=False, *args, **kwargs):

    # Quit if no repo linked
    if not node_settings.user or not node_settings.repo:
        return

    connection = GitHub.from_settings(node_settings.user_settings)

    branch, sha, branches = _get_refs(
        node_settings,
        branch=kwargs.get('branch'),
        sha=kwargs.get('sha'),
        connection=connection,
    )

    if branch is not None:
        ref = ref_to_params(branch, sha)
        can_edit = _check_permissions(
            node_settings, auth, connection, branch, sha
        )
        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 = ('GitHub: '
                '{user}/{repo}').format(user=node_settings.user,
                                                    repo=node_settings.repo)
    rv = {
        'addon': node_settings.config.short_name,
        'name': name_tpl,
        'extra': name_append,
        'kind': 'folder',
        'hasIcon': 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/',
        },
        'permissions': {
            'view': True,
            'edit': can_edit,
        },
        'accept': {
            'maxSize': node_settings.config.max_file_size,
            'extensions': node_settings.config.accept_extensions,
        }
    }

    return rubeus.build_dummy_folder(node_settings,'{user}/{repo}'.format(user=node_settings.user,
                                                    repo=node_settings.repo), 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/',
        },extra=name_append)


@must_be_contributor_or_public
@must_have_addon('github', 'node')
def github_root_folder_public(*args, **kwargs):
    """View function returning the root container for a GitHub repo. In
    contrast to other add-ons, this is exposed via the API for GitHub to
    accommodate switching between branches and commits.

    """
    node_settings = kwargs['node_addon']
    auth = kwargs['auth']
    data = request.args.to_dict()
    parent = data.pop('parent', 'null')

    return github_hgrid_data(node_settings, auth=auth, parent=parent, contents=False, **data)
{'fraction_non_alphanumeric': 0.1196116504854369, 'fraction_numerical': 0.001035598705501618, 'mean_word_length': 2.455277280858676, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4423931', 'n_tokens_mistral': 2291, 'n_tokens_neox': 2241, 'n_words': 530}