Skip to content

Commit 7f9b357

Browse files
authored
Merge pull request #38 from milin/optionally_capitalize
Add option to add tickets to trailer.
2 parents 1c1db80 + a703234 commit 7f9b357

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

giticket/giticket.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def capitalize(text, capitalize_spaces):
1919
return text[:capitalize_spaces].upper() + text[capitalize_spaces:] if text else text
2020

2121

22-
def update_commit_message(filename, regex, mode, format_string, conventionalcommits=False, capitalize_spaces=0):
22+
def update_commit_message(filename, regex, mode, format_string, conventionalcommits=False, capitalize_spaces=0, to_trailer=False):
2323
with io.open(filename, 'r+') as fd:
2424
contents = fd.readlines()
2525
commit_msg = contents[0].rstrip('\r\n')
@@ -36,6 +36,18 @@ def update_commit_message(filename, regex, mode, format_string, conventionalcomm
3636
tickets = [branch.split(six.text_type('_'))[0]]
3737
tickets = [t.strip() for t in tickets]
3838

39+
if to_trailer:
40+
trailer = 'Refs: {tickets}\n'.format(tickets=', '.join(tickets))
41+
if contents and not contents[-1].endswith('\n'):
42+
contents[-1] += '\n'
43+
if not contents or contents[-1].strip() != '':
44+
contents.append('\n')
45+
contents.append(trailer)
46+
fd.seek(0)
47+
fd.writelines(contents)
48+
fd.truncate()
49+
return
50+
3951
if conventionalcommits and (match := re.match(conventionalcommit_regex, commit_msg)):
4052
# If the commit message matches the Conventional Commits spec, we can use the captured groups.
4153
type = match.group('type')
@@ -86,6 +98,7 @@ def main(argv=None):
8698
parser.add_argument('filenames', nargs='+')
8799
parser.add_argument('--conventionalcommits', action='store_true')
88100
parser.add_argument('--capitalize', nargs='?', const=1, type=int, default=0)
101+
parser.add_argument('--to_trailer', action='store_true')
89102
parser.add_argument('--regex')
90103
parser.add_argument('--format', nargs='?')
91104
parser.add_argument('--mode', nargs='?', const=underscore_split_mode,
@@ -97,7 +110,7 @@ def main(argv=None):
97110
return 1
98111
regex = args.regex or r'[A-Z]+-\d+' # noqa
99112
format_string = args.format or '{ticket} {commit_msg}' # noqa
100-
update_commit_message(args.filenames[0], regex, args.mode, format_string, args.conventionalcommits, args.capitalize)
113+
update_commit_message(args.filenames[0], regex, args.mode, format_string, args.conventionalcommits, args.capitalize, args.to_trailer)
101114

102115

103116
if __name__ == '__main__':

tests/test_giticket.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,39 @@ def test_update_commit_message_capitalize_conventionalcommits(mock_branch_name,
181181
assert path.read() == "feat(JIRA-5678): add new feature\n"
182182

183183

184+
@mock.patch(TESTING_MODULE + '.get_branch_name')
185+
def test_update_commit_message_to_trailer(mock_branch_name, tmpdir):
186+
mock_branch_name.return_value = "JIRA-1234_new_feature"
187+
path = tmpdir.join('file.txt')
188+
path.write("Subject line\n\nBody text.")
189+
update_commit_message(six.text_type(path), r'[A-Z]+-\d+',
190+
'regex_match', '{ticket}: {commit_msg}',
191+
to_trailer=True)
192+
assert path.read() == "Subject line\n\nBody text.\n\nRefs: JIRA-1234\n"
193+
194+
195+
@mock.patch(TESTING_MODULE + '.get_branch_name')
196+
def test_update_commit_message_to_trailer_subject_only(mock_branch_name, tmpdir):
197+
mock_branch_name.return_value = "JIRA-1234_new_feature"
198+
path = tmpdir.join('file.txt')
199+
path.write("Subject line")
200+
update_commit_message(six.text_type(path), r'[A-Z]+-\d+',
201+
'regex_match', '{ticket}: {commit_msg}',
202+
to_trailer=True)
203+
assert path.read() == "Subject line\n\nRefs: JIRA-1234\n"
204+
205+
206+
@mock.patch(TESTING_MODULE + '.get_branch_name')
207+
def test_update_commit_message_to_trailer_multiple_tickets(mock_branch_name, tmpdir):
208+
mock_branch_name.return_value = "JIRA-1234-JIRA-5678"
209+
path = tmpdir.join('file.txt')
210+
path.write("Subject line")
211+
update_commit_message(six.text_type(path), r'[A-Z]+-\d+',
212+
'regex_match', '{ticket}: {commit_msg}',
213+
to_trailer=True)
214+
assert path.read() == "Subject line\n\nRefs: JIRA-1234, JIRA-5678\n"
215+
216+
184217
@mock.patch(TESTING_MODULE + '.get_branch_name')
185218
def test_update_commit_message_capitalize_disabled_by_default(mock_branch_name, tmpdir):
186219
mock_branch_name.return_value = "JIRA-1234_new_feature"
@@ -246,10 +279,12 @@ def test_main(mock_update_commit_message, mock_argparse):
246279
mock_args.mode = 'underscore_split'
247280
mock_args.conventionalcommits = True
248281
mock_args.capitalize = 0
282+
mock_args.to_trailer = False
249283
mock_argparse.ArgumentParser.return_value.parse_args.return_value = mock_args
250284
main()
251285
mock_update_commit_message.assert_called_once_with('foo.txt', r'[A-Z]+-\d+',
252286
'underscore_split',
253287
'{ticket} {commit_msg}',
254288
True,
255-
0)
289+
0,
290+
False)

0 commit comments

Comments
 (0)