Skip to content

Commit 4e14363

Browse files
committed
refactor: reduce #275 its cognitive complexity
1 parent dc169ba commit 4e14363

4 files changed

Lines changed: 221 additions & 159 deletions

File tree

‎commit_check/author.py‎

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,69 @@
11
"""Check git author name and email"""
22
import re
3+
from typing import Optional
34
from commit_check import YELLOW, RESET_COLOR, PASS, FAIL
4-
from commit_check.util import get_commit_info, has_commits, print_error_header, print_error_message, print_suggestion
5+
from commit_check.util import (
6+
get_commit_info,
7+
has_commits,
8+
print_error_header,
9+
print_error_message,
10+
print_suggestion,
11+
)
512

613

7-
def check_author(checks: list, check_type: str) -> int:
8-
if has_commits() is False:
9-
return PASS # pragma: no cover
14+
_AUTHOR_FORMAT_MAP = {
15+
"author_name": "an",
16+
"author_email": "ae",
17+
}
1018

19+
20+
def _find_check(checks: list, check_type: str) -> Optional[dict]:
21+
"""Return the first check dict matching check_type, else None."""
1122
for check in checks:
12-
if check['check'] == check_type:
13-
if check['regex'] == "":
14-
print(
15-
f"{YELLOW}Not found regex for {check_type}. skip checking.{RESET_COLOR}",
16-
)
17-
return PASS
18-
if check_type == "author_name":
19-
format_str = "an"
20-
if check_type == 'author_email':
21-
format_str = "ae"
22-
config_value = str(get_commit_info(format_str))
23-
result = re.match(check['regex'], config_value)
24-
if result is None:
25-
if not print_error_header.has_been_called:
26-
print_error_header()
27-
print_error_message(
28-
check['check'], check['regex'],
29-
check['error'], config_value,
30-
)
31-
if check['suggest']:
32-
print_suggestion(check['suggest'])
33-
return FAIL
23+
if check.get("check") == check_type:
24+
return check
25+
return None
26+
27+
28+
def _get_author_value(check_type: str) -> str:
29+
"""Fetch the author value from git for the given check type."""
30+
format_str = _AUTHOR_FORMAT_MAP.get(check_type, "")
31+
return str(get_commit_info(format_str))
32+
33+
34+
def _evaluate_check(check: dict, value: str) -> int:
35+
"""Evaluate a single author check against the provided value."""
36+
regex = check.get("regex", "")
37+
if regex == "":
38+
print(f"{YELLOW}Not found regex for {check.get('check')}. skip checking.{RESET_COLOR}")
39+
return PASS
40+
41+
if re.match(regex, value) is None:
42+
if not print_error_header.has_been_called:
43+
print_error_header()
44+
check_name = str(check.get("check", ""))
45+
error_msg = str(check.get("error", ""))
46+
print_error_message(check_name, regex, error_msg, value)
47+
if check.get("suggest"):
48+
print_suggestion(check["suggest"])
49+
return FAIL
3450
return PASS
51+
52+
53+
def check_author(checks: list, check_type: str) -> int:
54+
"""Validate author name or email according to configured regex."""
55+
if has_commits() is False:
56+
return PASS # pragma: no cover
57+
58+
check = _find_check(checks, check_type)
59+
if not check:
60+
return PASS
61+
62+
# If regex is empty, skip without fetching author info
63+
regex = check.get("regex", "")
64+
if regex == "":
65+
print(f"{YELLOW}Not found regex for {check_type}. skip checking.{RESET_COLOR}")
66+
return PASS
67+
68+
value = _get_author_value(check_type)
69+
return _evaluate_check(check, value)

‎commit_check/branch.py‎

Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,39 @@
44
from commit_check.util import get_branch_name, git_merge_base, print_error_header, print_error_message, print_suggestion, has_commits
55

66

7-
def check_branch(checks: list) -> int:
7+
def _find_branch_check(checks: list) -> dict | None:
8+
"""Return the first branch check config or None if not present."""
89
for check in checks:
9-
if check['check'] == 'branch':
10-
if check['regex'] == "":
11-
print(
12-
f"{YELLOW}Not found regex for branch naming. skip checking.{RESET_COLOR}",
13-
)
14-
return PASS
15-
branch_name = get_branch_name()
16-
result = re.match(check['regex'], branch_name)
17-
if result is None:
18-
if not print_error_header.has_been_called:
19-
print_error_header() # pragma: no cover
20-
print_error_message(
21-
check['check'], check['regex'],
22-
check['error'], branch_name,
23-
)
24-
if check['suggest']:
25-
print_suggestion(check['suggest'])
26-
return FAIL
27-
return PASS
10+
if check.get('check') == 'branch':
11+
return check
12+
return None
13+
14+
15+
def check_branch(checks: list) -> int:
16+
check = _find_branch_check(checks)
17+
if not check:
18+
return PASS
19+
20+
regex = check.get('regex', "")
21+
if regex == "":
22+
print(
23+
f"{YELLOW}Not found regex for branch naming. skip checking.{RESET_COLOR}",
24+
)
25+
return PASS
26+
27+
branch_name = get_branch_name()
28+
if re.match(regex, branch_name):
29+
return PASS
30+
31+
if not print_error_header.has_been_called:
32+
print_error_header() # pragma: no cover
33+
print_error_message(
34+
check['check'], regex,
35+
check['error'], branch_name,
36+
)
37+
if check.get('suggest'):
38+
print_suggestion(check['suggest'])
39+
return FAIL
2840

2941

3042
def check_merge_base(checks: list) -> int:
@@ -36,24 +48,30 @@ def check_merge_base(checks: list) -> int:
3648
if has_commits() is False:
3749
return PASS # pragma: no cover
3850

39-
for check in checks:
40-
if check['check'] == 'merge_base':
41-
if check['regex'] == "":
42-
print(
43-
f"{YELLOW}Not found target branch for checking merge base. skip checking.{RESET_COLOR}",
44-
)
45-
return PASS
46-
target_branch = check['regex'] if "origin/" in check['regex'] else f"origin/{check['regex']}"
47-
current_branch = get_branch_name()
48-
result = git_merge_base(target_branch, current_branch)
49-
if result != 0:
50-
if not print_error_header.has_been_called:
51-
print_error_header() # pragma: no cover
52-
print_error_message(
53-
check['check'], check['regex'],
54-
check['error'], current_branch,
55-
)
56-
if check['suggest']:
57-
print_suggestion(check['suggest'])
58-
return FAIL
59-
return PASS
51+
# locate merge_base rule, if any
52+
merge_check = next((c for c in checks if c.get('check') == 'merge_base'), None)
53+
if not merge_check:
54+
return PASS
55+
56+
regex = merge_check.get('regex', "")
57+
if regex == "":
58+
print(
59+
f"{YELLOW}Not found target branch for checking merge base. skip checking.{RESET_COLOR}",
60+
)
61+
return PASS
62+
63+
target_branch = regex if "origin/" in regex else f"origin/{regex}"
64+
current_branch = get_branch_name()
65+
result = git_merge_base(target_branch, current_branch)
66+
if result == 0:
67+
return PASS
68+
69+
if not print_error_header.has_been_called:
70+
print_error_header() # pragma: no cover
71+
print_error_message(
72+
merge_check['check'], regex,
73+
merge_check['error'], current_branch,
74+
)
75+
if merge_check.get('suggest'):
76+
print_suggestion(merge_check['suggest'])
77+
return FAIL

0 commit comments

Comments
 (0)