Commit f6368e63 authored by Plup's avatar Plup

refact: reviewed the pqrsing function

parent 9fbc0318
......@@ -13,53 +13,60 @@ re_parser = re.compile(
re.DOTALL
)
TYPES = {
'major': 'major',
'minor': 'minor',
'feature': 'feature',
'update': 'update',
'bugfix': 'bugfix',
'ui': 'ui',
'syntax': 'syntax',
'other': 'other',
}
TYPES = [
'major',
'minor',
'feature',
'update',
'bugfix',
'ui',
'syntax',
'other'
]
def parse_commit_message(message: str) -> Tuple[int, str, str, Tuple[str, str, str]]:
"""
Parses a commit message according to the StartinBlox rules to define release
Parses a commit message according to the StartinBlox release rules
:param message: A string of the commit message
:return: A tuple of (level to bump, type of change, scope of change, a tuple with descriptions)
:raises UnknownCommitMessageStyleError: if regular expression matching fails"""
"""
try:
# default to patch
level_bump = 1
# parse message
parsed = re_parser.match(message)
if not parsed:
raise UnknownCommitMessageStyleError(
'Unable to parse the given commit message: {}'.format(message)
)
# parse message
parsed = re_parser.match(message)
# calculate release level
level_bump = 0
if parsed.group('type') == 'major':
level_bump = max([level_bump, 3])
# catch type
_type = parsed.group('type')
if _type not in TYPES:
_type = 'other'
elif parsed.group('type') == 'minor':
level_bump = max([level_bump, 2])
# calculate release level
if _type == 'major':
level_bump = 3
else:
level_bump = 1
elif _type == 'minor':
level_bump = 2
body, footer = parse_text_block(parsed.group('text'))
subject = parsed.group('subject')
body, footer = parse_text_block(parsed.group('text'))
# set type to 'other' by default
_type = TYPES['other']
if parsed.group('type') in TYPES:
_type = TYPES[parsed.group('type')]
except AttributeError:
# if regex doesn't match make a patch with raw commit message
_type = 'other'
subject = message.split("\n", 1)[0]
try:
body, footer = parse_text_block(message.split("\n", 1)[1])
except IndexError:
body = ''
footer = ''
return (
level_bump,
_type,
'',
(parsed.group('subject'), body, footer)
(subject, body, footer)
)
......@@ -3,52 +3,63 @@ import unittest
import commit_parser.parser
import semantic_release
from commit_parser.parser import TYPES
class TestParser(unittest.TestCase):
def test_parse_commit_message(self):
with self.assertRaises(semantic_release.errors.UnknownCommitMessageStyleError):
commit_parser.parser.parse_commit_message("my dummy commit message")
def test_parse_commit_messager_wrong_format(self):
result = commit_parser.parser.parse_commit_message("my dummy commit message")
expected = (1, 'other', '', ('my dummy commit message', '', ''))
self.assertEqual(result, expected)
def test_parse_commit_messager_wrong_format_multiline(self):
message = \
'''This is a dummy commit message
with a body
and a footer!'''
result = commit_parser.parser.parse_commit_message(message)
expected = (1, 'other', '', ('This is a dummy commit message', ' with a body', 'and a footer!'))
self.assertEqual(result, expected)
def test_parse_commit_message_other_type(self):
result = commit_parser.parser.parse_commit_message("test: trigger pipeline")
expected = (1, TYPES['other'], '', ("trigger pipeline", '',''))
expected = (1, 'other', '', ("trigger pipeline", '',''))
self.assertEqual(result, expected)
def test_parse_commit_message_major_type(self):
result = commit_parser.parser.parse_commit_message("major: trigger pipeline")
expected = (3, TYPES['major'], '', ("trigger pipeline", '',''))
expected = (3, 'major', '', ("trigger pipeline", '',''))
self.assertEqual(result, expected)
def test_parse_commit_message_minor_type(self):
result = commit_parser.parser.parse_commit_message("minor: add multi provider login")
expected = (2, TYPES['minor'], '', ("add multi provider login", '',''))
expected = (2, 'minor', '', ("add multi provider login", '',''))
self.assertEqual(result, expected)
def test_parse_commit_message_feature_type(self):
result = commit_parser.parser.parse_commit_message("feature: settings attributs are now passed to element")
expected = (1, TYPES['feature'], '', ("settings attributs are now passed to element", '', ''))
expected = (1, 'feature', '', ("settings attributs are now passed to element", '', ''))
self.assertEqual(result, expected)
def test_parse_commit_message_update_type(self):
result = commit_parser.parser.parse_commit_message("update: this is an update")
expected = (1, TYPES['update'], '', ("this is an update", '', ''))
expected = (1, 'update', '', ("this is an update", '', ''))
self.assertEqual(result, expected)
def test_parse_commit_message_bugfix_type(self):
result = commit_parser.parser.parse_commit_message("bugfix: this is a bugfix")
expected = (1, TYPES['bugfix'], '', ("this is a bugfix", '', ''))
expected = (1, 'bugfix', '', ("this is a bugfix", '', ''))
self.assertEqual(result, expected)
def test_parse_commit_message_ui_type(self):
result = commit_parser.parser.parse_commit_message("ui: here comes the new UI")
expected = (1, TYPES['ui'], '', ("here comes the new UI", '', ''))
expected = (1, 'ui', '', ("here comes the new UI", '', ''))
self.assertEqual(result, expected)
def test_parse_commit_message_syntax_type(self):
result = commit_parser.parser.parse_commit_message("syntax: just a typo !")
expected = (1, TYPES['syntax'], '', ("just a typo !", '', ''))
expected = (1, 'syntax', '', ("just a typo !", '', ''))
self.assertEqual(result, expected)
if __name__ == '__main__':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment