|
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,59 @@ 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(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 the first changed section in CHANGELOG') # EOF |
| 193 | + |
| 194 | + if line.startswith('## '): |
| 195 | + line = line.replace(f'## {source_branch_major_version}', f'## {target_branch_major_version}') |
| 196 | + # we have found the first section, so now handle things differently |
| 197 | + break |
| 198 | + |
| 199 | + # found_content tracks whether we hit two headings in a row |
| 200 | + found_content = False |
| 201 | + output += '\n' |
| 202 | + while True: |
| 203 | + line = f.readline() |
| 204 | + if not line: |
| 205 | + break # EOF |
| 206 | + line = line.rstrip('\n') |
| 207 | + |
| 208 | + # filter out changenote entries that apply only to newer versions |
| 209 | + match = some_versions_only_regex.search(line) |
| 210 | + if match: |
| 211 | + if int(target_branch_major_version) < int(match.group(1)): |
| 212 | + continue |
| 213 | + |
| 214 | + if line.startswith('## '): |
| 215 | + line = line.replace(f'## {source_branch_major_version}', f'## {target_branch_major_version}') |
| 216 | + if found_content == False: |
| 217 | + # we have found two headings in a row, so we need to add the placeholder message. |
| 218 | + output += 'No user facing changes.\n' |
| 219 | + found_content = False |
| 220 | + output += f'\n{line}\n\n' |
| 221 | + else: |
| 222 | + if line.strip() != '': |
| 223 | + found_content = True |
| 224 | + # we use the original line here, rather than the stripped version |
| 225 | + # so that we preserve indentation |
| 226 | + output += line + '\n' |
| 227 | + |
| 228 | + with open('CHANGELOG.md', 'w') as f: |
| 229 | + f.write(output) |
| 230 | + |
177 | 231 | def update_changelog(version):
|
178 | 232 | if (os.path.exists('CHANGELOG.md')):
|
179 | 233 | content = ''
|
@@ -201,7 +255,7 @@ def main():
|
201 | 255 | '--repository-nwo',
|
202 | 256 | type=str,
|
203 | 257 | required=True,
|
204 |
| - help='The nwo of the repository, for example github/codeql-action.' |
| 258 | + help='The nwo of the repository, for example nickfyson-org/codeql-action.' |
205 | 259 | )
|
206 | 260 | parser.add_argument(
|
207 | 261 | '--source-branch',
|
@@ -324,13 +378,7 @@ def main():
|
324 | 378 |
|
325 | 379 | # Migrate the changelog notes from vLatest version numbers to vOlder version numbers
|
326 | 380 | 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']) |
| 381 | + process_changelog_for_backports(source_branch_major_version, target_branch_major_version) |
334 | 382 |
|
335 | 383 | # Amend the commit generated by `npm version` to update the CHANGELOG
|
336 | 384 | run_git('add', 'CHANGELOG.md')
|
|
0 commit comments