feat: add markdown generator to generate markdown lines
parent
758c5173c8
commit
e6ecf05dd8
|
@ -107,6 +107,10 @@ func (a *action) getConventionalCommits(c *cli.Context, commits []git.Commit) []
|
|||
return conventionalCommits
|
||||
}
|
||||
|
||||
func (a *action) generateMarkdown(c *cli.Context, commits []convention.Commit) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *action) log(format string, v ...interface{}) {
|
||||
if a.verbose {
|
||||
log.Printf(format, v...)
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
package changelog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/haunt98/changeloguru/pkg/convention"
|
||||
)
|
||||
|
||||
const (
|
||||
markdownTitle = "# CHANGELOG"
|
||||
|
||||
defaultLinesLen = 10
|
||||
)
|
||||
|
||||
type MarkdownGenerator struct {
|
||||
path string
|
||||
version string
|
||||
t time.Time
|
||||
}
|
||||
|
||||
func NewMarkdownGenerator(path string, version string, t time.Time) *MarkdownGenerator {
|
||||
return &MarkdownGenerator{
|
||||
path: path,
|
||||
version: version,
|
||||
t: t,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *MarkdownGenerator) Generate(commits []convention.Commit) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *MarkdownGenerator) getLines(commits []convention.Commit) []string {
|
||||
if len(commits) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
lines := make([]string, 0, defaultLinesLen)
|
||||
lines = append(lines, markdownTitle)
|
||||
|
||||
lines = append(lines, g.composeVersionHeader())
|
||||
|
||||
addedLines := make([]string, 0, defaultLinesLen)
|
||||
fixedLines := make([]string, 0, defaultLinesLen)
|
||||
othersLines := make([]string, 0, defaultLinesLen)
|
||||
|
||||
for _, commit := range commits {
|
||||
t := getType(commit.Type)
|
||||
switch t {
|
||||
case addedType:
|
||||
addedLines = append(addedLines, commit.RawHeader)
|
||||
case fixedType:
|
||||
fixedLines = append(fixedLines, commit.RawHeader)
|
||||
case othersType:
|
||||
othersLines = append(othersLines, commit.RawHeader)
|
||||
default:
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
lines = append(lines, g.composeTypeHeader(addedType))
|
||||
lines = append(lines, addedLines...)
|
||||
|
||||
lines = append(lines, g.composeTypeHeader(fixedType))
|
||||
lines = append(lines, fixedLines...)
|
||||
|
||||
lines = append(lines, g.composeTypeHeader(othersType))
|
||||
lines = append(lines, othersLines...)
|
||||
|
||||
return lines
|
||||
}
|
||||
|
||||
func (g *MarkdownGenerator) composeVersionHeader() string {
|
||||
year, month, day := g.t.Date()
|
||||
return fmt.Sprintf("# %s (%d-%d-%d)", g.version, year, month, day)
|
||||
}
|
||||
|
||||
func (g *MarkdownGenerator) composeTypeHeader(t string) string {
|
||||
return fmt.Sprintf("## %s", t)
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
package changelog
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/haunt98/changeloguru/pkg/convention"
|
||||
"github.com/sebdah/goldie/v2"
|
||||
)
|
||||
|
||||
func TestMarkdownGeneratorGetLines(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
commits []convention.Commit
|
||||
}{
|
||||
{
|
||||
name: "empty",
|
||||
},
|
||||
{
|
||||
name: "1 commit feat",
|
||||
commits: []convention.Commit{
|
||||
{
|
||||
RawHeader: "feat: description",
|
||||
Type: convention.FeatType,
|
||||
Scope: "",
|
||||
Description: "description",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "1 commit fixed",
|
||||
commits: []convention.Commit{
|
||||
{
|
||||
RawHeader: "fix: description",
|
||||
Type: convention.FixType,
|
||||
Scope: "",
|
||||
Description: "description",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "1 commit other",
|
||||
commits: []convention.Commit{
|
||||
{
|
||||
RawHeader: "ci: description",
|
||||
Type: convention.CiType,
|
||||
Scope: "",
|
||||
Description: "description",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "mixed",
|
||||
commits: []convention.Commit{
|
||||
{
|
||||
RawHeader: "feat: description feat",
|
||||
Type: convention.FeatType,
|
||||
Scope: "",
|
||||
Description: "description feat",
|
||||
},
|
||||
{
|
||||
RawHeader: "fix: description fix",
|
||||
Type: convention.FixType,
|
||||
Scope: "",
|
||||
Description: "description fix",
|
||||
},
|
||||
{
|
||||
RawHeader: "ci: description ci",
|
||||
Type: convention.CiType,
|
||||
Scope: "",
|
||||
Description: "description ci",
|
||||
},
|
||||
{
|
||||
RawHeader: "build: description build",
|
||||
Type: convention.BuildType,
|
||||
Scope: "",
|
||||
Description: "description build",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
g := goldie.New(t)
|
||||
|
||||
generator := NewMarkdownGenerator("path", "v1.0.0", time.Time{})
|
||||
lines := generator.getLines(tc.commits)
|
||||
g.AssertJson(t, t.Name(), lines)
|
||||
})
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
[
|
||||
"# CHANGELOG",
|
||||
"# v1.0.0 (1-1-1)",
|
||||
"## Added",
|
||||
"feat: description",
|
||||
"## Fixed",
|
||||
"## Others"
|
||||
]
|
|
@ -0,0 +1,8 @@
|
|||
[
|
||||
"# CHANGELOG",
|
||||
"# v1.0.0 (1-1-1)",
|
||||
"## Added",
|
||||
"## Fixed",
|
||||
"fix: description",
|
||||
"## Others"
|
||||
]
|
|
@ -0,0 +1,8 @@
|
|||
[
|
||||
"# CHANGELOG",
|
||||
"# v1.0.0 (1-1-1)",
|
||||
"## Added",
|
||||
"## Fixed",
|
||||
"## Others",
|
||||
"ci: description"
|
||||
]
|
|
@ -0,0 +1 @@
|
|||
null
|
|
@ -0,0 +1,11 @@
|
|||
[
|
||||
"# CHANGELOG",
|
||||
"# v1.0.0 (1-1-1)",
|
||||
"## Added",
|
||||
"feat: description feat",
|
||||
"## Fixed",
|
||||
"fix: description fix",
|
||||
"## Others",
|
||||
"ci: description ci",
|
||||
"build: description build"
|
||||
]
|
|
@ -0,0 +1,20 @@
|
|||
package changelog
|
||||
|
||||
import "github.com/haunt98/changeloguru/pkg/convention"
|
||||
|
||||
const (
|
||||
addedType = "Added"
|
||||
fixedType = "Fixed"
|
||||
othersType = "Others"
|
||||
)
|
||||
|
||||
func getType(conventionType string) string {
|
||||
switch conventionType {
|
||||
case convention.FeatType:
|
||||
return addedType
|
||||
case convention.FixType:
|
||||
return fixedType
|
||||
default:
|
||||
return othersType
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package convention
|
||||
|
||||
const (
|
||||
FixType = "fix"
|
||||
FeatType = "feat"
|
||||
BuildType = "build"
|
||||
ChoreType = "chore"
|
||||
CiType = "ci"
|
||||
DocsType = "docs"
|
||||
StyleType = "style"
|
||||
RefactorType = "refactor"
|
||||
PerfType = "perf"
|
||||
TestType = "test"
|
||||
)
|
Loading…
Reference in New Issue