From 37839781aa4de86982f21af9ba75ff795f94a566 Mon Sep 17 00:00:00 2001 From: Francois Peverali Date: Fri, 24 Feb 2023 15:36:34 +0100 Subject: [PATCH 1/4] initial script --- scripts/release_publish.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 scripts/release_publish.py diff --git a/scripts/release_publish.py b/scripts/release_publish.py new file mode 100644 index 0000000..71742f2 --- /dev/null +++ b/scripts/release_publish.py @@ -0,0 +1,35 @@ +# replace_version in files + # Retrieve the current git branch name + # Save version pattern from branch name to new_version + # for each file in checklist (e.g. sushi-config.yaml) find old_version and replace with new_version - see checklist https://wiki.gematik.de/display/PTDATA/%28TEMPLATE%29+Release+Checkliste + # """ files: + # package.json + # sushi-config,yaml + # ISIk Basis anpassen (Warnung) + # RuleSet (s.u.) + # IG. Einführung """ + +import re +import subprocess + +# Retrieve the current git branch name +git_branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).strip().decode() + +# Define the regex pattern to match the version string +super_pattern = r'version\s*:\s*\d+\.\d+\.\d+' +#TODO pattern equals substring without "version" +#TODO define pattern_sushi_config = + +# Define the replacement string with the current git branch name +replacement = f'version: {git_branch}' + +# Open the input file and read its contents +with open('test.txt', 'r') as input_file: + input_text = input_file.read() + +# Use regex to search and replace all occurrences of the version string +output_text = re.sub(pattern, replacement, input_text) + +# Write the modified contents to the same file +with open('test.txt', 'w') as output_file: + output_file.write(output_text) \ No newline at end of file From c65c8ff9afb1542fba4684e54563d9abe22573d3 Mon Sep 17 00:00:00 2001 From: Robert Hoffmann Date: Mon, 27 Feb 2023 14:33:49 +0100 Subject: [PATCH 2/4] initial untested implementation --- scripts/release_publish.py | 92 +++++++++++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 20 deletions(-) diff --git a/scripts/release_publish.py b/scripts/release_publish.py index 71742f2..de238e1 100644 --- a/scripts/release_publish.py +++ b/scripts/release_publish.py @@ -4,32 +4,84 @@ # for each file in checklist (e.g. sushi-config.yaml) find old_version and replace with new_version - see checklist https://wiki.gematik.de/display/PTDATA/%28TEMPLATE%29+Release+Checkliste # """ files: # package.json - # sushi-config,yaml - # ISIk Basis anpassen (Warnung) - # RuleSet (s.u.) - # IG. Einführung """ + # sushi-config.yaml + # ISIk Basis anpassen (Warnung) TODO: Gibt es hier eine Datei? + # RuleSet (s.u.) TODO: Einheitliche Bennenung der RuleSet-Datei? + # IG. Einführung """ TODO: Gibt es eine Versionierung des IG import re import subprocess +import os -# Retrieve the current git branch name -git_branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).strip().decode() +class FileWithVersionToUpdate: + def __init__(self, filename, version_regex) -> None: + self.filename = filename + self.version_regex = version_regex + self.location = None -# Define the regex pattern to match the version string -super_pattern = r'version\s*:\s*\d+\.\d+\.\d+' -#TODO pattern equals substring without "version" -#TODO define pattern_sushi_config = + def set_file_location(self, location): + self.location = location -# Define the replacement string with the current git branch name -replacement = f'version: {git_branch}' -# Open the input file and read its contents -with open('test.txt', 'r') as input_file: - input_text = input_file.read() +def get_new_release_version_number() -> str: + # Retrieve the current git branch name + git_branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).strip().decode() + return git_branch + +def replace_version_in_files(files : list, new_release_version: str): + if files is None: + print("Error: No Files found!") + return + + for file in files: + replace_version_in_file(file,new_release_version) + +def replace_version_in_file(file: FileWithVersionToUpdate,new_release_version: str): + # Define the replacement string with the current git branch name + replacement = f'version: {new_release_version}' + + # Open the input file and read its contents + with open(file.location, 'r') as input_file: + input_text = input_file.read() + + # Use regex to search and replace all occurrences of the version string + output_text = re.sub(file.version_regex, replacement, input_text) + print(f"Info: Replaced version with '{replacement}' in file '{file.location}'.") + + # Write the modified contents to the same file + with open(file.location, 'w') as output_file: + output_file.write(output_text) + +def get_file_to_update_list(): + file_list = [] + file_list.append(FileWithVersionToUpdate('package.json',"version\s*:\s*\d+\.\d+\.\d+")) + file_list.append(FileWithVersionToUpdate('sushi-config.yaml',"version\s*:\s*\d+\.\d+\.\d+")) + file_list.append(FileWithVersionToUpdate('ruleset.fsh',"version\s*:\s*\d+\.\d+\.\d+")) + file_list.append(FileWithVersionToUpdate('package.json',"version\s*:\s*\d+\.\d+\.\d+")) + return file_list + +def locate_files_in_current_project(files: list): + for current_file in files: + + file_location = find_file(current_file.filename, "..") + if file_location is not None: + current_file.set_file_location(file_location) + else: + print(f"Warning: File '{current_file.filename}' not found.") + +def find_file(name, path="."): + for root, dirs, files in os.walk(path): + + if name in files: + print(f"Info: Searching for '{name}' in {root}.") + return os.path.join(root, name) + return None -# Use regex to search and replace all occurrences of the version string -output_text = re.sub(pattern, replacement, input_text) +def main(): + file_to_update_list = get_file_to_update_list() + file_list = locate_files_in_current_project(file_to_update_list) + new_release_version = get_new_release_version_number() + replace_version_in_files(file_list, new_release_version) -# Write the modified contents to the same file -with open('test.txt', 'w') as output_file: - output_file.write(output_text) \ No newline at end of file +if __name__ == "__main__": + main() \ No newline at end of file From b3f4fcec32c033c83289222bcdc073a5fb141a55 Mon Sep 17 00:00:00 2001 From: Robert Hoffmann Date: Mon, 27 Feb 2023 15:28:25 +0100 Subject: [PATCH 3/4] fixed bug that prevented file location --- scripts/release_publish.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/release_publish.py b/scripts/release_publish.py index de238e1..9c36264 100644 --- a/scripts/release_publish.py +++ b/scripts/release_publish.py @@ -61,19 +61,21 @@ def get_file_to_update_list(): return file_list def locate_files_in_current_project(files: list): + return_list = [] for current_file in files: - - file_location = find_file(current_file.filename, "..") + file_location = find_file(current_file.filename, ".") if file_location is not None: current_file.set_file_location(file_location) + return_list.append(current_file) else: print(f"Warning: File '{current_file.filename}' not found.") + return return_list def find_file(name, path="."): for root, dirs, files in os.walk(path): if name in files: - print(f"Info: Searching for '{name}' in {root}.") + print(f"Info: Found '{name}' in {root}.") return os.path.join(root, name) return None From 61a6935670d735158030b4d5b1bcda232c7ebeb1 Mon Sep 17 00:00:00 2001 From: Robert Hoffmann Date: Fri, 3 Mar 2023 16:08:50 +0100 Subject: [PATCH 4/4] added option to ouput and replaced correct regexes --- scripts/release_publish.py | 72 ++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/scripts/release_publish.py b/scripts/release_publish.py index 9c36264..538a2d5 100644 --- a/scripts/release_publish.py +++ b/scripts/release_publish.py @@ -1,17 +1,7 @@ -# replace_version in files - # Retrieve the current git branch name - # Save version pattern from branch name to new_version - # for each file in checklist (e.g. sushi-config.yaml) find old_version and replace with new_version - see checklist https://wiki.gematik.de/display/PTDATA/%28TEMPLATE%29+Release+Checkliste - # """ files: - # package.json - # sushi-config.yaml - # ISIk Basis anpassen (Warnung) TODO: Gibt es hier eine Datei? - # RuleSet (s.u.) TODO: Einheitliche Bennenung der RuleSet-Datei? - # IG. Einführung """ TODO: Gibt es eine Versionierung des IG - import re import subprocess import os +import argparse class FileWithVersionToUpdate: def __init__(self, filename, version_regex) -> None: @@ -23,8 +13,7 @@ def set_file_location(self, location): self.location = location -def get_new_release_version_number() -> str: - # Retrieve the current git branch name +def get_new_release_version_from_branch_name() -> str: git_branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).strip().decode() return git_branch @@ -37,29 +26,23 @@ def replace_version_in_files(files : list, new_release_version: str): replace_version_in_file(file,new_release_version) def replace_version_in_file(file: FileWithVersionToUpdate,new_release_version: str): - # Define the replacement string with the current git branch name - replacement = f'version: {new_release_version}' - - # Open the input file and read its contents with open(file.location, 'r') as input_file: input_text = input_file.read() - # Use regex to search and replace all occurrences of the version string - output_text = re.sub(file.version_regex, replacement, input_text) - print(f"Info: Replaced version with '{replacement}' in file '{file.location}'.") + output_text = re.sub(file.version_regex, rf'\g<1>{new_release_version}\g<3>', input_text) + print(f"Info: Replaced version with '{new_release_version}' in file '{file.location}'.") - # Write the modified contents to the same file with open(file.location, 'w') as output_file: output_file.write(output_text) def get_file_to_update_list(): file_list = [] - file_list.append(FileWithVersionToUpdate('package.json',"version\s*:\s*\d+\.\d+\.\d+")) - file_list.append(FileWithVersionToUpdate('sushi-config.yaml',"version\s*:\s*\d+\.\d+\.\d+")) - file_list.append(FileWithVersionToUpdate('ruleset.fsh',"version\s*:\s*\d+\.\d+\.\d+")) - file_list.append(FileWithVersionToUpdate('package.json',"version\s*:\s*\d+\.\d+\.\d+")) + file_list.append(FileWithVersionToUpdate('package.json', r'("version":\s*")([\d\.]+)(")')) + file_list.append(FileWithVersionToUpdate('sushi-config.yaml', r'(version:\s*")(\d+\.\d+\.\d+)(")')) + file_list.append(FileWithVersionToUpdate('ruleset.fsh', r'(\*\s*version\s*=\s*")([\d\.]+)(")')) return file_list + def locate_files_in_current_project(files: list): return_list = [] for current_file in files: @@ -79,10 +62,47 @@ def find_file(name, path="."): return os.path.join(root, name) return None +def get_latest_release_tag(): + cmd = 'git describe --abbrev=0 --tags --match "v*.*.*" HEAD' + try: + output = subprocess.check_output(cmd, shell=True) + return output.decode().strip() + except subprocess.CalledProcessError: + return None + +def output_commit_messages_since_last_release(): + latest_release_tag = get_latest_release_tag() + if latest_release_tag is None: + print("Warning: No release tag found.") + return + + cmd = f'git log --pretty=format:"%s" {latest_release_tag}..HEAD' + try: + output = subprocess.check_output(cmd, shell=True) + print(output.decode()) + except subprocess.CalledProcessError: + print("Warning: Failed to get commit messages.") + def main(): + parser = argparse.ArgumentParser(description='Update release version number') + parser.add_argument('-b', '--branch', action='store_true', help='get new version from branch name') + parser.add_argument('-v', '--version', type=str, help='specify new version number') + parser.add_argument('-o', '--output', action='store_true', help='output commit messages since last release') + + args = parser.parse_args() + + if args.version: + new_release_version = args.version + elif args.branch: + new_release_version = get_new_release_version_from_branch_name() + else: + parser.error('No new release version specified. Please use either -v or -b to specify the new release version.') + + if args.output: + output_commit_messages_since_last_release() + file_to_update_list = get_file_to_update_list() file_list = locate_files_in_current_project(file_to_update_list) - new_release_version = get_new_release_version_number() replace_version_in_files(file_list, new_release_version) if __name__ == "__main__":