CheckPRTemplate.py 4.9 KB
Newer Older
T
tianshuo78520a 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re
import sys

19
import httpx
T
tianshuo78520a 已提交
20 21 22 23

PR_checkTemplate = ['Paddle']

REPO_TEMPLATE = {
24
    "Paddle": r'''### PR types(.*[^\s].*)### PR changes(.*[^\s].*)### Description(.*[^\s].*)'''
T
tianshuo78520a 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
}


def re_rule(body, CHECK_TEMPLATE):
    PR_RE = re.compile(CHECK_TEMPLATE, re.DOTALL)
    result = PR_RE.search(body)
    return result


def parameter_accuracy(body):
    PR_dic = {}
    PR_types = [
        'New features',
        'Bug fixes',
        'Function optimization',
        'Performance optimization',
        'Breaking changes',
        'Others',
    ]
    PR_changes = ['OPs', 'APIs', 'Docs', 'Others']
    body = re.sub("\r\n", "", body)
    type_end = body.find('### PR changes')
47
    changes_end = body.find('### Description')
T
tianshuo78520a 已提交
48 49 50 51 52 53 54 55 56
    PR_dic['PR types'] = body[len('### PR types') : type_end]
    PR_dic['PR changes'] = body[type_end + 14 : changes_end]
    message = ''
    for key in PR_dic:
        test_list = PR_types if key == 'PR types' else PR_changes
        test_list_lower = [l.lower() for l in test_list]
        value = PR_dic[key].strip().split(',')
        single_mess = ''
        if len(value) == 1 and value[0] == '':
57
            message += f'{key} should be in {test_list}. but now is None.'
T
tianshuo78520a 已提交
58 59 60 61 62 63
        else:
            for i in value:
                i = i.strip().lower()
                if i not in test_list_lower:
                    single_mess += '%s.' % i
            if len(single_mess) != 0:
64
                message += '{} should be in {}. but now is [{}].'.format(
T
tianshuo78520a 已提交
65 66 67 68 69 70 71 72 73 74 75
                    key,
                    test_list,
                    single_mess,
                )
    return message


def checkComments(url):
    headers = {
        'Authorization': 'token ' + GITHUB_API_TOKEN,
    }
76 77 78
    response = httpx.get(
        url, headers=headers, timeout=None, follow_redirects=True
    ).json()
T
tianshuo78520a 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91
    return response


def checkPRTemplate(repo, body, CHECK_TEMPLATE):
    """
    Check if PR's description meet the standard of template
    Args:
        body: PR's Body.
        CHECK_TEMPLATE: check template str.
    Returns:
        res: True or False
    """
    res = False
92
    note = r'<!-- Demo: https://github.com/PaddlePaddle/Paddle/pull/24810 -->\r\n|<!-- One of \[ New features \| Bug fixes \| Function optimization \| Performance optimization \| Breaking changes \| Others \] -->|<!-- One of \[ OPs \| APIs \| Docs \| Others \] -->|<!-- Describe what you’ve done -->'
T
tianshuo78520a 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    if body is None:
        body = ''
    body = re.sub(note, "", body)
    result = re_rule(body, CHECK_TEMPLATE)
    message = ''
    if len(CHECK_TEMPLATE) == 0 and len(body) == 0:
        res = False
    elif result is not None:
        message = parameter_accuracy(body)
        res = True if message == '' else False
    elif result is None:
        res = False
        message = parameter_accuracy(body)
    return res, message


def pull_request_event_template(event, repo, *args, **kwargs):
    pr_effect_repos = PR_checkTemplate
    pr_num = event['number']
    url = event["comments_url"]
    BODY = event["body"]
    sha = event["head"]["sha"]
    title = event["title"]
    pr_user = event["user"]["login"]
117
    print(f'receive data : pr_num: {pr_num}, title: {title}, user: {pr_user}')
T
tianshuo78520a 已提交
118 119 120 121 122 123 124
    if repo in pr_effect_repos:
        CHECK_TEMPLATE = REPO_TEMPLATE[repo]
        global check_pr_template
        global check_pr_template_message
        check_pr_template, check_pr_template_message = checkPRTemplate(
            repo, BODY, CHECK_TEMPLATE
        )
125
        print(f"check_pr_template: {check_pr_template} pr: {pr_num}")
T
tianshuo78520a 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        if check_pr_template is False:
            print("ERROR MESSAGE:", check_pr_template_message)
            sys.exit(7)
        else:
            sys.exit(0)


def get_a_pull(pull_id):
    url = "https://api.github.com/repos/PaddlePaddle/Paddle/pulls/ " + str(
        pull_id
    )

    payload = {}
    headers = {
        'Authorization': 'token ' + GITHUB_API_TOKEN,
        'Accept': 'application/vnd.github+json',
    }
143 144 145
    response = httpx.request(
        "GET", url, headers=headers, data=payload, follow_redirects=True
    )
T
tianshuo78520a 已提交
146 147 148 149 150 151 152 153 154 155 156 157
    return response.json()


def main(org, repo, pull_id):
    pull_info = get_a_pull(pull_id)
    pull_request_event_template(pull_info, repo)


if __name__ == "__main__":
    AGILE_PULL_ID = os.getenv("AGILE_PULL_ID")
    GITHUB_API_TOKEN = os.getenv("GITHUB_API_TOKEN")
    main("PaddlePaddle", "Paddle", AGILE_PULL_ID)