Skip to content

修复 Gitlab 不同版本 mr 请求区别 #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion gitlab_integration/gitlab_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def get_info(self):
if response.status_code == 200:
return response.json()
else:
log.error(f"获取项目信息失败: {response.status_code} {response.text}")
return None

@retry(stop_max_attempt_number=3, wait_fixed=2000)
Expand Down Expand Up @@ -202,4 +203,16 @@ def _build_authenticated_url(self, repo_url):
elif repo_url.startswith("http://"):
return f"http://oauth2:{token}@{repo_url[7:]}"
else:
raise ValueError("Unsupported URL scheme")
raise ValueError("Unsupported URL scheme")

def is_merge_request_opened(gitlab_payload) -> bool:
"""
判断是否是merge request打开事件
"""
try:
gitlab_merge_request_old = gitlab_payload.get("object_attributes").get("state") == "opened" and gitlab_payload.get("object_attributes").get("merge_status") == "preparing"
gitlab_merge_request_new = gitlab_payload.get("object_attributes").get("state") == "merged" and gitlab_payload.get("object_attributes").get("merge_status") == "can_be_merged"
return gitlab_merge_request_old or gitlab_merge_request_new
except Exception as e:
log.error(f"判断是否是merge request打开事件失败: {e}")
return False
4 changes: 2 additions & 2 deletions gitlab_integration/webhook_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from response_module.response_controller import ReviewResponse
from review_engine.review_engine import ReviewEngine
from utils.logger import log

from gitlab_integration.gitlab_fetcher import is_merge_request_opened

class WebhookListener:
def __init__(self):
Expand Down Expand Up @@ -53,7 +53,7 @@ def handle_merge_request(self, gitlab_payload, reply):
"""
处理合并请求事件
"""
if gitlab_payload.get("object_attributes").get("state") == "opened" and gitlab_payload.get("object_attributes").get("merge_status") == "preparing":
if is_merge_request_opened(gitlab_payload):
log.info("首次merge_request ", gitlab_payload)
project_id = gitlab_payload.get('project')['id']
merge_request_iid = gitlab_payload.get("object_attributes")["iid"]
Expand Down
2 changes: 1 addition & 1 deletion review_engine/handler/default_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,6 @@ def default_handle(self, changes, merge_info, hook_info, reply, model):

else:
log.error(f"获取merge_request信息失败,project_id: {hook_info['project']['id']} |"
f" merge_iid: {hook_info['object_attributes']['iid']}")
f" merge_iid: {hook_info['object_attributes']['iid']} | merge_info: {merge_info}")