|
| 1 | +import datetime |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +EMPTY_CHANGELOG = """# CodeQL Action Changelog |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +def get_today_string(): |
| 10 | + today = datetime.datetime.today() |
| 11 | + return '{:%d %b %Y}'.format(today) |
| 12 | + |
| 13 | +# Include everything up to and after the first heading, |
| 14 | +# but not the first heading and body. |
| 15 | +def drop_unreleased_section(lines: list[str]): |
| 16 | + before_first_section = '' |
| 17 | + after_first_section = '' |
| 18 | + found_first_section = False |
| 19 | + skipped_first_section = False |
| 20 | + |
| 21 | + for i, line in enumerate(lines): |
| 22 | + if line.startswith('## ') and not found_first_section: |
| 23 | + found_first_section = True |
| 24 | + elif line.startswith('## ') and found_first_section: |
| 25 | + skipped_first_section = True |
| 26 | + |
| 27 | + if not found_first_section: |
| 28 | + before_first_section += line |
| 29 | + if skipped_first_section: |
| 30 | + after_first_section += line |
| 31 | + |
| 32 | + return (before_first_section, after_first_section) |
| 33 | + |
| 34 | +def update_changelog(target_version, rollback_version, new_version): |
| 35 | + before_first_section = EMPTY_CHANGELOG |
| 36 | + after_first_section = '' |
| 37 | + |
| 38 | + if (os.path.exists('CHANGELOG.md')): |
| 39 | + with open('CHANGELOG.md', 'r') as f: |
| 40 | + (before_first_section, after_first_section) = drop_unreleased_section(f.readlines()) |
| 41 | + |
| 42 | + newHeader = f'## {new_version} - {get_today_string()}\n' |
| 43 | + |
| 44 | + print(before_first_section, end="") |
| 45 | + print(newHeader) |
| 46 | + print(f"This release rolls back {rollback_version} due to issues with that release. It is identical to {target_version}.\n") |
| 47 | + print(after_first_section) |
| 48 | + |
| 49 | +# We expect three version strings as input: |
| 50 | +# |
| 51 | +# - target_version: the version that we are re-releasing as `new_version` |
| 52 | +# - rollback_version: the version that we are rolling back, typically the one that followed `target_version` |
| 53 | +# - new_version: the new version that we are releasing `target_version` as, typically the one that follows `rollback_version` |
| 54 | +# |
| 55 | +# Example: python3 .github/workflows/script/rollback_changelog.py "1.2.3" "1.2.4" "1.2.5" |
| 56 | +if len(sys.argv) < 4: |
| 57 | + raise Exception('Expecting argument: target_version rollback_version new_version') |
| 58 | + |
| 59 | +target_version = sys.argv[1] |
| 60 | +rollback_version = sys.argv[2] |
| 61 | +new_version = sys.argv[3] |
| 62 | +update_changelog(target_version, rollback_version, new_version) |
0 commit comments