Skip to content

Instantly share code, notes, and snippets.

@nestoralonso
Created August 7, 2018 15:34
Show Gist options
  • Save nestoralonso/47c96f4639a5e1059a23e9b2cbb56a21 to your computer and use it in GitHub Desktop.
Save nestoralonso/47c96f4639a5e1059a23e9b2cbb56a21 to your computer and use it in GitHub Desktop.
prepare the commit log message using the JIRA Ticket code
#!/usr/bin/env python3
#
# prepare the commit log message using the JIRA Ticket code.
#
# Given that the your branch is called something like bugfix/PROJETO-21012
# Then your commit message will look like
#
# [PROJETO-21012] Fixes the bug
#
import subprocess
import re
import sys
b_name = subprocess.Popen("git rev-parse --abbrev-ref HEAD", shell=True, stdout=subprocess.PIPE).stdout.read().decode("utf-8")
print('b_name=', b_name)
ticket_res = re.match("[^/]+/([A-Z]+-[0-9]+).*$", b_name)
print('ticket_res=', ticket_res)
ticket_name = ''
if ticket_res:
ticket_name = ticket_res.groups()[0]
if not ticket_name:
print('Ticket name not found')
exit(0)
ticket_id = '[%s]' % ticket_name
print('ticket_id', ticket_id)
with open(sys.argv[1], 'r') as f:
prev_contents = f.read()
has_ticket_id = prev_contents.strip().startswith(ticket_id)
print(has_ticket_id )
# print('ticket_name=', ticket_name)
if not has_ticket_id:
with open(sys.argv[1], 'w') as f:
f.write('{} '.format(ticket_id))
f.write(prev_contents + '\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment