From 001886ac92c87d55ba7a5aadecbe9b09887fdfa9 Mon Sep 17 00:00:00 2001 From: B Date: Sun, 27 Oct 2024 14:29:01 -0500 Subject: [PATCH 01/14] chore(core): test icons flow --- .pre-commit-config.yaml | 7 + commit_msg_version_bump/main.py | 4 +- control_commit/main.py | 235 ++++++++++++++++++++++++++++++++ 3 files changed, 244 insertions(+), 2 deletions(-) create mode 100644 control_commit/main.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index dd8f3cc..7b12f63 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -64,6 +64,13 @@ repos: language: system types: [yaml] files: ^docker-compose(\.dev|\.prod)?\.yml$ + - id: validate-commit + name: validate-commit + entry: python control_commit/main.py --log-level=DEBUG + always_run: true + pass_filenames: false + language: system + stages: [pre-push] - id: commit-msg-version-check name: commit-msg-version-check entry: python commit_msg_version_bump/main.py --log-level=DEBUG diff --git a/commit_msg_version_bump/main.py b/commit_msg_version_bump/main.py index 0a7f37d..14a074d 100644 --- a/commit_msg_version_bump/main.py +++ b/commit_msg_version_bump/main.py @@ -238,7 +238,7 @@ def amend_commit(new_commit_msg: str) -> None: subprocess.run(["git", "commit", "--amend", "-m", new_commit_msg], check=True) logger.info("Successfully amended the commit with the new version bump.") logger.info( - "Please perform a force push using 'git push' to update the remote repository. Avoid use --force" + "Please perform a push using 'git push' to update the remote repository. Avoid use --force" ) except subprocess.CalledProcessError as e: logger.error(f"Failed to amend the commit: {e}") @@ -283,7 +283,7 @@ def main() -> None: amend_commit(updated_commit_msg) logger.info( - "Aborting the current push. Please perform a force push using 'git push'. Avoid use --force" + "Aborting the current push. Please perform a push using 'git push'. Avoid use --force" ) sys.exit(1) else: diff --git a/control_commit/main.py b/control_commit/main.py new file mode 100644 index 0000000..fcbb65a --- /dev/null +++ b/control_commit/main.py @@ -0,0 +1,235 @@ +#!/usr/bin/env python3 +""" +commit_msg_icon_adder.py + +A script to validate commit messages and add appropriate icons based on commit types. +Ensures that commit messages follow a specific structure and naming conventions. +Adds icons to commit messages that do not contain square brackets []. + +Usage: + commit_msg_icon_adder.py [--log-level {INFO,DEBUG}] +""" + +import argparse +import logging +import re +import sys +from logging.handlers import RotatingFileHandler + +# Mapping of commit types to icons +TYPE_MAPPING = { + "feat": "✨", + "fix": "🐛", + "docs": "📝", + "style": "💄", + "refactor": "♻️", + "perf": "⚡️", + "test": "✅", + "chore": "🔧", +} + +# Regular expressions for detecting commit types and validating commit message structure +COMMIT_TYPE_REGEX = re.compile(r"^(?Pfeat|fix|docs|style|refactor|perf|test|chore)") +COMMIT_MESSAGE_REGEX = re.compile( + r"^(?Pfeat|fix|docs|style|refactor|perf|test|chore)" + r"(?:\((?P[a-z0-9\-]+)\))?:\s+" + r"(?P[a-z].+)$" +) + +# Initialize the logger +logger = logging.getLogger(__name__) + + +def parse_arguments() -> argparse.Namespace: + """ + Parses command-line arguments. + + Returns: + argparse.Namespace: Parsed arguments. + """ + parser = argparse.ArgumentParser( + description=( + "Validate commit messages and add icons based on commit types. " + "Ensures commit messages follow the format: type(scope): description." + ) + ) + parser.add_argument( + "--log-level", + choices=["INFO", "DEBUG"], + default="INFO", + help="Set the logging level. Default is INFO.", + ) + return parser.parse_args() + + +def configure_logger(log_level: str) -> None: + """ + Configures logging for the script. + + Args: + log_level (str): Logging level as a string (e.g., 'INFO', 'DEBUG'). + """ + numeric_level = getattr(logging, log_level.upper(), None) + if not isinstance(numeric_level, int): + raise ValueError(f"Invalid log level: {log_level}") + + logger.setLevel(numeric_level) + + # Set up log rotation: max size 5MB, keep 5 backup files + file_handler = RotatingFileHandler( + "commit_msg_icon_adder.log", maxBytes=5 * 1024 * 1024, backupCount=5 + ) + console_handler = logging.StreamHandler() + + formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") + file_handler.setFormatter(formatter) + console_handler.setFormatter(formatter) + + logger.handlers.clear() + logger.addHandler(file_handler) + logger.addHandler(console_handler) + + +def read_commit_message(file_path: str) -> str: + """ + Reads the commit message from the given file. + + Args: + file_path (str): Path to the commit message file. + + Returns: + str: The commit message. + """ + try: + with open(file_path, "r", encoding="utf-8") as file: + commit_msg = file.read().strip() + logger.debug(f"Original commit message: {commit_msg}") + return commit_msg + except FileNotFoundError: + logger.error(f"Commit message file not found: {file_path}") + sys.exit(1) + except Exception as e: + logger.error(f"Error reading commit message file: {e}") + sys.exit(1) + + +def validate_commit_message(commit_msg: str) -> bool: + """ + Validates the commit message against the required structure and lowercase naming. + + Args: + commit_msg (str): The commit message to validate. + + Returns: + bool: True if valid, False otherwise. + """ + match = COMMIT_MESSAGE_REGEX.match(commit_msg) + if match: + logger.debug("Commit message structure is valid.") + return True + else: + logger.error("Invalid commit message structure. Ensure it follows the format:") + logger.error("type(scope): description") + logger.error(" - type: feat, fix, docs, style, refactor, perf, test, chore (lowercase)") + logger.error(" - scope: optional, lowercase, alphanumeric and hyphens") + logger.error(" - description: starts with a lowercase letter") + return False + + +def add_icon_to_commit_message(commit_type: str, existing_commit_msg: str) -> str: + """ + Adds an icon to the commit message based on its type if it doesn't already have one. + + Args: + commit_type (str): The type of the commit (e.g., 'chore', 'fix'). + existing_commit_msg (str): The original commit message. + + Returns: + str: The commit message with the icon prepended. + """ + icon = TYPE_MAPPING.get(commit_type.lower(), "") + if not existing_commit_msg.startswith(icon): + new_commit_msg = f"{icon} {existing_commit_msg}" + logger.debug(f"Updated commit message with icon: {new_commit_msg}") + return new_commit_msg + logger.debug("Icon already present in commit message.") + return existing_commit_msg + + +def write_commit_message(file_path: str, commit_msg: str) -> None: + """ + Writes the updated commit message back to the commit message file. + + Args: + file_path (str): Path to the commit message file. + commit_msg (str): The updated commit message. + """ + try: + with open(file_path, "w", encoding="utf-8") as file: + file.write(commit_msg + "\n") + logger.debug(f"Commit message written to file: {file_path}") + except Exception as e: + logger.error(f"Error writing to commit message file: {e}") + sys.exit(1) + + +def main() -> None: + """ + Main function to validate commit messages and add icons if necessary. + Exits with code 1 if validation fails. + """ + args = parse_arguments() + configure_logger(args.log_level) + + commit_msg_file = ".git/COMMIT_EDITMSG" + commit_msg = read_commit_message(commit_msg_file) + + # Validate the commit message structure and naming + if not validate_commit_message(commit_msg): + logger.error("Commit message validation failed. Aborting commit.") + sys.exit(1) + + # Check if the commit message contains square brackets + if not has_square_brackets(commit_msg): + logger.debug("Commit message does not contain square brackets. Proceeding to add icon.") + + # Determine the type of commit to get the appropriate icon + type_match = COMMIT_TYPE_REGEX.match(commit_msg) + if type_match: + commit_type = type_match.group("type") + logger.debug(f"Detected commit type: {commit_type}") + else: + commit_type = "chore" # Default to 'chore' if no type is found + logger.debug("No commit type detected. Defaulting to 'chore'.") + exit(1) + + # Add the icon to the existing commit message + updated_commit_msg = add_icon_to_commit_message(commit_type, commit_msg) + + # Write the updated commit message back to the file + write_commit_message(commit_msg_file, updated_commit_msg) + + # Inform the user and abort the commit to allow them to review the amended message + logger.info( + "Commit message has been updated with an icon. Please review the commit message." + ) + sys.exit(1) + else: + logger.debug("Commit message contains square brackets. No icon added.") + + +def has_square_brackets(commit_msg: str) -> bool: + """ + Checks if the commit message contains square brackets. + + Args: + commit_msg (str): The commit message. + + Returns: + bool: True if square brackets are present, False otherwise. + """ + return bool(re.search(r"\[.*?]", commit_msg)) + + +if __name__ == "__main__": + main() From d985a875373aab22ac0478d348a4787924f9ae02 Mon Sep 17 00:00:00 2001 From: B Date: Sun, 27 Oct 2024 14:44:04 -0500 Subject: [PATCH 02/14] chore(core): test icons --- commit_msg_version_bump/main.py | 6 +- control_commit/main.py | 143 ++++++++++++++++++++------------ pyproject.toml | 2 +- 3 files changed, 90 insertions(+), 61 deletions(-) diff --git a/commit_msg_version_bump/main.py b/commit_msg_version_bump/main.py index 14a074d..61a1c0e 100644 --- a/commit_msg_version_bump/main.py +++ b/commit_msg_version_bump/main.py @@ -310,11 +310,7 @@ def determine_version_bump(commit_msg: str) -> Optional[str]: elif "patch" in keyword: return "patch" else: - # Fallback based on commit type - type_match = COMMIT_TYPE_REGEX.match(commit_msg) - if type_match: - commit_type = type_match.group("type").lower() - return VERSION_BUMP_MAPPING.get(commit_type) + return None return None diff --git a/control_commit/main.py b/control_commit/main.py index fcbb65a..7ef4a0c 100644 --- a/control_commit/main.py +++ b/control_commit/main.py @@ -1,13 +1,10 @@ #!/usr/bin/env python3 """ -commit_msg_icon_adder.py +control_commit/main.py A script to validate commit messages and add appropriate icons based on commit types. Ensures that commit messages follow a specific structure and naming conventions. Adds icons to commit messages that do not contain square brackets []. - -Usage: - commit_msg_icon_adder.py [--log-level {INFO,DEBUG}] """ import argparse @@ -77,17 +74,34 @@ def configure_logger(log_level: str) -> None: # Set up log rotation: max size 5MB, keep 5 backup files file_handler = RotatingFileHandler( - "commit_msg_icon_adder.log", maxBytes=5 * 1024 * 1024, backupCount=5 + "commit_msg_icon_adder.log", + maxBytes=5 * 1024 * 1024, + backupCount=5, + encoding="utf-8", # Ensure UTF-8 encoding to handle emojis ) - console_handler = logging.StreamHandler() - formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") file_handler.setFormatter(formatter) - console_handler.setFormatter(formatter) + + # Create a safe console handler that replaces unencodable characters + class SafeStreamHandler(logging.StreamHandler): + def emit(self, record): + try: + msg = self.format(record) + # Replace characters that can't be encoded + msg = msg.encode(self.stream.encoding, errors="replace").decode( + self.stream.encoding + ) + self.stream.write(msg + self.terminator) + self.flush() + except Exception: + self.handleError(record) + + safe_console_handler = SafeStreamHandler() + safe_console_handler.setFormatter(formatter) logger.handlers.clear() logger.addHandler(file_handler) - logger.addHandler(console_handler) + logger.addHandler(safe_console_handler) def read_commit_message(file_path: str) -> str: @@ -148,11 +162,11 @@ def add_icon_to_commit_message(commit_type: str, existing_commit_msg: str) -> st str: The commit message with the icon prepended. """ icon = TYPE_MAPPING.get(commit_type.lower(), "") - if not existing_commit_msg.startswith(icon): + if icon and not existing_commit_msg.startswith(icon): new_commit_msg = f"{icon} {existing_commit_msg}" logger.debug(f"Updated commit message with icon: {new_commit_msg}") return new_commit_msg - logger.debug("Icon already present in commit message.") + logger.debug("Icon already present in commit message or no icon defined for commit type.") return existing_commit_msg @@ -173,10 +187,23 @@ def write_commit_message(file_path: str, commit_msg: str) -> None: sys.exit(1) +def has_square_brackets(commit_msg: str) -> bool: + """ + Checks if the commit message contains square brackets. + + Args: + commit_msg (str): The commit message. + + Returns: + bool: True if square brackets are present, False otherwise. + """ + return bool(re.search(r"\[.*?\]", commit_msg)) + + def main() -> None: """ Main function to validate commit messages and add icons if necessary. - Exits with code 1 if validation fails. + Exits with code 1 if validation fails or after adding an icon. """ args = parse_arguments() configure_logger(args.log_level) @@ -184,51 +211,57 @@ def main() -> None: commit_msg_file = ".git/COMMIT_EDITMSG" commit_msg = read_commit_message(commit_msg_file) - # Validate the commit message structure and naming - if not validate_commit_message(commit_msg): - logger.error("Commit message validation failed. Aborting commit.") - sys.exit(1) - - # Check if the commit message contains square brackets - if not has_square_brackets(commit_msg): - logger.debug("Commit message does not contain square brackets. Proceeding to add icon.") - - # Determine the type of commit to get the appropriate icon - type_match = COMMIT_TYPE_REGEX.match(commit_msg) - if type_match: - commit_type = type_match.group("type") - logger.debug(f"Detected commit type: {commit_type}") + # Verify if the commit message already starts with an icon + icon_present = False + for icon in TYPE_MAPPING.values(): + if commit_msg.startswith(f"{icon} "): + icon_present = True + commit_msg_without_icon = commit_msg[len(icon) + 1 :] + logger.debug(f"Commit message already has icon '{icon}'.") + break + + if icon_present: + # Validate the commit message without the icon + if not validate_commit_message(commit_msg_without_icon): + logger.error("Commit message validation failed after removing icon. Aborting commit.") + sys.exit(1) else: - commit_type = "chore" # Default to 'chore' if no type is found - logger.debug("No commit type detected. Defaulting to 'chore'.") - exit(1) - - # Add the icon to the existing commit message - updated_commit_msg = add_icon_to_commit_message(commit_type, commit_msg) - - # Write the updated commit message back to the file - write_commit_message(commit_msg_file, updated_commit_msg) - - # Inform the user and abort the commit to allow them to review the amended message - logger.info( - "Commit message has been updated with an icon. Please review the commit message." - ) - sys.exit(1) + logger.debug("Commit message with icon is valid.") + sys.exit(0) # Valid commit message with icon; proceed else: - logger.debug("Commit message contains square brackets. No icon added.") - - -def has_square_brackets(commit_msg: str) -> bool: - """ - Checks if the commit message contains square brackets. - - Args: - commit_msg (str): The commit message. - - Returns: - bool: True if square brackets are present, False otherwise. - """ - return bool(re.search(r"\[.*?]", commit_msg)) + # Validate the original commit message + if not validate_commit_message(commit_msg): + logger.error("Commit message validation failed. Aborting commit.") + sys.exit(1) + + # Check if the commit message contains square brackets + if not has_square_brackets(commit_msg): + logger.debug("Commit message does not contain square brackets. Proceeding to add icon.") + + # Determine the type of commit to get the appropriate icon + type_match = COMMIT_TYPE_REGEX.match(commit_msg) + if type_match: + commit_type = type_match.group("type") + logger.debug(f"Detected commit type: {commit_type}") + else: + commit_type = "chore" # Default to 'chore' if no type is found + logger.debug("No commit type detected. Defaulting to 'chore'.") + sys.exit(1) + + # Add the icon to the existing commit message + updated_commit_msg = add_icon_to_commit_message(commit_type, commit_msg) + + # Write the updated commit message back to the file + write_commit_message(commit_msg_file, updated_commit_msg) + + # Inform the user and abort the commit to allow them to review the amended message + logger.info( + "Commit message has been updated with an icon. Please review and finalize the commit." + ) + sys.exit(1) + else: + logger.debug("Commit message contains square brackets. No icon added.") + sys.exit(0) # Valid commit message without needing an icon; proceed if __name__ == "__main__": diff --git a/pyproject.toml b/pyproject.toml index d828076..fcb6837 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -71,5 +71,5 @@ ensure_newline_before_comments = true rcfile = ".pylintrc" [build-system] -requires = ["poetry-core>=1.0.17"] +requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api" From 547b5e43c7d05ca0a747478b195c854826e77ee6 Mon Sep 17 00:00:00 2001 From: B Date: Sun, 27 Oct 2024 14:51:30 -0500 Subject: [PATCH 03/14] =?UTF-8?q?=F0=9F=94=A7=20chore(core):=20test=20icon?= =?UTF-8?q?s=20[patch=20candidate]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- control_commit/main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/control_commit/main.py b/control_commit/main.py index 7ef4a0c..ff0a241 100644 --- a/control_commit/main.py +++ b/control_commit/main.py @@ -205,6 +205,7 @@ def main() -> None: Main function to validate commit messages and add icons if necessary. Exits with code 1 if validation fails or after adding an icon. """ + global commit_msg_without_icon args = parse_arguments() configure_logger(args.log_level) From 02ecdf2e216c83f1dc7447555b48c8feea0c58fa Mon Sep 17 00:00:00 2001 From: B Date: Sun, 27 Oct 2024 14:51:55 -0500 Subject: [PATCH 04/14] =?UTF-8?q?=F0=9F=94=A7=20Bump=20version:=201.0.17?= =?UTF-8?q?=20=E2=86=92=201.0.18?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .bumpversion.cfg | 2 +- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 2be3cc8..afb2df0 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 1.0.17 +current_version = 1.0.18 commit = True tag = False diff --git a/pyproject.toml b/pyproject.toml index fcb6837..c7aee6c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "scripts" -version = "1.0.17" +version = "1.0.18" description = "CICD Core Scripts" authors = ["B "] license = "Apache 2.0" From f8aa376c7e787ac5af11145f44614d5d28c94f65 Mon Sep 17 00:00:00 2001 From: B Date: Sun, 27 Oct 2024 14:54:17 -0500 Subject: [PATCH 05/14] =?UTF-8?q?=F0=9F=94=A7=20chore(core):=20test=20icon?= =?UTF-8?q?s=20[patch=20candidate]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- control_commit/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/control_commit/main.py b/control_commit/main.py index ff0a241..03c8aba 100644 --- a/control_commit/main.py +++ b/control_commit/main.py @@ -138,7 +138,7 @@ def validate_commit_message(commit_msg: str) -> bool: bool: True if valid, False otherwise. """ match = COMMIT_MESSAGE_REGEX.match(commit_msg) - if match: + if match or commit_msg.__contains__("Bump version:"): logger.debug("Commit message structure is valid.") return True else: From 23003af784173f87723671f7766183ee36e7b7e2 Mon Sep 17 00:00:00 2001 From: B Date: Sun, 27 Oct 2024 14:54:34 -0500 Subject: [PATCH 06/14] =?UTF-8?q?=F0=9F=94=A7=20Bump=20version:=201.0.18?= =?UTF-8?q?=20=E2=86=92=201.0.19?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .bumpversion.cfg | 2 +- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index afb2df0..382f9ba 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 1.0.18 +current_version = 1.0.19 commit = True tag = False diff --git a/pyproject.toml b/pyproject.toml index c7aee6c..9a702ed 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "scripts" -version = "1.0.18" +version = "1.0.19" description = "CICD Core Scripts" authors = ["B "] license = "Apache 2.0" From 3c6373a27bc33f6d73e833ec4aa511d423b943e1 Mon Sep 17 00:00:00 2001 From: B Date: Sun, 27 Oct 2024 14:59:43 -0500 Subject: [PATCH 07/14] chore(core): test icons --- control_commit/main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/control_commit/main.py b/control_commit/main.py index 03c8aba..cbc27cf 100644 --- a/control_commit/main.py +++ b/control_commit/main.py @@ -266,4 +266,5 @@ def main() -> None: if __name__ == "__main__": + """""" main() From 8396010d37bff5ab25ea3ff5b61d4a6733e5dcf8 Mon Sep 17 00:00:00 2001 From: B Date: Sun, 27 Oct 2024 15:03:11 -0500 Subject: [PATCH 08/14] =?UTF-8?q?=F0=9F=94=A7=20chore(core):=20test=20icon?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- control_commit/main.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/control_commit/main.py b/control_commit/main.py index cbc27cf..1321f9a 100644 --- a/control_commit/main.py +++ b/control_commit/main.py @@ -10,6 +10,7 @@ import argparse import logging import re +import subprocess import sys from logging.handlers import RotatingFileHandler @@ -170,20 +171,25 @@ def add_icon_to_commit_message(commit_type: str, existing_commit_msg: str) -> st return existing_commit_msg -def write_commit_message(file_path: str, commit_msg: str) -> None: +def amend_commit(new_commit_msg: str) -> None: """ - Writes the updated commit message back to the commit message file. + Amends the current commit with the new commit message. Args: - file_path (str): Path to the commit message file. - commit_msg (str): The updated commit message. + new_commit_msg (str): The new commit message. + + Raises: + subprocess.CalledProcessError: If git amend fails. """ try: - with open(file_path, "w", encoding="utf-8") as file: - file.write(commit_msg + "\n") - logger.debug(f"Commit message written to file: {file_path}") - except Exception as e: - logger.error(f"Error writing to commit message file: {e}") + # Amend the commit with the new commit message + subprocess.run(["git", "commit", "--amend", "-m", new_commit_msg], check=True) + logger.info("Successfully amended the commit with the new commit message.") + logger.info( + "Please perform a push using 'git push' to update the remote repository. Avoid use --force" + ) + except subprocess.CalledProcessError as e: + logger.error(f"Failed to amend the commit: {e}") sys.exit(1) @@ -253,7 +259,7 @@ def main() -> None: updated_commit_msg = add_icon_to_commit_message(commit_type, commit_msg) # Write the updated commit message back to the file - write_commit_message(commit_msg_file, updated_commit_msg) + amend_commit(updated_commit_msg) # Inform the user and abort the commit to allow them to review the amended message logger.info( From a9c91e7eb637b647e5db270946e67e07d50be571 Mon Sep 17 00:00:00 2001 From: B Date: Sun, 27 Oct 2024 15:09:22 -0500 Subject: [PATCH 09/14] feat(core): added commit icons and commit regex validation [minor candidate] --- .pre-commit-config.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7b12f63..5d65829 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -66,14 +66,14 @@ repos: files: ^docker-compose(\.dev|\.prod)?\.yml$ - id: validate-commit name: validate-commit - entry: python control_commit/main.py --log-level=DEBUG + entry: python control_commit/main.py --log-level=INFO always_run: true pass_filenames: false language: system stages: [pre-push] - id: commit-msg-version-check name: commit-msg-version-check - entry: python commit_msg_version_bump/main.py --log-level=DEBUG + entry: python commit_msg_version_bump/main.py --log-level=INFO always_run: true language: system pass_filenames: false @@ -81,13 +81,13 @@ repos: stages: [pre-push] - id: bump-year name: bump-year - entry: python bump_year/main.py + entry: python bump_year/main.py --log-level=INFO always_run: true pass_filenames: false language: system - id: generate-changelog name: generate-changelog - entry: python generate_changelog/main.py + entry: python generate_changelog/main.py --log-level=INFO always_run: true pass_filenames: false language: system From a3a8b282a4e9b511266e49b32fd2dd32f69e2a06 Mon Sep 17 00:00:00 2001 From: B Date: Sun, 27 Oct 2024 15:09:39 -0500 Subject: [PATCH 10/14] =?UTF-8?q?=E2=9C=A8=20Bump=20version:=201.0.19=20?= =?UTF-8?q?=E2=86=92=201.1.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .bumpversion.cfg | 2 +- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 382f9ba..14f4c6f 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 1.0.19 +current_version = 1.1.0 commit = True tag = False diff --git a/pyproject.toml b/pyproject.toml index 9a702ed..e699d35 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "scripts" -version = "1.0.19" +version = "1.1.0" description = "CICD Core Scripts" authors = ["B "] license = "Apache 2.0" From 3e5a722e518ac249b92bb92dde23605520c8d9ae Mon Sep 17 00:00:00 2001 From: B Date: Sun, 27 Oct 2024 15:14:05 -0500 Subject: [PATCH 11/14] feat(core): fix commit icons and commit regex validation [patch candidate] --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5d65829..4d78026 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -66,7 +66,7 @@ repos: files: ^docker-compose(\.dev|\.prod)?\.yml$ - id: validate-commit name: validate-commit - entry: python control_commit/main.py --log-level=INFO + entry: python control_commit/main.py --log-level=DEBUG always_run: true pass_filenames: false language: system From 1f5f3d32eb5b8e473bd2c37e355f4a58405afbb1 Mon Sep 17 00:00:00 2001 From: B Date: Sun, 27 Oct 2024 15:14:27 -0500 Subject: [PATCH 12/14] =?UTF-8?q?=E2=9C=A8=20Bump=20version:=201.1.0=20?= =?UTF-8?q?=E2=86=92=201.1.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .bumpversion.cfg | 2 +- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 14f4c6f..7e64ea2 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 1.1.0 +current_version = 1.1.1 commit = True tag = False diff --git a/pyproject.toml b/pyproject.toml index e699d35..04e1e4e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "scripts" -version = "1.1.0" +version = "1.1.1" description = "CICD Core Scripts" authors = ["B "] license = "Apache 2.0" From 0f11360403d107c3bf490263231a277bf7503f22 Mon Sep 17 00:00:00 2001 From: B Date: Sun, 27 Oct 2024 15:25:58 -0500 Subject: [PATCH 13/14] =?UTF-8?q?=F0=9F=90=9B=20fix(core):=20fix=20commit?= =?UTF-8?q?=20icons=20and=20commit=20regex=20validation=20[patch=20candida?= =?UTF-8?q?te]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commit_msg_version_bump/main.py | 23 ++++++++++++---- control_commit/main.py | 48 +++++++++++++++------------------ 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/commit_msg_version_bump/main.py b/commit_msg_version_bump/main.py index 61a1c0e..d013f03 100644 --- a/commit_msg_version_bump/main.py +++ b/commit_msg_version_bump/main.py @@ -120,6 +120,7 @@ def get_latest_commit_message() -> str: stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, + encoding="utf-8", ).stdout.strip() logger.debug(f"Latest commit message: {message}") return message @@ -201,7 +202,11 @@ def bump_version(part: str) -> None: subprocess.CalledProcessError: If bump2version fails. """ try: - subprocess.run(["bump2version", part], check=True) + subprocess.run( + ["bump2version", part], + check=True, + encoding="utf-8", + ) logger.info(f"Successfully bumped the {part} version.") except subprocess.CalledProcessError as error: logger.error(f"Failed to bump the {part} version: {error}") @@ -216,7 +221,11 @@ def stage_changes(pyproject_path: str = "pyproject.toml") -> None: pyproject_path (str): Path to the file to stage. """ try: - subprocess.run(["git", "add", pyproject_path], check=True) + subprocess.run( + ["git", "add", pyproject_path], + check=True, + encoding="utf-8", + ) logger.debug(f"Staged {pyproject_path} for commit.") except subprocess.CalledProcessError as e: logger.error(f"Failed to stage {pyproject_path}: {e}") @@ -235,10 +244,14 @@ def amend_commit(new_commit_msg: str) -> None: """ try: # Amend the commit with the new commit message - subprocess.run(["git", "commit", "--amend", "-m", new_commit_msg], check=True) + subprocess.run( + ["git", "commit", "--amend", "-m", new_commit_msg], + check=True, + encoding="utf-8", + ) logger.info("Successfully amended the commit with the new version bump.") logger.info( - "Please perform a push using 'git push' to update the remote repository. Avoid use --force" + "Please perform a push using 'git push' to update the remote repository. Avoid using --force" ) except subprocess.CalledProcessError as e: logger.error(f"Failed to amend the commit: {e}") @@ -283,7 +296,7 @@ def main() -> None: amend_commit(updated_commit_msg) logger.info( - "Aborting the current push. Please perform a push using 'git push'. Avoid use --force" + "Aborting the current push. Please perform a push using 'git push'. Avoid using --force" ) sys.exit(1) else: diff --git a/control_commit/main.py b/control_commit/main.py index 1321f9a..42e17e0 100644 --- a/control_commit/main.py +++ b/control_commit/main.py @@ -240,35 +240,29 @@ def main() -> None: if not validate_commit_message(commit_msg): logger.error("Commit message validation failed. Aborting commit.") sys.exit(1) + logger.debug("Commit message does not contain square brackets. Proceeding to add icon.") - # Check if the commit message contains square brackets - if not has_square_brackets(commit_msg): - logger.debug("Commit message does not contain square brackets. Proceeding to add icon.") - - # Determine the type of commit to get the appropriate icon - type_match = COMMIT_TYPE_REGEX.match(commit_msg) - if type_match: - commit_type = type_match.group("type") - logger.debug(f"Detected commit type: {commit_type}") - else: - commit_type = "chore" # Default to 'chore' if no type is found - logger.debug("No commit type detected. Defaulting to 'chore'.") - sys.exit(1) - - # Add the icon to the existing commit message - updated_commit_msg = add_icon_to_commit_message(commit_type, commit_msg) - - # Write the updated commit message back to the file - amend_commit(updated_commit_msg) - - # Inform the user and abort the commit to allow them to review the amended message - logger.info( - "Commit message has been updated with an icon. Please review and finalize the commit." - ) - sys.exit(1) + # Determine the type of commit to get the appropriate icon + type_match = COMMIT_TYPE_REGEX.match(commit_msg) + if type_match: + commit_type = type_match.group("type") + logger.debug(f"Detected commit type: {commit_type}") else: - logger.debug("Commit message contains square brackets. No icon added.") - sys.exit(0) # Valid commit message without needing an icon; proceed + commit_type = "chore" # Default to 'chore' if no type is found + logger.debug("No commit type detected. Defaulting to 'chore'.") + sys.exit(1) + + # Add the icon to the existing commit message + updated_commit_msg = add_icon_to_commit_message(commit_type, commit_msg) + + # Write the updated commit message back to the file + amend_commit(updated_commit_msg) + + # Inform the user and abort the commit to allow them to review the amended message + logger.info( + "Commit message has been updated with an icon. Please review and finalize the commit." + ) + sys.exit(1) if __name__ == "__main__": From 29d20d6fa2b4b6d04e39ed376e368333ba4c7ae6 Mon Sep 17 00:00:00 2001 From: B Date: Sun, 27 Oct 2024 15:26:40 -0500 Subject: [PATCH 14/14] =?UTF-8?q?=F0=9F=94=A7=20Bump=20version:=201.1.1=20?= =?UTF-8?q?=E2=86=92=201.1.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .bumpversion.cfg | 2 +- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 7e64ea2..99ef481 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 1.1.1 +current_version = 1.1.2 commit = True tag = False diff --git a/pyproject.toml b/pyproject.toml index 04e1e4e..196513a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "scripts" -version = "1.1.1" +version = "1.1.2" description = "CICD Core Scripts" authors = ["B "] license = "Apache 2.0"