-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathjira-commit-msg.py
More file actions
72 lines (51 loc) · 1.51 KB
/
jira-commit-msg.py
File metadata and controls
72 lines (51 loc) · 1.51 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
#!/usr/bin/python
import sys
import re
import subprocess
MESSAGE_REGEX = '^DDC-[\d]{4}\. [\w\d .,:;+]*\.$'
BRANCHNAME_REGEX = '/(DDC-[\d]{4})-' #should contain a capturing group
def current_branch_name():
"""Gets the current GIT branch name.
Returns:
string: The current branch name.
"""
return subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
def get_jira_issue_hint(branch_name):
"""Extracts the Jira issue number from the branch name.
Args:
branch_name (str): The branch name to parse.
Returns:
string: The Jira issue number, or sample issue number.
"""
match = re.findall(BRANCHNAME_REGEX, branch_name)
if match and match[0]:
return match[0]
return 'DDC-XXXX'
def valid_commit_message(message):
"""Function to validate the commit message.
Args:
message (str): The message to validate.
Returns:
bool: True for valid messages, False otherwise.
"""
if not re.match(MESSAGE_REGEX, message):
name = current_branch_name()
issue_number = get_jira_issue_hint(name)
print 'ERROR: Missing Jira number in commmit message.'
print 'Hint: {0}. Commit message.'.format(issue_number)
return False
print 'Commit message is valid.'
return True
def main():
"""Main function."""
message_file = sys.argv[1]
try:
txt_file = open(message_file, 'r')
commit_message = txt_file.read()
finally:
txt_file.close()
if not valid_commit_message(commit_message):
sys.exit(1)
sys.exit(0)
if __name__ == "__main__":
main()