From 085b20a10fd64941ffb4d6d5954ca5409604f529 Mon Sep 17 00:00:00 2001 From: Joseph Manley Date: Fri, 31 Jan 2020 13:14:26 -0500 Subject: [PATCH 1/7] Added getVersion() groovy function --- vars/getVersion.groovy | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 vars/getVersion.groovy diff --git a/vars/getVersion.groovy b/vars/getVersion.groovy new file mode 100644 index 0000000..22b3c93 --- /dev/null +++ b/vars/getVersion.groovy @@ -0,0 +1,14 @@ +#!/usr/bin/env groovy + +/** + * Run semver_get_version to return the current version of the repository + * + * @param flags Flags or arguments to pass to semver_get_version + * @return "1.9.2" + */ +def call(flags='') { + def VERSION + VERSION = sh(returnStdout: true, script: "semver_get_version ${flags}") + return VERSION.trim() +} + From be9ad8b05d2e08c866b378a1eda66a461b9847be Mon Sep 17 00:00:00 2001 From: Joseph Manley Date: Thu, 6 Feb 2020 13:08:52 -0500 Subject: [PATCH 2/7] Update regex to support GitLab --- semver/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/semver/__init__.py b/semver/__init__.py index 73a9ac0..ea9c9c1 100644 --- a/semver/__init__.py +++ b/semver/__init__.py @@ -20,7 +20,7 @@ NO_GIT_FLOW = Exception('No git flow branch found') class SemVer(object): - GET_COMMIT_MESSAGE = re.compile(r"Merge (branch|pull request) '?(.+)'? (into|from) ([\w/-]+)") + GET_COMMIT_MESSAGE = re.compile(r"Merge (branch|pull request) '?(.+)'? (into|from) '?([\w\-]+)'?") # Merge pull request #1 from RightBrain-Networks/feature/PLAT-185-versioning def __init__(self,global_user=False): From 182ac9e3e2d9df160edab345fc448e33f56ef7d4 Mon Sep 17 00:00:00 2001 From: Joseph Manley Date: Thu, 27 Feb 2020 20:15:37 +0000 Subject: [PATCH 3/7] Use version from config file as default Use version from config file as default Use version from config file as default --- semver/get_version.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/semver/get_version.py b/semver/get_version.py index 37745dd..e2bf78d 100644 --- a/semver/get_version.py +++ b/semver/get_version.py @@ -19,15 +19,25 @@ def get_tag_version(): logger.debug("Tag expression: " + str(tag_expression)) - version = "0.0.0" + # Default version is `0.0.0` or what is found in + version = get_file_version() + # If a version is found in tags, use that the lastest tagged version tagged_versions = subprocess.Popen(['git','tag','--sort=taggerdate', '-l',tag_expression], stdout=subprocess.PIPE, stderr=DEVNULL, cwd=".").stdout.read().decode('utf-8').rstrip().split('\n') if len(tagged_versions) > 0 and tagged_versions[-1] != "": version = tagged_versions[-1] + logger.debug("Tag Version: " + str(version)) return version +def get_file_version(config): + version = config.get('bumpversion','current_version') + if not version: + config.set('bumpversion', 'current_version', '0.0.0') + version = '0.0.0' + return version + def get_version(dot=False): version = get_tag_version() From 95de1360fd969a38b70c2c08c9f5107f746b60ba Mon Sep 17 00:00:00 2001 From: Joseph Manley Date: Thu, 27 Feb 2020 21:15:33 +0000 Subject: [PATCH 4/7] Use version from config file as default --- semver/get_version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/semver/get_version.py b/semver/get_version.py index e2bf78d..6dd8f9a 100644 --- a/semver/get_version.py +++ b/semver/get_version.py @@ -20,7 +20,7 @@ def get_tag_version(): logger.debug("Tag expression: " + str(tag_expression)) # Default version is `0.0.0` or what is found in - version = get_file_version() + version = get_file_version(config) # If a version is found in tags, use that the lastest tagged version tagged_versions = subprocess.Popen(['git','tag','--sort=taggerdate', '-l',tag_expression], From 76538e5516e8e1b4c8c7916fadd2340af0717005 Mon Sep 17 00:00:00 2001 From: Joseph Manley Date: Thu, 27 Feb 2020 21:21:34 +0000 Subject: [PATCH 5/7] Update `README.md` with `current_version`'s purpose --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6f58749..109d78b 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ tag_name = v{new_version} message = Bump version: {current_version} -> {new_version} ``` -The `current_version` exists to tell bumpversion what the current version is. To have auto-semver manage this value, set it to `0.0.0`. The `commit` and `tag` options determine whether to create a new Git commit and a new Git tag, respectively. The `tag_name` represents what the name of the Git tag will be, and by default is set to `{new_version}`, which will be substitued with the new version during runtime. This can be changed as desired - for example, `v{new_version}` could resolve to `v1.15.5`. The `message` option is what the message used if there is a git commit. +The `current_version` exists to tell bumpversion what the current version is. Auto-semver uses this value as the default version if not version if found in the tags. The `commit` and `tag` options determine whether to create a new Git commit and a new Git tag, respectively. The `tag_name` represents what the name of the Git tag will be, and by default is set to `{new_version}`, which will be substitued with the new version during runtime. This can be changed as desired - for example, `v{new_version}` could resolve to `v1.15.5`. The `message` option is what the message used if there is a git commit. ### File updates From f3c894453820855e44b8b18bcdccc1dcd12ff332 Mon Sep 17 00:00:00 2001 From: Joseph Manley Date: Thu, 27 Feb 2020 21:56:15 +0000 Subject: [PATCH 6/7] Update regex --- semver/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/semver/__init__.py b/semver/__init__.py index ea9c9c1..44532cd 100644 --- a/semver/__init__.py +++ b/semver/__init__.py @@ -20,7 +20,7 @@ NO_GIT_FLOW = Exception('No git flow branch found') class SemVer(object): - GET_COMMIT_MESSAGE = re.compile(r"Merge (branch|pull request) '?(.+)'? (into|from) '?([\w\-]+)'?") + 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): @@ -57,7 +57,7 @@ class SemVer(object): if str(matches.group(4)) == branch: self.merged_branch = matches.group(2) else: - self.merged_branch = matches.group(4) + self.merged_branch = matches.group(5) self.main_branch = branch return bool(matches) From cb05f46aa60e93cdf1f767e805a5b5bcde634d36 Mon Sep 17 00:00:00 2001 From: Joseph Manley Date: Fri, 28 Feb 2020 16:56:50 +0000 Subject: [PATCH 7/7] Testing regex --- semver/__init__.py | 6 ++++-- semver/tests.py | 24 ++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) 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'])