示例#1
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)
示例#2
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 = {
            "action": request.json["action"],
            "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"],
        }

        # If the PR contains at least one Python file
        pythonic_pr = helpers.check_pythonic_pr(data)

        if pythonic_pr:
            # 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 ['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}", 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 and data["action"] == "opened":
                PERMITTED_TO_COMMENT = False
            # Simply do not comment no-error messages when a PR is opened
            if not ERROR and data["action"] == "opened":
                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')