-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
136 lines (131 loc) · 3.23 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
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
"github.com/hashicorp/go-version"
)
var (
source = flag.String("source", "", "--source ~/tmp")
output = flag.String("output", "CHANGELOG.md", "--output CHANGELOG.md")
fetch = flag.Bool("fetch", false, "--fetch")
verbose = flag.Bool("verbose", false, "--verbose")
)
type record struct {
Version string
Date string
Features []string
BugFixes []string
}
func main() {
flag.Parse()
if *fetch {
if err := fetchGitRepository(); err != nil {
fmt.Println(err)
return
}
}
gittags, err := getGitTags()
if err != nil {
fmt.Println(err)
return
}
if *verbose {
fmt.Println(gittags)
}
tags := strings.Split(gittags, "\n")
records := make([]record, 0, len(tags))
for _, t := range tags {
segs := strings.Split(t, ";")
if len(segs) < 3 || len(segs[0]) == 0 {
continue
}
version := segs[0]
date := strings.ReplaceAll(segs[1], "-", "/")
records = append(records, record{Version: version, Date: date})
}
sort.Slice(records, func(i, j int) bool {
s, t := records[i].Version, records[j].Version
vs, err := version.NewVersion(s)
if err != nil {
fmt.Printf("Invalid go version: %s\n", s)
return s < t
}
vt, err := version.NewVersion(t)
if err != nil {
fmt.Printf("Invalid go version: %s\n", t)
return s < t
}
return vs.LessThan(vt)
})
for i := 0; i < len(records); i++ {
var v1, v2 string
if i > 0 {
v1 = records[i-1].Version
}
v2 = records[i].Version
gitcommits, err := getGitLogs(v1, v2)
if err != nil {
fmt.Println(err)
continue
}
if *verbose {
fmt.Println(string(gitcommits))
}
commits := strings.Split(string(gitcommits), "\n")
records[i].Features = make([]string, 0)
records[i].BugFixes = make([]string, 0)
for _, c := range commits {
msg, cs := c, strings.SplitN(c, " ", 2)
if len(cs) > 1 {
msg = cs[1]
}
if len(msg) == 0 {
continue
}
if strings.HasPrefix(msg, "[fix]") {
records[i].BugFixes = append(records[i].BugFixes, strings.TrimSpace(strings.TrimPrefix(msg, "[fix]")))
} else if strings.HasPrefix(msg, "[feat]") {
records[i].Features = append(records[i].Features, strings.TrimSpace(strings.TrimPrefix(msg, "[feat]")))
} else if strings.HasPrefix(msg, "[feature]") {
records[i].Features = append(records[i].Features, strings.TrimSpace(strings.TrimPrefix(msg, "[feature]")))
} else if strings.Contains(msg, "fix") {
records[i].BugFixes = append(records[i].BugFixes, msg)
} else {
records[i].Features = append(records[i].Features, msg)
}
}
}
var buf bytes.Buffer
for i := len(records) - 1; i >= 0; i-- {
r := records[i]
buf.WriteString(fmt.Sprintf("# %s (%s)\n", strings.TrimPrefix(r.Version, "v"), r.Date))
if len(r.Features) > 0 {
buf.WriteString("\n### Features\n\n")
for _, c := range r.Features {
if len(c) > 0 {
buf.WriteString(fmt.Sprintf("- %s\n", c))
}
}
}
if len(r.BugFixes) > 0 {
buf.WriteString("\n### Bug Fixes\n\n")
for _, c := range r.BugFixes {
if len(c) > 0 {
buf.WriteString(fmt.Sprintf("- %s\n", c))
}
}
}
if i != 0 {
buf.WriteString("\n")
}
}
if err := ioutil.WriteFile(*output, buf.Bytes(), os.ModePerm); err != nil {
fmt.Println(err)
return
}
}