-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.go
93 lines (68 loc) · 1.92 KB
/
release.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
package k6dist
import (
"bytes"
_ "embed"
"encoding/json"
"os"
"path/filepath"
"regexp"
"github.com/Masterminds/semver/v3"
"github.com/grafana/k6dist/internal/registry"
)
//go:embed NOTES.md.tpl
var defaultNotesTemplate string
const (
notesFooterBegin = "<!--```json"
notesFooterEnd = "```-->"
)
type footerData struct {
Version string `json:"version,omitempty"`
Modules registry.Modules `json:"modules,omitempty"`
}
func notesFooter(version *semver.Version, registry registry.Registry) (string, error) {
var buff bytes.Buffer
buff.WriteString(notesFooterBegin)
buff.WriteRune('\n')
encoder := json.NewEncoder(&buff)
encoder.SetEscapeHTML(false)
data := &footerData{Version: version.Original(), Modules: registry.ToModules()}
if err := encoder.Encode(data); err != nil {
return "", err
}
buff.WriteString(notesFooterEnd)
buff.WriteRune('\n')
return buff.String(), nil
}
func expandNotes(name string, version *semver.Version, reg registry.Registry, tmplfile string) (string, error) {
var tmplsrc string
if len(tmplfile) != 0 {
bin, err := os.ReadFile(filepath.Clean(tmplfile)) //nolint:forbidigo
if err != nil {
return "", err
}
tmplsrc = string(bin)
} else {
tmplsrc = defaultNotesTemplate
}
data, err := newReleaseData(name, version, reg)
if err != nil {
return "", err
}
return expandTemplate("notes", tmplsrc, data)
}
var reModules = regexp.MustCompile("(?ms:^" + notesFooterBegin + "(?P<state>.*)" + notesFooterEnd + ")")
func parseNotes(notes []byte) (bool, *semver.Version, registry.Modules, error) {
match := reModules.FindSubmatch(notes)
if match == nil {
return false, nil, nil, nil
}
var data footerData
if err := json.Unmarshal(match[reModules.SubexpIndex("state")], &data); err != nil {
return false, nil, nil, err
}
version, err := semver.NewVersion(data.Version)
if err != nil {
return false, nil, nil, err
}
return true, version, data.Modules, nil
}