Exemple #1
0
    def save(self, *args, **kwargs):
        """names are enforced as all lowercase to avoid duplication, along
           with removing all special characters except for dashes.
        """
        from askci.apps.main.utils import lowercase_cleaned_name

        if self.pk is None:
            self.tag = lowercase_cleaned_name(self.tag)
        return super(Article, self).save(*args, **kwargs)
Exemple #2
0
def create_webhooks_issues(text, message, provider):
    """given a validated webhook, take some text (topics) and parse for
       unique terms or concepts. If found, and the article exists, open
       a corresponding issue

       Parameters
       ==========
       text: should be a raw string of text to parse for topics. Illegal
               characters (other than -) will be removed, and lowercased
       message: a message to post in the issue. Should be prepared with a url
                from the provider
       provider: the provider that sent the webhook, will be added to the title
    """
    from askci.apps.main.github import open_issue

    # Words to always skip
    skip = get_stopwords()

    # Remove  html tags
    cleaned = [re.sub("<.*?>", " ", x) for x in text.split(" ")]

    # Parse text and look for terms
    cleaned = [
        lowercase_cleaned_name(x.strip()) for x in cleaned if lowercase_cleaned_name(x)
    ]

    cleaned = set([x for x in cleaned if x not in skip])
    print(cleaned)

    for term in cleaned:
        try:
            article = Article.objects.get(name=term)
            if article.webhook_issues:
                print("opening issue for %s" % article)
                title = "Updated content from %s for term %s" % (provider, article.name)
                res = open_issue(article.owner, article, title, summary=message)
                print("Response for issue %s" % res)
        except Article.DoesNotExist:
            pass
Exemple #3
0
def new_article(request):
    """create a new article, only if the user has the appropriate GitHub 
       credentials. This means forking the template repository,
       renaming it to be about the term, and then editing it to have an
       updated name. In the future we will add topics (tags) here as well.
    """
    if not has_articles_opening(request):
        return redirect("index")

    if request.method == "POST":
        namespace = request.POST.get("namespace")
        summary = request.POST.get("summary")
        term = lowercase_cleaned_name(request.POST.get("term"))
        repository = "askci-term-%s" % term
        repository = os.path.join(namespace, repository)

        # We only have one template repo to start (used as fork)
        template = TemplateRepository.objects.last()

        # Generate the template repository
        repo = copy_repository_template(
            user=request.user,
            template=template.repo,
            repository=repository,
            description=summary,
        )

        # The article repo was created!
        if repo:

            # Generate the webhooks (push/deployment and pull request)
            secret = str(uuid.uuid4())

            webhook = create_webhook(request.user, repo, secret)
            webhook_pr = create_webhook(request.user,
                                        repo,
                                        secret,
                                        events=["pull_request"])

            # repository event triggered when renamed, etc.
            webhook_repo = create_webhook(request.user,
                                          repo,
                                          secret,
                                          events=["repository"])

            # Save both to webhooks json object
            webhooks = {
                "push-deploy": webhook,
                "pull_request": webhook_pr,
                "repository": webhook_repo,
            }

            if "errors" in webhook:
                messages.info(request, "Errors: %s" % webhook["errors"])
                return redirect("index")

            # namespace defaults to library
            article = Article.objects.create(
                name=term,
                template=template,
                owner=request.user,
                secret=secret,
                webhook=webhooks,
                repo=repo,
                summary=summary,
            )

            add_article_tags(article)

            # Run the first task
            res = django_rq.enqueue(update_article, article_uuid=article.uuid)

            messages.info(
                request,
                "%s has been created! Refresh the page for updated content." %
                term,
            )
            return redirect(reverse("article_details", args=[article.name]))

        # if we get here, there was an error
        messages.warning(
            request,
            "There was an error creating %s. " % repository,
            "Make sure that the template %s organization " % template.repo,
            "is authenticated with the application here.",
        )

    # username/orgs that the user has admin for (to create webhook)
    namespaces = get_admin_namespaces(request.user)

    # In the future the user might select from one or more templates
    context = {
        "templates": TemplateRepository.objects.all(),
        "namespaces": namespaces
    }
    return render(request, "articles/new_article.html", context)