diff --git a/.github/workflows/merge-pr.yml b/.github/workflows/merge-pr.yml new file mode 100644 index 00000000..05946e8d --- /dev/null +++ b/.github/workflows/merge-pr.yml @@ -0,0 +1,51 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: Post-merge tasks + +on: + pull_request: + branches: + - master + types: + - closed + +env: + PYTHON_VERSION: 3.10 + +jobs: + if_merged: + if: github.event.pull_request.merged == true + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: ${{ env.PYTHON_VERSION }} + - name: Install dependencies + run: | + pip install -r tools/requirements.txt + - name: Update Changelog + run: | + python3 tools/changelog.py --token secrets.GITHUB_TOKEN CHANGELOG.md + - name: Commit and push Changelog + env: + CI_COMMIT_MESSAGE: Update Changelog + CI_COMMIT_AUTHOR: Fabric Repo Workflows + run: | + git config --global user.name "${{ env.CI_COMMIT_AUTHOR }}" + git config --global user.email "username@users.noreply.github.com" + git commit -a -m "${{ env.CI_COMMIT_MESSAGE }}" + git push diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b82c678..4c268bd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. ## Unreleased + - [[#761](https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/pull/761)] Fix gke hub module features condition (ludoo) - [[#760](https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/pull/760)] GKE hub module refactor (ludoo) - [[#759](https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/pull/759)] FIX: Missing value to format principalSet (imp14a) @@ -58,6 +59,7 @@ All notable changes to this project will be documented in this file. - [[#675](https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/pull/675)] FAST: Fix audit logs when using pubsub as destination (juliocc) - [[#674](https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/pull/674)] FAST: Remove team folders comment from 01 variables, clarify README (ludoo) - [[#672](https://github.com/GoogleCloudPlatform/cloud-foundation-fabric/pull/672)] Module attribution and version updater tool, plus release automation (rosmo) + ## [16.0.0] - 2022-06-06 diff --git a/tools/changelog.py b/tools/changelog.py new file mode 100755 index 00000000..e5113f8e --- /dev/null +++ b/tools/changelog.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python3 +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import click +import collections +import os +import pprint +import re + +import ghapi.all +import iso8601 + +MARK_BEGIN = '' +MARK_END = '' +ORG = 'GoogleCloudPlatform' +REPO = 'cloud-foundation-fabric' + +PullRequest = collections.namedtuple('PullRequest', 'id author title merged_at') + + +def format_pull(pr): + url = f'https://github.com/{ORG}/{REPO}/pull/' + return f'- [[#{pr.id}]({url}{pr.id})] {pr.title} ({pr.author})' + + +def get_api(token, owner=ORG, name=REPO): + return ghapi.all.GhApi(owner=owner, repo=name, token=token) + + +def get_pulls(token, api=None): + api = api or get_api(token) + release = api.repos.get_latest_release() + release_published_at = iso8601.parse_date(release.published_at) + + while True: + page = 1 + for item in api.pulls.list(base='master', state='closed', sort='updated', + direction='desc', page=page, per_page=100): + try: + merged_at = iso8601.parse_date(item['merged_at']) + except iso8601.ParseError: + continue + pr = PullRequest(item['number'], item['user']['login'], item['title'], + merged_at) + if pr.merged_at <= release_published_at: + page = None + break + yield pr + if page is None: + break + page += 1 + + +def write_doc(path, snippet): + 'Replace changelog file.' + try: + doc = open(path).read() + except (IOError, OSError) as e: + raise SystemExit(f'Error opening {path}: {e.args[0]}') + m = re.search('(?sm)%s\n(.*)\n%s' % (MARK_BEGIN, MARK_END), doc) + if not m: + raise SystemExit('Mark not found.') + start, end = m.start(), m.end() + try: + open(path, 'w').write('\n'.join([ + doc[:start].rstrip(), + f'\n{MARK_BEGIN}', + snippet, + f'{MARK_END}\n', + doc[end:].lstrip(), + ])) + except (IOError, OSError) as e: + raise SystemExit(f'Error replacing {path}: {e.args[0]}') + + +@click.command +@click.option('--token', required=True, envvar='GH_TOKEN') +@click.argument('changelog', required=False, type=click.Path(exists=True)) +def main(token, changelog=None): + buffer = [] + for pr in get_pulls(token=token): + buffer.append(format_pull(pr)) + buffer = '\n'.join(buffer) + if not changelog: + print(buffer) + else: + write_doc(changelog, buffer) + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/tools/requirements.txt b/tools/requirements.txt index 8c7dd54a..1b01cfa0 100644 --- a/tools/requirements.txt +++ b/tools/requirements.txt @@ -1,5 +1,7 @@ click deepdiff +ghapi +iso8601 marko requests yamale