Exemplo n.º 1
0
    def append_conf(self, **kwargs):
        """Modify the given ``conf.py`` file from a whitelisted user's project.
        """

        # Pull config data
        try:
            conf_py_path = version_utils.get_conf_py_path(self.version)
        except ProjectImportError:
            self._write_config()
            self.create_index(extension='rst')

        project = self.version.project
        # Open file for appending.
        outfile = codecs.open(project.conf_file(self.version.slug), encoding='utf-8', mode='a')
        outfile.write("\n")
        conf_py_path = version_utils.get_conf_py_path(self.version)
        remote_version = version_utils.get_vcs_version_slug(self.version)
        github_info = version_utils.get_github_username_repo(self.version)
        bitbucket_info = version_utils.get_bitbucket_username_repo(self.version)
        if github_info[0] is None:
            display_github = False
        else:
            display_github = True
        if bitbucket_info[0] is None:
            display_bitbucket = False
        else:
            display_bitbucket = True

        rtd_ctx = Context({
            'current_version': self.version.slug,
            'project': project,
            'settings': settings,
            'static_path': STATIC_DIR,
            'template_path': TEMPLATE_DIR,
            'conf_py_path': conf_py_path,
            'api_host': getattr(settings, 'SLUMBER_API_HOST', 'https://readthedocs.org'),
            # GitHub
            'github_user': github_info[0],
            'github_repo': github_info[1],
            'github_version':  remote_version,
            'display_github': display_github,
            # BitBucket
            'bitbucket_user': bitbucket_info[0],
            'bitbucket_repo': bitbucket_info[1],
            'bitbucket_version':  remote_version,
            'display_bitbucket': display_bitbucket,
            'commit': self.version.project.vcs_repo(self.version.slug).commit,
        })

        # Avoid hitting database and API if using Docker build environment
        if getattr(settings, 'DONT_HIT_API', False):
            rtd_ctx['versions'] = project.active_versions()
            rtd_ctx['downloads'] = self.version.get_downloads(pretty=True)
        else:
            rtd_ctx['versions'] = project.api_versions()
            rtd_ctx['downloads'] = (apiv2.version(self.version.pk)
                                    .downloads.get()['downloads'])

        rtd_string = template_loader.get_template('doc_builder/conf.py.tmpl').render(rtd_ctx)
        outfile.write(rtd_string)
Exemplo n.º 2
0
def handle_project_import(sender, **kwargs):
    """
    Add post-commit hook on project import.
    """

    project = sender
    request = kwargs.get('request')

    for provider in ['github', 'bitbucket']:
        if provider in project.repo:
            for user in project.users.all():
                tokens = SocialToken.objects.filter(account__user__username=user.username, app__provider=provider)
            for token in tokens:

                if provider == 'github':
                    session = OAuth2Session(
                        client_id=token.app.client_id,
                        token={
                            'access_token': str(token.token),
                            'token_type': 'bearer'
                        }
                    )
                    try:
                        owner, repo = build_utils.get_github_username_repo(version=None, repo_url=project.repo)
                        data = json.dumps({
                            'name': 'readthedocs',
                            'active': True,
                            'config': {'url': 'https://readthedocs.org/github'}
                        })
                        resp = session.post(
                            'https://api.github.com/repos/{owner}/{repo}/hooks'.format(owner=owner, repo=repo),
                            data=data,
                            headers={'content-type': 'application/json'}
                        )
                        log.info("Creating GitHub webhook response code: {code}".format(code=resp.status_code))
                        if resp.status_code == 201:
                            messages.success(request, _('GitHub webhook activated'))
                    except:
                        log.exception('GitHub Hook creation failed', exc_info=True)
                elif provider == 'bitbucket':
                    session = OAuth1Session(
                        token.app.client_id,
                        client_secret=token.app.secret,
                        resource_owner_key=token.token,
                        resource_owner_secret=token.token_secret
                    )
                    try:
                        owner, repo = build_utils.get_bitbucket_username_repo(version=None, repo_url=project.repo)
                        data = {
                            'type': 'Read the Docs',
                        }
                        resp = session.post(
                            'https://api.bitbucket.org/1.0/repositories/{owner}/{repo}/services'.format(owner=owner, repo=repo),
                            data=data,
                        )
                        log.info("Creating BitBucket webhook response code: {code}".format(code=resp.status_code))
                        if resp.status_code == 201:
                            messages.success(request, _('BitBucket webhook activated'))
                    except:
                        log.exception('BitBucket Hook creation failed', exc_info=True)
Exemplo n.º 3
0
    def append_conf(self, **kwargs):
        """Modify the given ``conf.py`` file from a whitelisted user's project.
        """
        project = self.version.project
        #Open file for appending.
        outfile = codecs.open(project.conf_file(self.version.slug),
                              encoding='utf-8', mode='a')
        outfile.write("\n")
        conf_py_path = version_utils.get_conf_py_path(self.version)
        remote_version = version_utils.get_vcs_version_slug(self.version)
        gitlab_info = version_utils.get_gitlab_username_repo(self.version)
        github_info = version_utils.get_github_username_repo(self.version)
        bitbucket_info = version_utils.get_bitbucket_username_repo(self.version)
        if gitlab_info[0] is None:
            display_gitlab = False
        else:
            display_gitlab = True
        if github_info[0] is None:
            display_github = False
        else:
            display_github = True
        if bitbucket_info[0] is None:
            display_bitbucket = False
        else:
            display_bitbucket = True

        rtd_ctx = Context({
            'versions': project.api_versions(),
            'downloads': self.version.get_downloads(pretty=True),
            'current_version': self.version.slug,
            'project': project,
            'settings': settings,
            'static_path': STATIC_DIR,
            'template_path': TEMPLATE_DIR,
            'conf_py_path': conf_py_path,
            'downloads': apiv2.version(self.version.pk).downloads.get()['downloads'],
            'api_host': getattr(settings, 'SLUMBER_API_HOST', 'https://readthedocs.org'),
            # GitLab
            'gitlab_user': gitlab_info[0],
            'gitlab_repo': gitlab_info[1],
            'gitlab_version':  remote_version,
            'display_gitlab': display_gitlab,
            # GitHub
            'github_user': github_info[0],
            'github_repo': github_info[1],
            'github_version':  remote_version,
            'display_github': display_github,
            # BitBucket
            'bitbucket_user': bitbucket_info[0],
            'bitbucket_repo': bitbucket_info[1],
            'bitbucket_version':  remote_version,
            'display_bitbucket': display_bitbucket,
        })
        rtd_string = template_loader.get_template('doc_builder/conf.py.tmpl').render(rtd_ctx)
        outfile.write(rtd_string)
Exemplo n.º 4
0
def handle_project_import(sender, **kwargs):
    """
    Add post-commit hook on project import.
    """

    project = sender
    request = kwargs.get('request')

    for provider in ['github', 'bitbucket']:
        if provider in project.repo:
            session = oauth_utils.get_oauth_session(user=request.user, provider=provider)
            if not session:
                break
            if provider == 'github':
                try:
                    owner, repo = build_utils.get_github_username_repo(version=None, repo_url=project.repo)
                    data = json.dumps({
                        'name': 'readthedocs',
                        'active': True,
                        'config': {'url': 'https://{domain}/github'.format(domain=settings.PRODUCTION_DOMAIN)}
                    })
                    resp = session.post(
                        'https://api.github.com/repos/{owner}/{repo}/hooks'.format(owner=owner, repo=repo),
                        data=data,
                        headers={'content-type': 'application/json'}
                    )
                    log.info("Creating GitHub webhook response code: {code}".format(code=resp.status_code))
                    if resp.status_code == 201:
                        messages.success(request, _('GitHub webhook activated'))
                except:
                    log.exception('GitHub Hook creation failed', exc_info=True)
            elif provider == 'bitbucket':
                try:
                    owner, repo = build_utils.get_bitbucket_username_repo(version=None, repo_url=project.repo)
                    data = {
                        'type': 'POST',
                        'url': 'https://{domain}/bitbucket'.format(domain=settings.PRODUCTION_DOMAIN),
                    }
                    resp = session.post(
                        'https://api.bitbucket.org/1.0/repositories/{owner}/{repo}/services'.format(owner=owner, repo=repo),
                        data=data,
                    )
                    log.info("Creating BitBucket webhook response code: {code}".format(code=resp.status_code))
                    if resp.status_code == 200:
                        messages.success(request, _('BitBucket webhook activated'))
                except:
                    log.exception('BitBucket Hook creation failed', exc_info=True)
