diff --git a/semver/__init__.py b/semver/__init__.py index 44532cd..987763a 100644 --- a/semver/__init__.py +++ b/semver/__init__.py @@ -18,9 +18,11 @@ NO_MERGE_FOUND = Exception('No merge found') NOT_MAIN_BRANCH = Exception('Not merging into a main branch') NO_GIT_FLOW = Exception('No git flow branch found') +# Important regex +GET_COMMIT_MESSAGE = re.compile(r"Merge (branch|pull request) '?([^']+)'? (into|from) (?:'(.+)'|[^\/]+\/(.+))") + class SemVer(object): - GET_COMMIT_MESSAGE = re.compile(r"Merge (branch|pull request) '?(.+)'? (into|from) (?:'(.+)'|[^\/]+\/(.+))") # Merge pull request #1 from RightBrain-Networks/feature/PLAT-185-versioning def __init__(self,global_user=False): @@ -52,7 +54,7 @@ class SemVer(object): message = str(p.stdout.read()) branch = b.stdout.read().decode('utf-8').rstrip() logger.info('Main branch is ' + branch) - matches = self.GET_COMMIT_MESSAGE.search(message) + matches = GET_COMMIT_MESSAGE.search(message) if matches: if str(matches.group(4)) == branch: self.merged_branch = matches.group(2) diff --git a/semver/tests.py b/semver/tests.py index 8e18ea2..ac6555c 100644 --- a/semver/tests.py +++ b/semver/tests.py @@ -1,7 +1,7 @@ -import unittest, os, subprocess, semver +import unittest, os, subprocess, re, semver from semver.logger import logging, logger, console_logger -from semver import get_version, NO_MERGE_FOUND +from semver import get_version, NO_MERGE_FOUND, GET_COMMIT_MESSAGE config_data = """ [bumpversion] @@ -78,6 +78,26 @@ class TestGetTagVersion(unittest.TestCase): create_git_environment() tag = get_version.get_tag_version() self.assertEqual(tag, "0.0.0") + +class TestGetCommitMessageRegex(unittest.TestCase): + def test_github_message(self): + matches = GET_COMMIT_MESSAGE.search("Merge pull request #1 from user/branch") + if matches: + self.assertEqual(matches.group(4), None) + self.assertEqual(matches.group(5), "branch") + else: + self.assertTrue(False) + pass + def test_gitlab_message(self): + matches = GET_COMMIT_MESSAGE.search("Merge branch 'branch' into 'master'") + if matches: + self.assertEqual(matches.group(4), "master") + self.assertEqual(matches.group(2), "branch") + else: + self.assertTrue(False) + def test_non_merge_message(self): + matches = GET_COMMIT_MESSAGE.search("Example unrelated commit message that should get 0 matches") + self.assertEqual(matches, None) def create_git_environment(): subprocess.call(['rm', '-rf', './.git'])