<commit_before>from tornado import web, gen

from docker.errors import NotFound
from jupyterhub.handlers.base import BaseHandler
from IPython.html.utils import url_path_join
from tornado.httputil import url_concat
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
import json

import re

@gen.coroutine
def _fork_github_repo(url, token):
    http_client = AsyncHTTPClient()

    headers={"User-Agent": "JupyterHub",
             "Authorization": "token {}".format(token)
    }

    result = re.findall('^https://github.com/([^/]+)/([^/]+).*', url)
    if not result:
        raise ValueError('URL is not a github URL')

    owner, repo = result[0]

    api_url = "https://api.github.com/repos/%s/%s/forks" % (owner, repo)

    req = HTTPRequest(api_url,
                      method="POST",
                      headers=headers,
                      body='',
                      )

    resp = yield http_client.fetch(req)
    return json.loads(resp.body.decode('utf8', 'replace'))

@gen.coroutine
def _github_fork_exists(username, url, token):
    http_client = AsyncHTTPClient()

    headers={"User-Agent": "JupyterHub",
             "Authorization": "token {}".format(token)
    }

    result = re.findall('^https://github.com/([^/]+)/([^/]+).*', url)
    if not result:
        raise ValueError('URL is not a github URL')

    owner, repo = result[0]
    api_url = "https://api.github.com/repos/%s/%s" % (username, repo)

    req = HTTPRequest(api_url,
                      method="GET",
                      headers=headers,
                      )

    try:
        resp = yield http_client.fetch(req)
        return True
    except:
        return False

@gen.coroutine
def _repository_changed(user):
    try:
        setup = yield user.spawner.docker(
                'exec_create',
                container=user.spawner.container_id,
                cmd="bash -c 'cd analysis/ && \
                     (git fetch --unshallow > /dev/null 2>&1; true) && \
                     git diff --name-only'",
                )
        out = yield user.spawner.docker(
                'exec_start',
                exec_id=setup['Id'],
        )
    except NotFound:
        return False
    if out:
        return True
    else:
        return False

@gen.coroutine
def _push_github_repo(user, url, token):
    result = re.findall('^https://github.com/([^/]+)/([^/]+).*', url)
    if not result:
        raise ValueError('URL is not a github URL')

    owner, repo = result[0]
    fork_url = "https://{}@github.com/{}/{}".format(token, user.name, repo)

    out = yield user.spawner.docker(
            'exec_create',
            container=user.spawner.container_id,
            cmd="bash -c 'cd analysis/ && \
                 git config --global user.email \"<EMAIL>\" && \
                 git config --global user.name \"Everware\" && \
                 (git fetch --unshallow; true) && \
                 git add . && \
                 git commit -m \"Update through everware\" && \
                 (git remote add everware-fork {}; true) && \
                 (git checkout -b everware; true) && \
                 git push everware-fork everware'".format(fork_url),
            )
    response = yield user.spawner.docker(
            'exec_start',
            exec_id=out['Id'],
    )


