Skip to content

Commit 8220b21

Browse files
author
Cheng Pan
authored
Merge pull request #355 from leakingtapan/release-automation
Implement auto generating change log
2 parents 14cbd79 + 393f8b1 commit 8220b21

File tree

1 file changed

+83
-10
lines changed

1 file changed

+83
-10
lines changed

hack/release

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import argparse
1818
import hashlib
19+
import json
1920
import os
2021
import requests
2122

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

37-
if __name__=="__main__":
38-
parser = argparse.ArgumentParser(description='Generate release CHANGELOG')
39-
parser.add_argument('--version', metavar='version', type=str, required=True, help='the version to release')
40-
parser.add_argument('--repo', metavar='repo', type=str, default='kubernetes-sigs/aws-ebs-csi-driver', help='the full github repository name')
41-
42-
arg = parser.parse_args()
43-
44-
version = arg.version
45-
repo = arg.repo
46-
38+
def print_header(repo, version):
4739
# Title
4840
print('# {}'.format(version))
4941

@@ -56,3 +48,84 @@ if __name__=="__main__":
5648
print('--------- | ------------')
5749
file_sha512(version+".zip", repo)
5850
file_sha512(version+".tar.gz", repo)
51+
52+
class Github:
53+
def __init__(self, user, token):
54+
self._url = 'https://api.github.com'
55+
self._user = user
56+
self._token = token
57+
58+
def get_commits(self, repo, since):
59+
resp = requests.get('{}/repos/{}/compare/{}...master'.format(self._url, repo, since),
60+
auth=(self._user, self._token))
61+
jsonResp = json.loads(resp.content)
62+
return jsonResp['commits']
63+
64+
def to_pr_numbers(self, repo, commit):
65+
sha = commit['sha']
66+
resp = requests.get('{}/repos/{}/commits/{}/pulls'.format(self._url, repo, sha),
67+
headers={'Accept': 'application/vnd.github.groot-preview+json'},
68+
auth=(self._user, self._token))
69+
jsonResp = json.loads(resp.content)
70+
ret = []
71+
for pr in jsonResp:
72+
ret.append(pr['number'])
73+
74+
return ret
75+
76+
def get_pr(self, repo, pr_number):
77+
resp = requests.get('{}/repos/{}/pulls/{}'.format(self._url, repo, pr_number),
78+
auth=(self._user, self._token))
79+
jsonResp = json.loads(resp.content)
80+
return jsonResp
81+
82+
def print_release_note(self, repo, since):
83+
# remove merge commits
84+
commits = self.get_commits(repo, since)
85+
commits = filter(lambda c: not c['commit']['message'].startswith('Merge pull request'), commits)
86+
pr_numbers = set()
87+
for commit in commits:
88+
numbers = self.to_pr_numbers(repo, commit)
89+
for pr in numbers:
90+
pr_numbers.add(pr)
91+
92+
# dedupe pr numbers
93+
pr_numbers = sorted(list(pr_numbers))
94+
95+
for number in pr_numbers:
96+
pr = self.get_pr(repo, number)
97+
user = pr['user']['login']
98+
print('* {} ([#{}]({}), [@{}](https://github.com/{}))'.format(pr['title'], pr['number'], pr['html_url'], user, user))
99+
100+
def print_sha(args):
101+
version = args.version
102+
repo = args.repo
103+
print_header(repo, version)
104+
105+
def print_notes(args):
106+
repo = args.repo
107+
since = args.since
108+
user = args.github_user
109+
token = args.github_token
110+
111+
g = Github(user, token)
112+
g.print_release_note(repo, since)
113+
114+
if __name__=="__main__":
115+
parser = argparse.ArgumentParser(description='Generate release CHANGELOG')
116+
parser.add_argument('--repo', metavar='repo', type=str, default='kubernetes-sigs/aws-ebs-csi-driver', help='the full github repository name')
117+
parser.add_argument('--github-user', metavar='user', type=str, help='the github user for github api')
118+
parser.add_argument('--github-token', metavar='token', type=str, help='the github token for github api')
119+
120+
subParsers = parser.add_subparsers(title='subcommands', description='[note|sha]')
121+
122+
noteParser = subParsers.add_parser('note', help='generate release notes')
123+
noteParser.add_argument('--since', metavar='since', type=str, required=True, help='since version tag')
124+
noteParser.set_defaults(func=print_notes)
125+
126+
shaParser = subParsers.add_parser('sha', help='generate SHA for released version tag')
127+
shaParser.add_argument('--version', metavar='version', type=str, required=True, help='the version to release')
128+
shaParser.set_defaults(func=print_sha)
129+
130+
args = parser.parse_args()
131+
args.func(args)

0 commit comments

Comments
 (0)