-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathCheckPRTemplate.py
153 lines (132 loc) · 4.84 KB
/
CheckPRTemplate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# 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
import requests
PR_checkTemplate = ['Paddle']
REPO_TEMPLATE = {
"Paddle": r'''### PR types(.*[^\s].*)### PR changes(.*[^\s].*)### Describe(.*[^\s].*)'''
}
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')
changes_end = body.find('### Describe')
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] == '':
message += '%s should be in %s. but now is None.' % (key, test_list)
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:
message += '%s should be in %s. but now is [%s].' % (
key,
test_list,
single_mess,
)
return message
def checkComments(url):
headers = {
'Authorization': 'token ' + GITHUB_API_TOKEN,
}
response = requests.get(url, headers=headers).json()
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
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 this PR does -->'
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"]
print(f'接收数据:pr_num: {pr_num}, title: {title}, user: {pr_user}')
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
)
print("check_pr_template: %s pr: %s" % (check_pr_template, pr_num))
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',
}
response = requests.request("GET", url, headers=headers, data=payload)
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)