/
release_notes.go
104 lines (81 loc) · 2.2 KB
/
release_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
package main
import (
"bufio"
"os"
"regexp"
"strings"
"github.com/streamingfast/cli"
"go.uber.org/zap"
)
var headerRegex = regexp.MustCompile(`^##([^#]+)`)
// readVersionFromChangelog will try to infer the default version from the changelog file.
func readVersionFromChangelog(changelogFile string) string {
if !cli.FileExists(changelogFile) {
return ""
}
file, err := os.Open(changelogFile)
cli.NoError(err, "Unable to open changelog %q", changelogFile)
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if headerRegex.MatchString(line) {
return extractVersionFromHeader(line)
}
}
cli.NoError(scanner.Err(), "Unable to scan lines from changelog")
// We found nothing!
return ""
}
var versionRegex = regexp.MustCompile(`v?([0-9]+\.[0-9]+\.[0-9]+[a-zA-Z0-9\-_]*)`)
func extractVersionFromHeader(header string) string {
matches := headerRegex.FindAllStringSubmatch(header, 1)
if len(matches) != 1 {
return ""
}
groups := matches[0]
if len(groups) != 2 {
return ""
}
version := strings.TrimSpace(groups[1])
if version == "" {
return ""
}
normalizedVersion := strings.ToLower(version)
if normalizedVersion != "unreleased" && normalizedVersion != "next" {
groups := versionRegex.FindStringSubmatch(normalizedVersion)
if len(groups) != 2 {
// Nothing found, should had 2 group
return ""
}
return "v" + groups[1]
}
return ""
}
func readReleaseNotes(changelogFile string) string {
if !cli.FileExists(changelogFile) {
return ""
}
file, err := os.Open(changelogFile)
cli.NoError(err, "Unable to open changelog %q", changelogFile)
defer file.Close()
foundFirstHeader := false
var releaseNotes []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if !foundFirstHeader && headerRegex.MatchString(line) {
foundFirstHeader = true
continue
}
if foundFirstHeader {
if headerRegex.MatchString(line) {
break
}
releaseNotes = append(releaseNotes, line)
}
}
zlog.Debug("computed changelog lines", zap.Strings("release_lines", releaseNotes))
cli.NoError(scanner.Err(), "Unable to scan lines from changelog")
return trimBlankLines(strings.Join(releaseNotes, "\n"))
}