Skip to content

Commit

Permalink
feat(publish_changelog): Open changelog PR against upstream docs repo. (
Browse files Browse the repository at this point in the history
  • Loading branch information
jtk54 committed May 1, 2017
1 parent 9193801 commit f1e6192
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 21 deletions.
66 changes: 49 additions & 17 deletions dev/publish_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# local git repository for spinnaker.github.io, and then pushing a commit
# to origin/master.
#
# A private key that has access to --githubio_repo needs to be added
# A private key that has access to --github_publisher's repos needs to be added
# to a running ssh-agent on the machine this script will run on:
#
# > <copy or rsync the key to the vm>
Expand All @@ -35,17 +35,25 @@
import re
import sys

from github import Github

from publish_bom import BomPublisher
from spinnaker.run import check_run_quick

# Path to the posts directory in the spinnaker.github.io site.
POSTS_DIR = '_posts'
POSTS_DIR = '_changelogs'

class ChangelogPublisher():
class ChangelogPublisher(BomPublisher):

def __init__(self, options, changelog_gist_uri=None):
self.__version = options.version
self.__githubio_repo_uri = options.githubio_repo_uri
self.__changelog_branch = ''
self.__changelog_gist_uri = changelog_gist_uri or options.changelog_gist_uri
self.__github_publisher = options.github_publisher
self.__github_token = options.github_token
self.__github = Github(self.__github_publisher, self.__github_token)
self.__githubio_repo_uri = ('git@github.com:{user}/spinnaker.github.io.git'
.format(user=self.__github_publisher))
self.__version = options.release_version

def __checkout_githubio_repo(self):
"""Clones the spinnaker.github.io git repo.
Expand All @@ -54,12 +62,15 @@ def __checkout_githubio_repo(self):

def __format_changelog_post(self):
# Initialized with 'front matter' necessary for the post.
timestamp = '{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now())
# Also tag the post so we can group by <major>.<minor> version.
timestamp = '{:%Y-%m-%d %H:%M:%S %Z}'.format(datetime.datetime.now())
major, minor, _ = self.__version.split('.')
version_post_tag = '.'.join([major, minor])
post_lines = [
'---',
'title: Spinnaker Changelog {version}'.format(version=self.__version),
'title: Version {version}'.format(version=self.__version),
'date: {date}'.format(date=timestamp),
'categories: changelogs',
'tags: "changelogs {0}"'.format(version_post_tag),
'---',
''
]
Expand Down Expand Up @@ -87,35 +98,56 @@ def __publish_post(self, post_content):
with open(post_path, 'w') as post_file:
post_file.write(post_content)

# Create a new branch based on upstream/master to push the changelog to.
check_run_quick('git -C {0} remote add upstream git@github.com:spinnaker/spinnaker.github.io.git'
.format(repo_name))
check_run_quick('git -C {0} fetch upstream'.format(repo_name))
self.__changelog_branch = '{version}-changelog'.format(version=self.__version)
check_run_quick('git -C {0} checkout -b {1}'.format(repo_name, self.__changelog_branch))
check_run_quick('git -C {0} add {1}'.format(repo_name, post_rel_path))
message = 'Changelog for version {0} auto-generated by {1}'.format(self.__version, __file__)
check_run_quick('git -C {0} commit -m "{1}"'.format(repo_name, message))
check_run_quick('git -C {0} push origin master'.format(repo_name))
check_run_quick('git -C {0} push -f origin {1}'.format(repo_name, self.__changelog_branch))

def __open_changelog_pull_request(self):
"""Opens a pull request from --github_publisher's repo to the upstream 'spinnaker' repo.
Uses 'hub' to open the pull request (https://github.com/github/hub).
This assumes that 'hub' is installed on the machine running this script.
"""
title = 'Changelog for version {0}'.format(self.__version)
branch_head = '{user}:{branch}'.format(user=self.__github_publisher, branch=self.__changelog_branch)
with open('message', 'w') as msg_file:
# TODO(jacobkiefer): Add notification to spinnaker/google-reviewers in body.
message = '{title}'.format(title=title)
msg_file.write(message)

base = 'spinnaker:master'
check_run_quick('hub -C spinnaker.github.io pull-request -b {base} -h {head} -F message'
.format(base=base, head=branch_head, msg=''))

def publish_changelog(self):
self.__checkout_githubio_repo()
post = self.__format_changelog_post()
self.__publish_post(post)
self.__open_changelog_pull_request()

@classmethod
def init_argument_parser(cls, parser):
"""Initialize command-line arguments."""
parser.add_argument('--githubio_repo_uri', default='', required=True,
help='The ssh uri of the spinnaker.github.io repo to'
'commit the changelog post to, e.g. git@github.com:spinnaker/spinnaker.github.io.')
parser.add_argument('--version', default='', required=True,
help='The version of Spinnaker that corresponds to the changelog.')
parser.add_argument('--changelog_gist_uri', default='',
help='A uri that points to a gist containing the changelog.')
help='The URI of the changelog gist to publish.')
super(ChangelogPublisher, cls).init_argument_parser(parser)

@classmethod
def main(cls):
parser = argparse.ArgumentParser()
cls.init_argument_parser(parser)
options = parser.parse_args()

result_publisher = cls(options)
result_publisher.publish_changelog()
changelog_publisher = cls(options)
changelog_publisher.publish_changelog()


if __name__ == '__main__':
sys.exit(ChangelogPublisher.main())
10 changes: 6 additions & 4 deletions dev/publish_spinnaker_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@


def init_argument_parser(parser):
BomPublisher.init_argument_parser(parser)
# ChangelogPublisher subclasses BomPublisher, only need to initialize
# the subclass's flags.
ChangelogPublisher.init_argument_parser(parser)

def main():
Expand All @@ -36,12 +37,13 @@ def main():
bom_publisher = BomPublisher(options)
bom_publisher.unpack_bom()
gist_uri = bom_publisher.publish_changelog_gist()

changelog_publisher = ChangelogPublisher(options, changelog_gist_uri=gist_uri)
changelog_publisher.publish_changelog()

bom_publisher.push_branch_and_tags()
bom_publisher.publish_release_bom()

result_publisher = ChangelogPublisher(options, changelog_gist_uri=gist_uri)
result_publisher.publish_changelog()


if __name__ == '__main__':
sys.exit(main())

0 comments on commit f1e6192

Please sign in to comment.