-
Notifications
You must be signed in to change notification settings - Fork 4
/
bumper.go
291 lines (257 loc) · 6.75 KB
/
bumper.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package bump
import (
"bytes"
"fmt"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/Masterminds/semver/v3"
)
//go:generate mockgen -source=$GOFILE -package=mock -destination=./mock/mock_${GOPACKAGE}.go
type Gh interface {
ViewRepository() (sout, eout *bytes.Buffer, err error)
ListRelease(repo string, isCurrent bool) (sout, eout *bytes.Buffer, err error)
ViewRelease(repo string, isCurrent bool) (sout, eout *bytes.Buffer, err error)
CreateRelease(version string, repo string, isCurrent bool, option *ReleaseOption) (sout, eout *bytes.Buffer, err error)
}
//go:generate mockgen -source=$GOFILE -package=mock -destination=./mock/mock_${GOPACKAGE}.go
type Prompter interface {
Input(question string, validator survey.Validator) (string, error)
Select(question string, options []string) (string, error)
Confirm(question string) (bool, error)
}
type ReleaseOption struct {
IsDraft bool
IsPrerelease bool
DiscussionCategory string
GenerateNotes bool
Notes string
NotesFilename string
Target string
Title string
AssetFiles []string
}
type bumper struct {
gh Gh
prompter Prompter
repository string
isCurrent bool
isDraft bool
isPrerelease bool
discussionCategory string
generateNotes bool
notes string
notesFilename string
target string
title string
assetFiles []string
bumpType BumpType
yes bool
}
func NewBumper(gh Gh) *bumper {
return &bumper{
gh: gh,
prompter: newPrompter(),
}
}
func (b *bumper) WithRepository(repository string) error {
if repository != "" {
b.repository = repository
return nil
}
repo, err := b.resolveRepository()
if err != nil {
return err
}
b.repository = repo
b.isCurrent = true
return nil
}
func (b *bumper) WithDraft() {
b.isDraft = true
}
func (b *bumper) WithPrerelease() {
b.isPrerelease = true
}
func (b *bumper) WithDiscussionCategory(category string) {
b.discussionCategory = category
}
func (b *bumper) WithGenerateNotes() {
b.generateNotes = true
}
func (b *bumper) WithNotes(notes string) {
b.notes = notes
}
func (b *bumper) WithNotesFile(filename string) {
b.notesFilename = filename
}
func (b *bumper) WithTarget(target string) {
b.target = target
}
func (b *bumper) WithTitle(title string) {
b.title = title
}
func (b *bumper) WithAssetFiles(files []string) {
b.assetFiles = files
}
func (b *bumper) WithBumpType(s string) error {
bumpType, err := ParseBumpType(s)
if err != nil {
return err
}
b.bumpType = bumpType
return nil
}
func (b *bumper) WithYes() {
b.yes = true
}
func (b *bumper) Bump() error {
releases, err := b.listReleases()
if err != nil {
return err
}
fmt.Println(releases)
if len(b.assetFiles) > 0 {
fmt.Println("Asset files:")
fmt.Println(strings.Join(b.assetFiles, "\n"))
fmt.Println()
}
current, isInitial, err := b.currentVersion()
if err != nil {
return err
}
var nextVer *semver.Version
if isInitial {
nextVer = current
} else if b.bumpType.Valid() == nil && !b.bumpType.IsBlank() {
nextVer, err = incrementVersion(current, b.bumpType.String())
if err != nil {
return err
}
} else {
nextVer, err = b.nextVersion(current)
if err != nil {
return err
}
}
// Skip approval if --yes is set
if !b.yes {
ok, err := b.approve(nextVer)
if err != nil {
return err
}
if !ok {
fmt.Println("Bump was canceled.")
return nil
}
}
result, err := b.createRelease(nextVer.Original())
if err != nil {
return err
}
fmt.Println("Release was created.")
fmt.Println(result)
return nil
}
func (b *bumper) resolveRepository() (string, error) {
sout, _, err := b.gh.ViewRepository()
if err != nil {
return "", err
}
viewOut := strings.Split(sout.String(), "\n")[0]
repo := strings.TrimSpace(strings.Split(viewOut, ":")[1])
return repo, nil
}
func (b *bumper) listReleases() (string, error) {
sout, _, err := b.gh.ListRelease(b.repository, b.isCurrent)
if err != nil {
return "", err
}
return fmt.Sprintf("Tags:\n%s", sout.String()), nil
}
func (b *bumper) currentVersion() (current *semver.Version, isInitial bool, err error) {
sout, eout, err := b.gh.ViewRelease(b.repository, b.isCurrent)
if err != nil {
if strings.Contains(eout.String(), "release not found") {
current, err = b.newVersion()
if err != nil {
return nil, false, err
}
return current, true, nil
}
return nil, false, err
}
viewOut := strings.Split(sout.String(), "\n")[1]
tag := strings.TrimSpace(strings.Split(viewOut, ":")[1])
current, err = semver.NewVersion(tag)
if err != nil {
return nil, false, fmt.Errorf("invalid version. err: %w", err)
}
return current, false, nil
}
func (b *bumper) newVersion() (*semver.Version, error) {
validate := func(v interface{}) error {
input, ok := v.(string)
if !ok {
return fmt.Errorf("invalid input type. input: %v", v)
}
_, err := semver.NewVersion(input)
if err != nil {
return fmt.Errorf("invalid version. err: %w", err)
}
return nil
}
version, err := b.prompter.Input("New version", validate)
if err != nil {
return nil, fmt.Errorf("failed to prompt. err: %w", err)
}
return semver.NewVersion(version)
}
func (b *bumper) nextVersion(current *semver.Version) (*semver.Version, error) {
question := fmt.Sprintf("Select next version. current: %s", current.Original())
options := []string{"patch", "minor", "major"}
bumpType, err := b.prompter.Select(question, options)
if err != nil {
return nil, fmt.Errorf("failed to prompt. err: %w", err)
}
return incrementVersion(current, bumpType)
}
func incrementVersion(current *semver.Version, bumpType string) (*semver.Version, error) {
var next semver.Version
switch bumpType {
case "major":
next = current.IncMajor()
case "minor":
next = current.IncMinor()
case "patch":
next = current.IncPatch()
default:
return nil, fmt.Errorf("invalid type")
}
return &next, nil
}
func (b *bumper) approve(next *semver.Version) (bool, error) {
question := fmt.Sprintf("Create release %s ?", next.Original())
isApproved, err := b.prompter.Confirm(question)
if err != nil {
return false, fmt.Errorf("failed to prompt. err: %w", err)
}
return isApproved, nil
}
func (b *bumper) createRelease(version string) (string, error) {
option := &ReleaseOption{
IsDraft: b.isDraft,
IsPrerelease: b.isPrerelease,
DiscussionCategory: b.discussionCategory,
GenerateNotes: b.generateNotes,
Notes: b.notes,
NotesFilename: b.notesFilename,
Target: b.target,
Title: b.title,
AssetFiles: b.assetFiles,
}
sout, _, err := b.gh.CreateRelease(version, b.repository, b.isCurrent, option)
if err != nil {
return "", err
}
return sout.String(), nil
}