diff --git a/giticket/giticket.py b/giticket/giticket.py index 17f57f6..a765c77 100644 --- a/giticket/giticket.py +++ b/giticket/giticket.py @@ -19,7 +19,7 @@ def capitalize(text, capitalize_spaces): return text[:capitalize_spaces].upper() + text[capitalize_spaces:] if text else text -def update_commit_message(filename, regex, mode, format_string, conventionalcommits=False, capitalize_spaces=0): +def update_commit_message(filename, regex, mode, format_string, conventionalcommits=False, capitalize_spaces=0, to_trailer=False): with io.open(filename, 'r+') as fd: contents = fd.readlines() commit_msg = contents[0].rstrip('\r\n') @@ -36,6 +36,18 @@ def update_commit_message(filename, regex, mode, format_string, conventionalcomm tickets = [branch.split(six.text_type('_'))[0]] tickets = [t.strip() for t in tickets] + if to_trailer: + trailer = 'Refs: {tickets}\n'.format(tickets=', '.join(tickets)) + if contents and not contents[-1].endswith('\n'): + contents[-1] += '\n' + if not contents or contents[-1].strip() != '': + contents.append('\n') + contents.append(trailer) + fd.seek(0) + fd.writelines(contents) + fd.truncate() + return + if conventionalcommits and (match := re.match(conventionalcommit_regex, commit_msg)): # If the commit message matches the Conventional Commits spec, we can use the captured groups. type = match.group('type') @@ -86,6 +98,7 @@ def main(argv=None): parser.add_argument('filenames', nargs='+') parser.add_argument('--conventionalcommits', action='store_true') parser.add_argument('--capitalize', nargs='?', const=1, type=int, default=0) + parser.add_argument('--to_trailer', action='store_true') parser.add_argument('--regex') parser.add_argument('--format', nargs='?') parser.add_argument('--mode', nargs='?', const=underscore_split_mode, @@ -97,7 +110,7 @@ def main(argv=None): return 1 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, args.conventionalcommits, args.capitalize) + update_commit_message(args.filenames[0], regex, args.mode, format_string, args.conventionalcommits, args.capitalize, args.to_trailer) if __name__ == '__main__': diff --git a/tests/test_giticket.py b/tests/test_giticket.py index 9d75268..e3ca259 100644 --- a/tests/test_giticket.py +++ b/tests/test_giticket.py @@ -181,6 +181,39 @@ def test_update_commit_message_capitalize_conventionalcommits(mock_branch_name, assert path.read() == "feat(JIRA-5678): add new feature\n" +@mock.patch(TESTING_MODULE + '.get_branch_name') +def test_update_commit_message_to_trailer(mock_branch_name, tmpdir): + mock_branch_name.return_value = "JIRA-1234_new_feature" + path = tmpdir.join('file.txt') + path.write("Subject line\n\nBody text.") + update_commit_message(six.text_type(path), r'[A-Z]+-\d+', + 'regex_match', '{ticket}: {commit_msg}', + to_trailer=True) + assert path.read() == "Subject line\n\nBody text.\n\nRefs: JIRA-1234\n" + + +@mock.patch(TESTING_MODULE + '.get_branch_name') +def test_update_commit_message_to_trailer_subject_only(mock_branch_name, tmpdir): + mock_branch_name.return_value = "JIRA-1234_new_feature" + path = tmpdir.join('file.txt') + path.write("Subject line") + update_commit_message(six.text_type(path), r'[A-Z]+-\d+', + 'regex_match', '{ticket}: {commit_msg}', + to_trailer=True) + assert path.read() == "Subject line\n\nRefs: JIRA-1234\n" + + +@mock.patch(TESTING_MODULE + '.get_branch_name') +def test_update_commit_message_to_trailer_multiple_tickets(mock_branch_name, tmpdir): + mock_branch_name.return_value = "JIRA-1234-JIRA-5678" + path = tmpdir.join('file.txt') + path.write("Subject line") + update_commit_message(six.text_type(path), r'[A-Z]+-\d+', + 'regex_match', '{ticket}: {commit_msg}', + to_trailer=True) + assert path.read() == "Subject line\n\nRefs: JIRA-1234, JIRA-5678\n" + + @mock.patch(TESTING_MODULE + '.get_branch_name') def test_update_commit_message_capitalize_disabled_by_default(mock_branch_name, tmpdir): mock_branch_name.return_value = "JIRA-1234_new_feature" @@ -246,10 +279,12 @@ def test_main(mock_update_commit_message, mock_argparse): mock_args.mode = 'underscore_split' mock_args.conventionalcommits = True mock_args.capitalize = 0 + mock_args.to_trailer = False mock_argparse.ArgumentParser.return_value.parse_args.return_value = mock_args main() mock_update_commit_message.assert_called_once_with('foo.txt', r'[A-Z]+-\d+', 'underscore_split', '{ticket} {commit_msg}', True, - 0) + 0, + False)