diff --git a/Dockerfile b/Dockerfile index e271759..af9c6be 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,8 +6,8 @@ USER root RUN pip install --upgrade pip RUN yum update -y RUN yum -y remove git -RUN yum -y install https://centos7.iuscommunity.org/ius-release.rpm -RUN yum -y install git2u-all +RUN yum -y install https://packages.endpoint.com/rhel/7/os/x86_64/endpoint-repo-1.7-1.x86_64.rpm +RUN yum -y install git #Setup semver ADD / /semver diff --git a/README.md b/README.md index 109d78b..ce23a3d 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,7 @@ Runs with debug logging. ### semver_get_version -The `semver_get_version` command returns the version number if the `semver` command exited `0`. If `semver` exited anything else, `semver_get_version` will return the branch name. +The `semver_get_version` command returns the version number if the `semver` command exited `0`. If `semver` exited anything else, `semver_get_version` will return the branch name, or a pre-release version if format is specified #### Flags @@ -137,6 +137,14 @@ Replaces `/` with `.` in branch names. For example, `feature/test` becomes `feat Runs with debug logging. +`-f`/`--format` `(npm|maven)` + +Formats the output for a pre-release version for a specific repository type. For example, `feature/test` becomes `0.1.0-feature-test-SNAPSHOT` for maven with no build number, or `0.1.0-feature-test.0` for maven with no build number. + +`-b`/`--build-number` `number` + +Provides a build number for pre-release versions. + ### Jenkins Shared Library This repository is also home to a Jenkins shared library to assit in running auto-semver. @@ -168,4 +176,4 @@ pipeline #### runAutoSemver( String _dockerImage_ ) -**dockerImage:** The Docker image and tag to run auto-semver with. By default, it pulls `rightbrainnetworks/auto-semver:latest`. \ No newline at end of file +**dockerImage:** The Docker image and tag to run auto-semver with. By default, it pulls `rightbrainnetworks/auto-semver:latest`. diff --git a/semver/__init__.py b/semver/__init__.py index 987763a..5a7077e 100644 --- a/semver/__init__.py +++ b/semver/__init__.py @@ -1,7 +1,7 @@ import argparse import re import subprocess -from semver.get_version import get_tag_version +from semver.utils import get_tag_version from semver.logger import logging, logger, console_logger try: @@ -44,7 +44,6 @@ class SemVer(object): return list(filter(bool, [v.strip() for v in value.split(',')])) # based on commit message see what branches are involved in the merge - def get_branches(self): p = subprocess.Popen(['git', 'log', '-1'], stdout=subprocess.PIPE, cwd='.') @@ -76,15 +75,15 @@ class SemVer(object): for prefix in self.major_branches: if prefix == merged_prefix: self.version_type = 'major' - return True + return self.version_type for prefix in self.minor_branches: if prefix == merged_prefix: self.version_type = 'minor' - return True + return self.version_type for prefix in self.patch_branches: if prefix == merged_prefix: self.version_type = 'patch' - return True + return self.version_type return False # setup git settings so we can commit and tag @@ -104,15 +103,6 @@ class SemVer(object): config_file = "" with open(".bumpversion.cfg", "r") as file: config_file = file.read() - # Update .bumpconfig - #pattern = re.compile("current_version = [0-9.]*") - #current_config = re.search(pattern, config_file) - #if current_config: - #config_file.replace(current_config.group(0), "current_version = " + get_tag_version()) - #else: - #config_file.replace("[bumpversion]","[bumpversion]\ncurrent_version = " + get_tag_version()) - #with open(".bumpversion.cfg", "w") as file: - #file.write(config_file) # version repo logger.debug("Running bumpversion of type: " + self.version_type) diff --git a/semver/get_version.py b/semver/get_version.py index 0535a67..12ed552 100644 --- a/semver/get_version.py +++ b/semver/get_version.py @@ -1,44 +1,11 @@ import argparse -from semver.logger import logging, logger, console_logger +import re import subprocess -try: - from configparser import ConfigParser -except ImportError: - # Python < 3 - from ConfigParser import ConfigParser -try: - from subprocess import DEVNULL # py3k -except ImportError: - import os - DEVNULL = open(os.devnull, 'wb') +from semver.logger import logging, logger, console_logger +from semver.utils import get_tag_version, get_file_version, DEVNULL +from semver import SemVer -def get_tag_version(): - config = ConfigParser() - config.read('./.bumpversion.cfg') - tag_expression = config.get('bumpversion','tag_name').replace('{new_version}','[0-9]*.[0-9]*.[0-9]*') - - logger.debug("Tag expression: " + str(tag_expression)) - - # Default version is `0.0.0` or what is found in - 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=v:refname', '-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): +def get_version(build=0,version_format=None,dot=False): version = get_tag_version() # Get the commit hash of the version @@ -52,25 +19,44 @@ def get_version(dot=False): # do not match return the branch name else return the version if v_hash != c_hash: logger.debug("v_hash and c_hash do not match!") - b = subprocess.Popen(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], stdout=subprocess.PIPE, + branch = subprocess.Popen(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], stdout=subprocess.PIPE, stderr=DEVNULL, cwd='.').stdout.read().decode('utf-8').rstrip() + semver = SemVer() + semver.merged_branch = branch + logger.debug("merged branch is: {}".format(semver.merged_branch)) + version_type = semver.get_version_type() + logger.debug("version type is: {}".format(version_type)) + if version_type: + p = subprocess.Popen(['bumpversion', '--dry-run', '--verbose', '--current-version', get_tag_version(), version_type], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd='.') + bump_output = p.stdout.read().decode('utf-8').rstrip() + next_version = re.search("new_version=([0-9]*.[0-9]*.[0-9]*)", bump_output).group(1) + + if version_format == 'npm': + return "{}-{}.{}".format(next_version,branch.replace('/','-'),build) + if version_format == 'maven': + qualifier = 'SNAPSHOT' if build == 0 else build + return "{}-{}-{}".format(next_version,branch.replace('/','-'),qualifier) if dot: - b = b.replace('/','.') - return b + branch = branch.replace('/','.') + return branch return version def main(): parser = argparse.ArgumentParser(description='Get Version or Branch.') - parser.add_argument('-d','--dot', help='Switch out / for . to be used in docker tag', action='store_true', dest='dot') + parser.add_argument('-d', '--dot', help='Switch out / for . to be used in docker tag', action='store_true', dest='dot') parser.add_argument('-D', '--debug', help='Sets logging level to DEBUG', action='store_true', dest='debug', default=False) + parser.add_argument('-f', '--format', help='Format for pre-release version syntax', choices=['npm','maven'], default=None) + parser.add_argument('-b', '--build-number', help='Build number, used in pre-releases', default=0) + args = parser.parse_args() if args.debug: console_logger.setLevel(logging.DEBUG) - print(get_version(args.dot)) + print(get_version(args.build_number,args.format,args.dot)) if __name__ == '__main__': try: main() except: raise + diff --git a/semver/tests.py b/semver/tests.py index a391131..382383c 100644 --- a/semver/tests.py +++ b/semver/tests.py @@ -1,7 +1,7 @@ import unittest, os, subprocess, re, semver from semver.logger import logging, logger, console_logger -from semver import get_version, NO_MERGE_FOUND, GET_COMMIT_MESSAGE +from semver import get_version, utils, NO_MERGE_FOUND, GET_COMMIT_MESSAGE config_data = """ [bumpversion] @@ -54,12 +54,27 @@ class TestGetVersion(unittest.TestCase): def test_branch_dotting(self): create_git_environment() subprocess.call(['git', 'checkout', '-b', 'test/branch']) - branch = get_version.get_version(True) + branch = get_version.get_version(dot=True) self.assertEqual(branch, "test.branch") def test_branch_dotting_false(self): create_git_environment() subprocess.call(['git', 'checkout', '-b', 'test/branch']) - branch = get_version.get_version(False) + branch = get_version.get_version(dot=False) + self.assertEqual(branch, "test/branch") + def test_branch_npm_pre_release(self): + create_git_environment() + subprocess.call(['git', 'checkout', '-b', 'patch/branch']) + branch = get_version.get_version(version_format='npm') + self.assertEqual(branch, "0.0.1-patch-branch.0") + def test_branch_maven_pre_release(self): + create_git_environment() + subprocess.call(['git', 'checkout', '-b', 'minor/branch']) + branch = get_version.get_version(version_format='maven') + self.assertEqual(branch, "0.1.0-minor-branch-SNAPSHOT") + def test_branch_maven_bad_branch(self): + create_git_environment() + subprocess.call(['git', 'checkout', '-b', 'test/branch']) + branch = get_version.get_version(version_format='maven') self.assertEqual(branch, "test/branch") def test_get_version_run(self): create_git_environment() @@ -72,7 +87,7 @@ class TestGetTagVersion(unittest.TestCase): def test_get_version_tag(self): create_git_environment() subprocess.call(['git', 'tag', '1.0.0']) - tag = get_version.get_tag_version() + tag = utils.get_tag_version() self.assertEqual(tag, "1.0.0") def test_get_version_multiple(self): create_git_environment() @@ -87,7 +102,7 @@ class TestGetTagVersion(unittest.TestCase): subprocess.call(['git', 'tag', '1.1.0']) subprocess.call(['git', 'tag', '1.2.0']) subprocess.call(['git', 'tag', '1.2.1']) - tag = get_version.get_tag_version() + tag = utils.get_tag_version() self.assertEqual(tag, "1.2.1") def test_get_version_out_of_order(self): subprocess.call(['git', 'tag', '0.1.0']) @@ -101,11 +116,11 @@ class TestGetTagVersion(unittest.TestCase): subprocess.call(['git', 'tag', '1.1.7']) subprocess.call(['git', 'tag', '1.2.0']) subprocess.call(['git', 'tag', '0.2.1']) - tag = get_version.get_tag_version() + tag = utils.get_tag_version() self.assertEqual(tag, "8.1.0") def test_default_get_version_tag(self): create_git_environment() - tag = get_version.get_tag_version() + tag = utils.get_tag_version() self.assertEqual(tag, "0.0.0") class TestGetCommitMessageRegex(unittest.TestCase): @@ -145,4 +160,4 @@ if __name__ == "__main__": with open('.bumpversion.cfg', "w") as config: config.write(config_data) unittest.main() - os.chdir("..") \ No newline at end of file + os.chdir("..") diff --git a/semver/utils.py b/semver/utils.py new file mode 100644 index 0000000..88f813b --- /dev/null +++ b/semver/utils.py @@ -0,0 +1,41 @@ +import subprocess +from semver.logger import logging, logger, console_logger + +try: + from configparser import ConfigParser +except ImportError: + # Python < 3 + from ConfigParser import ConfigParser + +try: + from subprocess import DEVNULL # py3k +except ImportError: + import os + DEVNULL = open(os.devnull, 'wb') + +def get_tag_version(): + config = ConfigParser() + config.read('./.bumpversion.cfg') + tag_expression = config.get('bumpversion','tag_name').replace('{new_version}','[0-9]*.[0-9]*.[0-9]*') + + logger.debug("Tag expression: " + str(tag_expression)) + + # Default version is `0.0.0` or what is found in + 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=v:refname', '-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 +