Exemplo n.º 5
0
    def append_conf(self, **kwargs):
        """Modify the given ``conf.py`` file from a whitelisted user's project.
        """
        project = self.version.project
        # Open file for appending.
        outfile = codecs.open(project.conf_file(self.version.slug), encoding="utf-8", mode="a")
        outfile.write("\n")
        conf_py_path = version_utils.get_conf_py_path(self.version)
        remote_version = version_utils.get_vcs_version_slug(self.version)
        github_info = version_utils.get_github_username_repo(self.version)
        bitbucket_info = version_utils.get_bitbucket_username_repo(self.version)
        if github_info[0] is None:
            display_github = False
        else:
            display_github = True
        if bitbucket_info[0] is None:
            display_bitbucket = False
        else:
            display_bitbucket = True

        rtd_ctx = Context(
            {
                "versions": project.api_versions(),
                "downloads": self.version.get_downloads(pretty=True),
                "current_version": self.version.slug,
                "project": project,
                "settings": settings,
                "static_path": STATIC_DIR,
                "template_path": TEMPLATE_DIR,
                "conf_py_path": conf_py_path,
                "downloads": apiv2.version(self.version.pk).downloads.get()["downloads"],
                "api_host": getattr(settings, "SLUMBER_API_HOST", "https://readthedocs.org"),
                # GitHub
                "github_user": github_info[0],
                "github_repo": github_info[1],
                "github_version": remote_version,
                "display_github": display_github,
                # BitBucket
                "bitbucket_user": bitbucket_info[0],
                "bitbucket_repo": bitbucket_info[1],
                "bitbucket_version": remote_version,
                "display_bitbucket": display_bitbucket,
                "commit": self.version.project.vcs_repo(self.version.slug).commit,
            }
        )
        rtd_string = template_loader.get_template("doc_builder/conf.py.tmpl").render(rtd_ctx)
        outfile.write(rtd_string)
Exemplo n.º 6
0
    def append_conf(self, **kwargs):
        """Modify the given ``conf.py`` file from a whitelisted user's project.
        """
        project = self.version.project
        # Open file for appending.
        outfile = codecs.open(project.conf_file(self.version.slug),
                              encoding='utf-8', mode='a')
        outfile.write("\n")
        conf_py_path = version_utils.get_conf_py_path(self.version)
        remote_version = version_utils.get_vcs_version_slug(self.version)
        github_info = version_utils.get_github_username_repo(self.version)
        bitbucket_info = version_utils.get_bitbucket_username_repo(self.version)
        if github_info[0] is None:
            display_github = False
        else:
            display_github = True
        if bitbucket_info[0] is None:
            display_bitbucket = False
        else:
            display_bitbucket = True

        rtd_ctx = Context({
            'versions': project.api_versions(),
            'downloads': self.version.get_downloads(pretty=True),
            'current_version': self.version.slug,
            'project': project,
            'settings': settings,
            'static_path': STATIC_DIR,
            'template_path': TEMPLATE_DIR,
            'conf_py_path': conf_py_path,
            'downloads': apiv2.version(self.version.pk).downloads.get()['downloads'],
            'api_host': getattr(settings, 'SLUMBER_API_HOST', 'https://readthedocs.org'),
            # GitHub
            'github_user': github_info[0],
            'github_repo': github_info[1],
            'github_version':  remote_version,
            'display_github': display_github,
            # BitBucket
            'bitbucket_user': bitbucket_info[0],
            'bitbucket_repo': bitbucket_info[1],
            'bitbucket_version':  remote_version,
            'display_bitbucket': display_bitbucket,
            'commit': self.version.project.vcs_repo(self.version.slug).commit,
        })
        rtd_string = template_loader.get_template('doc_builder/conf.py.tmpl').render(rtd_ctx)
        outfile.write(rtd_string)
