Implemented debug logging

This commit is contained in:
Layla 2020-02-21 17:44:15 -05:00
parent 8fe6d81864
commit b76c873e26
5 changed files with 57 additions and 7 deletions

View File

@ -118,6 +118,10 @@ Does not push after versioning.
Shows helps screen.
`-D`/`--debug`
Runs with debug logging.
<a name="semver_get_version"></a>
### 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`
`-D`/`--debug`
Runs with debug logging.
### Jenkins Shared Library
This repository is also home to a Jenkins shared library to assit in running auto-semver.

View File

@ -2,6 +2,7 @@ import argparse
import re
import subprocess
from semver.get_version import get_tag_version
from semver.logger import logging, logger, console_logger
try:
from configparser import ConfigParser
@ -50,7 +51,7 @@ class SemVer(object):
cwd='.')
message = str(p.stdout.read())
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)
if matches:
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
def get_version_type(self):
print('Merged branch is ' + self.merged_branch)
logger.info('Merged branch is ' + self.merged_branch)
merged_prefix = None
matches = re.findall("[^\/]*/", self.merged_branch)
@ -112,6 +113,7 @@ class SemVer(object):
#file.write(config_file)
# version repo
logger.debug("Running bumpversion of type: " + self.version_type)
p = subprocess.Popen(['bumpversion', '--current-version', get_tag_version(), self.version_type],
cwd='.')
p.wait()
@ -152,10 +154,16 @@ def main():
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('-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()
if args.debug:
console_logger.setLevel(logging.DEBUG)
SemVer(global_user=args.global_user).run(push=args.push)
except Exception as e:
print(e)
logger.error(e)
if e == NO_MERGE_FOUND:
exit(1)
elif e == NOT_MAIN_BRANCH:

View File

@ -1,4 +1,5 @@
import argparse
from semver.logger import logging, logger, console_logger
import subprocess
try:
from configparser import ConfigParser
@ -16,12 +17,15 @@ def get_tag_version():
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))
version = "0.0.0"
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_version(dot=False):
@ -33,9 +37,11 @@ def get_version(dot=False):
# Get the current commit hash
c_hash = subprocess.Popen(['git', 'rev-parse', 'HEAD'], stdout=subprocess.PIPE,
stderr=DEVNULL, cwd='.').stdout.read().decode('utf-8').rstrip()
# If the version commit hash and current commit hash
# 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,
stderr=DEVNULL, cwd='.').stdout.read().decode('utf-8').rstrip()
if dot:
@ -47,9 +53,13 @@ def get_version(dot=False):
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', '--debug', help='Sets logging level to DEBUG', action='store_true', dest='debug', default=False)
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__':
try: main()

19
semver/logger.py Normal file
View 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)

View File

@ -10,16 +10,21 @@
*/
// 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)
docker_image.pull()
docker_image.inside{
def args = ""
if(debug)
{
args="--debug"
}
def RETURN_STATUS
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}"
env.SEMVER_STATUS = 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_RESOLVED_VERSION = getVersion('-d')
env.SEMVER_RESOLVED_VERSION = getVersion("-d ${args}")
env.VERSION = env.SEMVER_RESOLVED_VERSION
}