diff --git a/commit_linter/hooks/commit-msg--jira b/commit_linter/hooks/commit-msg--jira new file mode 100644 index 0000000..f16cb65 --- /dev/null +++ b/commit_linter/hooks/commit-msg--jira @@ -0,0 +1,66 @@ +#!/usr/bin/env python +import re, sys, os + +examples = """fix: navbar not responsive on mobile +JIRA-1/test: prepared test cases for user authentication +JIRA-001/chore: moved to semantic versioning +JIRA-999/feat(auth): added social login using twitter +""" + +types = """JIRA-0/build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm) +JIRA-0/ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs) +JIRA-0/docs: Documentation only changes +JIRA-0/feat: A new feature +JIRA-0/fix: A bug fix +JIRA-0/perf: A code change that improves performance +JIRA-0/refactor: A code change that neither fixes a bug nor adds a feature +JIRA-0/style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc) +JIRA-0/test: Adding missing tests or correcting existing tests +""" + + +def main(): + emojies = { + "new": ":tada:", + "build": ":construction:", + "ci": ":green_heart:", + "docs": ":books:", + "feat": ":sparkles:", + "fix": ":bug:", + "perf": ":racehorse:", + "refactor": ":hammer:", + "style": ":umbrella:", + "test": ":heavy_check_mark:", + "chore": ":arrow_up:", + "revert": ":rewind:", + } + pattern = r"\w+?-\d+?/(new|build|ci|docs|feat|fix|perf|refactor|style|test|chore|revert)(\([\w\-]+\))?:\s.*" + filename = sys.argv[1] + ss = open(filename, "r").read() + m = re.match(pattern, ss) + if m == None: + print("\nCOMMIT FAILED!") + print( + "\nPlease enter commit message in the conventional format and try to commit again. Examples:" + ) + print("\n" + examples) + print("\nCHECK COMMIT CONVENTIONS BELOW!\n" + types) + print( + "\nCheck the Conventional Commits at https://www.conventionalcommits.org/\n" + ) + sys.exit(1) + else: + commitPrefix = ss.split(":")[0] + commitPostfix = ss.split(":", 1)[1] + if "(" in commitPrefix: + commitEmojie = emojies.get(commitPrefix.split("(")[0].split("/")[1]) + newCommit = open(filename, "w") + newCommit.write(commitPrefix + ": " + commitEmojie + commitPostfix) + else: + commitEmojie = emojies.get(commitPrefix.split("/")[1]) + newCommit = open(filename, "w") + newCommit.write(commitPrefix + ": " + commitEmojie + commitPostfix) + + +if __name__ == "__main__": + main() diff --git a/commit_linter/main.py b/commit_linter/main.py index d8ad403..db260d5 100644 --- a/commit_linter/main.py +++ b/commit_linter/main.py @@ -5,15 +5,17 @@ import pkg_resources import sys -def enforce(): +def enforce(commit_type: str): githooks_path = pkg_resources.resource_filename( - __name__, "hooks/commit-msg") + __name__, f"hooks/commit-msg{commit_type}") user_repository = os.path.join(os.getcwd(), ".git/hooks") - if os.path.exists(user_repository+"/commit-msg"): + if not os.path.exists(githooks_path): + print("this config not support with commit-msg hook") + elif os.path.exists(user_repository+"/commit-msg"): print("your repo already enforced with commit-msg hook") else: - shutil.copy(githooks_path, user_repository) + shutil.copy(githooks_path, user_repository + '/commit-msg') st = os.stat(user_repository + '/commit-msg') os.chmod(user_repository + '/commit-msg', st.st_mode | stat.S_IEXEC) print("Your Repo has been enforced with git commit-msg hook") @@ -42,7 +44,11 @@ def main(): else: if len(sys.argv) > 1 : if sys.argv[1] == "install": - enforce() + if len(sys.argv) > 2 : + enforce(commit_type=sys.argv[2]) + else: + enforce(commit_type="") + elif sys.argv[1] == "remove": unenforce() else: @@ -51,7 +57,7 @@ def main(): print("commit-linter remove ==> will remove hooks from your repo") else: print("enforcing by default") - enforce() + enforce(commit_type="") if __name__ == "__main__":