Implemented debug logging
This commit is contained in:
		@ -118,6 +118,10 @@ Does not push after versioning.
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
Shows helps screen.
 | 
					Shows helps screen.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					`-D`/`--debug`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Runs with debug logging.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<a name="semver_get_version"></a>
 | 
					<a name="semver_get_version"></a>
 | 
				
			||||||
### semver_get_version
 | 
					### semver_get_version
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -129,6 +133,10 @@ The `semver_get_version` command returns the version number if the `semver` comm
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
Replaces `/` with `.` in branch names. For example, `feature/test` becomes `feature.test`
 | 
					Replaces `/` with `.` in branch names. For example, `feature/test` becomes `feature.test`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					`-D`/`--debug`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Runs with debug logging.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### Jenkins Shared Library
 | 
					### Jenkins Shared Library
 | 
				
			||||||
 | 
					
 | 
				
			||||||
This repository is also home to a Jenkins shared library to assit in running auto-semver.
 | 
					This repository is also home to a Jenkins shared library to assit in running auto-semver.
 | 
				
			||||||
 | 
				
			|||||||
@ -2,6 +2,7 @@ import argparse
 | 
				
			|||||||
import re
 | 
					import re
 | 
				
			||||||
import subprocess
 | 
					import subprocess
 | 
				
			||||||
from semver.get_version import get_tag_version
 | 
					from semver.get_version import get_tag_version
 | 
				
			||||||
 | 
					from semver.logger import logging, logger, console_logger
 | 
				
			||||||
 | 
					
 | 
				
			||||||
try: 
 | 
					try: 
 | 
				
			||||||
    from configparser import ConfigParser
 | 
					    from configparser import ConfigParser
 | 
				
			||||||
@ -50,7 +51,7 @@ class SemVer(object):
 | 
				
			|||||||
                             cwd='.')
 | 
					                             cwd='.')
 | 
				
			||||||
        message = str(p.stdout.read())
 | 
					        message = str(p.stdout.read())
 | 
				
			||||||
        branch = b.stdout.read().decode('utf-8').rstrip()
 | 
					        branch = b.stdout.read().decode('utf-8').rstrip()
 | 
				
			||||||
        print('Main branch is ' + branch)
 | 
					        logger.info('Main branch is ' + branch)
 | 
				
			||||||
        matches = self.GET_COMMIT_MESSAGE.search(message)
 | 
					        matches = self.GET_COMMIT_MESSAGE.search(message)
 | 
				
			||||||
        if matches:
 | 
					        if matches:
 | 
				
			||||||
            if str(matches.group(4)) == branch:
 | 
					            if str(matches.group(4)) == branch:
 | 
				
			||||||
@ -62,7 +63,7 @@ class SemVer(object):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    # based on branches involved see what type of versioning should be done
 | 
					    # based on branches involved see what type of versioning should be done
 | 
				
			||||||
    def get_version_type(self):
 | 
					    def get_version_type(self):
 | 
				
			||||||
        print('Merged branch is ' + self.merged_branch)
 | 
					        logger.info('Merged branch is ' + self.merged_branch)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        merged_prefix = None
 | 
					        merged_prefix = None
 | 
				
			||||||
        matches = re.findall("[^\/]*/", self.merged_branch)
 | 
					        matches = re.findall("[^\/]*/", self.merged_branch)
 | 
				
			||||||
@ -112,6 +113,7 @@ class SemVer(object):
 | 
				
			|||||||
            #file.write(config_file)
 | 
					            #file.write(config_file)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # version repo
 | 
					        # version repo
 | 
				
			||||||
 | 
					        logger.debug("Running bumpversion of type: " + self.version_type)
 | 
				
			||||||
        p = subprocess.Popen(['bumpversion', '--current-version', get_tag_version(), self.version_type],
 | 
					        p = subprocess.Popen(['bumpversion', '--current-version', get_tag_version(), self.version_type],
 | 
				
			||||||
                             cwd='.')
 | 
					                             cwd='.')
 | 
				
			||||||
        p.wait()
 | 
					        p.wait()
 | 
				
			||||||
