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
17 changes: 8 additions & 9 deletions giticket/giticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
conventionalcommit_regex = r'^(?P<type>build|chore|ci|docs|feat|fix|perf|refactor|style|test)(\((?P<scope>.+)\))?: (?P<subject>.+)'


def capitalize_first(text):
return text[:1].upper() + text[1:] if text else text
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=False):
def update_commit_message(filename, regex, mode, format_string, conventionalcommits=False, capitalize_spaces=0):
with io.open(filename, 'r+') as fd:
contents = fd.readlines()
commit_msg = contents[0].rstrip('\r\n')
Expand All @@ -45,8 +45,6 @@ def update_commit_message(filename, regex, mode, format_string, conventionalcomm
else:
scope = ', '.join(tickets)
subject = match.group('subject')
if capitalize:
subject = capitalize_first(subject)
format_string = '{type}({scope}): {subject}'
new_commit_msg = format_string.format(
type=type, scope=scope, subject=subject
Expand All @@ -56,8 +54,8 @@ def update_commit_message(filename, regex, mode, format_string, conventionalcomm
ticket=tickets[0], tickets=', '.join(tickets),
commit_msg=commit_msg
)
if capitalize:
new_commit_msg = capitalize_first(new_commit_msg)
if capitalize_spaces:
new_commit_msg = capitalize(new_commit_msg, capitalize_spaces)

contents[0] = six.text_type(new_commit_msg + "\n")
fd.seek(0)
Expand Down Expand Up @@ -87,7 +85,7 @@ def main(argv=None):
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='+')
parser.add_argument('--conventionalcommits', action='store_true')
parser.add_argument('--capitalize', action='store_true')
parser.add_argument('--capitalize', nargs='?')
parser.add_argument('--regex')
parser.add_argument('--format', nargs='?')
parser.add_argument('--mode', nargs='?', const=underscore_split_mode,
Expand All @@ -99,7 +97,8 @@ 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)
capitalize_spaces = args.capitalize or 0
update_commit_message(args.filenames[0], regex, args.mode, format_string, args.conventionalcommits, capitalize_spaces)


if __name__ == '__main__':
Expand Down
21 changes: 16 additions & 5 deletions tests/test_giticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,29 @@ def test_update_commit_message_capitalize(mock_branch_name, tmpdir):
path.write("Test commit message")
update_commit_message(six.text_type(path), r'[a-zA-Z]+-\d+',
'regex_match', '{ticket}: {commit_msg}',
capitalize=True)
capitalize_spaces=1)
assert path.read() == "Jira-1234: Test commit message\n"


@mock.patch(TESTING_MODULE + '.get_branch_name')
def test_update_commit_message_capitalize_multiple_chars(mock_branch_name, tmpdir):
mock_branch_name.return_value = "jira-1234"
path = tmpdir.join('file.txt')
path.write("Test commit message")
update_commit_message(six.text_type(path), r'[a-zA-Z]+-\d+',
'regex_match', '{ticket}: {commit_msg}',
capitalize_spaces=4)
assert path.read() == "JIRA-1234: Test commit message\n"


@mock.patch(TESTING_MODULE + '.get_branch_name')
def test_update_commit_message_capitalize_already_uppercase(mock_branch_name, tmpdir):
mock_branch_name.return_value = "JIRA-1234_new_feature"
path = tmpdir.join('file.txt')
path.write("Test commit message")
update_commit_message(six.text_type(path), r'[A-Z]+-\d+',
'regex_match', '{ticket}: {commit_msg}',
capitalize=True)
capitalize_spaces=1)
assert path.read() == "JIRA-1234: Test commit message\n"


Expand All @@ -166,8 +177,8 @@ def test_update_commit_message_capitalize_conventionalcommits(mock_branch_name,
path.write("feat: add new feature")
update_commit_message(six.text_type(path), r'[A-Z]+-\d+',
'regex_match', '{commit_msg}',
conventionalcommits=True, capitalize=True)
assert path.read() == "feat(JIRA-5678): Add new feature\n"
conventionalcommits=True, capitalize_spaces=1)
assert path.read() == "feat(JIRA-5678): add new feature\n"


@mock.patch(TESTING_MODULE + '.get_branch_name')
Expand Down Expand Up @@ -234,7 +245,7 @@ def test_main(mock_update_commit_message, mock_argparse):
mock_args.format = None
mock_args.mode = 'underscore_split'
mock_args.conventionalcommits = True
mock_args.capitalize = False
mock_args.capitalize = None
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+',
Expand Down
Loading