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..3bb5175 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", {}).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"] 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():