-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrelease.go
107 lines (89 loc) · 2.3 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package changelog
import (
"fmt"
"net/url"
"regexp"
"strings"
"time"
"github.com/pkg/errors"
)
// Release is a single changelog version
type Release struct {
Version *string
Date *time.Time
Yanked bool
URL *string
Changes *Changes
}
// ToString returns a Markdown formatted Release struct.
func (r *Release) ToString() (string, string) {
var o []string
var u string
if r.Version != nil {
if r.Date != nil {
o = append(o, fmt.Sprintf("## [%v] - %v\n", *r.Version, formatDate(r.Date)))
} else {
o = append(o, fmt.Sprintf("## [%v]\n", *r.Version))
}
} else {
o = append(o, "## [Unreleased]\n")
}
if r.Changes != nil {
o = append(o, r.Changes.ToString())
}
if r.URL != nil {
if r.Version != nil {
u = fmt.Sprintf("[%v]: %v", *r.Version, *r.URL)
} else {
u = fmt.Sprintf("[Unreleased]: %v", *r.URL)
}
}
return strings.Join(o, "\n"), u
}
func formatDate(date *time.Time) string {
return date.Format(DateFormat)
}
// SetVersion configures a Semantic Version of a release.
func (r *Release) SetVersion(version string) error {
m := regexp.MustCompile(SemVerRegex)
if m.MatchString(version) {
r.Version = &version
return nil
}
return errors.New(fmt.Sprintf("invalid semantic version %v, expected to match regex %v", version, SemVerRegex))
}
// SetDate configures a date of the release.
// Expected format: YYYY-MM-DD
func (r *Release) SetDate(date string) error {
m := regexp.MustCompile(DateRegex)
if m.MatchString(date) {
d := parseDate(date)
if d == nil {
return errors.New(fmt.Sprintf("invalid date %v, expected format %v", date, DateFormat))
}
r.Date = d
return nil
}
return errors.New(fmt.Sprintf("invalid date %v, expected to match regex %v", date, DateRegex))
}
// SetURL configures a URL of the release
func (r *Release) SetURL(link string) error {
_, err := url.Parse(link)
if err != nil {
return err
}
r.URL = &link
return nil
}
// AddNotice adds a notice to the release.
//
// This is a helper function that wraps Changes.AddNotice function.
func (r *Release) AddNotice(notice string) {
r.Changes.AddNotice(notice)
}
// AddChange adds a scoped change to the release.
//
// This is a helper function that wraps Changes.AddChange function.
func (r *Release) AddChange(scope string, change string) error {
return r.Changes.AddChange(scope, change)
}