Skip to content

Commit

Permalink
improve handling of changelog processing for backports
Browse files Browse the repository at this point in the history
  • Loading branch information
nickfyson committed Dec 15, 2023
1 parent 511f073 commit 8e4a6c7
Showing 1 changed file with 53 additions and 5 deletions.
58 changes: 53 additions & 5 deletions .github/update-release-branch.py
@@ -1,5 +1,6 @@
import argparse
import datetime
import re
from github import Github
import json
import os
Expand Down Expand Up @@ -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 = ''
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 8e4a6c7

Please sign in to comment.