-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
notes.go
519 lines (439 loc) · 14.2 KB
/
notes.go
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//go:build tools
// +build tools
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// main is the main package for the release notes generator.
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"os"
"os/exec"
"regexp"
"sort"
"strings"
"sync"
"time"
)
/*
This tool prints all the titles of all PRs from previous release to HEAD.
This needs to be run *before* a tag is created.
Use these as the base of your release notes.
*/
const (
features = ":sparkles: New Features"
bugs = ":bug: Bug Fixes"
documentation = ":book: Documentation"
proposals = ":memo: Proposals"
warning = ":warning: Breaking Changes"
other = ":seedling: Others"
unknown = ":question: Sort these by hand"
)
var (
outputOrder = []string{
proposals,
warning,
features,
bugs,
other,
documentation,
unknown,
}
repo = flag.String("repository", "kubernetes-sigs/cluster-api", "The repo to run the tool from.")
fromTag = flag.String("from", "", "The tag or commit to start from.")
since = flag.String("since", "", "Include commits starting from and including this date. Accepts format: YYYY-MM-DD")
until = flag.String("until", "", "Include commits up to and including this date. Accepts format: YYYY-MM-DD")
numWorkers = flag.Int("workers", 10, "Number of concurrent routines to process PR entries. If running into GitHub rate limiting, use 1.")
prefixAreaLabel = flag.Bool("prefix-area-label", true, "If enabled, will prefix the area label.")
addKubernetesVersionSupport = flag.Bool("add-kubernetes-version-support", true, "If enabled, will add the Kubernetes version support header.")
tagRegex = regexp.MustCompile(`^\[release-[\w-\.]*\]`)
userFriendlyAreas = map[string]string{
"e2e-testing": "e2e",
"provider/control-plane-kubeadm": "KCP",
"provider/infrastructure-docker": "CAPD",
"dependency": "Dependency",
"devtools": "Devtools",
"machine": "Machine",
"api": "API",
"machinepool": "MachinePool",
"clustercachetracker": "ClusterCacheTracker",
"clusterclass": "ClusterClass",
"testing": "Testing",
"release": "Release",
"machineset": "MachineSet",
"clusterresourceset": "ClusterResourceSet",
"machinedeployment": "MachineDeployment",
"ipam": "IPAM",
"provider/bootstrap-kubeadm": "CAPBK",
"provider/infrastructure-in-memory": "CAPIM",
"provider/core": "Core",
"runtime-sdk": "Runtime SDK",
"ci": "CI",
}
releaseBackportMarker = regexp.MustCompile(`(?m)^\[release-\d\.\d\]\s*`)
)
func main() {
flag.Parse()
os.Exit(run())
}
func lastTag() string {
if fromTag != nil && *fromTag != "" {
return *fromTag
}
cmd := exec.Command("git", "describe", "--tags", "--abbrev=0")
out, err := cmd.Output()
if err != nil {
return firstCommit()
}
return string(bytes.TrimSpace(out))
}
func firstCommit() string {
cmd := exec.Command("git", "rev-list", "--max-parents=0", "HEAD")
out, err := cmd.Output()
if err != nil {
return "UNKNOWN"
}
return string(bytes.TrimSpace(out))
}
// Since git doesn't include the last day in rev-list we want to increase 1 day to include it in the interval.
func increaseDateByOneDay(date string) (string, error) {
layout := "2006-01-02"
datetime, err := time.Parse(layout, date)
if err != nil {
return "", err
}
datetime = datetime.Add(time.Hour * 24)
return datetime.Format(layout), nil
}
const (
missingAreaLabelPrefix = "MISSING_AREA"
areaLabelPrefix = "area/"
multipleAreaLabelsPrefix = "MULTIPLE_AREAS["
documentationAreaLabel = "documentation"
)
type githubPullRequest struct {
Labels []githubLabel `json:"labels"`
}
type githubLabel struct {
Name string `json:"name"`
}
func getAreaLabel(merge string) (string, error) {
// Get pr id from merge commit
prID := strings.Replace(strings.TrimSpace(strings.Split(merge, " ")[3]), "#", "", -1)
cmd := exec.Command("gh", "api", fmt.Sprintf("repos/%s/pulls/%s", *repo, prID)) //nolint:gosec
out, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("%s: %v", string(out), err)
}
pr := &githubPullRequest{}
if err := json.Unmarshal(out, pr); err != nil {
return "", err
}
var areaLabels []string
for _, label := range pr.Labels {
if area, ok := trimAreaLabel(label.Name); ok {
if userFriendlyArea, ok := userFriendlyAreas[area]; ok {
area = userFriendlyArea
}
areaLabels = append(areaLabels, area)
}
}
switch len(areaLabels) {
case 0:
return missingAreaLabelPrefix, nil
case 1:
return areaLabels[0], nil
default:
return multipleAreaLabelsPrefix + strings.Join(areaLabels, "/") + "]", nil
}
}
// trimAreaLabel removes the "area/" prefix from area labels and returns it.
// If the label is an area label, the second return value is true, otherwise false.
func trimAreaLabel(label string) (string, bool) {
trimmed := strings.TrimPrefix(label, areaLabelPrefix)
if len(trimmed) < len(label) {
return trimmed, true
}
return label, false
}
func run() int {
if err := ensureInstalledDependencies(); err != nil {
fmt.Println(err)
return 1
}
var commitRange string
var cmd *exec.Cmd
if *since != "" && *until != "" {
commitRange = fmt.Sprintf("%s - %s", *since, *until)
lastDay, err := increaseDateByOneDay(*until)
if err != nil {
fmt.Println(err)
return 1
}
cmd = exec.Command("git", "rev-list", "HEAD", "--since=\""+*since+"\"", "--until=\""+lastDay+"\"", "--merges", "--pretty=format:%B") //nolint:gosec
} else if *since != "" || *until != "" {
fmt.Println("--since and --until are required together or both unset")
return 1
} else {
commitRange = lastTag()
cmd = exec.Command("git", "rev-list", commitRange+"..HEAD", "--merges", "--pretty=format:%B") //nolint:gosec
}
merges := map[string][]string{
features: {},
bugs: {},
documentation: {},
warning: {},
other: {},
unknown: {},
}
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("Error")
fmt.Println(string(out))
return 1
}
commits := []*commit{}
outLines := strings.Split(string(out), "\n")
for _, line := range outLines {
line = strings.TrimSpace(line)
last := len(commits) - 1
switch {
case strings.HasPrefix(line, "commit"):
commits = append(commits, &commit{})
case strings.HasPrefix(line, "Merge"):
commits[last].merge = line
continue
case line == "":
default:
commits[last].body = line
}
}
results := make(chan releaseNoteEntryResult)
commitCh := make(chan *commit)
var wg sync.WaitGroup
wg.Add(*numWorkers)
for i := 0; i < *numWorkers; i++ {
go func() {
for commit := range commitCh {
processed := releaseNoteEntryResult{}
processed.prEntry, processed.err = generateReleaseNoteEntry(commit)
results <- processed
}
wg.Done()
}()
}
go func() {
for _, c := range commits {
commitCh <- c
}
close(commitCh)
}()
go func() {
wg.Wait()
close(results)
}()
for result := range results {
if result.err != nil {
fmt.Println(result.err)
os.Exit(0)
}
if result.prEntry.title == "" {
continue
}
if result.prEntry.section == documentation {
merges[result.prEntry.section] = append(merges[result.prEntry.section], result.prEntry.prNumber)
} else {
merges[result.prEntry.section] = append(merges[result.prEntry.section], result.prEntry.title)
}
}
if *addKubernetesVersionSupport {
// TODO Turn this into a link (requires knowing the project name + organization)
fmt.Print(`## 👌 Kubernetes version support
- Management Cluster: v1.**X**.x -> v1.**X**.x
- Workload Cluster: v1.**X**.x -> v1.**X**.x
[More information about version support can be found here](https://cluster-api.sigs.k8s.io/reference/versions.html)
`)
}
fmt.Printf("## Changes since %v\n---\n", commitRange)
fmt.Printf("## :chart_with_upwards_trend: Overview\n")
if count := len(commits); count == 1 {
fmt.Println("- 1 new commit merged")
} else if count > 1 {
fmt.Printf("- %d new commits merged\n", count)
}
if count := len(merges[warning]); count == 1 {
fmt.Println("- 1 breaking change :warning:")
} else if count > 1 {
fmt.Printf("- %d breaking changes :warning:\n", count)
}
if count := len(merges[features]); count == 1 {
fmt.Println("- 1 feature addition ✨")
} else if count > 1 {
fmt.Printf("- %d feature additions ✨\n", count)
}
if count := len(merges[bugs]); count == 1 {
fmt.Println("- 1 bug fixed 🐛")
} else if count > 1 {
fmt.Printf("- %d bugs fixed 🐛\n", count)
}
fmt.Println()
for _, key := range outputOrder {
mergeslice := merges[key]
if len(mergeslice) == 0 {
continue
}
switch key {
case documentation:
if len(mergeslice) == 1 {
fmt.Printf(
":book: Additionally, there has been 1 contribution to our documentation and book. (%s) \n\n",
mergeslice[0],
)
} else {
fmt.Printf(
":book: Additionally, there have been %d contributions to our documentation and book. (%s) \n\n",
len(mergeslice),
strings.Join(mergeslice, ", "),
)
}
default:
fmt.Println("## " + key)
sort.Slice(mergeslice, func(i int, j int) bool {
str1 := strings.ToLower(mergeslice[i])
str2 := strings.ToLower(mergeslice[j])
return str1 < str2
})
for _, merge := range mergeslice {
fmt.Println(merge)
}
fmt.Println()
}
}
fmt.Println("")
fmt.Println("_Thanks to all our contributors!_ 😊")
return 0
}
func trimTitle(title string) string {
// Remove a tag prefix if found.
title = tagRegex.ReplaceAllString(title, "")
return strings.TrimSpace(title)
}
type commit struct {
merge string
body string
}
func formatMerge(line, prNumber string) string {
if prNumber == "" {
return line
}
return fmt.Sprintf("%s (%s)", line, prNumber)
}
func ensureInstalledDependencies() error {
if !commandExists("git") {
return errors.New("git not available. Git is required to be present in the PATH")
}
if !commandExists("gh") {
return errors.New("gh GitHub CLI not available. GitHub CLI is required to be present in the PATH. Refer to https://cli.github.com/ for installation")
}
return nil
}
func commandExists(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}
// releaseNoteEntryResult is the result of processing a PR to create a release note item.
// Used to aggregate the line item and error when processing concurrently.
type releaseNoteEntryResult struct {
prEntry *releaseNoteEntry
err error
}
// releaseNoteEntry represents a line item in the release notes.
type releaseNoteEntry struct {
title string
section string
prNumber string
}
func modifyEntryTitle(title string, prefixes []string) string {
entryWithoutTag := title
for _, prefix := range prefixes {
entryWithoutTag = strings.TrimLeft(strings.TrimPrefix(entryWithoutTag, prefix), " ")
}
return strings.ToUpper(string(entryWithoutTag[0])) + entryWithoutTag[1:]
}
// generateReleaseNoteEntry processes a commit into a PR line item for the release notes.
func generateReleaseNoteEntry(c *commit) (*releaseNoteEntry, error) {
entry := &releaseNoteEntry{}
if c.body == "" {
c.body = "ERROR: BODY MISSING. FIX MANUALLY"
}
entry.title = trimTitle(c.body)
var fork string
var area string
if *prefixAreaLabel {
var err error
area, err = getAreaLabel(c.merge)
if err != nil {
return nil, err
}
}
switch {
case strings.HasPrefix(entry.title, ":sparkles:"), strings.HasPrefix(entry.title, "✨"):
entry.section = features
entry.title = modifyEntryTitle(entry.title, []string{":sparkles:", "✨"})
case strings.HasPrefix(entry.title, ":bug:"), strings.HasPrefix(entry.title, "🐛"):
entry.section = bugs
entry.title = modifyEntryTitle(entry.title, []string{":bug:", "🐛"})
case strings.HasPrefix(entry.title, ":book:"), strings.HasPrefix(entry.title, "📖"):
entry.section = documentation
entry.title = modifyEntryTitle(entry.title, []string{":book:", "📖"})
if strings.Contains(entry.title, "CAEP") || strings.Contains(entry.title, "proposal") {
entry.section = proposals
}
case strings.HasPrefix(entry.title, ":seedling:"), strings.HasPrefix(entry.title, "🌱"):
entry.section = other
entry.title = modifyEntryTitle(entry.title, []string{":seedling:", "🌱"})
case strings.HasPrefix(entry.title, ":warning:"), strings.HasPrefix(entry.title, "⚠️"):
entry.section = warning
entry.title = modifyEntryTitle(entry.title, []string{":warning:", "⚠️"})
default:
entry.section = unknown
}
// If the area label indicates documentation, use documentation as the section
// no matter what was the emoji used. This takes into account that the area label
// tends to be more accurate than the emoji (data point observed by the release team).
// We handle this after the switch statement to make sure we remove all emoji prefixes.
if area == documentationAreaLabel {
entry.section = documentation
}
entry.title = strings.TrimSpace(entry.title)
entry.title = trimReleaseBackportMarker(entry.title)
if entry.title == "" {
return entry, nil
}
if *prefixAreaLabel {
entry.title = fmt.Sprintf("- %s: %s", area, entry.title)
} else {
entry.title = fmt.Sprintf("- %s", entry.title)
}
_, _ = fmt.Sscanf(c.merge, "Merge pull request %s from %s", &entry.prNumber, &fork)
entry.title = formatMerge(entry.title, entry.prNumber)
return entry, nil
}
// trimReleaseBackportMarker removes the `[release-x.x]` prefix from a PR title if present.
// These are mostly used for back-ported PRs in release branches.
func trimReleaseBackportMarker(title string) string {
return releaseBackportMarker.ReplaceAllString(title, "${1}")
}