Skip to content

Commit

Permalink
Merge pull request #355 from leakingtapan/release-automation
Browse files Browse the repository at this point in the history
Implement auto generating change log
  • Loading branch information
Cheng Pan authored Aug 25, 2019
2 parents 14cbd79 + 393f8b1 commit 8220b21
Showing 1 changed file with 83 additions and 10 deletions.
93 changes: 83 additions & 10 deletions hack/release
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import argparse
import hashlib
import json
import os
import requests

Expand All @@ -34,16 +35,7 @@ def download(fileName, repoName):
r = requests.get(url, allow_redirects=True)
open(fileName, 'wb').write(r.content)

if __name__=="__main__":
parser = argparse.ArgumentParser(description='Generate release CHANGELOG')
parser.add_argument('--version', metavar='version', type=str, required=True, help='the version to release')
parser.add_argument('--repo', metavar='repo', type=str, default='kubernetes-sigs/aws-ebs-csi-driver', help='the full github repository name')

arg = parser.parse_args()

version = arg.version
repo = arg.repo

def print_header(repo, version):
# Title
print('# {}'.format(version))

Expand All @@ -56,3 +48,84 @@ if __name__=="__main__":
print('--------- | ------------')
file_sha512(version+".zip", repo)
file_sha512(version+".tar.gz", repo)

class Github:
def __init__(self, user, token):
self._url = 'https://api.github.com'
self._user = user
self._token = token

def get_commits(self, repo, since):
resp = requests.get('{}/repos/{}/compare/{}...master'.format(self._url, repo, since),
auth=(self._user, self._token))
jsonResp = json.loads(resp.content)
return jsonResp['commits']

def to_pr_numbers(self, repo, commit):
sha = commit['sha']
resp = requests.get('{}/repos/{}/commits/{}/pulls'.format(self._url, repo, sha),
headers={'Accept': 'application/vnd.github.groot-preview+json'},
auth=(self._user, self._token))
jsonResp = json.loads(resp.content)
ret = []
for pr in jsonResp:
ret.append(pr['number'])

return ret

def get_pr(self, repo, pr_number):
resp = requests.get('{}/repos/{}/pulls/{}'.format(self._url, repo, pr_number),
auth=(self._user, self._token))
jsonResp = json.loads(resp.content)
return jsonResp

def print_release_note(self, repo, since):
# remove merge commits
commits = self.get_commits(repo, since)
commits = filter(lambda c: not c['commit']['message'].startswith('Merge pull request'), commits)
pr_numbers = set()
for commit in commits:
numbers = self.to_pr_numbers(repo, commit)
for pr in numbers:
pr_numbers.add(pr)

# dedupe pr numbers
pr_numbers = sorted(list(pr_numbers))

for number in pr_numbers:
pr = self.get_pr(repo, number)
user = pr['user']['login']
print('* {} ([#{}]({}), [@{}](https://github.com/{}))'.format(pr['title'], pr['number'], pr['html_url'], user, user))

def print_sha(args):
version = args.version
repo = args.repo
print_header(repo, version)

def print_notes(args):
repo = args.repo
since = args.since
user = args.github_user
token = args.github_token

g = Github(user, token)
g.print_release_note(repo, since)

if __name__=="__main__":
parser = argparse.ArgumentParser(description='Generate release CHANGELOG')
parser.add_argument('--repo', metavar='repo', type=str, default='kubernetes-sigs/aws-ebs-csi-driver', help='the full github repository name')
parser.add_argument('--github-user', metavar='user', type=str, help='the github user for github api')
parser.add_argument('--github-token', metavar='token', type=str, help='the github token for github api')

subParsers = parser.add_subparsers(title='subcommands', description='[note|sha]')

noteParser = subParsers.add_parser('note', help='generate release notes')
noteParser.add_argument('--since', metavar='since', type=str, required=True, help='since version tag')
noteParser.set_defaults(func=print_notes)

shaParser = subParsers.add_parser('sha', help='generate SHA for released version tag')
shaParser.add_argument('--version', metavar='version', type=str, required=True, help='the version to release')
shaParser.set_defaults(func=print_sha)

args = parser.parse_args()
args.func(args)

0 comments on commit 8220b21

Please sign in to comment.