-
-
Notifications
You must be signed in to change notification settings - Fork 8
refactor: reduce functions cognitive complexity #276
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4e14363
refactor: reduce #275 its cognitive complexity
shenxianpeng 9868f4f
Merge branch 'main' into bugfix/refactor-function
shenxianpeng 8d8d8df
refactor: move _find_check to util
shenxianpeng 6d4fff6
refactor: add _print_failure to call
shenxianpeng bfb2edb
refactor: add _print_failure to util
shenxianpeng bccd3dc
Update commit_check/util.py
shenxianpeng 8a4161e
fix: update util to pass lint
shenxianpeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,46 @@ | ||
| """Check git author name and email""" | ||
| import re | ||
| from commit_check import YELLOW, RESET_COLOR, PASS, FAIL | ||
| from commit_check.util import get_commit_info, has_commits, print_error_header, print_error_message, print_suggestion | ||
| from commit_check.util import ( | ||
| get_commit_info, | ||
| has_commits, | ||
| _find_check, | ||
| _print_failure, | ||
| ) | ||
|
|
||
|
|
||
| _AUTHOR_FORMAT_MAP = { | ||
| "author_name": "an", | ||
| "author_email": "ae", | ||
| } | ||
|
|
||
|
|
||
| def _get_author_value(check_type: str) -> str: | ||
| """Fetch the author value from git for the given check type.""" | ||
| format_str = _AUTHOR_FORMAT_MAP.get(check_type, "") | ||
| return str(get_commit_info(format_str)) | ||
|
|
||
|
|
||
| def check_author(checks: list, check_type: str) -> int: | ||
| """Validate author name or email according to configured regex.""" | ||
| if has_commits() is False: | ||
| return PASS # pragma: no cover | ||
|
|
||
| for check in checks: | ||
| if check['check'] == check_type: | ||
| if check['regex'] == "": | ||
| print( | ||
| f"{YELLOW}Not found regex for {check_type}. skip checking.{RESET_COLOR}", | ||
| ) | ||
| return PASS | ||
| if check_type == "author_name": | ||
| format_str = "an" | ||
| if check_type == 'author_email': | ||
| format_str = "ae" | ||
| config_value = str(get_commit_info(format_str)) | ||
| result = re.match(check['regex'], config_value) | ||
| if result is None: | ||
| if not print_error_header.has_been_called: | ||
| print_error_header() | ||
| print_error_message( | ||
| check['check'], check['regex'], | ||
| check['error'], config_value, | ||
| ) | ||
| if check['suggest']: | ||
| print_suggestion(check['suggest']) | ||
| return FAIL | ||
| return PASS | ||
| return PASS # pragma: no cover | ||
|
|
||
| check = _find_check(checks, check_type) | ||
| if not check: | ||
| return PASS | ||
|
|
||
| # If regex is empty, skip without fetching author info | ||
| regex = check.get("regex", "") | ||
| if regex == "": | ||
| print(f"{YELLOW}Not found regex for {check_type}. skip checking.{RESET_COLOR}") | ||
| return PASS | ||
|
|
||
| value = _get_author_value(check_type) | ||
|
|
||
| if re.match(regex, value): | ||
| return PASS | ||
|
|
||
| _print_failure(check, regex, value) | ||
|
|
||
| return FAIL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.