class HomeHandler(BaseHandler):
    """Render the user's home page."""

    @web.authenticated
    @gen.coroutine
    def get(self):
        user = self.get_current_user()
        repourl = self.get_argument('repourl', '')
        do_fork = self.get_argument('do_fork', False)
        do_push = self.get_argument('do_push', False)
        if repourl:
            self.log.info('Got %s in home' % repourl)
            self.redirect(url_concat(
                url_path_join(self.hub.server.base_url, 'spawn'), {
                    'repourl': repourl
                }
            ))
            return

        stat = yield user.spawner.poll()
        self.log.debug("The container is {}".format(repr(stat)))
        if user.running:
            if do_fork:
                self.log.info('Will fork %s' % user.spawner.repo_url)
                yield _fork_github_repo(
                        user.spawner.repo_url,
                        user.token,
                    )
                self.redirect('/hub/home')
                return
            if do_push:
                self.log.info('Will push to fork')
                yield _push_github_repo(
                        user,
                        user.spawner.repo_url,
                        user.token,
                    )
                self.redirect('/hub/home')
                return
            repo_url = user.spawner.repo_url
            fork_exists = yield _github_fork_exists(
                                    user.name,
                                    user.spawner.repo_url,
                                    user.token,<commit_msg>one more check<commit_after>from tornado import web, gen

from docker.errors import NotFound
from jupyterhub.handlers.base import BaseHandler
from IPython.html.utils import url_path_join
from tornado.httputil import url_concat
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
import json

import re

@gen.coroutine
def _fork_github_repo(url, token):
    http_client = AsyncHTTPClient()

    headers={"User-Agent": "JupyterHub",
             "Authorization": "token {}".format(token)
    }

    result = re.findall('^https://github.com/([^/]+)/([^/]+).*', url)
    if not result:
        raise ValueError('URL is not a github URL')

    owner, repo = result[0]

    api_url = "https://api.github.com/repos/%s/%s/forks" % (owner, repo)

    req = HTTPRequest(api_url,
                      method="POST",
                      headers=headers,
                      body='',
                      )

    resp = yield http_client.fetch(req)
    return json.loads(resp.body.decode('utf8', 'replace'))

@gen.coroutine
def _github_fork_exists(username, url, token):
    http_client = AsyncHTTPClient()

    headers={"User-Agent": "JupyterHub",
             "Authorization": "token {}".format(token)
    }

    result = re.findall('^https://github.com/([^/]+)/([^/]+).*', url)
    if not result:
        raise ValueError('URL is not a github URL')

    owner, repo = result[0]
    api_url = "https://api.github.com/repos/%s/%s" % (username, repo)

    req = HTTPRequest(api_url,
                      method="GET",
                      headers=headers,
                      )

    try:
        resp = yield http_client.fetch(req)
        return True
    except:
        return False

@gen.coroutine
def _repository_changed(user):
    try:
        setup = yield user.spawner.docker(
                'exec_create',
                container=user.spawner.container_id,
                cmd="bash -c 'cd analysis/ && \
                     (git fetch --unshallow > /dev/null 2>&1; true) && \
                     git diff --name-only'",
                )
        out = yield user.spawner.docker(
                'exec_start',
                exec_id=setup['Id'],
        )
    except NotFound:
        return False
    if out:
        return True
    else:
        return False

@gen.coroutine
def _push_github_repo(user, url, token):
    result = re.findall('^https://github.com/([^/]+)/([^/]+).*', url)
    if not result:
        raise ValueError('URL is not a github URL')

    owner, repo = result[0]
    fork_url = "https://{}@github.com/{}/{}".format(token, user.name, repo)

    out = yield user.spawner.docker(
            'exec_create',
            container=user.spawner.container_id,
            cmd="bash -c 'cd analysis/ && \
                 git config --global user.email \"<EMAIL>\" && \
                 git config --global user.name \"Everware\" && \
                 (git fetch --unshallow; true) && \
                 git add . && \
                 git commit -m \"Update through everware\" && \
                 (git remote add everware-fork {}; true) && \
                 (git checkout -b everware; true) && \
                 git push everware-fork everware'".format(fork_url),
            )
    response = yield user.spawner.docker(
            'exec_start',
            exec_id=out['Id'],
    )


class HomeHandler(BaseHandler):
    """Render the user's home page."""

    @web.authenticated
    @gen.coroutine
    def get(self):
        user = self.get_current_user()
        repourl = self.get_argument('repourl', '')
        do_fork = self.get_argument('do_fork', False)
        do_push = self.get_argument('do_push', False)
        if repourl:
            self.log.info('Got %s in home' % repourl)
            self.redirect(url_concat(
                url_path_join(self.hub.server.base_url, 'spawn'), {
                    'repourl': repourl
                }
            ))
            return

        stat = yield user.spawner.poll()
        self.log.debug("The container is {}".format(repr(stat)))
        if user.running and hasattr(user, "login_service") and user.login_service == "github":
            if do_fork:
                self.log.info('Will fork %s' % user.spawner.repo_url)
                yield _fork_github_repo(
                        user.spawner.repo_url,
                        user.token,
                    )
                self.redirect('/hub/home')
                return
            if do_push:
                self.log.info('Will push to fork')
                yield _push_github_repo(
                        user,
                        user.spawner.repo_url,
                        user.token,
                    )
                self.redirect('/hub/home')
                return
            repo_url = user.spawner.repo_url
            fork_exists = yield _github_fork_exists(
                                    user.name,
                                    user.spawner.repo_url,
                                    user.token,
{'fraction_non_alphanumeric': 0.12400603925515853, 'fraction_numerical': 0.0012078510317060896, 'mean_word_length': 1.5667786101782486, 'pattern_counts': {'":': 9, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 12, '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': '6853297', 'n_tokens_mistral': 2969, 'n_tokens_neox': 2812, 'n_words': 619}