-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
433 lines (394 loc) · 10.4 KB
/
main.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
"github.com/blang/semver"
"github.com/hassansin/gh-release/internal/github"
"github.com/manifoldco/promptui"
"github.com/pkg/errors"
)
const (
defaultEditor = "vim"
tagPrefix = "v"
releaseMsgFilename = "RELEASE_EDITMSG"
lineReset = "\033[2K\r"
)
var (
reRepo = regexp.MustCompile(`[/:]([a-z-]+)/([a-z-]+)`)
reSection = regexp.MustCompile(`^\[(.*)\]`)
reVal = regexp.MustCompile(`^\s+(\w+)\s*=\s*(.*)$`)
reComment = regexp.MustCompile(`^\s*#`)
bold = promptui.Styler(promptui.FGBold)
cyan = promptui.Styler(promptui.FGCyan, promptui.FGBold)
white = promptui.Styler(promptui.FGWhite, promptui.FGBold)
green = promptui.Styler(promptui.FGGreen, promptui.FGBold)
faint = promptui.Styler(promptui.FGFaint)
startBoldCyan = strings.Replace(cyan(""), promptui.ResetCode, "", -1)
)
func main() {
owner, name, head := mustGetCurrentRepo()
editorCmd := mustFindEditor()
token := mustGetToken()
client := github.New(owner, name, token)
if err := do(editorCmd, client, head); err != nil {
abort(err)
}
}
//abort exits with non-zero status, prints pretty error message instead of panic
func abort(err error) {
fmt.Printf("%v %v\n", promptui.IconBad, err)
os.Exit(1)
}
func do(editorCmd []string, client github.GithubClient, head string) error {
done := make(chan struct{})
go showProgress("getting current release", done)
repo, err := client.GetRepository()
if err != nil {
return err
}
if len(repo.Branches) == 0 {
return errors.New("Couldn't find any remote branch")
}
if repo.LatestRelease == nil {
return errors.New("no previous release") //@TODO
}
sortBranches(repo.Branches, head)
done <- struct{}{}
/*
fmt.Printf("%v %v %v\n\t%v, \n\t%v\n",
green(promptui.IconGood),
white("Current release:"),
cyan(repo.LatestRelease.Name),
faint("Tag: "+repo.LatestRelease.Tag.Name),
faint("Commit: "+repo.LatestRelease.Tag.Target.ShortID+" "+repo.LatestRelease.Tag.Target.Message))
*/
target, err := selectTarget(repo.Branches, repo.LatestRelease)
if err != nil || target == nil {
return err
}
lastRel := repo.LatestRelease.Tag.Name
version, err := nextVersion(lastRel)
if err != nil {
return err
}
var commits []*github.Commit
errCh := make(chan error)
go func() {
commits, err = client.CompareCommits(repo.LatestRelease.Tag.Target, target.Head)
errCh <- err
}()
tagName, err := promptTag(version, lastRel)
if err != nil || tagName == "" {
return err
}
if err := <-errCh; err != nil {
return err
}
if len(commits) == 0 {
return errors.Errorf("%v is already released", cyan(target.Name))
}
ed := newEditor(editorCmd)
title, body, err := ed.edit(releaseNotes(tagName, commits))
if err != nil {
return err
}
go showProgress("creating release", done)
release, err := client.CreateRelease(&github.Release{
Name: title,
Tag: github.Tag{
Name: tagName,
Target: target.Head,
},
Description: body,
})
done <- struct{}{}
if err != nil {
return err
}
fmt.Printf("%v New release(%v) created:\n %v\n", green(promptui.IconGood), cyan(release.Tag.Name), release.HTMLURL)
return nil
}
func nextVersion(tag string) (string, error) {
v, err := semver.Make(strings.TrimPrefix(tag, tagPrefix))
if err != nil {
return "", err
}
v.Patch++
version := v.String()
if strings.HasPrefix(tag, tagPrefix) {
version = tagPrefix + version
}
return version, nil
}
//sort branches by branch name length, keeping head at the top
func sortBranches(branches []*github.Branch, head string) {
sort.Slice(branches, func(i, j int) bool {
li := len(branches[i].Name)
lj := len(branches[j].Name)
if branches[i].Name == head {
li = 0
}
if branches[j].Name == head {
lj = 0
}
return li < lj
})
}
func promptTag(tag, lastRel string) (string, error) {
templates := &promptui.PromptTemplates{
Success: fmt.Sprintf(`{{ "%s" | green | bold }} {{"%s" | bold}} %v`, promptui.IconGood, "Tag:", startBoldCyan),
}
prompt := promptui.Prompt{
Label: fmt.Sprintf("Enter release tag %s", faint("(last release: "+cyan(lastRel)+")")),
AllowEdit: true,
Default: tag,
Templates: templates,
}
tagName, err := prompt.Run()
fmt.Print(promptui.ResetCode)
if err == promptui.ErrInterrupt || err == promptui.ErrEOF {
return "", nil
} else if err != nil {
return "", err
}
return tagName, nil
}
func selectTarget(branches []*github.Branch, rel *github.Release) (*github.Branch, error) {
type option struct {
Name string
Count int
Status string
}
var options []option
for _, b := range branches {
count := b.CommitCount - rel.Tag.CommitCount
status := "ahead"
if count < 0 {
count = -1 * count
status = "behind"
}
//options[i] = fmt.Sprintf("%s (%v commits %v)", b.Name, count, status)
options = append(options, option{
Name: b.Name,
Status: status,
Count: count,
})
}
templates := &promptui.SelectTemplates{
Label: fmt.Sprintf("%s {{.Name}} {{.Count}} {{.Status}}", promptui.IconInitial),
Active: fmt.Sprintf(`%s {{printf "%%v (%%v commits %%v)" (.Name|bold) (.Count | yellow) (.Status | red) }}`, "▣"),
Inactive: fmt.Sprintf(`%s {{printf "%%v (%%v commits %%v)" .Name (.Count | yellow) (.Status | red)}}`, "▢"),
Selected: fmt.Sprintf(`{{ "%s" | green | bold }} {{"%s" | bold}} {{.Name| cyan | bold }}`, promptui.IconGood, "Target:"),
}
prompt := promptui.Select{
Label: "Choose a target branch",
Items: options,
Templates: templates,
Size: 4,
}
i, _, err := prompt.Run()
if err == promptui.ErrInterrupt || err == promptui.ErrEOF {
return nil, nil
} else if err != nil {
return nil, err
}
return branches[i], nil
}
func mustGetToken() string {
token, err := getToken()
if err != nil {
abort(err)
}
if token == "" {
abort(errors.New("token not found in your gitconfig file"))
}
return token
}
func getToken() (string, error) {
u, err := user.Current()
if err != nil {
return "", err
}
gitconfigPath := filepath.Join(u.HomeDir, string(filepath.Separator), ".gitconfig")
data, err := ioutil.ReadFile(gitconfigPath)
if err != nil {
return "", err
}
config := parseConfig(data)
if config["github"] == nil {
return "", nil
}
return config["github"]["token"], nil
}
func mustGetCurrentRepo() (owner string, repo string, head string) {
mustBeGitRepo()
cmd := exec.Command("git", "ls-remote", "--get-url", "origin")
var out []byte
var err error
out, err = cmd.Output()
if err != nil {
panic(err)
}
if m := reRepo.FindStringSubmatch(string(out)); m != nil {
owner, repo = m[1], m[2]
}
cmd = exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
out, err = cmd.Output()
if err != nil {
panic(err)
}
head = strings.TrimSpace(string(out))
return
}
func mustBeGitRepo() {
cmd := exec.Command("git", "rev-parse", "--is-inside-work-tree")
out, err := cmd.Output()
if err != nil {
abort(err)
}
if strings.TrimSpace(string(out)) != "true" {
abort(errors.New("not a git repo"))
}
}
func parseSection(line string) string {
if reSection.MatchString(line) {
matches := reSection.FindStringSubmatch(line)
if matches != nil && len(matches) == 2 {
return matches[1]
}
}
return ""
}
func parseValue(line string) (string, string) {
if m := reVal.FindStringSubmatch(line); m != nil && len(m) == 3 {
return m[1], m[2]
}
return "", ""
}
func isComment(line string) bool {
return reComment.MatchString(line)
}
func parseConfig(data []byte) map[string]map[string]string {
config := make(map[string]map[string]string)
lines := strings.Split(string(data), "\n")
section := "" //current section tracking while parsing
for _, line := range lines {
if isComment(line) {
continue
}
if key, val := parseValue(line); section != "" && key != "" {
config[section][key] = val
continue
}
//start of a section
section = parseSection(line)
if section != "" {
config[section] = make(map[string]string)
}
}
return config
}
func releaseNotes(title string, commits []*github.Commit) string {
notes := ""
for i := len(commits) - 1; i >= 0; i-- {
c := commits[i]
lines := strings.Split(c.Message, "\n")
notes += fmt.Sprintf("#* [%v] - %v (%v)\n", c.ShortID, lines[0], c.Author)
}
return fmt.Sprintf(`#%v
#
# Please enter the realease title as the first line. Lines starting
# with '#' will be ignored, and an empty title & message aborts the operation.
# By removing starting '#' of lines below, you can put them in release body.
#
#**Commits**
#
%v`, title, notes)
}
func mustFindEditor() []string {
env := os.Getenv("EDITOR")
if env == "" {
env = defaultEditor
}
re := regexp.MustCompile("\\s")
parts := re.Split(env, -1)
path, err := exec.LookPath(parts[0])
if err != nil {
abort(fmt.Errorf("unable to find editor(%v): %v", parts[0], err))
}
return append([]string{path}, parts[1:]...)
}
func newEditor(cmd []string) *editor {
gitDir := ".git"
if dir, ok := os.LookupEnv("GIT_DIR"); ok {
gitDir = dir
}
file := path.Join(gitDir, releaseMsgFilename)
return &editor{
cmd: cmd,
file: file,
mode: 0644,
}
}
type editor struct {
cmd []string
file string
mode os.FileMode
}
func (ed editor) edit(msg string) (string, string, error) {
if err := ioutil.WriteFile(ed.file, []byte(msg), ed.mode); err != nil {
return "", "", fmt.Errorf("unable to write release message: %v", err)
}
cmd := exec.Command(ed.cmd[0], append(ed.cmd[1:], ed.file)...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return "", "", fmt.Errorf("edit error: %v", err)
}
data, err := ioutil.ReadFile(ed.file)
if err != nil {
return "", "", err
}
title, body := parseReleaseMsg(data)
return title, body, nil
}
func parseReleaseMsg(data []byte) (string, string) {
lines := strings.Split(string(data), "\n")
newLines := lines[:0]
for _, line := range lines {
if isComment(line) {
continue
}
newLines = append(newLines, line)
}
if len(newLines) == 0 {
return "", ""
}
return newLines[0], strings.TrimSpace(strings.Join(newLines[1:], "\n"))
}
func showProgress(msg string, done chan struct{}) {
progress := []string{"⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"}
defer fmt.Print(faint(lineReset))
i := 1
for {
fmt.Print(faint(lineReset))
fmt.Printf("%v %v", green(progress[i%len(progress)]), faint(msg))
select {
case <-done:
return
default:
time.Sleep(50 * time.Millisecond)
i++
}
}
}