def _open_repository(config): repository_path = config["main"]["repository"] logging.debug("Open repo at %s", repository_path) try: return Repo(repository_path) except (NoSuchPathError, InvalidGitRepositoryError) as err: print_error(f"Cannot open git repo at {repository_path}. Error: {err}") sys.exit(ERROR_EXIT_CODE)
def _finish(all_clear): if all_clear: logging.info("All clear.") print_success("All clear.") else: print_error("Some checks are failed.") logging.info("Some checks are failed.") logging.info("Exit with state %d", FAILED_EXIT_CODE) sys.exit(FAILED_EXIT_CODE)
def _parse_value_from_file(file_config, option, section): option_value = file_config[section][option] try: if option in _FILE_CONFIG_INT_OPTIONS: option_value = file_config[section].getint(option) elif option in _FILE_CONFIG_BOOL_OPTIONS: option_value = file_config[section].getboolean(option) except ValueError: print_error(f"Cannot parse option {section}:{option}") sys.exit(ERROR_EXIT_CODE) return option_value
def _read_file_config(file_config, file_config_path): config_filename = file_config_path try: with open(config_filename) as config_fp: file_config.read_file(config_fp) except FileNotFoundError: if file_config_path != DEFAULTS["main"]["config"]: print_error(f"Cannot open file {config_filename}") sys.exit(ERROR_EXIT_CODE) except OSError: print_error(f"Cannot open file {config_filename}") sys.exit(ERROR_EXIT_CODE)
def _validate(config): if config["rules.settings"]["min_length"] > config["rules.settings"][ "max_length"]: print_error( "rules.settings.min_length is greater than rules.settings.max_length" ) sys.exit(ERROR_EXIT_CODE) if config["merge_rules.settings"]["min_length"] > config[ "merge_rules.settings"]["max_length"]: print_error( "merge_rules.settings.min_length is greater than merge_rules.settings.max_length" ) sys.exit(ERROR_EXIT_CODE)
def _process_failed_commits(failed_commits, rule): all_clear = True if failed_commits: print_error("ERROR") all_clear = False logging.info("Failed %d commit for '%s' rule", len(failed_commits), rule) for commit in failed_commits: logging.debug("Hash: %s, message: '%s'", commit.hexsha, commit.summary) print(f"Hash: {commit.hexsha}, message: '{commit.summary}'") else: logging.info("Errors not found for rule '%s'", rule) print_success("SUCCESS") return all_clear
def _generic_check(commit_range, predicate, *, check_merge_commits): desired_parents_count = 2 if check_merge_commits else 1 try: return list( filter( predicate, filter( lambda commit: len(commit.parents) == desired_parents_count, commit_range, ), )) except GitCommandError as err: print_error(f"Cannot read commit in rage. Error: {err}") sys.exit(ERROR_EXIT_CODE)
def test_error(self, mock_print): line = "Error" print_error(line) mock_print.assert_called_once_with(line, sep=" ", end="\n", formatter=BColors.fail)