@ -152,10 +154,16 @@ def main():
 | 
				
			|||||||
        parser = argparse.ArgumentParser(description='Bump Semantic Version.')
 | 
					        parser = argparse.ArgumentParser(description='Bump Semantic Version.')
 | 
				
			||||||
        parser.add_argument('-n','--no-push', help='Do not try to push', action='store_false', dest='push')
 | 
					        parser.add_argument('-n','--no-push', help='Do not try to push', action='store_false', dest='push')
 | 
				
			||||||
        parser.add_argument('-g','--global-user', help='Set git user at a global level, helps in jenkins', action='store_true', dest='global_user')
 | 
					        parser.add_argument('-g','--global-user', help='Set git user at a global level, helps in jenkins', action='store_true', dest='global_user')
 | 
				
			||||||
 | 
					        parser.add_argument('-D', '--debug', help='Sets logging level to DEBUG', action='store_true', dest='debug', default=False)
 | 
				
			||||||
        args = parser.parse_args()
 | 
					        args = parser.parse_args()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if args.debug:
 | 
				
			||||||
 | 
					            console_logger.setLevel(logging.DEBUG)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        SemVer(global_user=args.global_user).run(push=args.push)
 | 
					        SemVer(global_user=args.global_user).run(push=args.push)
 | 
				
			||||||
    except Exception as e:
 | 
					    except Exception as e:
 | 
				
			||||||
        print(e)
 | 
					        logger.error(e)
 | 
				
			||||||
        if e == NO_MERGE_FOUND:
 | 
					        if e == NO_MERGE_FOUND:
 | 
				
			||||||
            exit(1)
 | 
					            exit(1)
 | 
				
			||||||
        elif e == NOT_MAIN_BRANCH:
 | 
					        elif e == NOT_MAIN_BRANCH:
 | 
				
			||||||
 | 
				
			|||||||
@ -1,4 +1,5 @@
 | 
				
			|||||||
import argparse
 | 
					import argparse
 | 
				
			||||||
 | 
					from semver.logger import logging, logger, console_logger
 | 
				
			||||||
import subprocess
 | 
					import subprocess
 | 
				
			||||||
try:
 | 
					try:
 | 
				
			||||||
    from configparser import ConfigParser
 | 
					    from configparser import ConfigParser
 | 
				
			||||||
@ -16,12 +17,15 @@ def get_tag_version():
 | 
				
			|||||||
    config.read('./.bumpversion.cfg')
 | 
					    config.read('./.bumpversion.cfg')
 | 
				
			||||||
    tag_expression = config.get('bumpversion','tag_name').replace('{new_version}','[0-9]*.[0-9]*.[0-9]*')
 | 
					    tag_expression = config.get('bumpversion','tag_name').replace('{new_version}','[0-9]*.[0-9]*.[0-9]*')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    logger.debug("Tag expression: " + str(tag_expression))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    version = "0.0.0"
 | 
					    version = "0.0.0"
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    tagged_versions = subprocess.Popen(['git','tag','--sort=taggerdate', '-l',tag_expression],
 | 
					    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')
 | 
					        stdout=subprocess.PIPE, stderr=DEVNULL, cwd=".").stdout.read().decode('utf-8').rstrip().split('\n')
 | 
				
			||||||
    if len(tagged_versions) > 0 and tagged_versions[-1] != "":
 | 
					    if len(tagged_versions) > 0 and tagged_versions[-1] != "":
 | 
				
			||||||
        version = tagged_versions[-1]
 | 
					        version = tagged_versions[-1]
 | 
				
			||||||
 | 
					    logger.debug("Tag Version: " + str(version))
 | 
				
			||||||
    return version
 | 
					    return version
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_version(dot=False):
 | 
					def get_version(dot=False):
 | 
				
			||||||
@ -33,9 +37,11 @@ def get_version(dot=False):
 | 
				
			|||||||
    # Get the current commit hash
 | 
					    # Get the current commit hash
 | 
				
			||||||
    c_hash = subprocess.Popen(['git', 'rev-parse', 'HEAD'], stdout=subprocess.PIPE,
 | 
					    c_hash = subprocess.Popen(['git', 'rev-parse', 'HEAD'], stdout=subprocess.PIPE,
 | 
				
			||||||
                             stderr=DEVNULL, cwd='.').stdout.read().decode('utf-8').rstrip()
 | 
					                             stderr=DEVNULL, cwd='.').stdout.read().decode('utf-8').rstrip()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # If the version commit hash and current commit hash
 | 
					    # If the version commit hash and current commit hash
 | 
				
			||||||
    # do not match return the branch name else return the version
 | 
					    # do not match return the branch name else return the version
 | 
				
			||||||
    if v_hash != c_hash:
 | 
					    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,
 | 
					        b = subprocess.Popen(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], stdout=subprocess.PIPE,
 | 
				
			||||||
                            stderr=DEVNULL, cwd='.').stdout.read().decode('utf-8').rstrip()
 | 
					                            stderr=DEVNULL, cwd='.').stdout.read().decode('utf-8').rstrip()
 | 
				
			||||||
        if dot:
 | 
					        if dot:
 | 
				
			||||||
