refactor: split filter outside generate markdown and rst

main
sudo pacman -Syu 2021-06-16 17:56:10 +00:00
parent ad0be6321c
commit 3b8d13654e
5 changed files with 103 additions and 78 deletions

View File

@ -3,5 +3,5 @@ package changelog
const ( const (
title = "CHANGELOG" title = "CHANGELOG"
defaultNodesLen = 10 defaultLen = 10
) )

View File

@ -0,0 +1,40 @@
package changelog
import (
"github.com/haunt98/changeloguru/internal/convention"
)
func filter(commits []convention.Commit, scopes map[string]struct{}) map[string][]convention.Commit {
if len(commits) == 0 {
return nil
}
filteredCommits := make(map[string][]convention.Commit)
filteredCommits[addedType] = make([]convention.Commit, 0, defaultLen)
filteredCommits[fixedType] = make([]convention.Commit, 0, defaultLen)
filteredCommits[othersType] = make([]convention.Commit, 0, defaultLen)
for _, commit := range commits {
// If scopes is empty or commit scope is empty, pass all commits
if len(scopes) != 0 && commit.Scope != "" {
// Skip commit outside scopes
if _, ok := scopes[commit.Scope]; !ok {
continue
}
}
t := getType(commit.Type)
switch t {
case addedType:
filteredCommits[addedType] = append(filteredCommits[addedType], commit)
case fixedType:
filteredCommits[fixedType] = append(filteredCommits[fixedType], commit)
case othersType:
filteredCommits[othersType] = append(filteredCommits[othersType], commit)
default:
continue
}
}
return filteredCommits
}

View File

@ -0,0 +1,12 @@
package changelog
import (
"fmt"
"time"
"github.com/haunt98/clock"
)
func generateVersionHeaderValue(version string, when time.Time) string {
return fmt.Sprintf("%s (%s)", version, clock.FormatDate(when))
}

View File

