@@ -12,18 +12,16 @@ def get_merge_request_id(branch_name, project_id):
1212 :param project_id: 项目id
1313 :return: 如果分支存在 mr 则返回mrid / 如果不存在mr 则返回 ""
1414 """
15- # 构建API请求URL
1615 url = f"{ gitlab_server_url } /api/v4/projects/{ project_id } /merge_requests"
1716
18- # 发送API请求,检查是否有与分支相关的Merge Request
1917 params = {
2018 "source_branch" : branch_name ,
2119 "state" : "opened" # 可以根据需求选择合适的状态(opened、closed、merged等)
2220 }
21+
2322 headers = {"Private-Token" : gitlab_private_token }
2423 response = requests .get (url , params = params , headers = headers )
2524
26- # 解析JSON响应并检查是否有相关的Merge Request
2725 if response .status_code == 200 :
2826 merge_requests = response .json ()
2927 if len (merge_requests ) > 0 :
@@ -38,27 +36,19 @@ def get_merge_request_id(branch_name, project_id):
3836
3937@retry (stop_max_attempt_number = 3 , wait_fixed = 2000 )
4038def get_commit_list (merge_request_iid , project_id ):
41- # Create API URL for the merge request commits
4239 api_url = f"{ gitlab_server_url } /api/v4/projects/{ project_id } /merge_requests/{ merge_request_iid } /commits"
43- # Set the private token in the header
40+
4441 headers = {"PRIVATE-TOKEN" : gitlab_private_token }
45-
46- # Make a GET request to the API URL
4742 response = requests .get (api_url , headers = headers )
43+
4844 commit_list = []
49- # If the response code is 200, the API call was successful
5045 if response .status_code == 200 :
51- # Get the commits from the response
5246 commits = response .json ()
53- # Iterate through the commits and print the commit ID and message
5447 for commit in commits :
5548 print (f"Commit ID: { commit ['id' ]} , Message: { commit ['message' ]} " )
56- # Append the commit ID to the list
5749 commit_list .append (commit ['id' ])
5850 else :
59- # Log an error if the API call was unsuccessful
6051 log .error (f"Failed to fetch commits. Status code: { response .status_code } " )
61- # Return the list of commit IDs
6252 return commit_list
6353
6454
@@ -117,40 +107,38 @@ def add_comment_to_mr(project_id, merge_request_id, comment):
117107
118108
119109@retry (stop_max_attempt_number = 3 , wait_fixed = 2000 )
120- def get_mr_comment_info (project_id , mr_iid , ):
121- url = f"{ gitlab_server_url } /api/v4/projects/{ project_id } /merge_requests/{ mr_iid } /notes"
110+ def get_merge_request_comments (project_id , merge_request_iid ):
111+ url = f"{ gitlab_server_url } /api/v4/projects/{ project_id } /merge_requests/{ merge_request_iid } /notes"
122112
123- # 发送API请求
124113 headers = {"Private-Token" : gitlab_private_token }
125114 response = requests .get (url , headers = headers )
126115
127- comments_info = ""
128- # 解析JSON响应
116+ comments_content = ""
129117 if response .status_code == 200 :
130118 comments = response .json ()
131119 for comment in comments :
132- author = comment ['author' ]['username' ]
133- comment_text = comment ['body' ]
134- print (f"Author: { author } " )
135- print (f"Comment: { comment_text } " )
136- comments_info += comment_text
120+ author_username = comment ['author' ]['username' ]
121+ comment_body = comment ['body' ]
122+ print (f"Author: { author_username } " )
123+ print (f"Comment: { comment_body } " )
124+ comments_content += comment_body
137125
138126 else :
139127 print (f" 获取mr comment 失败, Status code: { response .status_code } " )
140- return comments_info
128+ return comments_content
141129
142130
143131@retry (stop_max_attempt_number = 3 , wait_fixed = 2000 )
144132def get_commit_change_file (push_info ):
145133 # 获取提交列表
146- commits = push_info ['commits' ]
147- add_file = []
148- modify_file = []
134+ commit_list = push_info ['commits' ]
135+ added_files_list = []
136+ modified_files_list = []
149137 # 遍历提交
150- for commit in commits :
138+ for commit in commit_list :
151139 added_files = commit .get ('added' , [])
152140 modified_files = commit .get ('modified' , [])
153- add_file += added_files
154- modify_file += modified_files
141+ added_files_list += added_files
142+ modified_files_list += modified_files
155143
156- return add_file + modify_file
144+ return added_files_list + modified_files_list
0 commit comments