Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,38 @@ By default it's ``'{ticket} {commit_msg}``, where ``ticket`` is replaced with th
Pass ``--mode=`` or update ``args: [--mode=regex_match]`` in your .yaml file to extract ticket by the regex rather than relying on branch name convention.
With this mode you can also make use of ``{tickets}`` placeholder in ``format`` argument value to put multiple comma-separated tickets in the commit message in case your branch contains more than one ticket.

Pass ``--capitalize`` or ``--capitalize=N`` to capitalize the first ``N`` characters of the resulting commit message. When passed without a value, defaults to capitalizing the first character (``--capitalize=1``).

Pass ``--to_trailer`` to append the ticket(s) as a `git trailer <https://git-scm.com/docs/git-interpret-trailers>`_ at the bottom of the commit message instead of modifying the subject line. For example, committing on branch ``JIRA-1234_awesome_feature`` will append::

Refs: JIRA-1234

Pass ``--trailer_token=`` to customise the trailer key written by ``--to_trailer``. Defaults to ``Refs``. For example, ``--trailer_token=Fixes`` produces::

Fixes: JIRA-1234

It is best used along with pre-commit_. You can use it along with pre-commit by adding the following hook in your ``.pre-commit-config.yaml`` file.

::

repos:
- repo: https://github.com/milin/giticket
rev: v1.4
rev: v1.92
hooks:
- id: giticket
args: ['--regex=PROJ-[0-9]', '--format={ticket} {commit_msg}'] # Optional

Alternatively, to append tickets as a trailer instead of modifying the subject line:

::

repos:
- repo: https://github.com/milin/giticket
rev: v1.92
hooks:
- id: giticket
args: ['--to_trailer', '--trailer_token=Refs'] # --trailer_token is optional, defaults to Refs


You need to have precommit setup to use this hook.
--------------------------------------------------
Expand Down
7 changes: 4 additions & 3 deletions giticket/giticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, to_trailer=False):
def update_commit_message(filename, regex, mode, format_string, conventionalcommits=False, capitalize_spaces=0, to_trailer=False, trailer_token='Refs'):
with io.open(filename, 'r+') as fd:
contents = fd.readlines()
commit_msg = contents[0].rstrip('\r\n')
Expand All @@ -37,7 +37,7 @@ def update_commit_message(filename, regex, mode, format_string, conventionalcomm
tickets = [t.strip() for t in tickets]

if to_trailer:
trailer = 'Refs: {tickets}\n'.format(tickets=', '.join(tickets))
trailer = '{token}: {tickets}\n'.format(token=trailer_token, tickets=', '.join(tickets))
if contents and not contents[-1].endswith('\n'):
contents[-1] += '\n'
if not contents or contents[-1].strip() != '':
Expand Down Expand Up @@ -99,6 +99,7 @@ def main(argv=None):
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('--trailer_token', nargs='?', default='Refs')
parser.add_argument('--regex')
parser.add_argument('--format', nargs='?')
parser.add_argument('--mode', nargs='?', const=underscore_split_mode,
Expand All @@ -110,7 +111,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, args.to_trailer)
update_commit_message(args.filenames[0], regex, args.mode, format_string, args.conventionalcommits, args.capitalize, args.to_trailer, args.trailer_token)


if __name__ == '__main__':
Expand Down
18 changes: 16 additions & 2 deletions tests/test_giticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ def test_update_commit_message_to_trailer_multiple_tickets(mock_branch_name, tmp
assert path.read() == "Subject line\n\nRefs: JIRA-1234, JIRA-5678\n"


@mock.patch(TESTING_MODULE + '.get_branch_name')
def test_update_commit_message_to_trailer_custom_token(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, trailer_token='Fixes')
assert path.read() == "Subject line\n\nFixes: JIRA-1234\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"
Expand Down Expand Up @@ -280,14 +291,16 @@ def test_main(mock_update_commit_message, mock_argparse):
mock_args.conventionalcommits = True
mock_args.capitalize = 0
mock_args.to_trailer = False
mock_args.trailer_token = 'Refs'
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,
False)
False,
'Refs')


@mock.patch(TESTING_MODULE + '.update_commit_message')
Expand All @@ -298,7 +311,8 @@ def test_main_to_trailer_only_is_allowed(mock_update_commit_message):
'{ticket} {commit_msg}',
False,
0,
True)
True,
'Refs')


def test_main_errors_without_format_or_conventionalcommits_or_to_trailer():
Expand Down
Loading