-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcommit-msg.py
More file actions
executable file
·87 lines (74 loc) · 2.73 KB
/
commit-msg.py
File metadata and controls
executable file
·87 lines (74 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
import re
import subprocess
import sys
OK_C = "\033[92m"
WARN_C = "\033[93m"
FAIL_C = "\033[91m"
END_C = "\033[0m"
JIRA_REGEX = "(SPII|PCRM|NEMS|KF|IGWAY|NRL|APM|APMSPII|SPY3|SPINECLI)\-(\d+)" # Example match: SPII-12345
FEATURE_REGEX = (
"feature\/\w+\-(\w+\-\d+)\-" # Example match: feature/das-SPII-12345-foo
)
OLD_FEATURE_REGEX = "feature\/\w+\-(\d+)\-" # Example match: feature/das-12345-foo
sys.stdin = open("/dev/tty") # Ensure that input can be taken by hook
MSG_FILE = sys.argv[1] # commit-msg hook provides file name as first arg
def append_to_commit_message(jira_id):
print("Appended {0} to commit message.".format(jira_id))
with open(MSG_FILE, "a") as f:
f.write("{0}".format(jira_id))
def prepend_to_commit_message(jira_id):
print("Prepended {} to commit message.".format(jira_id))
with open(MSG_FILE, "r") as f:
text = f.read()
text = "{} {}".format(jira_id, text)
with open(MSG_FILE, "w") as f:
f.write(text)
# Check if JIRA ID already exists in commit message
print("I am doing something")
with open(MSG_FILE, "r") as f:
msg = ""
for line in f:
if not line.startswith("#"):
msg += line.rstrip()
if msg == "":
# No commit message let Git fail the commot
sys.exit(0)
commitMatch = re.search(JIRA_REGEX, msg)
if commitMatch:
print(
"JIRA {0}{1}-{2}{3} referenced by commit message.".format(
OK_C, commitMatch.group(1), commitMatch.group(2), END_C
)
)
sys.exit(0)
print(WARN_C + "No JIRA ID referenced by commit message." + END_C)
# Check if KEY-n+ can be used from branch name
branch = subprocess.check_output("git rev-parse --abbrev-ref HEAD", shell=True)
branchMatch = re.search(FEATURE_REGEX, str(branch))
if branchMatch:
response = input(
"Use {0}{1}{2} from branch name? (y/n) ".format(
OK_C, branchMatch.group(1), END_C
)
)
if response.strip().lower().startswith("y"):
prepend_to_commit_message(branchMatch.group(1))
sys.exit(0)
oldBranchMatch = re.search(OLD_FEATURE_REGEX, str(branch))
if oldBranchMatch:
jira_id = "SPII-{}".format(oldBranchMatch.group(1))
response = input(
"Use {0}{1}{2} from branch name? (y/n) ".format(OK_C, jira_id, END_C)
)
if response.strip().lower().startswith("y"):
prepend_to_commit_message(jira_id)
sys.exit(0)
# All else fails ask what they want to use
jira_id = input("Please enter the JIRA ID (for example SPII-1234 or NEMS-123): ")
if not re.search(JIRA_REGEX, jira_id):
print(FAIL_C + "Did not provide valid JIRA ID. Commit Aborted." + END_C)
sys.exit(1)
else:
prepend_to_commit_message(jira_id)
sys.exit(0)