feat: add markdown generator to generate markdown lines

main
hau 2020-11-10 17:04:13 +07:00
parent 758c5173c8
commit e6ecf05dd8
10 changed files with 247 additions and 0 deletions

View File

@ -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...)

81
pkg/changelog/markdown.go Normal file
View File

@ -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)
}

View File

@ -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)
})
}
}

View File

@ -0,0 +1,8 @@
[
"# CHANGELOG",
"# v1.0.0 (1-1-1)",
"## Added",
"feat: description",
"## Fixed",
"## Others"
]

View File

@ -0,0 +1,8 @@
[
"# CHANGELOG",
"# v1.0.0 (1-1-1)",
"## Added",
"## Fixed",
"fix: description",
"## Others"
]

View File

@ -0,0 +1,8 @@
[
"# CHANGELOG",
"# v1.0.0 (1-1-1)",
"## Added",
"## Fixed",
"## Others",
"ci: description"
]

View File

@ -0,0 +1 @@
null

View File

@ -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"
]

20
pkg/changelog/types.go Normal file
View File

@ -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
}
}

14
pkg/convention/types.go Normal file
View File

@ -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"
)