|
1 | 1 | import argparse
|
2 | 2 | import datetime
|
| 3 | +import re |
3 | 4 | from github import Github
|
4 | 5 | import json
|
5 | 6 | import os
|
@@ -174,6 +175,56 @@ def get_today_string():
|
174 | 175 | today = datetime.datetime.today()
|
175 | 176 | return '{:%d %b %Y}'.format(today)
|
176 | 177 |
|
| 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 | + |
177 | 228 | def update_changelog(version):
|
178 | 229 | if (os.path.exists('CHANGELOG.md')):
|
179 | 230 | content = ''
|
@@ -322,15 +373,13 @@ def main():
|
322 | 373 | subprocess.check_output(['npm', 'version', version, '--no-git-tag-version'])
|
323 | 374 | run_git('add', 'package.json', 'package-lock.json')
|
324 | 375 |
|
| 376 | + # TODO move this logic into the process_changelog_for_backports function |
325 | 377 | # Migrate the changelog notes from vLatest version numbers to vOlder version numbers
|
326 | 378 | print(f'Migrating changelog notes from v{source_branch_major_version} to v{target_branch_major_version}')
|
327 | 379 | subprocess.check_output(['sed', '-i', f's/^## {source_branch_major_version}\./## {target_branch_major_version}./g', 'CHANGELOG.md'])
|
328 | 380 |
|
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) |
334 | 383 |
|
335 | 384 | # Amend the commit generated by `npm version` to update the CHANGELOG
|
336 | 385 | run_git('add', 'CHANGELOG.md')
|
|
0 commit comments