-
Notifications
You must be signed in to change notification settings - Fork 1
/
commits.go
228 lines (198 loc) · 5.31 KB
/
commits.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
package gits
import (
"bytes"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/util"
"strings"
)
type CommitInfo struct {
Kind string
Feature string
Message string
group *CommitGroup
}
type CommitGroup struct {
Title string
Order int
}
var (
groupCounter = 0
// ConventionalCommitTitles textual descriptions for
// Conventional Commit types: https://conventionalcommits.org/
ConventionalCommitTitles = map[string]*CommitGroup{
"feat": createCommitGroup("Features"),
"fix": createCommitGroup("Bug Fixes"),
"perf": createCommitGroup("Performance Improvements"),
"refactor": createCommitGroup("Code Refactoring"),
"docs": createCommitGroup("Documentation"),
"test": createCommitGroup("Tests"),
"revert": createCommitGroup("Reverts"),
"style": createCommitGroup("Styles"),
"chore": createCommitGroup("Chores"),
"": createCommitGroup(""),
}
unknownKindOrder = groupCounter + 1
)
func createCommitGroup(title string) *CommitGroup {
groupCounter += 1
return &CommitGroup{
Title: title,
Order: groupCounter,
}
}
// ConventionalCommitTypeToTitle returns the title of the conventional commit type
// see: https://conventionalcommits.org/
func ConventionalCommitTypeToTitle(kind string) *CommitGroup {
answer := ConventionalCommitTitles[strings.ToLower(kind)]
if answer == nil {
answer = &CommitGroup{strings.Title(kind), unknownKindOrder}
}
return answer
}
// ParseCommit parses a conventional commit
// see: https://conventionalcommits.org/
func ParseCommit(message string) *CommitInfo {
answer := &CommitInfo{
Message: message,
}
idx := strings.Index(message, ":")
if idx > 0 {
answer.Kind = message[0:idx]
rest := strings.TrimSpace(message[idx+1:])
if strings.HasPrefix(rest, "(") {
idx = strings.Index(rest, ")")
if idx > 0 {
answer.Feature = strings.TrimSpace(rest[1:idx])
rest = strings.TrimSpace(rest[idx+1:])
}
}
answer.Message = rest
}
return answer
}
func (c *CommitInfo) Group() *CommitGroup {
if c.group == nil {
c.group = ConventionalCommitTitles[strings.ToLower(c.Kind)]
}
return c.group
}
func (c *CommitInfo) Title() string {
return c.Group().Title
}
func (c *CommitInfo) Order() int {
return c.Group().Order
}
type GroupAndCommitInfos struct {
group *CommitGroup
commits []string
}
// GenerateMarkdown generates the markdown document for the commits
func GenerateMarkdown(releaseSpec *v1.ReleaseSpec, gitInfo *GitRepositoryInfo) (string, error) {
commitInfos := []*CommitInfo{}
groupAndCommits := map[int]*GroupAndCommitInfos{}
for _, cs := range releaseSpec.Commits {
message := cs.Message
if message != "" {
ci := ParseCommit(message)
description := "* " + describeCommit(gitInfo, &cs, ci) + "\n"
group := ci.Group()
if group != nil {
gac := groupAndCommits[group.Order]
if gac == nil {
gac = &GroupAndCommitInfos{
group: group,
commits: []string{},
}
groupAndCommits[group.Order] = gac
}
gac.commits = append(gac.commits, description)
}
commitInfos = append(commitInfos, ci)
}
}
issues := releaseSpec.Issues
prs := releaseSpec.PullRequests
var buffer bytes.Buffer
if len(commitInfos) == 0 && len(issues) == 0 && len(prs) == 0 {
return "", nil
}
buffer.WriteString("## Changes\n")
if len(issues) > 0 {
buffer.WriteString("\n### Issues\n\n")
for _, issue := range issues {
buffer.WriteString("* " + describeIssue(gitInfo, &issue) + "\n")
}
}
if len(prs) > 0 {
buffer.WriteString("\n### Pull Requests\n\n")
for _, pr := range prs {
buffer.WriteString("* " + describeIssue(gitInfo, &pr) + "\n")
}
}
hasTitle := false
for i := 0; i <= unknownKindOrder; i++ {
gac := groupAndCommits[i]
if gac != nil && len(gac.commits) > 0 {
group := gac.group
if group != nil {
legend := ""
buffer.WriteString("\n")
if group.Title == "" && hasTitle {
group.Title = "Other Changes"
legend = "These commits did not use [Conventional Commits](https://conventionalcommits.org/) formatted messages:\n\n"
}
if group.Title != "" {
hasTitle = true
buffer.WriteString("### " + group.Title + "\n\n" + legend)
}
}
for _, msg := range gac.commits {
buffer.WriteString(msg)
}
}
}
return buffer.String(), nil
}
func describeIssue(info *GitRepositoryInfo, issue *v1.IssueSummary) string {
return "[#" + issue.ID + "](" + issue.URL + ") " + issue.Title + describeUser(info, issue.User)
}
func describeUser(info *GitRepositoryInfo, user *v1.UserDetails) string {
answer := ""
if user != nil {
userText := ""
login := user.Login
url := user.URL
label := login
if label == "" {
label = user.Name
}
if url == "" && login != "" {
url = util.UrlJoin(info.HostURL(), login)
}
if url == "" {
userText = label
} else {
if label != "" {
userText = "[" + label + "](" + url + ")"
}
}
if userText != "" {
answer = " (" + userText + ")"
}
}
return answer
}
func describeCommit(info *GitRepositoryInfo, cs *v1.CommitSummary, ci *CommitInfo) string {
prefix := ""
if ci.Feature != "" {
prefix = ci.Feature + ": "
}
message := strings.TrimSpace(ci.Message)
lines := strings.Split(message, "\n")
// TODO add link to issue etc...
user := cs.Author
if user == nil {
user = cs.Committer
}
return prefix + lines[0] + describeUser(info, user)
}