hooks/commit-msg
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import sys
from subprocess import check_output
# Collect the parameters
commit_msg_filepath = sys.argv[1]
# Figure out which branch we're on
branch = check_output([
'git', 'symbolic-ref', '--short',
'HEAD',
]).strip().decode('utf-8')
print(r"commit-msg: On branch '{}'".format(branch))
regex = re.compile('^feature|^bugfix')
# Check the commit message if we're on an issue branch
if re.match(regex, branch):
print("commit-msg: Oh hey, it's an issue branch.")
# Matches any unique issue code
pattern = re.compile(r'(^feature|^bugfix)\/([A-Z]{3,5}-[0-9]+)')
issue = re.search(pattern, branch).group(2) # Extract issue code
print('Issue number : {}.'.format(issue))
with open(commit_msg_filepath, 'r') as f:
try:
from jira import JIRA
options = {'server': 'https://home.nabla.mobi/jira'}
jira = JIRA(options, verify=False, basic_auth=('test', 'TODO'))
issue_to_check = jira.issue(issue)
print(issue_to_check.fields.project.key) # 'JRA'
print(issue_to_check.fields.issuetype.name) # 'New Feature'
print(issue_to_check.fields.reporter.displayName)
required_message = '{} : {} by {}'.format(
issue_to_check, issue_to_check.fields.issuetype.name, issue_to_check.fields.reporter.displayName,
)
except: # noqa: ignore=E722
print('Oops! JIRA is failing. Switch to manual...')
required_message = '%s' % issue
content = f.read()
if not content.startswith(required_message):
print(
'commit-msg: ERROR! The commit message must start with {}'.format(
required_message,
),
)
sys.exit(1)