|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import re |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | + |
| 7 | +def read_file(file_name): |
| 8 | + with open(file_name, 'r', encoding="utf8") as file: |
| 9 | + return file.read() |
| 10 | + |
| 11 | + |
| 12 | +def write_file(file_name, data): |
| 13 | + with open(file_name, 'w', encoding="utf8") as file: |
| 14 | + file.write(data) |
| 15 | + |
| 16 | +unchanged_sections = [ |
| 17 | +"""### 🚧 Known Issues |
| 18 | +
|
| 19 | +- |
| 20 | +
|
| 21 | +""", |
| 22 | +"""### 🔧 Compatibility Notes |
| 23 | +
|
| 24 | +- |
| 25 | +
|
| 26 | +""", |
| 27 | +"""### ✨ New Functionality |
| 28 | +
|
| 29 | +- |
| 30 | +
|
| 31 | +""", |
| 32 | +"""### 📈 Improvements |
| 33 | +
|
| 34 | +- |
| 35 | +
|
| 36 | +""", |
| 37 | +"""### 🐛 Fixed Issues |
| 38 | +
|
| 39 | +- |
| 40 | +"""] |
| 41 | + |
| 42 | +def remove_unchanged_sections(file, unchanged_sections): |
| 43 | + for unchanged_section in unchanged_sections: |
| 44 | + # if file contains unchanged_section, remove it |
| 45 | + file = re.sub(unchanged_section, "", file) |
| 46 | + return file |
| 47 | + |
| 48 | +def set_header(file, version): |
| 49 | + date = datetime.today().strftime('%B %d, %Y') |
| 50 | + # Replace the first line with: ## 5.2.0 - January 17, 2024 |
| 51 | + file = re.sub("^## .*", "## " + version + " - " + date, file) |
| 52 | + return file |
| 53 | + |
| 54 | +def link_github_release(file, version): |
| 55 | + old_github_release_link = "\[All Release Changes\]\(https://github.com/SAP/ai-sdk-java/releases/\)" |
| 56 | + new_github_release_link = "[All Release Changes](https://github.com/SAP/ai-sdk-java/releases/tag/rel%2F"+version+")" |
| 57 | + file = re.sub(old_github_release_link, new_github_release_link, file) |
| 58 | + return file |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +releases_pattern = re.compile(r"^## ") |
| 63 | +def count_releases(filename): |
| 64 | + count = 0 |
| 65 | + with open(filename, 'r', encoding="utf-8") as file: |
| 66 | + for line in file: |
| 67 | + if releases_pattern.match(line): |
| 68 | + count += 1 |
| 69 | + return count |
| 70 | + |
| 71 | +def find_target_file(version): |
| 72 | + # release-notes-X-to-Y.mdx with every 15 versions the index increases by 15 and stays the same for 15 versions |
| 73 | + minor_version = int(version.split(".")[1]) |
| 74 | + index = minor_version // 15 * 15 |
| 75 | + return "release-notes-" + str(index) + "-to-" + str(index + 14) + ".mdx" |
| 76 | + |
| 77 | +def write_release_notes(folder, target_file): |
| 78 | + absolute_target_file = os.path.join(folder, target_file) |
| 79 | + |
| 80 | + # if target_file is a file, prepend the new release notes at the top |
| 81 | + if os.path.isfile(absolute_target_file): |
| 82 | + existing_file = read_file(absolute_target_file) |
| 83 | + write_file(absolute_target_file, file + "\n" + existing_file) |
| 84 | + # if target_file is not a file, create it |
| 85 | + else: |
| 86 | + write_file(absolute_target_file, file) |
| 87 | + |
| 88 | + |
| 89 | +file_name = "docs/release-notes/release_notes.md" |
| 90 | + |
| 91 | +if __name__ == '__main__': |
| 92 | + parser = argparse.ArgumentParser(description='SAP Cloud SDK for AI (for Java) - Release Notes formatting script.') |
| 93 | + |
| 94 | + parser.add_argument('--version', metavar='VERSION', help='The version to be released.', required=True) |
| 95 | + parser.add_argument('--folder', metavar='FOLDER', help='The ai-sdk-java/docs/release-notes folder.', required=True) |
| 96 | + args = parser.parse_args() |
| 97 | + |
| 98 | + file = read_file(file_name) |
| 99 | + file = remove_unchanged_sections(file, unchanged_sections) |
| 100 | + file = set_header(file, args.version) |
| 101 | + file = link_github_release(file, args.version) |
| 102 | + |
| 103 | + target_file = find_target_file(args.version) |
| 104 | + |
| 105 | + |
| 106 | + folder_path = args.folder |
| 107 | + write_release_notes(folder_path, target_file) |
| 108 | + |
| 109 | + # delete (temporary) release-notes file so it does not appear in the released version |
| 110 | + os.remove(file_name) |
0 commit comments