Skip to content

feat(review): 增加 Merge 触发分支白名单配置 #38

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
3 changes: 2 additions & 1 deletion gitlab_integration/webhook_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"]
Expand Down
14 changes: 12 additions & 2 deletions utils/args_check.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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():
Expand Down