diff --git a/Makefile b/Makefile index a718e3d9091d4f803a2bee094d4f11db1e17d190..c045e1b51127d1080cfcc1c292e286516deaae87 100644 --- a/Makefile +++ b/Makefile @@ -30,3 +30,12 @@ build-image: push-image: docker push iqbberlin/testcenter-frontend:latest + +tag-major: + scripts/new_version.py major + +tag-minor: + scripts/new_version.py minor + +tag-patch: + scripts/new_version.py patch diff --git a/scripts/new_version.py b/scripts/new_version.py new file mode 100755 index 0000000000000000000000000000000000000000..5e326d6a30bb3d01def177afad9cf9e8aef5dd07 --- /dev/null +++ b/scripts/new_version.py @@ -0,0 +1,98 @@ +#!/usr/bin/python3 +"""This script shall be used to create a new release. + +It consists of several steps: + +STEP 1 - Update version: +A parameter has to be passed which can be one of the following: 'major'/'minor'/'patch'. +Depending on this parameter the version of the software will be updated in the +corresponding file. The file which holds this information can be set at the top +of the script with the VERSION_FILE variable and the regular expression to find +the string can be set as well. + +STEP 2 - Run Tests: +Starting the software stack via docker and run the tests. +Thise tests may change additional files, which have to be commited as well. + +STEP 3 - Git commit and tag +Several steps are taken to update the git repository. The shell commands are the following: + git add {VERSION_FILE} + git commit -m "Update version to {new_version}" + git push + git tag {new_version} + git push origin {new_version} +""" +import sys +import time +import re +import subprocess + +VERSION_FILE = 'package.json' +VERSION_REGEX = '(?<=version": ")(.*)(?=")' +ADDITIONAL_FILES_TO_COMMIT = [] + + +def _parse_version() -> str: + match = pattern.search(file_content) + if match: + return match.group() + else: + sys.exit('Version pattern not found in file. Check your regex!') + + +def _update_version_in_file(new_version): + new_file_content = pattern.sub(new_version, file_content) + with open(VERSION_FILE, 'w') as f: + f.write(new_file_content) + + +def _increment_version(old_version): + version_part = sys.argv[1] + old_version_as_list = old_version.split('.') + if version_part == 'major': + new_version = f'{int(old_version_as_list[0]) + 1}.0.0' + elif version_part == 'minor': + new_version = f'{old_version_as_list[0]}.{int(old_version_as_list[1]) + 1}.0' + else: + new_version = f'{old_version_as_list[0]}.{old_version_as_list[1]}.{int(old_version_as_list[2]) + 1}' + return new_version + + +def _run_software(): + subprocess.run('make run-detached', shell=True, check=True) + + +def _stop_software(): + subprocess.run('make stop', shell=True, check=True) + + +def _run_tests(): + time.sleep(10) + subprocess.run('make test-unit', shell=True, check=True) + time.sleep(10) + subprocess.run('make test-e2e', shell=True, check=True) + + +def _git_tag(): + print(f"Creating git tag for version {new_version}") + subprocess.run(f"git add {VERSION_FILE}", shell=True, check=True) + for file in ADDITIONAL_FILES_TO_COMMIT: + subprocess.run(f"git add {file}", shell=True, check=True) + subprocess.run(f"git commit -m \"Update version to {new_version}\"", shell=True, check=True) + subprocess.run("git push origin master", shell=True, check=True) + subprocess.run(f"git tag {new_version}", shell=True, check=True) + subprocess.run(f"git push origin {new_version}", shell=True, check=True) + + +if len(sys.argv) < 2: + sys.exit('No parameter given. Use \'major\'/\'minor\'/\'patch\'!') +pattern = re.compile(VERSION_REGEX) +with open(VERSION_FILE) as version_file: + file_content = version_file.read() + old_version = _parse_version() +new_version = _increment_version(old_version) +_update_version_in_file(new_version) +_run_software() +_run_tests() +_stop_software() +_git_tag()