Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update changelog on pull request merge #762

Merged
merged 2 commits into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .github/workflows/merge-pr.yml
Original file line number Diff line number Diff line change
@@ -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 "[email protected]"
git commit -a -m "${{ env.CI_COMMIT_MESSAGE }}"
git push
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.

## Unreleased

<!-- BEGIN CHANGELOG -->
- [[#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)
Expand Down Expand Up @@ -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)
<!-- END CHANGELOG -->

## [16.0.0] - 2022-06-06

Expand Down
103 changes: 103 additions & 0 deletions tools/changelog.py
Original file line number Diff line number Diff line change
@@ -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 = '<!-- BEGIN CHANGELOG -->'
MARK_END = '<!-- END CHANGELOG -->'
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()
2 changes: 2 additions & 0 deletions tools/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
click
deepdiff
ghapi
iso8601
marko
requests
yamale
Expand Down