Example #1
0
def handle_issue_comment(request):
    ghrequest = models.GHRequest(request, request.headers["X-GitHub-Event"])

    if not ghrequest.OK:
        return utils.Response(ghrequest)

    # Get the .pep8speaks.yml config file from the repository
    config = helpers.get_config(ghrequest.repository, ghrequest.base_branch,
                                ghrequest.after_commit_hash)

    splitted_comment = ghrequest.comment.lower().split()

    # If diff is required
    params1 = ["@pep8speaks", "suggest", "diff"]
    condition1 = all(p in splitted_comment for p in params1)
    # If asked to pep8ify
    params2 = ["@pep8speaks", "pep8ify"]
    condition2 = all(p in splitted_comment for p in params2)

    if condition1:
        return _create_diff(ghrequest, config)
    elif condition2:
        return _pep8ify(ghrequest, config)

    return utils.Response(ghrequest)
Example #2
0
def handle_review(request):
    """
    Handle the request when a new review is submitted
    """

    ghrequest = models.GHRequest(request, request.headers["X-GitHub-Event"])

    if not ghrequest.review_body:
        return utils.Response(ghrequest)

    # Get the .pep8speaks.yml config file from the repository
    config = helpers.get_config(ghrequest.repository, ghrequest.base_branch)

    condition1 = ghrequest.action == "submitted"
    # Mainly the summary of the review matters
    ## pep8speaks must be mentioned
    condition2 = "@pep8speaks" in ghrequest.review_body
    ## Check if asked to pep8ify
    condition3 = "pep8ify" in ghrequest.review_body

    ## If pep8ify is not there, all other reviews with body summary
    ## having the mention of pep8speaks, will result in commenting
    ## with autpep8 diff gist.
    conditions_matched = condition1 and condition2 and condition3

    if conditions_matched:
        return _pep8ify(ghrequest, config)
    else:
        conditions_matched = condition1 and condition2
        if conditions_matched:
            return _create_diff(ghrequest, config)
        else:
            return utils.Response(ghrequest)
Example #3
0
def handle_pull_request(request):
    ghrequest = models.GHRequest(request, request.headers["X-GitHub-Event"])

    if not ghrequest.OK:
        return utils.Response(ghrequest)

    # If the PR contains at least one Python file
    pythonic_pr = helpers.check_pythonic_pr(ghrequest.repository,
                                            ghrequest.pr_number)

    if not pythonic_pr:
        return utils.Response(ghrequest)

    helpers.update_users(ghrequest.repository)

    # Get the config from .pep8speaks.yml file of the repository
    config = helpers.get_config(ghrequest.repository, ghrequest.base_branch,
                                ghrequest.after_commit_hash)

    # Personalising the messages obtained from the config file
    # Replace {name} with name of the author
    if "message" in config:
        for act in ['opened', 'updated']:
            # can be either "opened" or "updated"
            for pos in config["message"][act]:
                # can be either "header" or "footer"
                msg = config["message"][act][pos]
                new_msg = msg.replace("{name}", ghrequest.author)
                config["message"][act][pos] = new_msg

    # Updates ghrequest with the results
    # This function runs pycodestyle
    helpers.run_pycodestyle(ghrequest, config)

    # Construct the comment
    header, body, footer, ERROR = helpers.prepare_comment(ghrequest, config)

    # If there is nothing in the comment body, no need to make the comment
    # But in case of PR update, make sure to update the comment with no issues.
    ONLY_UPDATE_COMMENT_BUT_NOT_CREATE = False
    if len(body) == 0:
        return utils.Response(ghrequest)

    # Simply do not comment no-error messages when a PR is opened
    if not ERROR and ghrequest.action == "opened":
        return utils.Response(ghrequest)

    # Concatenate comment parts
    comment = header + body + footer

    # Do not make duplicate comment made on the PR by the bot
    # Check if asked to keep quiet
    if not helpers.comment_permission_check(ghrequest):
        return utils.Response(ghrequest)

    # NOW, Interact with the PR and make/update the comment
    helpers.create_or_update_comment(ghrequest, comment,
                                     ONLY_UPDATE_COMMENT_BUT_NOT_CREATE)

    return utils.Response(ghrequest)