Repository navigation
Expand file tree
/
Copy pathchangelog.go
More file actions
112 lines (82 loc) · 2.18 KB
/
Copy pathchangelog.go
File metadata and controls
112 lines (82 loc) · 2.18 KB
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
package openapigeneration
import (
_ "embed"
"regexp"
"strings"
"github.com/speakeasy-api/openapi/sequencedmap"
)
//go:embed CHANGELOG.md
var ChangeLog string
type Option func(*options)
func WithPreviousVersion(version string) Option {
return func(o *options) {
o.previousVersion = version
}
}
func WithTargetVersion(version string) Option {
return func(o *options) {
o.targetVersion = version
}
}
func WithSpecificVersion(version string) Option {
return func(o *options) {
o.specificVersion = version
}
}
type options struct {
previousVersion string
targetVersion string
specificVersion string
}
func GetChangeLog(opts ...Option) string {
o := &options{}
for _, opt := range opts {
opt(o)
}
versions := getChangelogVersions()
targetVersionFound := false
sections := []string{}
for version, section := range versions.All() {
if version == o.specificVersion || (version == o.targetVersion && o.specificVersion == "") || (o.targetVersion == "" && o.specificVersion == "") {
targetVersionFound = true
}
if o.previousVersion != "" && version == o.previousVersion && o.specificVersion == "" {
break
}
if targetVersionFound {
sections = append(sections, section)
if o.specificVersion != "" {
break
}
}
}
return strings.Join(sections, "\n\n")
}
func GetLatestVersion() string {
versions := getChangelogVersions()
return versions.First().GetKey()
}
var versionMatchRegex = regexp.MustCompile(`^\[(v[0-9]+\.[0-9]+\.[0-9]+)\]`)
func getChangelogVersions() *sequencedmap.Map[string, string] {
changelog := strings.ReplaceAll(ChangeLog, "\r\n", "\n")
bodyParts := strings.Split(changelog, "\n\n\n[")
versions := sequencedmap.New[string, string]()
if len(bodyParts) != 2 {
return versions
}
footer := "[" + bodyParts[1]
links := strings.Split(footer, "\n")
sections := strings.Split(bodyParts[0], "\n\n## ")
for i, section := range sections {
if versionMatchRegex.MatchString(section) {
version := versionMatchRegex.FindStringSubmatch(section)[1]
block := "## " + section
linkIdx := len(links) - i
if linkIdx < len(links) {
block += "\n\n" + links[linkIdx]
}
versions.Set(version, block)
}
}
return versions
}