-
Notifications
You must be signed in to change notification settings - Fork 4
/
changelog_generator.go
84 lines (70 loc) · 1.8 KB
/
changelog_generator.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
package generator
import (
"fmt"
"strings"
"time"
"github.com/go-semantic-release/semantic-release/v2/pkg/generator"
"github.com/go-semantic-release/semantic-release/v2/pkg/semrel"
)
func trimSHA(sha string) string {
if len(sha) < 9 {
return sha
}
return sha[:8]
}
func formatCommit(c *semrel.Commit) string {
ret := "* "
if c.Scope != "" {
ret += fmt.Sprintf("**%s:** ", c.Scope)
}
ret += fmt.Sprintf("%s (%s)", c.Message, trimSHA(c.SHA))
return ret
}
var CGVERSION = "dev"
type DefaultChangelogGenerator struct {
emojis bool
}
func (g *DefaultChangelogGenerator) Init(m map[string]string) error {
emojis := false
emojiConfig := m["emojis"]
if emojiConfig == "true" {
emojis = true
}
g.emojis = emojis
return nil
}
func (g *DefaultChangelogGenerator) Name() string {
return "default"
}
func (g *DefaultChangelogGenerator) Version() string {
return CGVERSION
}
func (g *DefaultChangelogGenerator) Generate(changelogConfig *generator.ChangelogGeneratorConfig) string {
ret := fmt.Sprintf("## %s (%s)\n\n", changelogConfig.NewVersion, time.Now().UTC().Format("2006-01-02"))
clTypes := NewChangelogTypes()
for _, commit := range changelogConfig.Commits {
if changelogConfig.LatestRelease.SHA == commit.SHA {
break
}
if commit.Change != nil && commit.Change.Major {
bc := fmt.Sprintf("%s\n```\n%s\n```", formatCommit(commit), strings.Join(commit.Raw[1:], "\n"))
clTypes.AppendContent("%%bc%%", bc)
continue
}
if commit.Type == "" {
continue
}
clTypes.AppendContent(commit.Type, formatCommit(commit))
}
for _, ct := range clTypes {
if ct.Content == "" {
continue
}
emojiPrefix := ""
if g.emojis && ct.Emoji != "" {
emojiPrefix = fmt.Sprintf("%s ", ct.Emoji)
}
ret += fmt.Sprintf("#### %s%s\n\n%s\n", emojiPrefix, ct.Text, ct.Content)
}
return ret
}