Skip to content
Snippets Groups Projects
Commit 6dae3d9a authored by Christoph Witzko's avatar Christoph Witzko
Browse files

refactor: add changelog types

parent 9f4e8b78
No related branches found
No related tags found
No related merge requests found
...@@ -25,65 +25,6 @@ func formatCommit(c *semrel.Commit) string { ...@@ -25,65 +25,6 @@ func formatCommit(c *semrel.Commit) string {
return ret return ret
} }
var typeToText = map[string]string{
"%%bc%%": "Breaking Changes",
"feat": "Feature",
"fix": "Bug Fixes",
"revert": "Reverts",
"perf": "Performance Improvements",
"docs": "Documentation",
"test": "Tests",
"refactor": "Code Refactoring",
"style": "Styles",
"chore": "Chores",
"build": "Build",
"ci": "CI",
}
var typeOrder = map[int]string{
0: "%%bc%%",
1: "feat",
2: "fix",
3: "revert",
4: "perf",
5: "docs",
6: "test",
7: "refactor",
8: "style",
9: "chore",
10: "build",
11: "ci",
}
func getSortedKeys(m *map[string]string) []string {
types := make(map[int]string, len(*m))
keys := make([]string, len(*m))
i := 0
for k := range *m {
types[i] = k
i++
}
i = 0
for ki := 0; ki < len(typeOrder); ki++ {
for ti := range types {
if types[ti] == typeOrder[ki] {
keys[i] = types[ti]
delete(types, ti)
i++
}
}
}
for ti := range types {
keys[i] = types[ti]
i++
}
return keys
}
var CGVERSION = "dev" var CGVERSION = "dev"
type DefaultChangelogGenerator struct{} type DefaultChangelogGenerator struct{}
...@@ -102,27 +43,26 @@ func (g *DefaultChangelogGenerator) Version() string { ...@@ -102,27 +43,26 @@ func (g *DefaultChangelogGenerator) Version() string {
func (*DefaultChangelogGenerator) Generate(changelogConfig *generator.ChangelogGeneratorConfig) string { func (*DefaultChangelogGenerator) Generate(changelogConfig *generator.ChangelogGeneratorConfig) string {
ret := fmt.Sprintf("## %s (%s)\n\n", changelogConfig.NewVersion, time.Now().UTC().Format("2006-01-02")) ret := fmt.Sprintf("## %s (%s)\n\n", changelogConfig.NewVersion, time.Now().UTC().Format("2006-01-02"))
typeScopeMap := make(map[string]string) clTypes := NewChangelogTypes()
for _, commit := range changelogConfig.Commits { for _, commit := range changelogConfig.Commits {
if changelogConfig.LatestRelease.SHA == commit.SHA { if changelogConfig.LatestRelease.SHA == commit.SHA {
break break
} }
if commit.Change != nil && commit.Change.Major { if commit.Change != nil && commit.Change.Major {
typeScopeMap["%%bc%%"] += fmt.Sprintf("%s```\n%s\n```\n", formatCommit(commit), strings.Join(commit.Raw[1:], "\n")) bc := fmt.Sprintf("%s```\n%s\n```\n", formatCommit(commit), strings.Join(commit.Raw[1:], "\n"))
clTypes.AppendContent("%%bc%%", bc)
continue continue
} }
if commit.Type == "" { if commit.Type == "" {
continue continue
} }
typeScopeMap[commit.Type] += formatCommit(commit) clTypes.AppendContent(commit.Type, formatCommit(commit))
} }
for _, t := range getSortedKeys(&typeScopeMap) { for _, ct := range clTypes {
msg := typeScopeMap[t] if ct.Content == "" {
typeName, found := typeToText[t] continue
if !found {
typeName = t
} }
ret += fmt.Sprintf("#### %s\n\n%s\n", typeName, msg) ret += fmt.Sprintf("#### %s\n\n%s\n", ct.Text, ct.Content)
} }
return ret return ret
} }
package generator
type ChangelogType struct {
Type string
Text string
Content string
}
type ChangelogTypes []ChangelogType
func NewChangelogTypes() ChangelogTypes {
ret := make(ChangelogTypes, len(defaultTypes))
copy(ret, defaultTypes)
return ret
}
func (ct *ChangelogTypes) AppendContent(cType, content string) {
for i, cct := range *ct {
if cct.Type == cType {
(*ct)[i].Content += content
return
}
}
*ct = append(*ct, ChangelogType{
Type: cType,
Text: cType,
Content: content,
})
}
var defaultTypes = ChangelogTypes{
{
Type: "%%bc%%",
Text: "Breaking Changes",
},
{
Type: "feat",
Text: "Feature",
},
{
Type: "fix",
Text: "Bug Fixes",
},
{
Type: "revert",
Text: "Reverts",
},
{
Type: "perf",
Text: "Performance Improvements",
},
{
Type: "docs",
Text: "Documentation",
},
{
Type: "test",
Text: "Tests",
},
{
Type: "refactor",
Text: "Code Refactoring",
},
{
Type: "style",
Text: "Styles",
},
{
Type: "chore",
Text: "Chores",
},
{
Type: "build",
Text: "Build",
},
{
Type: "ci",
Text: "CI",
},
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment