AlbanAndrieu/nabla-servers-bower

View on GitHub
hooks/prepare-commit-msg

Summary

Maintainability
Test Coverage
#!/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]
if len(sys.argv) > 2:
    commit_type = sys.argv[2]
else:
    commit_type = ''
if len(sys.argv) > 3:
    commit_hash = sys.argv[3]
else:
    commit_hash = ''
print(
    'prepare-commit-msg: File: {}\nType: {}\nHash: {}'.format(
        commit_msg_filepath,
        commit_type, commit_hash,
    ),
)
# Figure out which branch we're on
branch = check_output([
    'git', 'symbolic-ref', '--short',
    'HEAD',
]).strip().decode('utf-8')
print(r"prepare-commit-msg: On branch '{}'".format(branch))
regex = re.compile('^feature|^bugfix')
# Populate the commit message with the issue #, if there is one
if re.match(regex, branch):
    print("prepare-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 : %s.') % issue
    print('Issue number : {}.'.format(issue))

    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

    with open(commit_msg_filepath, 'r+') as f:
        content = f.read()
        f.seek(0, 0)
        f.write('{} : {}'.format(required_message, content))