Exemplo n.º 7
0
    def _whitelisted(self, **kwargs):
        """Modify the given ``conf.py`` file from a whitelisted user's project.
        """
        project = self.version.project
        # Open file for appending.
        outfile = codecs.open(project.conf_file(self.version.slug), encoding="utf-8", mode="a")
        outfile.write("\n")
        conf_py_path = version_utils.get_conf_py_path(self.version)
        remote_version = version_utils.get_vcs_version(self.version)
        github_info = version_utils.get_github_username_repo(self.version)
        bitbucket_info = version_utils.get_bitbucket_username_repo(self.version)
        if github_info[0] is None:
            display_github = False
        else:
            display_github = True
        if bitbucket_info[0] is None:
            display_bitbucket = False
        else:
            display_bitbucket = True

        rtd_ctx = Context(
            {
                "versions": project.api_versions(),
                "downloads": self.version.get_downloads(pretty=True),
                "current_version": self.version.slug,
                "project": project,
                "settings": settings,
                "static_path": STATIC_DIR,
                "template_path": TEMPLATE_DIR,
                "conf_py_path": conf_py_path,
                "downloads": apiv2.version(self.version.pk).downloads.get()["downloads"],
                # GitHub
                "github_user": github_info[0],
                "github_repo": github_info[1],
                "github_version": remote_version,
                "display_github": display_github,
                # BitBucket
                "bitbucket_user": bitbucket_info[0],
                "bitbucket_repo": bitbucket_info[1],
                "bitbucket_version": remote_version,
                "display_bitbucket": display_bitbucket,
            }
        )
        rtd_string = Template(RTD_CONF_ADDITIONS).render(rtd_ctx)
        outfile.write(rtd_string)
Exemplo n.º 8
0
    def _whitelisted(self, **kwargs):
        """Modify the given ``conf.py`` file from a whitelisted user's project.
        """
        project = self.version.project
        #Open file for appending.
        outfile = codecs.open(project.conf_file(self.version.slug),
                              encoding='utf-8', mode='a')
        outfile.write("\n")
        conf_py_path = version_utils.get_conf_py_path(self.version)
        remote_version = version_utils.get_vcs_version(self.version)
        github_info = version_utils.get_github_username_repo(self.version)
        bitbucket_info = version_utils.get_bitbucket_username_repo(self.version)
        if github_info[0] is None:
            display_github = False
        else:
            display_github = True
        if bitbucket_info[0] is None:
            display_bitbucket = False
        else:
            display_bitbucket = True

        rtd_ctx = Context({
            'versions': project.api_versions(),
            'downloads': self.version.get_downloads(pretty=True),
            'current_version': self.version.slug,
            'project': project,
            'settings': settings,
            'static_path': STATIC_DIR,
            'template_path': TEMPLATE_DIR,
            'conf_py_path': conf_py_path,
            'downloads': apiv2.version(self.version.pk).downloads.get()['downloads'],
            # GitHub
            'github_user': github_info[0],
            'github_repo': github_info[1],
            'github_version':  remote_version,
            'display_github': display_github,
            # BitBucket
            'bitbucket_user': bitbucket_info[0],
            'bitbucket_repo': bitbucket_info[1],
            'bitbucket_version':  remote_version,
            'display_bitbucket': display_bitbucket,
        })
        rtd_string = Template(RTD_CONF_ADDITIONS).render(rtd_ctx)
        outfile.write(rtd_string)
