-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathgithub-changelog.cr
executable file
·457 lines (384 loc) · 9.75 KB
/
github-changelog.cr
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#! /usr/bin/env crystal
# This helper queries merged pull requests for a given milestone from the GitHub API
# and creates formatted changelog entries.
#
# Pull requests that are already referenced in CHANGELOG.md are omitted, which
# makes it easy to incrementally add entries.
#
# Entries are grouped by topic (based on topic labels) and ordered by merge date.
# Some annotations are automatically added based on labels.
#
# Usage:
#
# scripts/github-changelog.cr <milestone>
#
# Environment variables:
# GITHUB_TOKEN: Access token for the GitHub API (required)
require "http/client"
require "json"
abort "Missing GITHUB_TOKEN env variable" unless ENV["GITHUB_TOKEN"]?
api_token = ENV["GITHUB_TOKEN"]
case ARGV.size
when 0
abort "Missing <milestone> argument"
when 1
repository = "crystal-lang/crystal"
milestone = ARGV.first
when 2
repository = ARGV[0]
milestone = ARGV[1]
else
abort "Too many arguments. Usage:\n #{PROGRAM_NAME} [<GH repo ref>] <milestone>"
end
def query_prs(api_token, repository, milestone : String, cursor : String?)
query = <<-GRAPHQL
query($milestone: String, $owner: String!, $repository: String!, $cursor: String) {
repository(owner: $owner, name: $repository) {
milestones(query: $milestone, first: 1) {
nodes {
closedAt
description
dueOn
title
pullRequests(first: 100, after: $cursor) {
nodes {
number
title
mergedAt
permalink
author {
login
}
labels(first: 10) {
nodes {
name
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
}
}
GRAPHQL
owner, _, name = repository.partition("/")
variables = {
owner: owner,
repository: name,
milestone: milestone,
cursor: cursor,
}
response = HTTP::Client.post("https://api.github.com/graphql",
body: {query: query, variables: variables}.to_json,
headers: HTTP::Headers{
"Authorization" => "bearer #{api_token}",
}
)
unless response.success?
abort "GitHub API response: #{response.status}\n#{response.body}"
end
response
end
module LabelNameConverter
def self.from_json(pull : JSON::PullParser)
pull.on_key! "name" do
String.new(pull)
end
end
end
record Milestone,
closed_at : Time?,
description : String?,
due_on : Time?,
title : String,
pull_requests : Array(PullRequest) do
include JSON::Serializable
@[JSON::Field(key: "dueOn")]
@due_on : Time?
@[JSON::Field(key: "closedAt")]
@closed_at : Time?
@[JSON::Field(key: "pullRequests", root: "nodes")]
@pull_requests : Array(PullRequest)
def release_date
closed_at || due_on
end
end
record PullRequest,
number : Int32,
title : String,
merged_at : Time?,
permalink : String,
author : String?,
labels : Array(String) do
include JSON::Serializable
include Comparable(self)
@[JSON::Field(key: "mergedAt")]
@merged_at : Time?
@[JSON::Field(root: "login")]
@author : String?
@[JSON::Field(root: "nodes", converter: JSON::ArrayConverter(LabelNameConverter))]
@labels : Array(String)
def link_ref(io)
io << "[#" << number << "]"
end
def <=>(other : self)
sort_tuple <=> other.sort_tuple
end
def sort_tuple
{
type || "",
topic || [] of String,
deprecated? ? 0 : 1,
merged_at || Time.unix(0),
}
end
def infra_sort_tuple
{
topic || [] of String,
type || "",
deprecated? ? 0 : 1,
merged_at || Time.unix(0),
}
end
def primary_topic
topic.try(&.[0]?) || "other"
end
def sub_topic
topic.try(&.[1..].join(":").presence)
end
def topic
topics.fetch(0) do
STDERR.puts "Missing topic for ##{number}"
nil
end
end
def topics
topics = labels.compact_map { |label|
label.lchop?("topic:").try(&.split(/:|\//))
}
topics.reject! &.[0].==("multithreading")
topics.sort_by! { |parts|
topic_priority = case parts[0]
when "infrastructure" then 3
when "tools" then 2
when "lang" then 1
else 0
end
{-topic_priority, parts[0]}
}
end
def deprecated?
labels.includes?("deprecation")
end
def breaking?
labels.includes?("kind:breaking")
end
def regression?
labels.includes?("kind:regression")
end
def experimental?
labels.includes?("experimental")
end
def feature?
labels.includes?("kind:feature")
end
def fix?
labels.includes?("kind:bug")
end
def chore?
labels.includes?("kind:chore")
end
def refactor?
labels.includes?("kind:refactor")
end
def docs?
labels.includes?("kind:docs")
end
def specs?
labels.includes?("kind:specs")
end
def performance?
labels.includes?("performance")
end
def infra?
labels.any?(&.starts_with?("topic:infrastructure"))
end
def type
case
when feature? then "feature"
when docs? then "docs"
when specs? then "specs"
when fix? then "fix"
when chore? then "chore"
when performance? then "performance"
when refactor? then "refactor"
else nil
end
end
def section
case
when breaking? then "breaking"
when infra? then "infra"
else type || ""
end
end
def fixup?
md = title.match(/\[fixup #(.\d+)/) || return
md[1]?.try(&.to_i)
end
end
def query_milestone(api_token, repository, number)
cursor = nil
milestone = nil
while true
response = query_prs(api_token, repository, number, cursor)
parser = JSON::PullParser.new(response.body)
m = parser.on_key! "data" do
parser.on_key! "repository" do
parser.on_key! "milestones" do
parser.on_key! "nodes" do
parser.read_begin_array
Milestone.new(parser)
ensure
parser.read_end_array
end
end
end
end
if milestone
milestone.pull_requests.concat m.pull_requests
else
milestone = m
end
json = JSON.parse(response.body)
page_info = json.dig("data", "repository", "milestones", "nodes", 0, "pullRequests", "pageInfo")
break unless page_info["hasNextPage"].as_bool
cursor = page_info["endCursor"].as_s
end
milestone
end
milestone = query_milestone(api_token, repository, milestone)
struct ChangelogEntry
getter pull_requests : Array(PullRequest)
def initialize(pr : PullRequest)
@pull_requests = [pr]
end
def pr
pull_requests[0]
end
def to_s(io : IO)
if sub_topic = pr.sub_topic
io << "*(" << pr.sub_topic << ")* "
end
if pr.labels.includes?("security")
io << "**[security]** "
end
if pr.labels.includes?("breaking-change")
io << "**[breaking]** "
end
if pr.regression?
io << "**[regression]** "
end
if pr.experimental?
io << "**[experimental]** "
end
if pr.deprecated?
io << "**[deprecation]** "
end
io << pr.title.sub(/^\[?(?:#{pr.type}|#{pr.sub_topic})(?::|\]:?) /i, "")
io << " ("
pull_requests.join(io, ", ") do |pr|
pr.link_ref(io)
end
authors = collect_authors
if authors.present?
io << ", thanks "
authors.join(io, ", ") do |author|
io << "@" << author
end
end
io << ")"
end
def collect_authors
authors = [] of String
pull_requests.each do |pr|
author = pr.author || next
authors << author unless authors.includes?(author)
end
authors
end
def print_ref_labels(io)
pull_requests.each { |pr| print_ref_label(io, pr) }
end
def print_ref_label(io, pr)
pr.link_ref(io)
io << ": " << pr.permalink
io.puts
end
end
entries = milestone.pull_requests.compact_map do |pr|
ChangelogEntry.new(pr) unless pr.fixup?
end
milestone.pull_requests.each do |pr|
parent_number = pr.fixup? || next
parent_entry = entries.find { |entry| entry.pr.number == parent_number }
if parent_entry
parent_entry.pull_requests << pr
else
STDERR.puts "Unresolved fixup: ##{parent_number} for: #{pr.title} (##{pr.number})"
end
end
sections = entries.group_by(&.pr.section)
SECTION_TITLES = {
"breaking" => "Breaking changes",
"feature" => "Features",
"fix" => "Bugfixes",
"chore" => "Chores",
"performance" => "Performance",
"refactor" => "Refactor",
"docs" => "Documentation",
"specs" => "Specs",
"infra" => "Infrastructure",
"" => "other",
}
TOPIC_ORDER = %w[lang stdlib compiler tools other]
puts "## [#{milestone.title}] (#{milestone.release_date.try(&.to_s("%F")) || "unreleased"})"
if description = milestone.description.presence
puts
print "_", description
puts "_"
end
puts
puts "[#{milestone.title}]: https://github.com/#{repository}/releases/#{milestone.title}"
puts
def print_entries(entries)
entries.each do |entry|
puts "- #{entry}"
end
puts
entries.each(&.print_ref_labels(STDOUT))
puts
end
SECTION_TITLES.each do |id, title|
entries = sections[id]? || next
puts "### #{title}"
puts
if id == "infra"
entries.sort_by!(&.pr.infra_sort_tuple)
print_entries entries
else
topics = entries.group_by(&.pr.primary_topic)
topic_titles = topics.keys.sort_by! { |k| TOPIC_ORDER.index(k) || Int32::MAX }
topic_titles.each do |topic_title|
topic_entries = topics[topic_title]? || next
puts "#### #{topic_title}"
puts
topic_entries.sort_by!(&.pr)
print_entries topic_entries
end
end
end