Esempio n. 1
0
def handle_review(request):
    # Handle the request when a new review is submitted

    data = dict()
    data["after_commit_hash"] = request.json["pull_request"]["head"]["sha"],
    data["author"] = request.json["pull_request"]["user"]["login"]
    data["reviewer"] = request.json["review"]["user"]["login"]
    data["repository"] = request.json["repository"]["full_name"]
    data["diff_url"] = request.json["pull_request"]["diff_url"]
    data["sha"] = request.json["pull_request"]["head"]["sha"]
    data["review_url"] = request.json["review"]["html_url"]
    data["pr_number"] = request.json["pull_request"]["number"]

    # Get the .pep8speaks.yml config file from the repository
    config = helpers.get_config(data)

    condition1 = request.json["action"] == "submitted"
    # Mainly the summary of the review matters
    ## pep8speaks must be mentioned
    condition2 = "@pep8speaks" in request.json["review"]["body"]
    ## Check if asked to pep8ify
    condition3 = "pep8ify" in request.json["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(request, data, config)
    else:
        conditions_matched = condition1 and condition2
        if conditions_matched:
            return _create_diff(request, data, config)
Esempio n. 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)
Esempio n. 3
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)
Esempio n. 4
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)
Esempio n. 5
0
def handle_issue_comment(request):
    # A variable which is set to False whenever a criteria is not met
    # Ultimately if this is True, only then the comment is made
    PERMITTED_TO_COMMENT = True
    # This dictionary is used and updated after making API calls
    data = {}

    if request.json["action"] in ["created", "edited"]:
        if "pull_request" in request.json["issue"]:
            auth = (os.environ["BOT_USERNAME"], os.environ["BOT_PASSWORD"])
            pull_request = requests.get(
                request.json["issue"]["pull_request"]["url"],
                auth=auth).json()
            data["pull_request"] = pull_request
            data["after_commit_hash"] = pull_request["head"]["sha"],
            data["author"] = pull_request["user"]["login"]
            data["reviewer"] = request.json["comment"]["user"]["login"]
            data["repository"] = request.json["repository"]["full_name"]
            data["diff_url"] = pull_request["diff_url"]
            data["sha"] = pull_request["head"]["sha"]
            data["review_url"] = request.json["comment"]["html_url"]
            data["pr_number"] = pull_request["number"]
            data["comment"] = request.json["comment"]["body"]
            data["base_branch"] = request.json["repository"]["default_branch"]

            # Get the .pep8speaks.yml config file from the repository
            config = helpers.get_config(data)

            splitted_comment = data["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(request, data, config)
            elif condition2:
                return _pep8ify(request, data, config)
            else:
                js = json.dumps(data)
                return Response(js, status=200, mimetype='application/json')
        else:
            js = json.dumps(data)
            return Response(js, status=200, mimetype='application/json')
    elif request.json["action"] == "deleted":
        js = json.dumps(data)
        return Response(js, status=200, mimetype='application/json')
Esempio n. 6
0
def handle_pull_request(request):

    # A variable which is set to False whenever a criteria is not met
    # Ultimately if this is True, only then the comment is made
    PERMITTED_TO_COMMENT = True
    # This dictionary is used and updated after making API calls
    data = {}

    if request.json["action"] in ["synchronize", "opened", "reopened"]:
        data = {
            "after_commit_hash": request.json["pull_request"]["head"]["sha"],
            "repository": request.json["repository"]["full_name"],
            "author": request.json["pull_request"]["user"]["login"],
            "diff_url": request.json["pull_request"]["diff_url"],
            # Dictionary with filename matched with list of results
            "results": {},

            # Dictionary with filename matched with list of results caused by
            # pycodestyle arguments
            "extra_results": {},
            "pr_number": request.json["number"],
        }

        # Update users of the integration
        helpers.update_users(data["repository"])

        # Get the config from .pep8speaks.yml file of the repository
        config = helpers.get_config(data)

        # Personalising the messages obtained from the config file
        # Replace {name} with name of the author
        if "message" in config:
            for act in config["message"]:
                # 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}", data["author"])
                    config["message"][act][pos] = new_msg

        # Updates data dictionary with the results
        # This function runs the pep8 checker
        helpers.run_pycodestyle(data, config)

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

        # If there is nothing in the comment body, no need to make the comment
        if len(body) == 0:
            PERMITTED_TO_COMMENT = False
        if config[
                "no_blank_comment"]:  # If asked not to comment no-error messages
            if not ERROR:  # If there is no error in the PR
                PERMITTED_TO_COMMENT = False

        # 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(data, comment):
            PERMITTED_TO_COMMENT = False

        # Do not run on PR's created by pep8speaks which use autopep8
        # Too much noisy
        if data["author"] == "pep8speaks":
            PERMITTED_TO_COMMENT = False

        # Make the comment
        if PERMITTED_TO_COMMENT:
            helpers.create_or_update_comment(data, comment)
    js = json.dumps(data)
    return Response(js, status=200, mimetype='application/json')