-
Notifications
You must be signed in to change notification settings - Fork 16
/
giticket.py
78 lines (62 loc) · 2.36 KB
/
giticket.py
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
73
74
75
76
77
78
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals
import argparse
import io
import re
import subprocess
import sys
import six
underscore_split_mode = 'underscore_split'
regex_match_mode = 'regex_match'
def update_commit_message(filename, regex, mode, format_string):
with io.open(filename, 'r+') as fd:
contents = fd.readlines()
commit_msg = contents[0].rstrip('\r\n')
# Check if we can grab ticket info from branch name.
branch = get_branch_name()
# Bail if commit message already contains tickets
if any(re.search(regex, content) for content in contents):
return
tickets = re.findall(regex, branch)
if tickets:
if mode == underscore_split_mode:
tickets = [branch.split(six.text_type('_'))[0]]
tickets = [t.strip() for t in tickets]
new_commit_msg = format_string.format(
ticket=tickets[0], tickets=', '.join(tickets),
commit_msg=commit_msg
)
contents[0] = six.text_type(new_commit_msg + "\n")
fd.seek(0)
fd.writelines(contents)
fd.truncate()
def get_branch_name():
# Only git support for right now.
return subprocess.check_output(
[
'git',
'rev-parse',
'--abbrev-ref',
'HEAD',
],
).decode('UTF-8')
def main(argv=None):
"""This hook saves developers time by prepending ticket numbers to commit-msgs.
For this to work the following two conditions must be met:
- The ticket format regex specified must match.
- The branch name format must be <ticket number>_<rest of the branch name>
"""
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='+')
parser.add_argument('--regex')
parser.add_argument('--format')
parser.add_argument('--mode', nargs='?', const=underscore_split_mode,
default=underscore_split_mode,
choices=[underscore_split_mode, regex_match_mode])
args = parser.parse_args(argv)
regex = args.regex or r'[A-Z]+-\d+' # noqa
format_string = args.format or '{ticket} {commit_msg}' # noqa
update_commit_message(args.filenames[0], regex, args.mode, format_string)
if __name__ == '__main__':
sys.exit(main())