Skip to content

Commit f87484e

Browse files
committed
improve handling of changelog processing for backports
1 parent 511f073 commit f87484e

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

.github/update-release-branch.py

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import argparse
22
import datetime
3+
import re
34
from github import Github
45
import json
56
import os
@@ -174,6 +175,56 @@ def get_today_string():
174175
today = datetime.datetime.today()
175176
return '{:%d %b %Y}'.format(today)
176177

178+
def process_changelog_for_backports(target_version):
179+
180+
# changelog entries use a speficic format to indicate
181+
# that they only apply to newer versions
182+
regex = re.compile(r'\[v(\d+)\+ only\]')
183+
184+
output = ''
185+
186+
with open('CHANGELOG.md', 'r') as f:
187+
188+
# until we find the first section, just duplicate all lines
189+
while True:
190+
line = f.readline().rstrip()
191+
192+
output += line + '\n'
193+
if line.startswith('## '):
194+
# we have found the first section, so now handle things differently
195+
break
196+
197+
# found_content tracks whether we hit two headings in a row
198+
found_content = False
199+
output += '\n'
200+
while True:
201+
line = f.readline()
202+
if not line:
203+
break # EOF
204+
line = line.rstrip()
205+
206+
# filter out changenote entries that apply only to newer versions
207+
match = regex.search(line)
208+
if match:
209+
if int(target_version) < int(match.group(1)):
210+
continue
211+
212+
if line.startswith('## '):
213+
if found_content == False:
214+
# we have found two headings in a row, so we need to add the placeholder message.
215+
output += 'No user facing changes.\n'
216+
found_content = False
217+
output += '\n' +line + '\n\n'
218+
else:
219+
if line.strip() != '':
220+
found_content = True
221+
# we use the original line here, rather than the stripped version
222+
# so that we preserve indentation
223+
output += line + '\n'
224+
225+
with open('CHANGELOG.md', 'w') as f:
226+
f.write(output)
227+
177228
def update_changelog(version):
178229
if (os.path.exists('CHANGELOG.md')):
179230
content = ''
@@ -322,15 +373,13 @@ def main():
322373
subprocess.check_output(['npm', 'version', version, '--no-git-tag-version'])
323374
run_git('add', 'package.json', 'package-lock.json')
324375

376+
# TODO move this logic into the process_changelog_for_backports function
325377
# Migrate the changelog notes from vLatest version numbers to vOlder version numbers
326378
print(f'Migrating changelog notes from v{source_branch_major_version} to v{target_branch_major_version}')
327379
subprocess.check_output(['sed', '-i', f's/^## {source_branch_major_version}\./## {target_branch_major_version}./g', 'CHANGELOG.md'])
328380

329-
# Remove changelog notes from all versions that do not apply to the vOlder branch
330-
print(f'Removing changelog notes that do not apply to v{target_branch_major_version}')
331-
for v in range(int(source_branch_major_version), int(target_branch_major_version), -1):
332-
print(f'Removing changelog notes that are tagged [v{v}+ only\]')
333-
subprocess.check_output(['sed', '-i', f'/^- \[v{v}+ only\]/d', 'CHANGELOG.md'])
381+
# process changelog for backport to target release branch
382+
process_changelog_for_backports(target_branch_major_version)
334383

335384
# Amend the commit generated by `npm version` to update the CHANGELOG
336385
run_git('add', 'CHANGELOG.md')

0 commit comments

Comments
 (0)