@ -47,9 +53,13 @@ def get_version(dot=False):
 | 
				
			|||||||
def main():
 | 
					def main():
 | 
				
			||||||
    parser = argparse.ArgumentParser(description='Get Version or Branch.')
 | 
					    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)
 | 
				
			||||||
    args = parser.parse_args()
 | 
					    args = parser.parse_args()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    print(get_version(args.dot))
 | 
					    if args.debug:
 | 
				
			||||||
 | 
					        console_logger.setLevel(logging.DEBUG)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    logger.info(get_version(args.dot))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
    try: main()
 | 
					    try: main()
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										19
									
								
								semver/logger.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								semver/logger.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,19 @@
 | 
				
			|||||||
 | 
					import logging
 | 
				
			||||||
 | 
					import subprocess
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# create logger
 | 
				
			||||||
 | 
					logger = logging.getLogger('simple_example')
 | 
				
			||||||
 | 
					logger.setLevel(logging.DEBUG)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# create console handler and set level to INFO
 | 
				
			||||||
 | 
					console_logger = logging.StreamHandler()
 | 
				
			||||||
 | 
					console_logger.setLevel(logging.INFO)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# create formatter
 | 
				
			||||||
 | 
					formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# add formatter to console_logger
 | 
				
			||||||
 | 
					console_logger.setFormatter(formatter)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# add console_logger to logger
 | 
				
			||||||
 | 
					logger.addHandler(console_logger)
 | 
				
			||||||
@ -10,16 +10,21 @@
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Run Auto Semver
 | 
					// Run Auto Semver
 | 
				
			||||||
def call(dockerImage = "rightbrainnetworks/auto-semver:latest") {
 | 
					def call(dockerImage = "rightbrainnetworks/auto-semver:latest", debug = false) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def docker_image = docker.image(dockerImage)
 | 
					    def docker_image = docker.image(dockerImage)
 | 
				
			||||||
    docker_image.pull()
 | 
					    docker_image.pull()
 | 
				
			||||||
    docker_image.inside{
 | 
					    docker_image.inside{
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      def args = ""
 | 
				
			||||||
 | 
					      if(debug)
 | 
				
			||||||
 | 
					      {
 | 
				
			||||||
 | 
					        args="--debug"
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
      def RETURN_STATUS
 | 
					      def RETURN_STATUS
 | 
				
			||||||
      def regex = '^\\s*current_version\\s*=\\s*\\K[^\\s]+'
 | 
					      def regex = '^\\s*current_version\\s*=\\s*\\K[^\\s]+'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      RETURN_STATUS = sh(script: "semver -n", returnStatus: true)
 | 
					      RETURN_STATUS = sh(script: "semver -n ${args}", returnStatus: true)
 | 
				
			||||||
      echo "Semver Return Status: ${RETURN_STATUS}"
 | 
					      echo "Semver Return Status: ${RETURN_STATUS}"
 | 
				
			||||||
      env.SEMVER_STATUS = RETURN_STATUS
 | 
					      env.SEMVER_STATUS = RETURN_STATUS
 | 
				
			||||||
      switch (RETURN_STATUS) {
 | 
					      switch (RETURN_STATUS) {
 | 
				
			||||||
@ -36,7 +41,7 @@ def call(dockerImage = "rightbrainnetworks/auto-semver:latest") {
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      env.SEMVER_NEW_VERSION = sh(script: "grep -Po '${regex}' .bumpversion.cfg", returnStdout: true).trim()
 | 
					      env.SEMVER_NEW_VERSION = sh(script: "grep -Po '${regex}' .bumpversion.cfg", returnStdout: true).trim()
 | 
				
			||||||
      env.SEMVER_RESOLVED_VERSION = getVersion('-d')
 | 
					      env.SEMVER_RESOLVED_VERSION = getVersion("-d ${args}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      env.VERSION = env.SEMVER_RESOLVED_VERSION
 | 
					      env.VERSION = env.SEMVER_RESOLVED_VERSION
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user