-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
589 lines (486 loc) · 12.6 KB
/
main.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
package main
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"time"
"text/template"
"github.com/Masterminds/semver/v3"
"github.com/Masterminds/sprig/v3"
"github.com/spf13/pflag"
)
var opts struct {
Output string
InputDir string
TemplateFile string
Versions []string
}
func init() {
pflag.StringVarP(&opts.InputDir, "input", "i", "changelog", "read input files from `dir`")
pflag.StringVarP(&opts.Output, "output", "o", "", "write generated changelog to this `file` (default: print to stdout)")
pflag.StringVarP(&opts.TemplateFile, "template", "t", filepath.FromSlash("changelog/CHANGELOG.tmpl"), "read template from `file`")
pflag.StringSliceVar(&opts.Versions, "version", nil, "only print `version` (separate multiple versions with commas)")
pflag.Parse()
}
func die(msg string, args ...interface{}) {
if !strings.HasSuffix(msg, "\\n") {
msg += "\n"
}
fmt.Fprintf(os.Stderr, msg, args...)
os.Exit(1)
}
// files lists all file names in dir. The file name is split by _, and the first component is used as the key in the resulting map.
func files(dir string) []string {
d, err := os.Open(dir)
if err != nil {
die("error opening dir: %v", err)
}
names, err := d.Readdirnames(-1)
if err != nil {
_ = d.Close()
die("error listing dir: %v", err)
}
err = d.Close()
if err != nil {
die("error closing dir: %v", err)
}
sort.Strings(names)
var files []string
for _, name := range names {
// skip the template and versions file
if name == "TEMPLATE" || name == "releases" {
continue
}
// skip dot files
if strings.HasPrefix(name, ".") {
continue
}
files = append(files, filepath.Join(dir, name))
}
return files
}
// Release is one release, with an optional release date.
type Release struct {
path string
Version string
Date *time.Time
}
// ReleaseSlice allows sorting a slice of releases by the release date
// with Go < 1.8
type ReleaseSlice []Release
// Len is the number of elements in the collection.
func (s ReleaseSlice) Len() int {
return len(s)
}
// Less reports whether the element with
// index i should sort before the element with index j.
func (s ReleaseSlice) Less(i, j int) bool {
if s[i].Date == nil {
return true
}
if s[j].Date == nil {
return false
}
return s[j].Date.Before(*s[i].Date)
}
// Swap swaps the elements with indexes i and j.
func (s ReleaseSlice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
var versionRegex = regexp.MustCompile(`^([^_]+)(?:_(\d{4}-\d{2}-\d{2}))?$`)
// readReleases lists the directory and parses all releases from the subdir
// names there. A valid release subdir has the format "x.y.z_YYYY-MM-DD", the
// underscore and date is optional (for unreleased versions). The resulting
// slice is sorted by the release dates, starting with unreleased versions and
// continuing with the other versions, newest first.
func readReleases(dir string) (result []Release) {
f, err := os.Open(dir)
if err != nil {
die("unable to open dir: %v", err)
}
entries, err := f.Readdir(-1)
if err != nil {
die("unable to list directory: %v", err)
}
err = f.Close()
if err != nil {
die("close dir: %v", err)
}
for _, entry := range entries {
if !entry.Mode().IsDir() {
continue
}
if entry.Name() == "unreleased" {
rel := Release{
path: filepath.Join(dir, entry.Name()),
Version: "unreleased",
}
result = append(result, rel)
continue
}
data := versionRegex.FindStringSubmatch(entry.Name())
if len(data) == 0 {
die("invalid subdir name %v", filepath.Join(dir, entry.Name()))
continue
}
ver, err := semver.NewVersion(data[1])
if err != nil {
die("invalid subdir name %v. Parsing semver returned error: %v", filepath.Join(dir, entry.Name()), err)
}
date := data[2]
rel := Release{
path: filepath.Join(dir, entry.Name()),
Version: ver.String(),
}
if date != "" {
t, err := time.Parse("2006-01-02", date)
if err != nil {
die("unable to parse date %q: %v", date, err)
}
rel.Date = &t
}
result = append(result, rel)
}
sort.Sort(ReleaseSlice(result))
return result
}
// Entry describes a change.
type Entry struct {
Type string
TypeShort string
Title string
Paragraphs []string
URLs []*url.URL
Issues []string
IssueURLs []*url.URL
PRs []string
PRURLs []*url.URL
OtherURLs []*url.URL
PrimaryID int64
PrimaryURL *url.URL
}
// EntryTypePriority contains the list of valid types, order is priority in the changelog.
var EntryTypePriority = map[string]int{
"Security": 1,
"Bugfix": 2,
"Change": 3,
"Enhancement": 4,
}
// EntryTypeAbbreviation contains the shortened entry types for the overview.
var EntryTypeAbbreviation = map[string]string{
"Security": "Sec",
"Bugfix": "Fix",
"Change": "Chg",
"Enhancement": "Enh",
}
// EntrySlice allows sorting a slice of releases by the priority of the entry
// (as defined in EntryTypePriority) with Go < 1.8
type EntrySlice []Entry
// Len is the number of elements in the collection.
func (s EntrySlice) Len() int {
return len(s)
}
// Less reports whether the element with
// index i should sort before the element with index j.
func (s EntrySlice) Less(i, j int) bool {
if s[i].Type == s[j].Type {
return s[i].PrimaryID < s[j].PrimaryID
}
return EntryTypePriority[s[i].Type] < EntryTypePriority[s[j].Type]
}
// Swap swaps the elements with indexes i and j.
func (s EntrySlice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// Punctuation contains all the characters that are not allowed as the last character in the title.
const Punctuation = ".!?"
// Valid returns an error if the entry is invalid in any way.
func (e Entry) Valid() error {
if e.Type == "" {
return errors.New("entry title does not have a prefix, example: Bugfix: restore old behavior")
}
if e.Title == "" {
return errors.New("entry does not have a title")
}
if e.PrimaryID == 0 {
return errors.New("primary issue ID not found")
}
lastChar := e.Title[len(e.Title)-1]
if strings.ContainsAny(string(lastChar), Punctuation) {
return fmt.Errorf("title ends with punctuation, e.g. a character out of %q", Punctuation)
}
if _, ok := EntryTypePriority[e.Type]; !ok {
return fmt.Errorf("entry type %q is invalid, valid types: %v", e.Type, EntryTypePriority)
}
if len(e.Type)+len(e.Title)+1 > 80 {
return errors.New("title is too long (max 80 characters)")
}
return nil
}
func readFile(filename string) (e Entry) {
f, err := os.Open(filename)
if err != nil {
die("unable to open %v: %v", filename, err)
}
sc := bufio.NewScanner(f)
if !sc.Scan() {
die("unable to read first line from %v", filename)
}
title := sc.Text()
data := strings.SplitN(title, ": ", 2)
if len(data) == 2 {
e.Type = strings.TrimSpace(capitalize(data[0]))
e.TypeShort = EntryTypeAbbreviation[e.Type]
data = data[1:]
}
e.Title = strings.TrimSpace(capitalize(data[0]))
var text []string
var sect string
var verbatim bool // inside verbatim section
for sc.Scan() {
if sc.Err() != nil {
die("unable to read lines from %v: %v", filename, sc.Err())
}
trimmedText := strings.TrimSpace(sc.Text())
if !verbatim && strings.HasPrefix(trimmedText, "```") {
// start new paragraph
if sect != "" {
text = append(text, sect)
}
sect = trimmedText
verbatim = true
continue
}
// ignore new lines inside verbatim section
if !verbatim && trimmedText == "" {
if sect != "" {
text = append(text, sect)
}
sect = ""
continue
}
if verbatim {
if sect != "" {
sect += "\n"
}
sect += sc.Text()
} else {
if sect != "" {
sect += " "
}
sect += trimmedText
}
if verbatim && trimmedText == "```" {
verbatim = false
}
}
if verbatim {
die("unmatched verbatim tag in %v", filename)
}
err = f.Close()
if err != nil {
die("error closing %v: %v", filename, err)
}
if sect != "" {
text = append(text, sect)
}
if len(text) > 0 {
links := text[len(text)-1]
text = text[:len(text)-1]
sc = bufio.NewScanner(strings.NewReader(links))
sc.Split(bufio.ScanWords)
for sc.Scan() {
url, err := url.Parse(sc.Text())
if err != nil {
die("file %v: unable to parse url %q: %v", filename, sc.Text(), err)
}
e.URLs = append(e.URLs, url)
}
}
for _, par := range text {
e.Paragraphs = append(e.Paragraphs, capitalize(strings.TrimSpace(par)))
}
githubIDs(e.URLs, &e)
err = e.Valid()
if err != nil {
die("file %v: %v", filename, err)
}
return e
}
var (
issueRegexp = regexp.MustCompile(`/.*/.*/issues/(\d+)`)
pullRequestRegexp = regexp.MustCompile(`/.*/.*/pull/(\d+)`)
)
func safeParseInt(str string) int64 {
val, err := strconv.ParseInt(str, 10, 64)
if err != nil {
die("unable to parse issue/PR ID %q: %v", str, err)
}
return val
}
// githubIDs extracts all issue and pull request IDs from the urls.
func githubIDs(urls []*url.URL, e *Entry) {
for _, url := range urls {
if url.Host != "github.com" {
e.OtherURLs = append(e.OtherURLs, url)
continue
}
switch {
case issueRegexp.MatchString(url.Path):
data := issueRegexp.FindStringSubmatch(url.Path)
id := data[1]
e.Issues = append(e.Issues, id)
e.IssueURLs = append(e.IssueURLs, url)
if e.PrimaryID == 0 {
e.PrimaryID = safeParseInt(id)
e.PrimaryURL = url
}
case pullRequestRegexp.MatchString(url.Path):
data := pullRequestRegexp.FindStringSubmatch(url.Path)
id := data[1]
e.PRs = append(e.PRs, id)
e.PRURLs = append(e.PRURLs, url)
if e.PrimaryID == 0 {
e.PrimaryID = safeParseInt(id)
e.PrimaryURL = url
}
default:
e.OtherURLs = append(e.OtherURLs, url)
}
}
}
func readEntries(versions []Release) (entries map[string][]Entry) {
entries = make(map[string][]Entry)
for _, ver := range versions {
for _, file := range files(ver.path) {
entries[ver.Version] = append(entries[ver.Version], readFile(file))
}
}
// sort all entries according to priority, otherwise leave the original ordering
for ver, list := range entries {
sort.Stable(EntrySlice(list))
entries[ver] = list
}
return entries
}
// wrapIndent formats the text in a column smaller than width characters,
// indenting each new line with indent spaces.
func wrapIndent(text string, width, indent int) (result string, err error) {
if strings.HasPrefix(text, "```") {
parts := strings.Split(text, "\n")
sep := "\n" + strings.Repeat(" ", indent)
return strings.Join(parts, sep), nil
}
sc := bufio.NewScanner(strings.NewReader(text))
sc.Split(bufio.ScanWords)
cl := 0
for sc.Scan() {
if sc.Err() != nil {
return "", sc.Err()
}
spaceLen := 0
if cl > 0 {
// account for space between words, if there's already a word on the
// current line
spaceLen = 1
}
if cl+spaceLen+len(sc.Text()) > width {
result += "\n"
result += strings.Repeat(" ", indent)
cl = 0
}
if cl > 0 {
result += " "
cl++
}
result += sc.Text()
cl += len(sc.Text())
}
return result, nil
}
// capitalize returns a string with the first letter in upper case.
func capitalize(text string) string {
if text == "" {
return text
}
first, rest := text[0:1], text[1:]
return strings.ToUpper(first) + rest
}
var helperFuncs = template.FuncMap{
"wrapIndent": wrapIndent,
"capitalize": capitalize,
}
func main() {
buf, err := ioutil.ReadFile(opts.TemplateFile)
if err != nil {
die("unable to read template from %v: %v", opts.TemplateFile, err)
}
funcMap := sprig.GenericFuncMap()
for i, m := range helperFuncs {
funcMap[i] = m
}
templ, err := template.New("").Funcs(funcMap).Parse(string(buf))
if err != nil {
die("unable to compile template: %v", err)
}
type VersionChanges struct {
Version string
Date string
Entries []Entry
}
allReleases := readReleases(opts.InputDir)
var changes []VersionChanges
var releases []Release
if len(opts.Versions) == 0 {
releases = allReleases
} else {
for _, rel := range allReleases {
for _, ver := range opts.Versions {
if ver == rel.Version {
releases = append(releases, rel)
}
}
}
}
all := readEntries(releases)
for _, ver := range releases {
if len(all[ver.Version]) == 0 {
continue
}
vc := VersionChanges{
Version: ver.Version,
Entries: all[ver.Version],
}
if ver.Date != nil {
vc.Date = ver.Date.Format("2006-01-02")
} else {
vc.Date = "UNRELEASED"
}
changes = append(changes, vc)
}
wr := os.Stdout
if opts.Output != "" {
wr, err = os.Create(opts.Output)
if err != nil {
die("unable to create file %v: %v", opts.Output, err)
}
}
err = templ.Execute(wr, changes)
if err != nil {
die("error executing template: %v", err)
}
if opts.Output != "" {
err = wr.Close()
if err != nil {
die("error closing file %v: %v", opts.Output, err)
}
}
}