@ -1,12 +1,10 @@
package changelog package changelog
import ( import (
"fmt"
"strings" "strings"
"time" "time"
"github.com/haunt98/changeloguru/internal/convention" "github.com/haunt98/changeloguru/internal/convention"
"github.com/haunt98/clock"
"github.com/haunt98/markdown-go" "github.com/haunt98/markdown-go"
) )
@ -17,57 +15,37 @@ const (
) )
func GenerateMarkdown(commits []convention.Commit, scopes map[string]struct{}, version string, when time.Time) []markdown.Node { func GenerateMarkdown(commits []convention.Commit, scopes map[string]struct{}, version string, when time.Time) []markdown.Node {
if len(commits) == 0 { filteredCommits := filter(commits, scopes)
if filteredCommits == nil {
return nil return nil
} }
commitBases := make(map[string][]markdown.Node) addedNodes := convertToListMarkdownNodes(filteredCommits[addedType])
commitBases[addedType] = make([]markdown.Node, 0, defaultNodesLen) fixedNodes := convertToListMarkdownNodes(filteredCommits[fixedType])
commitBases[fixedType] = make([]markdown.Node, 0, defaultNodesLen) othersNodes := convertToListMarkdownNodes(filteredCommits[othersType])
commitBases[othersType] = make([]markdown.Node, 0, defaultNodesLen)
for _, commit := range commits { // 4 = 3 type header + 1 version header
// If scopes is empty or commit scope is empty, pass all commits nodes := make([]markdown.Node, 0, len(addedNodes)+len(fixedNodes)+len(othersNodes)+4)
if len(scopes) != 0 && commit.Scope != "" {
// Skip commit outside scopes
if _, ok := scopes[commit.Scope]; !ok {
continue
}
}
t := getType(commit.Type) // Adding each type
switch t {
case addedType:
commitBases[addedType] = append(commitBases[addedType], markdown.NewListItem(commit.String()))
case fixedType:
commitBases[fixedType] = append(commitBases[fixedType], markdown.NewListItem(commit.String()))
case othersType:
commitBases[othersType] = append(commitBases[othersType], markdown.NewListItem(commit.String()))
default:
continue
}
}
// Adding each type and header to nodes if len(addedNodes) != 0 {
nodes := make([]markdown.Node, 0, len(commitBases[addedType])+len(commitBases[fixedType])+len(commitBases[othersType]))
if len(commitBases[addedType]) != 0 {
nodes = append(nodes, markdown.NewHeader(thirdLevel, addedType)) nodes = append(nodes, markdown.NewHeader(thirdLevel, addedType))
nodes = append(nodes, commitBases[addedType]...) nodes = append(nodes, addedNodes...)
} }
if len(commitBases[fixedType]) != 0 { if len(fixedNodes) != 0 {
nodes = append(nodes, markdown.NewHeader(thirdLevel, fixedType)) nodes = append(nodes, markdown.NewHeader(thirdLevel, fixedType))
nodes = append(nodes, commitBases[fixedType]...) nodes = append(nodes, fixedNodes...)
} }
if len(commitBases[othersType]) != 0 { if len(othersNodes) != 0 {
nodes = append(nodes, markdown.NewHeader(thirdLevel, othersType)) nodes = append(nodes, markdown.NewHeader(thirdLevel, othersType))
nodes = append(nodes, commitBases[othersType]...) nodes = append(nodes, othersNodes...)
} }
// Adding title, version to nodes // Adding title
versionHeader := fmt.Sprintf("%s (%s)", version, clock.FormatDate(when)) versionHeader := generateVersionHeaderValue(version, when)
nodes = append([]markdown.Node{ nodes = append([]markdown.Node{
markdown.NewHeader(firstLevel, title), markdown.NewHeader(firstLevel, title),
markdown.NewHeader(secondLevel, versionHeader), markdown.NewHeader(secondLevel, versionHeader),
@ -87,3 +65,13 @@ func ParseMarkdown(data string) []markdown.Node {
return nodes return nodes
} }
func convertToListMarkdownNodes(commits []convention.Commit) []markdown.Node {
result := make([]markdown.Node, 0, len(commits))
for _, commit := range commits {
result = append(result, markdown.NewListItem(commit.String()))
}
return result
}

View File

@ -1,67 +1,42 @@
package changelog package changelog
import ( import (
"fmt"
"strings" "strings"
"time" "time"
"github.com/haunt98/changeloguru/internal/convention" "github.com/haunt98/changeloguru/internal/convention"
"github.com/haunt98/clock"
"github.com/haunt98/rst-go" "github.com/haunt98/rst-go"
) )
// GenerateRST base on GenerateMarkdown
func GenerateRST(commits []convention.Commit, scopes map[string]struct{}, version string, when time.Time) []rst.Node { func GenerateRST(commits []convention.Commit, scopes map[string]struct{}, version string, when time.Time) []rst.Node {
if len(commits) == 0 { filteredCommits := filter(commits, scopes)
if filteredCommits == nil {
return nil return nil
} }
commitBases := make(map[string][]rst.Node) addedNodes := convertToListRSTNodes(filteredCommits[addedType])
commitBases[addedType] = make([]rst.Node, 0, defaultNodesLen) fixedNodes := convertToListRSTNodes(filteredCommits[fixedType])
commitBases[fixedType] = make([]rst.Node, 0, defaultNodesLen) othersNodes := convertToListRSTNodes(filteredCommits[othersType])
commitBases[othersType] = make([]rst.Node, 0, defaultNodesLen)
for _, commit := range commits { nodes := make([]rst.Node, 0, len(addedNodes)+len(fixedNodes)+len(othersNodes)+4)
// If scopes is empty or commit scope is empty, pass all commits
if len(scopes) != 0 && commit.Scope != "" {
// Skip commit outside scopes
if _, ok := scopes[commit.Scope]; !ok {
continue
}
}
t := getType(commit.Type) if len(addedNodes) != 0 {
switch t {
case addedType:
commitBases[addedType] = append(commitBases[addedType], rst.NewListItem(commit.String()))
case fixedType:
commitBases[fixedType] = append(commitBases[fixedType], rst.NewListItem(commit.String()))
case othersType:
commitBases[othersType] = append(commitBases[othersType], rst.NewListItem(commit.String()))
default:
continue
}
}
// Adding each type and header to nodes
nodes := make([]rst.Node, 0, len(commitBases[addedType])+len(commitBases[fixedType])+len(commitBases[othersType]))
if len(commitBases[addedType]) != 0 {
nodes = append(nodes, rst.NewSubSection(addedType)) nodes = append(nodes, rst.NewSubSection(addedType))
nodes = append(nodes, commitBases[addedType]...) nodes = append(nodes, addedNodes...)
} }
if len(commitBases[fixedType]) != 0 { if len(fixedNodes) != 0 {
nodes = append(nodes, rst.NewSubSection(fixedType)) nodes = append(nodes, rst.NewSubSection(fixedType))
nodes = append(nodes, commitBases[fixedType]...) nodes = append(nodes, fixedNodes...)
} }
if len(commitBases[othersType]) != 0 { if len(othersNodes) != 0 {
nodes = append(nodes, rst.NewSubSection(othersType)) nodes = append(nodes, rst.NewSubSection(othersType))
nodes = append(nodes, commitBases[othersType]...) nodes = append(nodes, othersNodes...)
} }
// Adding title, version to nodes versionHeader := generateVersionHeaderValue(version, when)
versionHeader := fmt.Sprintf("%s (%s)", version, clock.FormatDate(when))
nodes = append([]rst.Node{ nodes = append([]rst.Node{
rst.NewTitle(title), rst.NewTitle(title),
rst.NewSection(versionHeader), rst.NewSection(versionHeader),
@ -81,3 +56,13 @@ func ParseRST(data string) []rst.Node {
return nodes return nodes
} }
func convertToListRSTNodes(commits []convention.Commit) []rst.Node {
result := make([]rst.Node, 0, len(commits))
for _, commit := range commits {
result = append(result, rst.NewListItem(commit.String()))
}
return result
}