Skip to content
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

feat: add jira ticket in commit #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions commit_linter/hooks/commit-msg--jira
Original file line number Diff line number Diff line change
@@ -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()
18 changes: 12 additions & 6 deletions commit_linter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand All @@ -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__":
Expand Down