Last active
February 1, 2017 21:09
-
-
Save cgiacomi/607354796e42aa3ef2f1bc27740c3b46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
exec < /dev/tty | |
./.git/hooks/commit-msg.py $1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
class bcolors: | |
HEADER = '\033[95m' | |
OKBLUE = '\033[94m' | |
OKGREEN = '\033[92m' | |
WARNING = '\033[93m' | |
FAIL = '\033[91m' | |
ENDC = '\033[0m' | |
BOLD = '\033[1m' | |
UNDERLINE = '\033[4m' | |
#http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python | |
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 bcolors.FAIL + 'ERROR: Missing Jira number in commmit message.' + bcolors.ENDC | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment