Skip to content

Commit 236ffc3

Browse files
authored
Merge pull request #34 from eaze/CI-997_add-support-for-conventional-commits
feat: added --conventionalcommit switch
2 parents c990da1 + 0338237 commit 236ffc3

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

giticket/giticket.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
underscore_split_mode = 'underscore_split'
1414
regex_match_mode = 'regex_match'
15+
conventionalcommit_regex = r'^(?P<type>build|chore|ci|docs|feat|fix|perf|refactor|style|test)(\((?P<scope>.+)\))?: (?P<subject>.+)'
1516

16-
17-
def update_commit_message(filename, regex, mode, format_string):
17+
def update_commit_message(filename, regex, mode, format_string, conventionalcommits=False):
1818
with io.open(filename, 'r+') as fd:
1919
contents = fd.readlines()
2020
commit_msg = contents[0].rstrip('\r\n')
@@ -31,10 +31,24 @@ def update_commit_message(filename, regex, mode, format_string):
3131
tickets = [branch.split(six.text_type('_'))[0]]
3232
tickets = [t.strip() for t in tickets]
3333

34-
new_commit_msg = format_string.format(
35-
ticket=tickets[0], tickets=', '.join(tickets),
36-
commit_msg=commit_msg
37-
)
34+
if conventionalcommits and (match := re.match(conventionalcommit_regex, commit_msg)):
35+
# If the commit message matches the Conventional Commits spec, we can use the captured groups.
36+
type = match.group('type')
37+
scope = match.group('scope')
38+
if scope:
39+
scope = scope + ',' + ', '.join(tickets)
40+
else:
41+
scope = ', '.join(tickets)
42+
subject = match.group('subject')
43+
format_string = '{type}({scope}): {subject}'
44+
new_commit_msg = format_string.format(
45+
type=type, scope=scope, subject=subject
46+
)
47+
else:
48+
new_commit_msg = format_string.format(
49+
ticket=tickets[0], tickets=', '.join(tickets),
50+
commit_msg=commit_msg
51+
)
3852

3953
contents[0] = six.text_type(new_commit_msg + "\n")
4054
fd.seek(0)
@@ -63,15 +77,19 @@ def main(argv=None):
6377
"""
6478
parser = argparse.ArgumentParser()
6579
parser.add_argument('filenames', nargs='+')
80+
parser.add_argument('--conventionalcommits', action='store_true')
6681
parser.add_argument('--regex')
67-
parser.add_argument('--format')
82+
parser.add_argument('--format', nargs='?')
6883
parser.add_argument('--mode', nargs='?', const=underscore_split_mode,
6984
default=underscore_split_mode,
7085
choices=[underscore_split_mode, regex_match_mode])
7186
args = parser.parse_args(argv)
87+
if not args.conventionalcommits and not args.format:
88+
parser.error('You must provide --format if not using --conventionalcommits')
89+
return 1
7290
regex = args.regex or r'[A-Z]+-\d+' # noqa
7391
format_string = args.format or '{ticket} {commit_msg}' # noqa
74-
update_commit_message(args.filenames[0], regex, args.mode, format_string)
92+
update_commit_message(args.filenames[0], regex, args.mode, format_string, args.conventionalcommits)
7593

7694

7795
if __name__ == '__main__':

tests/test_giticket.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ def test_ci_message_with_nl_regex_match_mode(mock_branch_name, msg, tmpdir):
124124
'regex_match', '{commit_msg} - {ticket}')
125125
assert path.read().split('\n')[0] == "{first_line} - {ticket}".format(first_line=first_line, ticket="JIRA-239")
126126

127+
# create a unit test to verify that if the --conventionalcommits flag is set,
128+
# the commit message is updated according to the conventional commit format
129+
@mock.patch(TESTING_MODULE + '.get_branch_name')
130+
def test_update_commit_message_conventionalcommits(mock_branch_name, tmpdir):
131+
mock_branch_name.return_value = "JIRA-5678_new_feature"
132+
path = tmpdir.join('file.txt')
133+
path.write("feat: add new feature")
134+
update_commit_message(six.text_type(path), r'[A-Z]+-\d+',
135+
'regex_match', '{commit_msg}', conventionalcommits=True)
136+
assert path.read() == "feat(JIRA-5678): add new feature\n"
127137

128138
@pytest.mark.parametrize('msg', (
129139
"""A descriptive header
@@ -178,8 +188,10 @@ def test_main(mock_update_commit_message, mock_argparse):
178188
mock_args.regex = None
179189
mock_args.format = None
180190
mock_args.mode = 'underscore_split'
191+
mock_args.conventionalcommits = True
181192
mock_argparse.ArgumentParser.return_value.parse_args.return_value = mock_args
182193
main()
183194
mock_update_commit_message.assert_called_once_with('foo.txt', r'[A-Z]+-\d+',
184195
'underscore_split',
185-
'{ticket} {commit_msg}')
196+
'{ticket} {commit_msg}',
197+
True)

0 commit comments

Comments
 (0)