From bbc94a9dec98e95937dd88d642c0902e062cc703 Mon Sep 17 00:00:00 2001 From: Ludovico Magnocavallo Date: Tue, 13 Sep 2022 11:41:28 +0200 Subject: [PATCH] fix changelog generator (#811) --- CHANGELOG.md | 23 ++++++++++++++++---- tools/changelog.py | 54 +++++++++++++++++++++++++--------------------- 2 files changed, 48 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index affaf977..bda695da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,26 @@ # Changelog All notable changes to this project will be documented in this file. + ## [Unreleased] + +### BLUEPRINTS + +- [[#809](https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/pull/809)] Renaming and moving blueprints ([juliocc](https://github.com/juliocc)) + +### DOCUMENTATION + +- [[#806](https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/pull/806)] Companion Guide ([ajlopezn](https://github.com/ajlopezn)) + +### FAST + +- [[#807](https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/pull/807)] FAST: refactor Gitlab template ([ludoo](https://github.com/ludoo)) + +### TOOLS + +- [[#810](https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/pull/810)] Fully recursive e2e test runner for examples ([juliocc](https://github.com/juliocc)) ## [18.0.0] - 2022-09-09 @@ -11,7 +28,7 @@ All notable changes to this project will be documented in this file. - [[#808](https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/pull/808)] Rename examples to blueprints ([juliocc](https://github.com/juliocc)) -### FAST +### FAST - [[#804](https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/pull/804)] GKE CI/CD ([ludoo](https://github.com/ludoo)) - [[#803](https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/pull/803)] FAST: fix GCS location in stage 00 and 01 ([miklosn](https://github.com/miklosn)) @@ -119,7 +136,6 @@ All notable changes to this project will be documented in this file. - [[#680](https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/pull/680)] Tools: fix `ValueError` raised in `check_names.py` when overlong names are detected ([27Bslash6](https://github.com/27Bslash6)) - [[#672](https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/pull/672)] Module attribution and version updater tool, plus release automation ([rosmo](https://github.com/rosmo)) - ## [16.0.0] - 2022-06-06 - add support for [Spot VMs](https://cloud.google.com/compute/docs/instances/spot) to `gke-nodepool` module @@ -611,7 +627,6 @@ All notable changes to this project will be documented in this file. - merge development branch with suite of new modules and end-to-end examples - [Unreleased]: https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/compare/v18.0.0...HEAD [18.0.0]: https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/compare/v16.0.0...v18.0.0 @@ -677,4 +692,4 @@ All notable changes to this project will be documented in this file. [1.3.0]: https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/compare/v1.2.0...v1.3.0 [1.2.0]: https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/compare/v1.1.0...v1.2.0 [1.1.0]: https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/compare/v1.0.0...v1.1.0 -[1.0.0]: https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/compare/v0.1...v1.0.0 \ No newline at end of file +[1.0.0]: https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/compare/v0.1...v1.0.0 diff --git a/tools/changelog.py b/tools/changelog.py index 3b071e92..a33b7129 100755 --- a/tools/changelog.py +++ b/tools/changelog.py @@ -19,13 +19,17 @@ import collections import ghapi.all import iso8601 +LINK_MARKER = '' ORG = 'GoogleCloudPlatform' REPO = 'cloud-foundation-fabric' URL = f'https://github.com/{ORG}/{REPO}' PullRequest = collections.namedtuple('PullRequest', 'id author title merged_at labels') -Release = collections.namedtuple('Release', 'name published since pulls') +FileRelease = collections.namedtuple('FileRelease', + 'name published since content') +GitRelease = collections.namedtuple('GitRelease', 'name published since pulls') +Section = collections.namedtuple('Section', 'text') class Error(Exception): @@ -51,39 +55,41 @@ def changelog_load(path): with open(path) as f: for l in f.readlines(): l = l.strip() + if l.startswith(LINK_MARKER): + break if l.startswith('## '): name, _, date = l[3:].partition(' - ') - releases.append(Release(name[1:-1], date, None, [])) - elif l.startswith('- '): - if not releases: - raise Error(f'Pull found with no releases: {l}') - releases[-1].pulls.append(l) + releases.append(FileRelease(name[1:-1], date, None, [])) + elif releases: + releases[-1].content.append(l) return releases except (IOError, OSError) as e: raise Error(f'Cannot open {path}: {e.args[0]}') -def changelog_dumps(releases, overrides=None): +def changelog_dumps(file_releases, git_releases=None): 'Return formatted changelog from structured data, overriding versions.' - overrides = overrides or {} + git_releases = git_releases or {} buffer = [ ('# Changelog\n\n' - 'All notable changes to this project will be documented in this file.\n') + 'All notable changes to this project will be documented in this file.\n' + '\n') ] ref_buffer = [''] - for i, release in enumerate(releases): - name, published, _, pulls = release - prev_name = releases[i + 1].name if i + 1 < len(releases) else '0.1' + for i, release in enumerate(file_releases): + name, published, _, items = release + prev_name = file_releases[i + + 1].name if i + 1 < len(file_releases) else '0.1' if name != 'Unreleased': - buffer.append(f'## [{name}] - {published}\n') + buffer.append(f'## [{name}] - {published}') ref_buffer.append(f'[{name}]: {URL}/compare/v{prev_name}...v{name}') else: - buffer.append(f'## [{name}]\n') + buffer.append(f'## [{name}]') ref_buffer.append(f'[Unreleased]: {URL}/compare/v{prev_name}...HEAD') - override = overrides.get(name, overrides.get(f'v{name}')) - if override: - buffer.append(f'\n') - pulls = group_pulls(override.pulls) + release = git_releases.get(name, git_releases.get(f'v{name}')) + if release: + buffer.append(f'') + pulls = group_pulls(release.pulls) for k in sorted(pulls.keys(), key=lambda s: s or ''): if k is not None: buffer.append(f'### {k}\n') @@ -91,10 +97,8 @@ def changelog_dumps(releases, overrides=None): buffer.append(format_pull(pull)) buffer.append('') else: - for pull in pulls: - buffer.append(pull) - buffer.append('') - return '\n'.join(buffer + [''] + ref_buffer) + buffer.append('\n'.join(items)) + return '\n'.join(buffer + ref_buffer + ['']) def format_pull(pull): @@ -157,10 +161,10 @@ def get_releases(api, filter_names=None): for r in _paginate(api.repos.list_releases): published = iso8601.parse_date(r['published_at']) if not filter_names or buffer.name in filter_names: - yield Release(buffer.name, buffer.published, published, []) + yield GitRelease(buffer.name, buffer.published, published, []) buffer = Buffer(r['name'], published) if buffer and (not filter_names or buffer.name in filter_names): - yield Release(buffer.name, buffer.published, None, []) + yield GitRelease(buffer.name, buffer.published, None, []) @click.command @@ -172,7 +176,7 @@ def get_releases(api, filter_names=None): ) @click.option('--token', required=True, envvar='GH_TOKEN', help='GitHub API token.') -@click.option('--write/-w', is_flag=True, required=False, default=False, +@click.option('--write', '-w', is_flag=True, required=False, default=False, help='Write modified changelog file.') @click.argument('changelog', required=False, default='CHANGELOG.md', type=click.Path(exists=True))