-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
255 lines (228 loc) · 6.5 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"regexp"
"sort"
"strconv"
"strings"
"text/template"
"github.com/3zcurdia/reportcopter/utils"
"github.com/russross/blackfriday"
"github.com/urfave/cli"
)
const layout = `
<!DOCTYPE html>
<html lang="en">
<head>
<title>Changelog</title>
</head>
<body>
{{.Content}}
</body>
</html>
`
// ChangeLog represents a collection of changes between versions
type ChangeLog struct {
NameTags string
Commits []CommitMessage
}
// CommitMessage contains the commit information
type CommitMessage struct {
ShortCommit string `json:"shortcommit"`
Commit string `json:"commit"`
Author string `json:"author"`
Email string `json:"email"`
Date string `json:"date"`
Message string `json:"message"`
}
// Template for html format
type Template struct {
Content string
}
// Report full report structure
type Report struct {
pattern string
originURL string
commitON bool
authorON bool
ChangeLog []ChangeLog
JSON string
Markdown string
HTML string
}
func fetchTags(releasePattern string, sortON bool) []string {
out, _ := exec.Command("git", "log", "--tags", "--simplify-by-decoration", "--pretty=\"%ai @%d\"").Output()
uncuratedTags := strings.Split(string(out), "\n")
var tags []string
validTag := regexp.MustCompile(fmt.Sprintf(`(.*\s)@\s.*tag:\s(%v)`, releasePattern))
var match [][]string
for _, tag := range uncuratedTags {
match = validTag.FindAllStringSubmatch(tag, -1)
if len(match) > 0 && len(match[0]) > 2 {
// fmt.Printf("%v => %v \n", match[0][1], match[0][2])
tags = append(tags, strings.Replace(match[0][2], ",", "", -1))
}
}
if sortON {
tags = utils.OnlyStable(tags)
sort.Sort(utils.ByVersion(tags))
}
return tags
}
func fetchChanges(releasePattern string, limit int, sortON bool) []ChangeLog {
var changeLogs []ChangeLog
tags := fetchTags(releasePattern, sortON)
var diffs []string
for i := 0; i < len(tags)-1; i++ {
diffs = append(diffs, fmt.Sprintf("%v..%v", tags[i+1], tags[i]))
if limit > 0 && limit < len(diffs) {
break
}
}
format := "--pretty=format:{\"shortcommit\":\"%h\", \"commit\":\"%H\", \"author\":\"%an\", \"email\":\"%ae\", \"date\":\"%ad\", \"message\":\"%f\"},"
var out []byte
var outs string
var data []CommitMessage
re := regexp.MustCompile(`,$`)
for _, diff := range diffs {
out, _ = exec.Command("git", "log", format, diff).Output()
outs = fmt.Sprintf("[%v]", re.ReplaceAllString(string(out), ""))
if err := json.Unmarshal([]byte(outs), &data); err != nil {
panic(err)
}
changeLogs = append(changeLogs, ChangeLog{NameTags: diff, Commits: data})
data = nil
}
return changeLogs
}
func (c *CommitMessage) toMarkdown(originURL string, commitON, authorON bool) string {
space := regexp.MustCompile(`-`)
var commitLink, authorLink string
if commitON {
commitLink = fmt.Sprintf("[%v](%vcommit/%v)", c.ShortCommit, originURL, c.Commit)
}
if authorON {
authorLink = fmt.Sprintf("[%v](mailto:%v)", c.Author, c.Email)
}
return fmt.Sprintf("* %v %v %v\n", commitLink, space.ReplaceAllString(c.Message, " "), authorLink)
}
func fetchProjectURL() string {
originURL, _ := exec.Command("git", "config", "--get", "remote.origin.url").Output()
re := regexp.MustCompile(`:`)
originURL = []byte(re.ReplaceAllString(string(originURL), `/`))
re = regexp.MustCompile(`\.git\n$`)
originURL = []byte(re.ReplaceAllString(string(originURL), `/`))
re = regexp.MustCompile(`^git@`)
originURL = []byte(re.ReplaceAllString(string(originURL), `https://`))
return string(originURL)
}
func buildReport(releasePattern string, commitLinksON, authorLinksON bool, limit int, sortON bool) Report {
report := Report{
pattern: releasePattern,
originURL: fetchProjectURL(),
commitON: commitLinksON,
authorON: authorLinksON,
}
report.ChangeLog = fetchChanges(releasePattern, limit, sortON)
byteJSON, _ := json.Marshal(report.ChangeLog)
report.JSON = string(byteJSON)
return report
}
func (r *Report) getMarkdown() string {
if len(r.Markdown) > 0 {
return r.Markdown
}
var mdBuffer bytes.Buffer
mdBuffer.WriteString("# Changelog\n")
for _, change := range r.ChangeLog {
mdBuffer.WriteString(fmt.Sprintf("\n## %v \n\n", change.NameTags))
for _, commit := range change.Commits {
mdBuffer.WriteString(commit.toMarkdown(string(r.originURL), r.commitON, r.authorON))
}
}
r.Markdown = mdBuffer.String()
return r.Markdown
}
func (r *Report) getHTML() string {
if len(r.HTML) > 0 {
return r.HTML
}
out := bytes.NewBuffer(nil)
html := blackfriday.MarkdownCommon([]byte(r.getMarkdown()))
t := template.Must(template.New("layout").Parse(layout))
err := t.Execute(out, Template{Content: string(html)})
if err != nil {
panic(err)
}
r.HTML = out.String()
return r.HTML
}
func main() {
app := cli.NewApp()
app.Name = "Changes reporter"
app.Usage = "Generate changelog report from git commits and tag releases"
app.Version = "0.0.4"
app.Author = "Luis Ezcurdia"
app.Email = "[email protected]"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "pattern, p",
Value: `v[\d{1,4}\.]{1,}`,
Usage: "Regular expresion for release tags",
},
cli.StringFlag{
Name: "format, f",
Value: "markdown",
Usage: "Output format for report",
},
cli.StringFlag{
Name: "limit, l",
Value: "-1",
Usage: "Limit the report to a determinated number of versions",
},
cli.StringFlag{
Name: "sort, s",
Value: "version",
Usage: "Sort tags by version",
},
cli.StringFlag{
Name: "no-commit",
Value: "false",
Usage: "Omit commit links on markdown and HTML reports",
},
cli.StringFlag{
Name: "no-author",
Value: "false",
Usage: "Omit author links on markdown and HTML reports",
},
cli.StringFlag{
Name: "only-message",
Value: "false",
Usage: "Only show commit messages on markdown and HTML reports",
},
}
app.Action = func(c *cli.Context) {
limit, _ := strconv.Atoi(c.String("limit"))
commitLinksON := strings.ToLower(c.String("no-commit")) == "false"
authorLinksON := strings.ToLower(c.String("no-author")) == "false"
sortByVersion := strings.ToLower(c.String("historic-sort")) == "version"
if strings.ToLower(c.String("only-message")) != "false" {
commitLinksON = false
authorLinksON = false
}
report := buildReport(c.String("pattern"), commitLinksON, authorLinksON, limit, sortByVersion)
switch strings.ToLower(c.String("format")) {
case "json":
fmt.Println(report.JSON)
case "html":
fmt.Print(report.getHTML())
default:
fmt.Println(report.getMarkdown())
}
}
app.Run(os.Args)
}