Repository navigation
Expand file tree
/
Copy pathchecker.py
More file actions
252 lines (189 loc) ยท 9.23 KB
/
Copy pathchecker.py
File metadata and controls
252 lines (189 loc) ยท 9.23 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import json
import re
import os
from google.cloud import language_v1
from collections import OrderedDict
DEFAULT_SUBJECT_LIMIT = 50
DEFAULT_BODY_LIMIT = 72
secret_file = "./secrets.json"
with open(secret_file) as f:
secrets = json.loads(f.read())
def get_secret(setting):
""" ๋น๋ฐ ๋ณ์๋ฅผ ๊ฐ์ ธ์ค๊ฑฐ๋ ๋ช
์์ ์์ธ๋ฅผ ๋ฐํ
"""
try:
return secrets[setting]
except KeyError:
error_msg = "Set the {} environment variable".format(setting)
raise print(error_msg)
credential_path = get_secret("GCP_SECRET_PATH")
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path
def check(message):
""" Check if a commit message is good or bad based on commit message convention
- check_type_is_specified()
commit message subject ์ commit type ์ ํฌํจ ์ฌ๋ถ ๋ฐ ์ดํ ๋ฌธ์ฅ์ด ๋ช
๋ นํ ํ๋จ์ ์ํด commit type ์ ์ ๊ฑฐํ
commit message ๋ฅผ ๋ฆฌํด ๋ฐ์.
- check_type_in_bracket()
commit message subject ์ bracket ์ ์ด๋ type ํฌํจ ์ฌ๋ถ ํ์ธ ex) [DevTools], ์ดํ ๋ช
๋ นํ ๋ฌธ์ฅ์ธ ์ง ํ๋จ์ ์ํด
bracket ๋ถ๋ถ์ ์ ์ธํ commit message ๋ฅผ ๋ฆฌํด ๋ฐ๋๋ค.
:param message: A commit message
:return:
"""
res = OrderedDict()
# subject_type_is_specified & subject_type_in_bracket ์ ๋ณด dict ์ ๋ฃ๊ธฐ ์ํจ
res["subject_type_is_specified"] = False
res["subject_type_in_bracket"] = False
# Git ์์ ์๋ ์์ฑ๋ commit, ์ซ์๋ง ์๋ case, ๋น commit message, commit message ๊ธ์ ์ 1๊ฐ ์ดํ case -> ์ฐ๋ ๊ธฐ data ๋ถ๋ฅ
if auto_commit_judge(message) or trash_commit_judge(message):
res["invalid_commit_message"] = 1
return # ์ด๋ฐ msg ๋ ๋ถ๋ฅ ์์
์ด ํ์
res["subject_is_separated_from_body"] = check_subject_is_separated_from_body(message)
res["subject_is_not_too_long"] = check_subject_is_not_too_long(message, DEFAULT_SUBJECT_LIMIT)
res["subject_does_not_end_with_punctuation"] = check_subject_does_not_end_with_punctuation(message)
# commit message subject ์ commit type ์ ํฌํจ ์ฌ๋ถ ํ์ธ
if check_type_is_specified(message):
res["subject_type_is_specified"] = True
parsed_message = check_type_is_specified(message)
# commit message subject ์ bracket ์ ์ด๋ type ํฌํจ ์ฌ๋ถ ํ์ธ ex) [DevTools]
elif check_type_in_bracket(message):
res["subject_type_in_bracket"] = True
parsed_message = check_type_in_bracket(message)
else: # commit message ๊ฐ ๋ค์ case ์ ํฌํจ๋์ง ์๋ ๊ฒฝ์ฐ (1. commit type, 2. Type in Bracket)
parsed_message = message
res["subject_uses_imperative"] = check_subject_uses_imperative(parsed_message) # commit message ๊ฐ ๋ช
๋ น๋ฌธ์ธ ์ง ํ์ธ
res["check_subject_is_capitalized"] = check_subject_is_capitalized(parsed_message)
res["body_lines_are_not_too_long"] = check_body_lines_are_not_too_long(message, DEFAULT_BODY_LIMIT)
return res
def trash_commit_judge(message):
if message.isdigit():
return True
elif message is None:
return True
elif len(message) <= 1:
return True
return False
def auto_commit_judge(message):
if message.find('Merge pull request #') != -1:
return True
elif message.find("Merge branch '") != -1:
return True
return False
def check_subject_is_separated_from_body(message):
""" Check if subject is separated from body
:param message: A commit message
:return check_result: subject ์ body ๊ฐ ๋ถ๋ฆฌ๋์ด ์์ผ๋ฉด True or not True (False)
"""
lines = message.splitlines()
if len(lines) > 1:
# The second line should be empty
check_result = not lines[1]
else:
# If there is just one line then this rule doesn't apply
check_result = True
return check_result
def check_subject_is_not_too_long(message, subject_limit):
""" Check if subject is too long
:param message: A commit message
:param subject_limit: Limited number of characters for subject
:return check_result: 50์๋ฅผ ๋์ง ์์ผ๋ฉด True or not Ture (False)
"""
lines = message.splitlines()
check_result = len(lines[0]) <= subject_limit
return check_result
def check_body_lines_are_not_too_long(message, body_limit):
""" Check if body is too long
:param message: A commit message
:param body_limit: Limited number of characters for body
:return check_result: 72์๋ฅผ ๋์ง ์์ผ๋ฉด True or not Ture (False)
"""
lines = message.splitlines()
check_result = True
for line in lines:
if len(line) > body_limit:
check_result = False
break
return check_result
def check_subject_does_not_end_with_punctuation(message):
""" Check if subject ends with period
:param message: A commit message
:return check_result: subject ๋ง์ง๋ง ๋ฌธ์๊ฐ punctuation ์ด ์๋ ๊ฒฝ์ฐ True / ๋ง๋ค๋ฉด False
"""
punctuation_pattern = re.compile(r'[!.?](?:\s+)?$(?<=)')
lines = message.splitlines()
check_result = not punctuation_pattern.match(lines[0][-1])
return check_result
def check_type_is_specified(message: str):
""" Determine if the commit message contains the specified commit type
:param message: A commit message
:return: ํน์ commit type ์ด ์๋ค๋ฉด ํด๋น type ์ด ์ ๊ฑฐ๋ string
"""
sub_type_msg = None
commit_type_list = ["feat:", "fix:", "design:", "build:", "chore:", "ci:", "docs:", "style:", "refactor:", "test:",
"rename:", "remove:", "perf"]
type_regex = r'[A-Za-z]*(\(.*\)|\s-\s.*)?:'
type_pattern = re.compile(type_regex) # solution:, feat(test.py): etc ์ ๊ท์
res = type_pattern.match(message)
if res is not None and res.start() == 0: # commit type ์ ๊ท์ ํํ์ด ์กด์ฌ + ์์น๊ฐ ๋งจ ์
scope_regex = r'\(.*\)|\s-\s.*' # feat(test.py): ์์ (test.py) ๋ฅผ ์ ๊ฑฐ
commit_message = re.sub(scope_regex, '', res.string)
commit_message = commit_message.split()
# ํน์ type ์ด ์๋ ๊ฒฝ์ฐ False ๋ฅผ ๋ฆฌํด
# -> ์ฒ๋ฆฌ ์์ ) solution: ๋ฑ ํ์ฌ, ์๊ธฐ๊ฐ ๊ตฌ๋ณ ๊ฐ๋ฅ type ์ ์ธ ๊ฒฝ์ฐ๋ ๋ฐ๋ก ์๊ฐํด๋์(์ด ๊ฒฝ์ฐ ๊ถ์ฅ์ ํด์ฃผ๋ ๋ฐฉํฅ)
# ex) ์ด๋ฐ type ์ ์ด๋ค๊ฐ์??
sub_type_msg = re.sub(type_regex, '', message).strip() if commit_message[0] in commit_type_list else None
return sub_type_msg
def check_type_in_bracket(message: str):
""" Determine if the type enclosed in the bracket exists in front of the commit message
ex)
[DevTools] Fix regex for formateWithStyles function
[DevTools][Bug] Fix regex for documents
:param message: A commit message
:return sub_bracket_msg: bracket ์ผ๋ก wrapped type ์ด ์๋ค๋ฉด ์ด๋ฅผ ์ ๊ฑฐํ commit message (string)
"""
bracket_regex = r'(\[[A-Za-z]*\])+'
pattern = re.compile(bracket_regex)
res = pattern.match(message)
sub_bracket_msg = re.sub(bracket_regex, '', message).strip() if res else None
return sub_bracket_msg
def check_subject_uses_imperative(message: str):
""" Determine if a commit message's mood is imperative
Follow the following rules
- message ๋ฅผ ๋ชจ๋ ์๋ฌธ์๋ก ๋ณ๊ฒฝ
- ์ ํ๋ ํฅ์์ ์ํด you + message ๋ก ๊ตฌ๋ฌธ ๋ถ์์ ์งํ
- ์ฒซ ๋ฒ์งธ ํ ํฐ(word) ๊ฐ ๋ถ์ฌ์ผ ๊ฒฝ์ฐ ๋ ๋ฒ์งธ ํ ํฐ(word) ๋ฅผ token ์ผ๋ก ์ง์
- "fix: correctly fix ~~" ์ ๊ฐ์ commit message ๋ฅผ ๊ณ ๋ ค
- token ์ด ๋์ฌ์ธ ๊ฒฝ์ฐ
- 3์ธ์นญ ๋์ฌ: runs, presents, fixes etc
- ๊ณผ๊ฑฐํ ๋์ฌ: fixed, updated etc
2๊ฐ์ง case ๋ฅผ ๊ณ ๋ ค
:param message: A commit message
:return:
"""
client = language_v1.LanguageServiceClient()
type_ = language_v1.Document.Type.PLAIN_TEXT
language = "en"
# 2์ธ์นญ ์ฃผ์ด you ๋ก ์ ํ๋ ์ํฅ (๊ทธ๊ฒ๋ ์์ฒญ!)
document = {"content": "you " + message.lower(), "type_": type_, "language": language}
encoding_type = language_v1.EncodingType.UTF8
response = client.analyze_syntax(request={'document': document, 'encoding_type': encoding_type})
token = response.tokens[1] # index 0 ์ you
# ์ฒซ ๋ฒ์งธ token ์ด ๋ถ์ฌ์ธ ๊ฒฝ์ฐ๋ฅผ ๊ณ ๋ ค ex) fix: correctly fix ~~
token = response.tokens[2] if language_v1.PartOfSpeech.Tag(token.part_of_speech.tag).name == "ADV" else token
part_of_speech = token.part_of_speech
if language_v1.PartOfSpeech.Tag(part_of_speech.tag).name != "VERB":
return False, "NO"
else:
if language_v1.PartOfSpeech.Tense(part_of_speech.tense).name == "PAST":
return False, "PAST"
if language_v1.PartOfSpeech.Person(part_of_speech.person).name == "THIRD":
return False, "THIRD"
return True, "IMPERATIVE"
def check_subject_is_capitalized(message: str):
""" Check if commit message subject is capitalized
:param message: A commit message
:return check_result: subject is capitalized True / or not True (False)
"""
lines = message.splitlines()
# Check if first character is in upper case
check_result = lines[0][0].isupper()
return check_result