From 12ded83cac1da4bc00963d34a807b15eef2d77c5 Mon Sep 17 00:00:00 2001 From: Fenhy Date: Sun, 6 Apr 2025 18:57:26 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat(review):=20=E5=A2=9E=E5=8A=A0=E5=88=86?= =?UTF-8?q?=E6=94=AF=E7=99=BD=E5=90=8D=E5=8D=95=E9=85=8D=E7=BD=AE=E5=B9=B6?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=90=88=E5=B9=B6=E8=AF=B7=E6=B1=82=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 config.py 中添加 MERGE_TRIGGER_ON_BRANCHES 配置项,用于指定需要进行审核的分支 - 修改 args_check.py,增加 branch_need_check 函数用于检查分支是否需要审核 - 修改 webhook_listener.py,增加分支白名单检查逻辑 --- config/config.py | 4 ++++ gitlab_integration/webhook_listener.py | 3 ++- utils/args_check.py | 14 ++++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/config/config.py b/config/config.py index bccf413..9773c9f 100644 --- a/config/config.py +++ b/config/config.py @@ -89,3 +89,7 @@ # context code lines 上下文关联代码行数 CONTEXT_LINES_NUM = 5 + +# Branch name or wildcard pattern to trigger on (use an empty array for all) +# example ["main", "master", "release/v*"] +MERGE_TRIGGER_ON_BRANCHES = [] \ No newline at end of file diff --git a/gitlab_integration/webhook_listener.py b/gitlab_integration/webhook_listener.py index 37d71f5..ef6392b 100644 --- a/gitlab_integration/webhook_listener.py +++ b/gitlab_integration/webhook_listener.py @@ -6,6 +6,7 @@ from gitlab_integration.gitlab_fetcher import GitlabMergeRequestFetcher, GitlabRepoManager from response_module.response_controller import ReviewResponse from review_engine.review_engine import ReviewEngine +from utils.args_check import branch_need_check from utils.logger import log from gitlab_integration.gitlab_fetcher import is_merge_request_opened @@ -53,7 +54,7 @@ def handle_merge_request(self, gitlab_payload, reply): """ 处理合并请求事件 """ - if is_merge_request_opened(gitlab_payload): + if branch_need_check(gitlab_payload.get("object_attributes")["target_branch"]) and 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"] diff --git a/utils/args_check.py b/utils/args_check.py index 81b07f3..b9f0baf 100644 --- a/utils/args_check.py +++ b/utils/args_check.py @@ -1,7 +1,7 @@ -import requests +import requests,fnmatch from tabulate import tabulate -from config.config import EXCLUDE_FILE_TYPES, IGNORE_FILE_TYPES +from config.config import EXCLUDE_FILE_TYPES, IGNORE_FILE_TYPES, MERGE_TRIGGER_ON_BRANCHES def check_config(): """ Check the configuration @@ -191,6 +191,16 @@ def file_need_check(file_path: str) -> bool: return is_excluded_file_type and not is_ignored_file_type +def branch_need_check(branch_name: str) -> bool: + """ + 检查分支是否需要进行审核 + + :param branch_name: 分支名称 + :return: 如果分支命中匹配规则,则返回True,否则返回False + """ + return not MERGE_TRIGGER_ON_BRANCHES or any(fnmatch.fnmatch(branch_name, pattern) for pattern in MERGE_TRIGGER_ON_BRANCHES) + + # 示例调用 if __name__ == "__main__": if check_config(): From 7cd0e8a0d98d2263f2e61255b076a8a9ccd4628c Mon Sep 17 00:00:00 2001 From: Fenhy Date: Sun, 6 Apr 2025 19:13:11 +0800 Subject: [PATCH 2/2] =?UTF-8?q?refactor(webhook=5Flistener):=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E5=90=88=E5=B9=B6=E8=AF=B7=E6=B1=82=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E5=A4=84=E7=90=86=E4=B8=AD=E7=9A=84=E5=88=86=E6=94=AF=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用 get 方法替代直接访问字典,避免 KeyError 异常 - 添加默认值以处理潜在的 NoneType 错误 --- gitlab_integration/webhook_listener.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitlab_integration/webhook_listener.py b/gitlab_integration/webhook_listener.py index ef6392b..3bb5175 100644 --- a/gitlab_integration/webhook_listener.py +++ b/gitlab_integration/webhook_listener.py @@ -54,7 +54,7 @@ def handle_merge_request(self, gitlab_payload, reply): """ 处理合并请求事件 """ - if branch_need_check(gitlab_payload.get("object_attributes")["target_branch"]) and is_merge_request_opened(gitlab_payload): + if branch_need_check(gitlab_payload.get("object_attributes", {}).get("target_branch", "")) and 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"]