Skip to content

Commit 0978396

Browse files
authored
Merge pull request #2041 from github/nickfyson/fix-changelog-backports
improve handling of changelog processing for backports
2 parents 144b7d5 + 8e086df commit 0978396

File tree

1 file changed

+56
-7
lines changed

1 file changed

+56
-7
lines changed

.github/update-release-branch.py

Lines changed: 56 additions & 7 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,60 @@ def get_today_string():
174175
today = datetime.datetime.today()
175176
return '{:%d %b %Y}'.format(today)
176177

178+
def process_changelog_for_backports(source_branch_major_version, target_branch_major_version):
179+
180+
# changelog entries can use the following format to indicate
181+
# that they only apply to newer versions
182+
some_versions_only_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()
191+
if not line:
192+
raise Exception('Could not find any change sections in CHANGELOG.md') # EOF
193+
194+
output += line
195+
if line.startswith('## '):
196+
line = line.replace(f'## {source_branch_major_version}', f'## {target_branch_major_version}')
197+
# we have found the first section, so now handle things differently
198+
break
199+
200+
# found_content tracks whether we hit two headings in a row
201+
found_content = False
202+
output += '\n'
203+
while True:
204+
line = f.readline()
205+
if not line:
206+
break # EOF
207+
line = line.rstrip('\n')
208+
209+
# filter out changenote entries that apply only to newer versions
210+
match = some_versions_only_regex.search(line)
211+
if match:
212+
if int(target_branch_major_version) < int(match.group(1)):
213+
continue
214+
215+
if line.startswith('## '):
216+
line = line.replace(f'## {source_branch_major_version}', f'## {target_branch_major_version}')
217+
if found_content == False:
218+
# we have found two headings in a row, so we need to add the placeholder message.
219+
output += 'No user facing changes.\n'
220+
found_content = False
221+
output += f'\n{line}\n\n'
222+
else:
223+
if line.strip() != '':
224+
found_content = True
225+
# we use the original line here, rather than the stripped version
226+
# so that we preserve indentation
227+
output += line + '\n'
228+
229+
with open('CHANGELOG.md', 'w') as f:
230+
f.write(output)
231+
177232
def update_changelog(version):
178233
if (os.path.exists('CHANGELOG.md')):
179234
content = ''
@@ -324,13 +379,7 @@ def main():
324379

325380
# Migrate the changelog notes from vLatest version numbers to vOlder version numbers
326381
print(f'Migrating changelog notes from v{source_branch_major_version} to v{target_branch_major_version}')
327-
subprocess.check_output(['sed', '-i', f's/^## {source_branch_major_version}\./## {target_branch_major_version}./g', 'CHANGELOG.md'])
328-
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'])
382+
process_changelog_for_backports(source_branch_major_version, 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)