generated from axetroy/go-cli-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 10
/
3_transformer.go
301 lines (262 loc) · 7.39 KB
/
3_transformer.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
package whatchanged
import (
"fmt"
"net/url"
"regexp"
"sort"
"strings"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/pkg/errors"
conventionalcommitparser "github.com/release-lab/conventional-commit-parser"
"github.com/release-lab/whatchanged/internal/client"
giturls "github.com/whilp/git-urls"
)
type BreakingChange struct {
Title string
Content string
}
type Footer struct {
BreakingChange *BreakingChange
Closes string
}
type Message struct {
raw string
Title string
Header *conventionalcommitparser.Header
Body string
Footer *Footer
}
type Commit struct {
Hash string
HashURL string
Short string
Message string
Author *object.Signature
Committer *object.Signature
Field Message
RevertCommitHash *string // Revert hash
RevertCommitHashURL *string // Revert hash URL
Closes *[]string // Closes issues
}
// https://github.com/angular/angular/blob/master/CONTRIBUTING.md#commit-message-header
type TemplateContext struct {
Version string
Date string
Build []*Commit
Ci []*Commit
Chore []*Commit
Docs []*Commit
Feat []*Commit
Fix []*Commit
Perf []*Commit
Refactor []*Commit
Test []*Commit
Style []*Commit
Revert []*Commit
BreakingChanges []*Commit
Commits []*Commit
Contributors []string
}
func generateCommitHashURL(remoteURL *url.URL, longHash string) string {
var (
shortHash string
)
if len(longHash) < 7 {
shortHash = longHash
} else {
shortHash = string(longHash[0:7])
}
if remoteURL != nil {
u := *remoteURL // https://github.com/golang/go/issues/38351
u.Path = u.Path + "/commit/" + longHash
return fmt.Sprintf("[`%s`](%s)", shortHash, u.String())
} else {
return shortHash
}
}
var githubOrgRegex = regexp.MustCompile(`^([^@]+)@github\.com:(\w+)\/(.+)$`)
func Transform(g *client.GitClient, splices []*ExtractSplice) ([]*TemplateContext, error) {
contexts := make([]*TemplateContext, 0)
remote, err := g.GetRemote()
if err != nil {
return nil, errors.WithStack(err)
}
var remoteURL *url.URL
if remote != nil || len(remote.URLs) > 0 {
for _, urlStr := range remote.URLs {
if remoteURL, err = giturls.Parse(urlStr); err != nil {
return nil, errors.WithStack(err)
} else {
if remoteURL.Host == "github.com" || remoteURL.Host == "gitlab.com" {
break
}
}
}
if remoteURL != nil {
urlPath := strings.TrimSuffix(remoteURL.Path, ".git")
switch remoteURL.Scheme {
case "http":
fallthrough
case "https":
if remoteURL, err = url.Parse(fmt.Sprintf("%s://%s%s", remoteURL.Scheme, remoteURL.Host, urlPath)); err != nil {
return nil, errors.WithStack(err)
}
case "file":
// not sure if this is a good fix here, but if you have a url that looks like private
// github urg ssh url, it'll end up having a "file" Scheme.
// so we check it it actually looks like a githubOrg ssh url and then return it
// eg. org-1232456@github.com:USER/REPO.git
if ms := githubOrgRegex.FindAllStringSubmatch(urlPath, -1); ms != nil {
if remoteURL, err = url.Parse(fmt.Sprintf("https://github.com/%s/%s", ms[0][2], ms[0][3])); err != nil {
return nil, errors.WithStack(err)
}
}
case "ssh":
if remoteURL, err = url.Parse(fmt.Sprintf("https://%s/%s", remoteURL.Host, urlPath)); err != nil {
return nil, errors.WithStack(err)
}
}
}
}
for _, splice := range splices {
ctx := &TemplateContext{
Version: splice.Name,
Contributors: make([]string, 0),
}
contributorMap := map[string]int{}
if splice.Tag != nil {
ctx.Date = splice.Tag.Date.Format("2006-01-02")
} else {
if len(splice.Commit) != 0 {
ctx.Date = splice.Commit[0].Committer.When.Format("2006-01-02")
}
}
if len(splice.Commit) != 0 {
ctx.Commits = make([]*Commit, 0)
}
for _, commit := range splice.Commit {
if count, ok := contributorMap[commit.Author.Name]; !ok {
contributorMap[commit.Author.Name] = 1
} else {
contributorMap[commit.Author.Name] = count + 1
}
ctx.Contributors = append(ctx.Contributors, commit.Author.Name)
hash := commit.Hash.String()
c := &Commit{
Hash: hash,
HashURL: generateCommitHashURL(remoteURL, hash),
Short: string(hash[0:7]),
Message: commit.Message,
Author: &commit.Author,
Committer: &commit.Committer,
}
msg := conventionalcommitparser.Parse(commit.Message)
header := msg.ParseHeader()
field := msg
c.Field = Message{
raw: commit.Message,
Title: header.Subject,
Body: msg.Body,
Header: &header,
}
if field.Footer != nil && len(field.Footer) > 0 {
// Specification
// BREAKING-CHANGE MUST be synonymous with BREAKING CHANGE, when used as a token in a footer.
breakingChangeFooter := field.GetFooterByField("BREAKING CHANGE", "BREAKING-CHANGE")
if breakingChangeFooter != nil {
if ctx.BreakingChanges == nil {
ctx.BreakingChanges = make([]*Commit, 0)
}
ctx.BreakingChanges = append(ctx.BreakingChanges, c)
c.Field.Footer = &Footer{
BreakingChange: &BreakingChange{
Title: breakingChangeFooter.Title,
Content: breakingChangeFooter.Content,
},
}
}
closes := field.GetCloses()
if len(closes) != 0 {
c.Closes = &closes
}
}
if header.Type == "revert" {
revertFooter := field.GetFooterByField("revert", "Revert")
if revertFooter != nil {
revertHash := revertFooter.Content
if len(revertHash) > 0 {
c.RevertCommitHash = &revertHash
revertHashURL := generateCommitHashURL(remoteURL, revertHash)
c.RevertCommitHashURL = &revertHashURL
}
}
}
ctx.Commits = append(ctx.Commits, c)
switch header.Type {
case "build":
if ctx.Build == nil {
ctx.Build = make([]*Commit, 0)
}
ctx.Build = append(ctx.Build, c)
case "ci":
if ctx.Ci == nil {
ctx.Ci = make([]*Commit, 0)
}
ctx.Ci = append(ctx.Ci, c)
case "chore":
if ctx.Chore == nil {
ctx.Chore = make([]*Commit, 0)
}
ctx.Chore = append(ctx.Chore, c)
case "docs":
if ctx.Docs == nil {
ctx.Docs = make([]*Commit, 0)
}
ctx.Docs = append(ctx.Docs, c)
case "feat":
if ctx.Feat == nil {
ctx.Feat = make([]*Commit, 0)
}
ctx.Feat = append(ctx.Feat, c)
case "fix":
if ctx.Fix == nil {
ctx.Fix = make([]*Commit, 0)
}
ctx.Fix = append(ctx.Fix, c)
case "perf":
if ctx.Perf == nil {
ctx.Perf = make([]*Commit, 0)
}
ctx.Perf = append(ctx.Perf, c)
case "refactor":
if ctx.Refactor == nil {
ctx.Refactor = make([]*Commit, 0)
}
ctx.Refactor = append(ctx.Refactor, c)
case "test":
if ctx.Test == nil {
ctx.Test = make([]*Commit, 0)
}
ctx.Test = append(ctx.Test, c)
case "style":
if ctx.Style == nil {
ctx.Style = make([]*Commit, 0)
}
ctx.Style = append(ctx.Style, c)
case "revert":
if ctx.Revert == nil {
ctx.Revert = make([]*Commit, 0)
}
ctx.Revert = append(ctx.Revert, c)
}
}
contributors := make([]string, 0, len(contributorMap))
for k := range contributorMap {
contributors = append(contributors, k)
}
sort.Strings(contributors)
ctx.Contributors = contributors
contexts = append(contexts, ctx)
}
return contexts, nil
}