2020-11-10 10:04:13 +00:00
|
|
|
package changelog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-11-10 10:36:50 +00:00
|
|
|
"strings"
|
2020-11-10 10:04:13 +00:00
|
|
|
"time"
|
|
|
|
|
2021-05-05 08:11:54 +00:00
|
|
|
"github.com/haunt98/changeloguru/internal/convention"
|
2021-04-15 04:59:01 +00:00
|
|
|
"github.com/haunt98/clock"
|
2021-05-05 09:12:59 +00:00
|
|
|
"github.com/haunt98/markdown-go"
|
2020-11-10 10:04:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-11-27 17:43:19 +00:00
|
|
|
firstLevel = 1
|
|
|
|
secondLevel = 2
|
|
|
|
thirdLevel = 3
|
2020-11-10 10:04:13 +00:00
|
|
|
)
|
|
|
|
|
2021-04-15 04:28:04 +00:00
|
|
|
func GenerateMarkdown(commits []convention.Commit, scopes map[string]struct{}, version string, when time.Time) []markdown.Node {
|
2020-11-10 17:06:04 +00:00
|
|
|
if len(commits) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-27 17:39:05 +00:00
|
|
|
commitBases := make(map[string][]markdown.Node)
|
2020-11-27 17:43:19 +00:00
|
|
|
commitBases[addedType] = make([]markdown.Node, 0, defaultNodesLen)
|
|
|
|
commitBases[fixedType] = make([]markdown.Node, 0, defaultNodesLen)
|
|
|
|
commitBases[othersType] = make([]markdown.Node, 0, defaultNodesLen)
|
2020-11-10 10:04:13 +00:00
|
|
|
|
|
|
|
for _, commit := range commits {
|
2021-03-22 05:44:54 +00:00
|
|
|
// 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)
|
2020-11-10 10:04:13 +00:00
|
|
|
switch t {
|
|
|
|
case addedType:
|
2021-01-05 10:56:11 +00:00
|
|
|
commitBases[addedType] = append(commitBases[addedType], markdown.NewListItem(commit.String()))
|
2020-11-10 10:04:13 +00:00
|
|
|
case fixedType:
|
2021-01-05 10:56:11 +00:00
|
|
|
commitBases[fixedType] = append(commitBases[fixedType], markdown.NewListItem(commit.String()))
|
2020-11-10 10:04:13 +00:00
|
|
|
case othersType:
|
2021-01-05 10:56:11 +00:00
|
|
|
commitBases[othersType] = append(commitBases[othersType], markdown.NewListItem(commit.String()))
|
2020-11-10 10:04:13 +00:00
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 10:06:48 +00:00
|
|
|
// Adding each type and header to nodes
|
|
|
|
nodes := make([]markdown.Node, 0, len(commitBases[addedType])+len(commitBases[fixedType])+len(commitBases[othersType]))
|
|
|
|
|
2020-11-27 17:36:34 +00:00
|
|
|
if len(commitBases[addedType]) != 0 {
|
2021-04-14 10:06:48 +00:00
|
|
|
nodes = append(nodes, markdown.NewHeader(thirdLevel, addedType))
|
|
|
|
nodes = append(nodes, commitBases[addedType]...)
|
2020-11-10 10:59:15 +00:00
|
|
|
}
|
2020-11-10 10:04:13 +00:00
|
|
|
|
2020-11-27 17:36:34 +00:00
|
|
|
if len(commitBases[fixedType]) != 0 {
|
2021-04-14 10:06:48 +00:00
|
|
|
nodes = append(nodes, markdown.NewHeader(thirdLevel, fixedType))
|
|
|
|
nodes = append(nodes, commitBases[fixedType]...)
|
2020-11-10 10:59:15 +00:00
|
|
|
}
|
2020-11-10 10:04:13 +00:00
|
|
|
|
2020-11-27 17:36:34 +00:00
|
|
|
if len(commitBases[othersType]) != 0 {
|
2021-04-14 10:06:48 +00:00
|
|
|
nodes = append(nodes, markdown.NewHeader(thirdLevel, othersType))
|
|
|
|
nodes = append(nodes, commitBases[othersType]...)
|
2020-11-10 10:59:15 +00:00
|
|
|
}
|
2020-11-10 10:04:13 +00:00
|
|
|
|
2021-04-14 10:06:48 +00:00
|
|
|
// Adding title, version to nodes
|
2021-04-15 04:59:01 +00:00
|
|
|
versionHeader := fmt.Sprintf("%s (%s)", version, clock.FormatDate(when))
|
2021-04-14 10:06:48 +00:00
|
|
|
nodes = append([]markdown.Node{
|
|
|
|
markdown.NewHeader(firstLevel, title),
|
2021-04-15 04:59:01 +00:00
|
|
|
markdown.NewHeader(secondLevel, versionHeader),
|
2021-04-14 10:06:48 +00:00
|
|
|
}, nodes...)
|
2020-11-10 10:04:13 +00:00
|
|
|
|
2021-04-14 10:06:48 +00:00
|
|
|
return nodes
|
|
|
|
}
|
2020-11-10 10:36:50 +00:00
|
|
|
|
2021-04-14 10:06:48 +00:00
|
|
|
func ParseMarkdown(data string) []markdown.Node {
|
2021-05-31 10:37:24 +00:00
|
|
|
lines := strings.Split(data, "\n\n")
|
2021-04-14 10:06:48 +00:00
|
|
|
nodes := markdown.Parse(lines)
|
2020-11-10 10:36:50 +00:00
|
|
|
|
2021-04-26 02:54:31 +00:00
|
|
|
// Remove title
|
2021-04-14 10:06:48 +00:00
|
|
|
if len(nodes) > 0 && markdown.Equal(nodes[0], markdown.NewHeader(firstLevel, title)) {
|
|
|
|
nodes = nodes[1:]
|
2020-11-10 10:36:50 +00:00
|
|
|
}
|
|
|
|
|
2021-04-14 10:06:48 +00:00
|
|
|
return nodes
|
2020-11-10 10:04:13 +00:00
|
|
|
}
|