Exemplo n.º 9
0
    def append_conf(self, **kwargs):
        """Modify the given ``conf.py`` file from a whitelisted user's project.
        """

        # Pull config data
        try:
            conf_py_path = version_utils.get_conf_py_path(self.version)
        except ProjectImportError:
            self._write_config()
            self.create_index(extension='rst')

        project = self.version.project
        # Open file for appending.
        try:
            outfile = codecs.open(project.conf_file(self.version.slug),
                                  encoding='utf-8',
                                  mode='a')
        except IOError:
            trace = sys.exc_info()[2]
            raise ProjectImportError('Conf file not found'), None, trace
        outfile.write("\n")
        conf_py_path = version_utils.get_conf_py_path(self.version)
        remote_version = version_utils.get_vcs_version_slug(self.version)
        github_info = version_utils.get_github_username_repo(self.version)
        bitbucket_info = version_utils.get_bitbucket_username_repo(
            self.version)
        if github_info[0] is None:
            display_github = False
        else:
            display_github = True
        if bitbucket_info[0] is None:
            display_bitbucket = False
        else:
            display_bitbucket = True

        rtd_ctx = Context({
            'current_version':
            self.version.slug,
            'project':
            project,
            'settings':
            settings,
            'static_path':
            STATIC_DIR,
            'template_path':
            TEMPLATE_DIR,
            'conf_py_path':
            conf_py_path,
            'api_host':
            getattr(settings, 'SLUMBER_API_HOST', 'https://readthedocs.org'),
            # GitHub
            'github_user':
            github_info[0],
            'github_repo':
            github_info[1],
            'github_version':
            remote_version,
            'display_github':
            display_github,
            # BitBucket
            'bitbucket_user':
            bitbucket_info[0],
            'bitbucket_repo':
            bitbucket_info[1],
            'bitbucket_version':
            remote_version,
            'display_bitbucket':
            display_bitbucket,
            'commit':
            self.version.project.vcs_repo(self.version.slug).commit,
        })

        # Avoid hitting database and API if using Docker build environment
        if getattr(settings, 'DONT_HIT_API', False):
            rtd_ctx['versions'] = project.active_versions()
            rtd_ctx['downloads'] = self.version.get_downloads(pretty=True)
        else:
            rtd_ctx['versions'] = project.api_versions()
            rtd_ctx['downloads'] = (apiv2.version(
                self.version.pk).get()['downloads'])

        rtd_string = template_loader.get_template(
            'doc_builder/conf.py.tmpl').render(rtd_ctx)
        outfile.write(rtd_string)
Exemplo n.º 10
0
    def append_conf(self, **kwargs):
        """Modify the given ``conf.py`` file from a whitelisted user's project.
        """

        # Pull config data
        try:
            conf_py_path = self.version.get_conf_py_path()
        except ProjectImportError:
            self._write_config()
            self.create_index(extension='rst')

        project = self.version.project
        # Open file for appending.
        try:
            outfile = codecs.open(project.conf_file(self.version.slug), encoding='utf-8', mode='a')
        except IOError:
            trace = sys.exc_info()[2]
            raise ProjectImportError('Conf file not found'), None, trace
        outfile.write("\n")
        conf_py_path = self.version.get_conf_py_path()
        remote_version = self.version.get_vcs_slug()

        github_user, github_repo = version_utils.get_github_username_repo(url=self.version.project.repo)
        github_version_is_editable = (self.version.type == 'branch')
        display_github = github_user is not None

        bitbucket_user, bitbucket_repo = version_utils.get_bitbucket_username_repo(url=self.version.project.repo)
        bitbucket_version_is_editable = (self.version.type == 'branch')
        display_bitbucket = bitbucket_user is not None

        rtd_ctx = Context({
            'current_version': self.version.verbose_name,
            'project': project,
            'settings': settings,
            'static_path': STATIC_DIR,
            'template_path': TEMPLATE_DIR,
            'conf_py_path': conf_py_path,
            'api_host': getattr(settings, 'SLUMBER_API_HOST', 'https://readthedocs.org'),
            # GitHub
            'github_user': github_user,
            'github_repo': github_repo,
            'github_version': remote_version,
            'github_version_is_editable': github_version_is_editable,
            'display_github': display_github,
            # BitBucket
            'bitbucket_user': bitbucket_user,
            'bitbucket_repo': bitbucket_repo,
            'bitbucket_version': remote_version,
            'bitbucket_version_is_editable': bitbucket_version_is_editable,
            'display_bitbucket': display_bitbucket,
            'commit': self.version.project.vcs_repo(self.version.slug).commit,
        })

        # Avoid hitting database and API if using Docker build environment
        if getattr(settings, 'DONT_HIT_API', False):
            rtd_ctx['versions'] = project.active_versions()
            rtd_ctx['downloads'] = self.version.get_downloads(pretty=True)
        else:
            rtd_ctx['versions'] = project.api_versions()
            rtd_ctx['downloads'] = (apiv2.version(self.version.pk)
                                    .get()['downloads'])

        rtd_string = template_loader.get_template('doc_builder/conf.py.tmpl').render(rtd_ctx)
        outfile.write(rtd_string)
