1
- import json
2
1
import os
3
2
import re
4
3
import time
5
4
6
5
import requests
7
6
7
+ from biz .utils .i18n import get_translator
8
8
from biz .utils .log import logger
9
9
10
+ _ = get_translator ()
11
+
10
12
# 从环境变量中获取支持的文件扩展名
11
13
SUPPORTED_EXTENSIONS = os .getenv ('SUPPORTED_EXTENSIONS' , '.java,.py,.php' ).split (',' )
12
14
@@ -21,9 +23,9 @@ def filter_changes(changes: list):
21
23
for change in changes :
22
24
# 优先检查status字段是否为"removed"
23
25
if change .get ('status' ) == 'removed' :
24
- logger .info (f "Detected file deletion via status field: { change .get ('new_path' )} " )
26
+ logger .info (_ ( "Detected file deletion via status field: {}" ). format ( change .get ('new_path' )) )
25
27
continue
26
-
28
+
27
29
# 如果没有status字段或status不为"removed",继续检查diff模式
28
30
diff = change .get ('diff' , '' )
29
31
if diff :
@@ -34,12 +36,12 @@ def filter_changes(changes: list):
34
36
if all (line .startswith ('-' ) or not line for line in diff_lines ):
35
37
logger .info (f"Detected file deletion via diff pattern: { change .get ('new_path' )} " )
36
38
continue
37
-
39
+
38
40
not_deleted_changes .append (change )
39
-
40
- logger .info (f "SUPPORTED_EXTENSIONS: { SUPPORTED_EXTENSIONS } " )
41
- logger .info (f "After filtering deleted files: { not_deleted_changes } " )
42
-
41
+
42
+ logger .info (_ ( "SUPPORTED_EXTENSIONS: {}" ). format ( SUPPORTED_EXTENSIONS ) )
43
+ logger .info (_ ( "After filtering deleted files: {}" ). format ( not_deleted_changes ) )
44
+
43
45
# 过滤 `new_path` 以支持的扩展名结尾的元素, 仅保留diff和new_path字段
44
46
filtered_changes = [
45
47
{
@@ -49,7 +51,7 @@ def filter_changes(changes: list):
49
51
for item in not_deleted_changes
50
52
if any (item .get ('new_path' , '' ).endswith (ext ) for ext in SUPPORTED_EXTENSIONS )
51
53
]
52
- logger .info (f "After filtering by extension: { filtered_changes } " )
54
+ logger .info (_ ( "After filtering by extension: {}" ). format ( filtered_changes ) )
53
55
return filtered_changes
54
56
55
57
@@ -78,7 +80,8 @@ def parse_pull_request_event(self):
78
80
def get_pull_request_changes (self ) -> list :
79
81
# 检查是否为 Pull Request Hook 事件
80
82
if self .event_type != 'pull_request' :
81
- logger .warn (f"Invalid event type: { self .event_type } . Only 'pull_request' event is supported now." )
83
+ logger .warn (
84
+ _ ("Invalid event type: {}. Only 'pull_request' event is supported now." ).format (self .event_type ))
82
85
return []
83
86
84
87
# GitHub pull request changes API可能存在延迟,多次尝试
@@ -93,7 +96,9 @@ def get_pull_request_changes(self) -> list:
93
96
}
94
97
response = requests .get (url , headers = headers )
95
98
logger .debug (
96
- f"Get changes response from GitHub (attempt { attempt + 1 } ): { response .status_code } , { response .text } , URL: { url } " )
99
+ _ ("Get changes response from GitHub (attempt {attempt}): {response_status_code}, {response_text}, URL: {url}" ).format (
100
+ attempt = {attempt + 1 }, response_status_code = response .status_code , response_text = response .text ,
101
+ url = url ))
97
102
98
103
# 检查请求是否成功
99
104
if response .status_code == 200 :
@@ -111,13 +116,14 @@ def get_pull_request_changes(self) -> list:
111
116
return changes
112
117
else :
113
118
logger .info (
114
- f"Changes is empty, retrying in { retry_delay } seconds... (attempt { attempt + 1 } /{ max_retries } ), URL: { url } " )
119
+ _ ("Changes is empty, retrying in {retry_delay} seconds... (attempt {attempt}/{max_retries}), URL: {url}" ).format (retry_delay = retry_delay ,
120
+ attempt = {attempt + 1 }, max_retries = max_retries , url = url ))
115
121
time .sleep (retry_delay )
116
122
else :
117
- logger .warn (f "Failed to get changes from GitHub (URL: { url } ): { response .status_code } , { response .text } " )
123
+ logger .warn (_ ( "Failed to get changes from GitHub (URL: {url}): {response.status_code}, {response.text}" ). format ( url , response . status_code , response . text ) )
118
124
return []
119
125
120
- logger .warning (f "Max retries ({ max_retries } ) reached. Changes is still empty." )
126
+ logger .warning (_ ( "Max retries ({}) reached. Changes is still empty." ). format ( max_retries ) )
121
127
return [] # 达到最大重试次数后返回空列表
122
128
123
129
def get_pull_request_commits (self ) -> list :
@@ -132,8 +138,8 @@ def get_pull_request_commits(self) -> list:
132
138
'Accept' : 'application/vnd.github.v3+json'
133
139
}
134
140
response = requests .get (url , headers = headers )
135
- logger .debug (f "Get commits response from GitHub: { response .status_code } , { response .text } " )
136
-
141
+ logger .debug (_ ( "Get commits response from GitHub: {}, {}" ). format ( response .status_code , response .text ) )
142
+
137
143
# 检查请求是否成功
138
144
if response .status_code == 200 :
139
145
# 将GitHub的commits转换为GitLab格式的commits
@@ -152,7 +158,7 @@ def get_pull_request_commits(self) -> list:
152
158
gitlab_format_commits .append (gitlab_commit )
153
159
return gitlab_format_commits
154
160
else :
155
- logger .warn (f "Failed to get commits: { response .status_code } , { response .text } " )
161
+ logger .warn (_ ( "Failed to get commits: {}, {}" ). format ( response .status_code , response .text ) )
156
162
return []
157
163
158
164
def add_pull_request_notes (self , review_result ):
@@ -165,11 +171,11 @@ def add_pull_request_notes(self, review_result):
165
171
'body' : review_result
166
172
}
167
173
response = requests .post (url , headers = headers , json = data )
168
- logger .debug (f "Add comment to GitHub PR { url } : { response .status_code } , { response .text } " )
174
+ logger .debug (_ ( "Add comment to GitHub PR {url}: {response_status_code}, {response_text}" ). format ( url = url , response_status_code = response .status_code , response_text = response .text ) )
169
175
if response .status_code == 201 :
170
- logger .info ("Comment successfully added to pull request." )
176
+ logger .info (_ ( "Comment successfully added to pull request." ) )
171
177
else :
172
- logger .error (f "Failed to add comment: { response .status_code } " )
178
+ logger .error (_ ( "Failed to add comment: {}" ). format ( response .status_code ) )
173
179
logger .error (response .text )
174
180
175
181
@@ -198,7 +204,7 @@ def parse_push_event(self):
198
204
def get_push_commits (self ) -> list :
199
205
# 检查是否为 Push 事件
200
206
if self .event_type != 'push' :
201
- logger .warn (f "Invalid event type: { self . event_type } . Only 'push' event is supported now." )
207
+ logger .warn (_ ( "Invalid event type: {}. Only 'push' event is supported now." ). format ( self . event_type ) )
202
208
return []
203
209
204
210
# 提取提交信息
@@ -212,19 +218,19 @@ def get_push_commits(self) -> list:
212
218
}
213
219
commit_details .append (commit_info )
214
220
215
- logger .info (f "Collected { len ( commit_details ) } commits from push event." )
221
+ logger .info (_ ( "Collected {} commits from push event." ). format ( len ( commit_details )) )
216
222
return commit_details
217
223
218
224
def add_push_notes (self , message : str ):
219
225
# 添加评论到 GitHub Push 请求的提交中(此处假设是在最后一次提交上添加注释)
220
226
if not self .commit_list :
221
- logger .warn ("No commits found to add notes to." )
227
+ logger .warn (_ ( "No commits found to add notes to." ) )
222
228
return
223
229
224
230
# 获取最后一个提交的ID
225
231
last_commit_id = self .commit_list [- 1 ].get ('id' )
226
232
if not last_commit_id :
227
- logger .error ("Last commit ID not found." )
233
+ logger .error (_ ( "Last commit ID not found." ) )
228
234
return
229
235
230
236
url = f"https://api.github.com/repos/{ self .repo_full_name } /commits/{ last_commit_id } /comments"
@@ -236,11 +242,11 @@ def add_push_notes(self, message: str):
236
242
'body' : message
237
243
}
238
244
response = requests .post (url , headers = headers , json = data )
239
- logger .debug (f "Add comment to commit { last_commit_id } : { response .status_code } , { response .text } " )
245
+ logger .debug (_ ( "Add comment to commit {last_commit_id}: {response_status_code}, {response_text}" ). format ( last_commit_id , response .status_code , response .text ) )
240
246
if response .status_code == 201 :
241
- logger .info ("Comment successfully added to push commit." )
247
+ logger .info (_ ( "Comment successfully added to push commit." ) )
242
248
else :
243
- logger .error (f "Failed to add comment: { response .status_code } " )
249
+ logger .error (_ ( "Failed to add comment: {}" ). format ( response .status_code ) )
244
250
logger .error (response .text )
245
251
246
252
def __repository_commits (self , sha : str = "" , per_page : int = 100 , page : int = 1 ):
@@ -252,13 +258,14 @@ def __repository_commits(self, sha: str = "", per_page: int = 100, page: int = 1
252
258
}
253
259
response = requests .get (url , headers = headers )
254
260
logger .debug (
255
- f"Get commits response from GitHub for repository_commits: { response .status_code } , { response .text } , URL: { url } " )
261
+ _ ("Get commits response from GitHub for repository_commits: {response_status_code}, {response_text}, URL: {url}" ).format (
262
+ response_status_code = response .status_code , response_text = response .text , url = url ))
256
263
257
264
if response .status_code == 200 :
258
265
return response .json ()
259
266
else :
260
267
logger .warn (
261
- f "Failed to get commits for sha { sha } : { response .status_code } , { response .text } " )
268
+ _ ( "Failed to get commits for sha {sha}: {response_status_code}, {response_text}" ). format ( sha = sha , response_status_code = response .status_code , response_text = response .text ) )
262
269
return []
263
270
264
271
def get_parent_commit_id (self , commit_id : str ) -> str :
@@ -269,7 +276,8 @@ def get_parent_commit_id(self, commit_id: str) -> str:
269
276
}
270
277
response = requests .get (url , headers = headers )
271
278
logger .debug (
272
- f"Get commit response from GitHub: { response .status_code } , { response .text } , URL: { url } " )
279
+ _ ("Get commit response from GitHub: {response_status_code}, {response_text}, URL: {url}" ).format (
280
+ response_status_code = response .status_code , response_text = response .text , url = url ))
273
281
274
282
if response .status_code == 200 and response .json ().get ('parents' ):
275
283
return response .json ().get ('parents' )[0 ].get ('sha' , '' )
@@ -284,7 +292,8 @@ def repository_compare(self, base: str, head: str):
284
292
}
285
293
response = requests .get (url , headers = headers )
286
294
logger .debug (
287
- f"Get changes response from GitHub for repository_compare: { response .status_code } , { response .text } , URL: { url } " )
295
+ _ ("Get changes response from GitHub for repository_compare: {response_status_code}, {response_text}, URL: {url}" ).format (
296
+ response_status_code = response .status_code , response_text = response .text , url = url ))
288
297
289
298
if response .status_code == 200 :
290
299
# 转换为GitLab格式的diffs
@@ -301,18 +310,18 @@ def repository_compare(self, base: str, head: str):
301
310
return diffs
302
311
else :
303
312
logger .warn (
304
- f "Failed to get changes for repository_compare: { response .status_code } , { response .text } " )
313
+ _ ( "Failed to get changes for repository_compare: {response.status_code}, {response.text}" ). format ( response_status_code = response . status_code , response_text = response . text ) )
305
314
return []
306
315
307
316
def get_push_changes (self ) -> list :
308
317
# 检查是否为 Push 事件
309
318
if self .event_type != 'push' :
310
- logger .warn (f "Invalid event type: { self . event_type } . Only 'push' event is supported now." )
319
+ logger .warn (_ ( "Invalid event type: {}. Only 'push' event is supported now." ). format ( self . event_type ) )
311
320
return []
312
321
313
322
# 如果没有提交,返回空列表
314
323
if not self .commit_list :
315
- logger .info ("No commits found in push event." )
324
+ logger .info (_ ( "No commits found in push event." ) )
316
325
return []
317
326
318
327
# 优先尝试compare API获取变更
@@ -330,12 +339,12 @@ def get_push_changes(self) -> list:
330
339
elif self .webhook_data .get ('deleted' , False ):
331
340
# 删除分支处理
332
341
return []
333
-
342
+
334
343
return self .repository_compare (before , after )
335
344
else :
336
345
# 如果before和after不存在,尝试通过commits获取
337
- logger .info ("before or after not found in webhook data, trying to get changes from commits." )
338
-
346
+ logger .info (_ ( "before or after not found in webhook data, trying to get changes from commits." ) )
347
+
339
348
changes = []
340
349
for commit in self .commit_list :
341
350
commit_id = commit .get ('id' )
@@ -344,5 +353,5 @@ def get_push_changes(self) -> list:
344
353
if parent_id :
345
354
commit_changes = self .repository_compare (parent_id , commit_id )
346
355
changes .extend (commit_changes )
347
-
348
- return changes
356
+
357
+ return changes
0 commit comments