forked from mavlink/MAVSDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_changelog.py
executable file
·199 lines (155 loc) · 6.13 KB
/
generate_changelog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
import argparse
import dateutil.parser
import datetime
import json
debug = False
def create_gql_client(token):
transport = RequestsHTTPTransport(url='https://api.github.com/graphql', use_json=True)
transport.headers = { "Authorization": "Bearer {}".format(token) }
client = Client(transport=transport)
return client
def query_releases(gql_client, debug=False):
query_releases = gql('''
{
repository(owner: "mavlink", name: "MAVSDK") {
releases(last: 10) {
nodes {
name
createdAt
tag {
name
}
}
}
}
}
''')
releases = gql_client.execute(query_releases)
releases = releases["repository"]["releases"]["nodes"]
if debug:
print(json.dumps(releases, indent=2))
return releases
def plus_one_minute(date_str):
date = dateutil.parser.parse(date_str)
date += datetime.timedelta(minutes=1)
return date.isoformat().replace('+00:00', 'Z')
def query_pull_requests(gql_client, releases, release_tag, debug=False):
previous_wanted_release = None
wanted_release = None
for release in releases:
previous_wanted_release = wanted_release
wanted_release = release
if not release["tag"]:
continue
if release["tag"]["name"] == release_tag:
break
if debug:
print(previous_wanted_release)
print(wanted_release)
merged_predicate = None
if previous_wanted_release is None:
merged_predicate = f"<={wanted_release['createdAt']}"
else:
# We add one minute because otherwise the last PR of the previous release
# tends to get included as well.
merged_predicate = f"{plus_one_minute(previous_wanted_release['createdAt'])[:-4]}..{wanted_release['createdAt'][:-4]}"
query_pull_requests_str = '''
{{
search(query: "repo:mavlink/MAVSDK is:pr is:merged merged:{merged_predicate}", type: ISSUE, last: 100) {{
edges {{
node {{
... on PullRequest {{
url
number
title
author {{
login
}}
reviews(last: 15) {{
nodes {{
author {{
login
}}
}}
}}
labels(last: 1) {{
edges {{
node {{
name
}}
}}
}}
mergedAt
}}
}}
}}
}}
}}
'''.format(merged_predicate=merged_predicate)
if debug:
print(query_pull_requests_str)
query_pull_requests = gql(query_pull_requests_str)
results_query = gql_client.execute(query_pull_requests)
result_pull_requests = list(map(lambda node: node["node"], results_query["search"]["edges"]))
if debug:
print(json.dumps(result_pull_requests, indent=2))
return result_pull_requests
def filter_by_label(pull_requests, label):
if debug:
print(json.dumps(pull_requests, indent=2))
for pull_request in pull_requests:
if (len(pull_request["labels"]["edges"]) == 0):
print(f"ERROR: PR is missing a label: {pull_request['url']}")
exit(1)
return list(filter(lambda pull_request: pull_request["labels"]["edges"][0]["node"]["name"] == label, pull_requests))
def collect_contributors(pull_requests):
contributors = []
for pull_request in pull_requests:
author = pull_request["author"]["login"]
contributors.append(f"@{author}")
contributors = set(contributors)
contributors = sorted(list(contributors), key=str.lower)
return contributors
def print_markdown_output(features, bugfixes, enhancements, documentation, contributors):
print("-----------")
print("CHANGELOG")
print("-----------")
if features:
print("### Features:\n")
for feature in features:
print(f"* {feature['title']} (#{feature['number']}).")
if bugfixes:
print("\n### Bugfixes:\n")
for bugfix in bugfixes:
print(f"* {bugfix['title']} (#{bugfix['number']}).")
if enhancements:
print("\n### Enhancements:\n")
for enhancement in enhancements:
print(f"* {enhancement['title']} (#{enhancement['number']}).")
if documentation:
print("\n### Documentation:\n")
for doc in documentation:
print(f"* {doc['title']} (#{doc['number']}).")
if contributors:
print("\n### Contributors:\n")
print(', '.join(contributors))
def main():
parser = argparse.ArgumentParser(description='Generate a changelog from pull requests between a given release tag and the previous one.')
parser.add_argument("--tag", help="The release tag for which the changelog should be generated")
parser.add_argument("--token", help="The token to access MAVSDK's GitHub API.")
parser.add_argument("--verbose", type=bool, nargs='?', const=True, default=False, help="Enable verbose output.")
args = parser.parse_args()
gql_client = create_gql_client(args.token)
releases = query_releases(gql_client, debug=args.verbose)
pull_requests = query_pull_requests(gql_client, releases, args.tag, debug=args.verbose)
features = filter_by_label(pull_requests, "feature")
bugfixes = filter_by_label(pull_requests, "bug")
enhancements = filter_by_label(pull_requests, "enhancement")
documentation = filter_by_label(pull_requests, "documentation")
contributors = collect_contributors(pull_requests)
print_markdown_output(features, bugfixes, enhancements, documentation, contributors)
if __name__ == '__main__':
main()