Exemplo n.º 11
0
def handle_project_import(sender, **kwargs):
    """
    Add post-commit hook on project import.
    """

    project = sender
    request = kwargs.get('request')

    for provider in ['github', 'bitbucket']:
        if provider in project.repo:
            session = oauth_utils.get_oauth_session(user=request.user,
                                                    provider=provider)
            if not session:
                break
            if provider == 'github':
                try:
                    owner, repo = build_utils.get_github_username_repo(
                        url=project.repo)
                    data = json.dumps({
                        'name': 'readthedocs',
                        'active': True,
                        'config': {
                            'url':
                            'https://{domain}/github'.format(
                                domain=settings.PRODUCTION_DOMAIN)
                        }
                    })
                    resp = session.post(
                        'https://api.github.com/repos/{owner}/{repo}/hooks'.
                        format(owner=owner, repo=repo),
                        data=data,
                        headers={'content-type': 'application/json'})
                    log.info(
                        "Creating GitHub webhook response code: {code}".format(
                            code=resp.status_code))
                    if resp.status_code == 201:
                        messages.success(request,
                                         _('GitHub webhook activated'))
                except:
                    log.exception('GitHub Hook creation failed', exc_info=True)
            elif provider == 'bitbucket':
                try:
                    owner, repo = build_utils.get_bitbucket_username_repo(
                        url=project.repo)
                    data = {
                        'type':
                        'POST',
                        'url':
                        'https://{domain}/bitbucket'.format(
                            domain=settings.PRODUCTION_DOMAIN),
                    }
                    resp = session.post(
                        'https://api.bitbucket.org/1.0/repositories/{owner}/{repo}/services'
                        .format(owner=owner, repo=repo),
                        data=data,
                    )
                    log.info(
                        "Creating BitBucket webhook response code: {code}".
                        format(code=resp.status_code))
                    if resp.status_code == 200:
                        messages.success(request,
                                         _('BitBucket webhook activated'))
                except:
                    log.exception('BitBucket Hook creation failed',
                                  exc_info=True)
Exemplo n.º 12
0
    def _whitelisted(self, **kwargs):
        """Modify the given ``conf.py`` file from a whitelisted user's project.
        """
        project = self.version.project
        #Open file for appending.
        outfile = codecs.open(project.conf_file(self.version.slug),
                              encoding='utf-8',
                              mode='a')
        outfile.write("\n")
        conf_py_path = version_utils.get_conf_py_path(self.version)
        remote_version = version_utils.get_vcs_version(self.version)
        github_info = version_utils.get_github_username_repo(self.version)
        bitbucket_info = version_utils.get_bitbucket_username_repo(
            self.version)
        if github_info[0] is None:
            display_github = False
        else:
            display_github = True
        if bitbucket_info[0] is None:
            display_bitbucket = False
        else:
            display_bitbucket = True

        rtd_ctx = Context({
            'versions':
            project.api_versions(),
            'downloads':
            self.version.get_downloads(pretty=True),
            'current_version':
            self.version.slug,
            'project':
            project,
            'settings':
            settings,
            'static_path':
            STATIC_DIR,
            'template_path':
            TEMPLATE_DIR,
            'conf_py_path':
            conf_py_path,
            'downloads':
            apiv2.version(self.version.pk).downloads.get()['downloads'],
            # GitHub
            'github_user':
            github_info[0],
            'github_repo':
            github_info[1],
            'github_version':
            remote_version,
            'display_github':
            display_github,
            # BitBucket
            'bitbucket_user':
            bitbucket_info[0],
            'bitbucket_repo':
            bitbucket_info[1],
            'bitbucket_version':
            remote_version,
            'display_bitbucket':
            display_bitbucket,
        })
        rtd_string = Template(RTD_CONF_ADDITIONS).render(rtd_ctx)
        outfile.write(rtd_string)