From 6dae3d9aba35a0bb8e50802a2ff152bee37d1333 Mon Sep 17 00:00:00 2001 From: Christoph Witzko <github@christophwitzko.com> Date: Thu, 12 Nov 2020 23:31:38 +0100 Subject: [PATCH] refactor: add changelog types --- pkg/generator/changelog_generator.go | 76 +++----------------------- pkg/generator/changelog_types.go | 80 ++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 68 deletions(-) create mode 100644 pkg/generator/changelog_types.go diff --git a/pkg/generator/changelog_generator.go b/pkg/generator/changelog_generator.go index bafad78..02d6631 100644 --- a/pkg/generator/changelog_generator.go +++ b/pkg/generator/changelog_generator.go @@ -25,65 +25,6 @@ func formatCommit(c *semrel.Commit) string { 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" type DefaultChangelogGenerator struct{} @@ -102,27 +43,26 @@ func (g *DefaultChangelogGenerator) Version() string { func (*DefaultChangelogGenerator) Generate(changelogConfig *generator.ChangelogGeneratorConfig) string { 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 { if changelogConfig.LatestRelease.SHA == commit.SHA { break } 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 } if commit.Type == "" { continue } - typeScopeMap[commit.Type] += formatCommit(commit) + clTypes.AppendContent(commit.Type, formatCommit(commit)) } - for _, t := range getSortedKeys(&typeScopeMap) { - msg := typeScopeMap[t] - typeName, found := typeToText[t] - if !found { - typeName = t + for _, ct := range clTypes { + if ct.Content == "" { + continue } - ret += fmt.Sprintf("#### %s\n\n%s\n", typeName, msg) + ret += fmt.Sprintf("#### %s\n\n%s\n", ct.Text, ct.Content) } return ret } diff --git a/pkg/generator/changelog_types.go b/pkg/generator/changelog_types.go new file mode 100644 index 0000000..e04aee6 --- /dev/null +++ b/pkg/generator/changelog_types.go @@ -0,0 +1,80 @@ +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", + }, +} -- GitLab