From 8e4a6c7a903526fda317aa47e226cbd5a4f1d358 Mon Sep 17 00:00:00 2001 From: nickfyson Date: Fri, 15 Dec 2023 16:14:00 +0000 Subject: [PATCH 1/6] improve handling of changelog processing for backports --- .github/update-release-branch.py | 58 +++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/.github/update-release-branch.py b/.github/update-release-branch.py index b17cafc7a..948e10dfb 100644 --- a/.github/update-release-branch.py +++ b/.github/update-release-branch.py @@ -1,5 +1,6 @@ import argparse import datetime +import re from github import Github import json import os @@ -174,6 +175,56 @@ def get_today_string(): today = datetime.datetime.today() return '{:%d %b %Y}'.format(today) +def process_changelog_for_backports(target_version): + + # changelog entries use a speficic format to indicate + # that they only apply to newer versions + regex = re.compile(r'\[v(\d+)\+ only\]') + + output = '' + + with open('CHANGELOG.md', 'r') as f: + + # until we find the first section, just duplicate all lines + while True: + line = f.readline().rstrip() + + output += line + '\n' + if line.startswith('## '): + # we have found the first section, so now handle things differently + break + + # found_content tracks whether we hit two headings in a row + found_content = False + output += '\n' + while True: + line = f.readline() + if not line: + break # EOF + line = line.rstrip() + + # filter out changenote entries that apply only to newer versions + match = regex.search(line) + if match: + if int(target_version) < int(match.group(1)): + continue + + if line.startswith('## '): + if found_content == False: + # we have found two headings in a row, so we need to add the placeholder message. + output += 'No user facing changes.\n' + found_content = False + output += '\n' +line + '\n\n' + else: + if line.strip() != '': + found_content = True + # we use the original line here, rather than the stripped version + # so that we preserve indentation + output += line + '\n' + + with open('CHANGELOG.md', 'w') as f: + f.write(output) + def update_changelog(version): if (os.path.exists('CHANGELOG.md')): content = '' @@ -326,11 +377,8 @@ def main(): print(f'Migrating changelog notes from v{source_branch_major_version} to v{target_branch_major_version}') subprocess.check_output(['sed', '-i', f's/^## {source_branch_major_version}\./## {target_branch_major_version}./g', 'CHANGELOG.md']) - # Remove changelog notes from all versions that do not apply to the vOlder branch - print(f'Removing changelog notes that do not apply to v{target_branch_major_version}') - for v in range(int(source_branch_major_version), int(target_branch_major_version), -1): - print(f'Removing changelog notes that are tagged [v{v}+ only\]') - subprocess.check_output(['sed', '-i', f'/^- \[v{v}+ only\]/d', 'CHANGELOG.md']) + # process changelog for backport to target release branch + process_changelog_for_backports(target_branch_major_version) # Amend the commit generated by `npm version` to update the CHANGELOG run_git('add', 'CHANGELOG.md') From e0c2b0a8a0cf7360f5f17b1ee83b98565e81cbaf Mon Sep 17 00:00:00 2001 From: nickfyson Date: Fri, 15 Dec 2023 17:02:32 +0000 Subject: [PATCH 2/6] change version numbers inside processing function as well --- .github/update-release-branch.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/.github/update-release-branch.py b/.github/update-release-branch.py index 948e10dfb..b518cddf8 100644 --- a/.github/update-release-branch.py +++ b/.github/update-release-branch.py @@ -175,7 +175,7 @@ def get_today_string(): today = datetime.datetime.today() return '{:%d %b %Y}'.format(today) -def process_changelog_for_backports(target_version): +def process_changelog_for_backports(source_branch_major_version, target_branch_major_version): # changelog entries use a speficic format to indicate # that they only apply to newer versions @@ -191,6 +191,7 @@ def process_changelog_for_backports(target_version): output += line + '\n' if line.startswith('## '): + line = line.replace(f'## {source_branch_major_version}', f'## {target_branch_major_version}') # we have found the first section, so now handle things differently break @@ -206,10 +207,11 @@ def process_changelog_for_backports(target_version): # filter out changenote entries that apply only to newer versions match = regex.search(line) if match: - if int(target_version) < int(match.group(1)): + if int(target_branch_major_version) < int(match.group(1)): continue if line.startswith('## '): + line = line.replace(f'## {source_branch_major_version}', f'## {target_branch_major_version}') if found_content == False: # we have found two headings in a row, so we need to add the placeholder message. output += 'No user facing changes.\n' @@ -375,10 +377,7 @@ def main(): # Migrate the changelog notes from vLatest version numbers to vOlder version numbers print(f'Migrating changelog notes from v{source_branch_major_version} to v{target_branch_major_version}') - subprocess.check_output(['sed', '-i', f's/^## {source_branch_major_version}\./## {target_branch_major_version}./g', 'CHANGELOG.md']) - - # process changelog for backport to target release branch - process_changelog_for_backports(target_branch_major_version) + process_changelog_for_backports(source_branch_major_version, target_branch_major_version) # Amend the commit generated by `npm version` to update the CHANGELOG run_git('add', 'CHANGELOG.md') From ee5301261095bb1b9520ab787f030a4267b4a839 Mon Sep 17 00:00:00 2001 From: Nick Fyson Date: Fri, 15 Dec 2023 18:44:26 +0000 Subject: [PATCH 3/6] Apply suggestions from code review Co-authored-by: Henry Mercer --- .github/update-release-branch.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/update-release-branch.py b/.github/update-release-branch.py index b518cddf8..c97cb4140 100644 --- a/.github/update-release-branch.py +++ b/.github/update-release-branch.py @@ -177,7 +177,7 @@ def get_today_string(): def process_changelog_for_backports(source_branch_major_version, target_branch_major_version): - # changelog entries use a speficic format to indicate + # changelog entries can use the following format to indicate # that they only apply to newer versions regex = re.compile(r'\[v(\d+)\+ only\]') @@ -216,7 +216,7 @@ def process_changelog_for_backports(source_branch_major_version, target_branch_m # we have found two headings in a row, so we need to add the placeholder message. output += 'No user facing changes.\n' found_content = False - output += '\n' +line + '\n\n' + output += f'\n{line}\n\n' else: if line.strip() != '': found_content = True From fda1796670fb9ec77d29cb1b657308221a7ea429 Mon Sep 17 00:00:00 2001 From: nickfyson Date: Fri, 15 Dec 2023 18:45:36 +0000 Subject: [PATCH 4/6] rename regex for clarity --- .github/update-release-branch.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/update-release-branch.py b/.github/update-release-branch.py index c97cb4140..f9587884b 100644 --- a/.github/update-release-branch.py +++ b/.github/update-release-branch.py @@ -179,7 +179,7 @@ def process_changelog_for_backports(source_branch_major_version, target_branch_m # changelog entries can use the following format to indicate # that they only apply to newer versions - regex = re.compile(r'\[v(\d+)\+ only\]') + some_versions_only_regex = re.compile(r'\[v(\d+)\+ only\]') output = '' @@ -205,7 +205,7 @@ def process_changelog_for_backports(source_branch_major_version, target_branch_m line = line.rstrip() # filter out changenote entries that apply only to newer versions - match = regex.search(line) + match = some_versions_only_regex.search(line) if match: if int(target_branch_major_version) < int(match.group(1)): continue From 0724061f76ec91db7d4fecc5e58b09291e4405b5 Mon Sep 17 00:00:00 2001 From: nickfyson Date: Mon, 18 Dec 2023 10:05:44 +0000 Subject: [PATCH 5/6] preserve trailing whitespace when transforming CHANGELOG --- .github/update-release-branch.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/update-release-branch.py b/.github/update-release-branch.py index f9587884b..7274a6305 100644 --- a/.github/update-release-branch.py +++ b/.github/update-release-branch.py @@ -187,9 +187,9 @@ def process_changelog_for_backports(source_branch_major_version, target_branch_m # until we find the first section, just duplicate all lines while True: - line = f.readline().rstrip() + line = f.readline() - output += line + '\n' + output += line if line.startswith('## '): line = line.replace(f'## {source_branch_major_version}', f'## {target_branch_major_version}') # we have found the first section, so now handle things differently @@ -202,7 +202,7 @@ def process_changelog_for_backports(source_branch_major_version, target_branch_m line = f.readline() if not line: break # EOF - line = line.rstrip() + line = line.rstrip('\n') # filter out changenote entries that apply only to newer versions match = some_versions_only_regex.search(line) From 8e086df084a76c93568b2f0b0f274bd67f083195 Mon Sep 17 00:00:00 2001 From: nickfyson Date: Mon, 18 Dec 2023 10:08:49 +0000 Subject: [PATCH 6/6] raise explicit exception if EOF found when looking for changelog sections --- .github/update-release-branch.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/update-release-branch.py b/.github/update-release-branch.py index 7274a6305..ecde537a2 100644 --- a/.github/update-release-branch.py +++ b/.github/update-release-branch.py @@ -188,6 +188,8 @@ def process_changelog_for_backports(source_branch_major_version, target_branch_m # until we find the first section, just duplicate all lines while True: line = f.readline() + if not line: + raise Exception('Could not find any change sections in CHANGELOG.md') # EOF output += line if line.startswith('## '):