-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchanges.go
141 lines (118 loc) · 2.78 KB
/
changes.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
package changelog
import (
"fmt"
"strings"
"github.com/pkg/errors"
)
// Changes are scoped changelog entries for a single version.
type Changes struct {
Added *[]string
Changed *[]string
Deprecated *[]string
Fixed *[]string
Notice *string
Removed *[]string
Security *[]string
}
// ToString returns a Markdown formatted Changes struct.
func (c *Changes) ToString() string {
var o []string
if c.Notice != nil {
o = append(o, fmt.Sprintf("%v\n", *c.Notice))
}
if c.Security != nil {
o = append(o, "### Security\n", fmt.Sprintf("%v\n", scopeToString(c.Security)))
}
if c.Changed != nil {
o = append(o, "### Changed\n", fmt.Sprintf("%v\n", scopeToString(c.Changed)))
}
if c.Added != nil {
o = append(o, "### Added\n", fmt.Sprintf("%v\n", scopeToString(c.Added)))
}
if c.Removed != nil {
o = append(o, "### Removed\n", fmt.Sprintf("%v\n", scopeToString(c.Removed)))
}
if c.Fixed != nil {
o = append(o, "### Fixed\n", fmt.Sprintf("%v\n", scopeToString(c.Fixed)))
}
if c.Deprecated != nil {
o = append(o, "### Deprecated\n", fmt.Sprintf("%v\n", scopeToString(c.Deprecated)))
}
return strings.Join(o, "\n")
}
func scopeToString(scope *[]string) string {
var o []string
for _, c := range *scope {
o = append(o, fmt.Sprintf("- %v", c))
}
return strings.Join(o, "\n")
}
// AddNotice adds a notice to the changes.
func (c *Changes) AddNotice(notice string) {
*c.Notice = notice
}
// AddChange adds a scoped change.
//
// Supported scopes: [added, changed, deprecated, removed, fixed, security].
func (c *Changes) AddChange(scope string, change string) error {
changesList := []string{change}
switch strings.ToLower(scope) {
case "added":
if change == "" {
return nil
}
if c.Added == nil {
c.Added = &changesList
} else {
*c.Added = append(*c.Added, change)
}
case "changed":
if change == "" {
return nil
}
if c.Changed == nil {
c.Changed = &changesList
} else {
*c.Changed = append(*c.Changed, change)
}
case "deprecated":
if change == "" {
return nil
}
if c.Deprecated == nil {
c.Deprecated = &changesList
} else {
*c.Deprecated = append(*c.Deprecated, change)
}
case "removed":
if change == "" {
return nil
}
if c.Removed == nil {
c.Removed = &changesList
} else {
*c.Removed = append(*c.Removed, change)
}
case "fixed":
if change == "" {
return nil
}
if c.Fixed == nil {
c.Fixed = &changesList
} else {
*c.Fixed = append(*c.Fixed, change)
}
case "security":
if change == "" {
return nil
}
if c.Security == nil {
c.Security = &changesList
} else {
*c.Security = append(*c.Security, change)
}
default:
return errors.New(fmt.Sprintf("unexpected scope: %v (supported: [added,changed,deprecated,removed,fixed,security])", scope))
}
return nil
}