Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new line char support for format #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ build/
dist/
*.egg-info/
.tox/
__pycache__
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Contributors
* kir0ul - https://github.com/kir0ul
* Daniil Penkin - https://github.com/detouched
* Joseph S. Tate - https://github.com/josephtate
* Ivan Studinsky - https://github.com/vanya909
31 changes: 20 additions & 11 deletions giticket/giticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@

import six

underscore_split_mode = 'underscore_split'
regex_match_mode = 'regex_match'
UNDERSCORE_SPLIT_MODE = 'underscore_split'
REGEX_MATCH_MODE = 'regex_match'


def update_commit_message(filename, regex, mode, format_string):
# All escape sequences will be escaped with double backslash, so need to
# replace escaped new-line character with normal one
format_string = format_string.replace('\\n', '\n')

with io.open(filename, 'r+') as fd:
contents = fd.readlines()
commit_msg = contents[0].rstrip('\r\n')
Expand All @@ -27,7 +31,7 @@ def update_commit_message(filename, regex, mode, format_string):

tickets = re.findall(regex, branch)
if tickets:
if mode == underscore_split_mode:
if mode == UNDERSCORE_SPLIT_MODE:
tickets = [branch.split(six.text_type('_'))[0]]
tickets = [t.strip() for t in tickets]

Expand All @@ -54,21 +58,26 @@ def get_branch_name():
).decode('UTF-8')


def get_args(argv=None):
"""Parse and return all args passed to script."""
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])
return parser.parse_args(argv)


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)
args = get_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)
Expand Down
Empty file added tests/__init__.py
Empty file.
45 changes: 32 additions & 13 deletions tests/test_giticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import mock
import pytest
import six

from giticket.giticket import get_branch_name
from giticket.giticket import main
from giticket.giticket import update_commit_message
from giticket.giticket import (
get_branch_name,
main,
update_commit_message,
REGEX_MATCH_MODE,
UNDERSCORE_SPLIT_MODE,
)

TESTING_MODULE = 'giticket.giticket'

Expand All @@ -25,7 +28,7 @@ def test_update_commit_message_no_modification(mock_branch_name, msg, tmpdir):
path = tmpdir.join('file.txt')
path.write(msg)
update_commit_message(six.text_type(path), r'[A-Z]+-\d+',
'underscore_split', '{ticket} {commit_msg}')
UNDERSCORE_SPLIT_MODE, '{ticket} {commit_msg}')
# Message should remain intact as it contains some ticket
assert path.read() == msg

Expand All @@ -44,7 +47,7 @@ def test_update_commit_message_underscore_split_mode(mock_branch_name,
path = tmpdir.join('file.txt')
path.write(COMMIT_MESSAGE)
update_commit_message(six.text_type(path), r'[A-Z]+-\d+',
'underscore_split', '{ticket}: {commit_msg}')
UNDERSCORE_SPLIT_MODE, '{ticket}: {commit_msg}')
assert path.read() == '{expected_ticket}: {message}'.format(
expected_ticket=test_data[1], message=COMMIT_MESSAGE
)
Expand All @@ -66,7 +69,7 @@ def test_update_commit_message_regex_match_mode(mock_branch_name,
path = tmpdir.join('file.txt')
path.write(COMMIT_MESSAGE)
update_commit_message(six.text_type(path), r'[A-Z]+-\d+',
'regex_match', '{ticket}: {commit_msg}')
REGEX_MATCH_MODE, '{ticket}: {commit_msg}')
assert path.read() == 'JIRA-1234: {message}'.format(message=COMMIT_MESSAGE)


Expand All @@ -83,7 +86,7 @@ def test_update_commit_message_multiple_ticket_first_selected(mock_branch_name,
path = tmpdir.join('file.txt')
path.write(COMMIT_MESSAGE)
update_commit_message(six.text_type(path), r'[A-Z]+-\d+',
'regex_match', '{ticket}: {commit_msg}')
REGEX_MATCH_MODE, '{ticket}: {commit_msg}')
assert path.read() == '{expected_ticket}: {message}'.format(
expected_ticket=test_data[1], message=COMMIT_MESSAGE
)
Expand All @@ -101,7 +104,7 @@ def test_update_commit_message_multiple_ticket_all_selected(mock_branch_name,
path = tmpdir.join('file.txt')
path.write(COMMIT_MESSAGE)
update_commit_message(six.text_type(path), r'[A-Z]+-\d+',
'regex_match', '{tickets}: {commit_msg}')
REGEX_MATCH_MODE, '{tickets}: {commit_msg}')
assert path.read() == '{expected_tickets}: {message}'.format(
expected_tickets=test_data[1], message=COMMIT_MESSAGE
)
Expand All @@ -121,10 +124,26 @@ def test_ci_message_with_nl_regex_match_mode(mock_branch_name, msg, tmpdir):
path = tmpdir.join('file.txt')
path.write(msg)
update_commit_message(six.text_type(path), r'[A-Z]+-\d+',
'regex_match', '{commit_msg} - {ticket}')
REGEX_MATCH_MODE, '{commit_msg} - {ticket}')
assert path.read().split('\n')[0] == "{first_line} - {ticket}".format(first_line=first_line, ticket="JIRA-239")


@mock.patch(TESTING_MODULE + '.get_branch_name')
def test_update_commit_message_with_new_line_characters(mock_branch_name, tmpdir):
msg = 'Test Message'
mock_branch_name.return_value = "team_name/2397/a_nice_feature"

path = tmpdir.join('file.txt')
path.write(msg)

path_with_expected_message = tmpdir.join('expected.txt')
path_with_expected_message.write(f'{msg}\n\nIssue: 2397\n')

update_commit_message(six.text_type(path), r'\d{4,}',
REGEX_MATCH_MODE, r'{commit_msg}\n\nIssue: {ticket}')
assert path.read() == path_with_expected_message.read()


@pytest.mark.parametrize('msg', (
"""A descriptive header

Expand All @@ -138,7 +157,7 @@ def test_update_commit_message_no_modification_if_ticket_in_body(mock_branch_nam
path = tmpdir.join('file.txt')
path.write(msg)
update_commit_message(six.text_type(path), r'\d{4,}',
'regex_match', '{commit_msg}\n\nIssue: {ticket}')
REGEX_MATCH_MODE, '{commit_msg}\n\nIssue: {ticket}')
assert path.read() == msg


Expand All @@ -162,9 +181,9 @@ def test_main(mock_update_commit_message, mock_argparse):
mock_args.filenames = ['foo.txt']
mock_args.regex = None
mock_args.format = None
mock_args.mode = 'underscore_split'
mock_args.mode = UNDERSCORE_SPLIT_MODE
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',
UNDERSCORE_SPLIT_MODE,
'{ticket} {commit_msg}')