From e6ecf05dd8039c372daa198353b4852ec3f7aef4 Mon Sep 17 00:00:00 2001 From: hau Date: Tue, 10 Nov 2020 17:04:13 +0700 Subject: [PATCH] feat: add markdown generator to generate markdown lines --- cmd/main.go | 4 + pkg/changelog/markdown.go | 81 ++++++++++++++++ pkg/changelog/markdown_test.go | 92 +++++++++++++++++++ .../1_commit_feat.golden | 8 ++ .../1_commit_fixed.golden | 8 ++ .../1_commit_other.golden | 8 ++ .../empty.golden | 1 + .../mixed.golden | 11 +++ pkg/changelog/types.go | 20 ++++ pkg/convention/types.go | 14 +++ 10 files changed, 247 insertions(+) create mode 100644 pkg/changelog/markdown.go create mode 100644 pkg/changelog/markdown_test.go create mode 100644 pkg/changelog/testdata/TestMarkdownGeneratorGetLines/1_commit_feat.golden create mode 100644 pkg/changelog/testdata/TestMarkdownGeneratorGetLines/1_commit_fixed.golden create mode 100644 pkg/changelog/testdata/TestMarkdownGeneratorGetLines/1_commit_other.golden create mode 100644 pkg/changelog/testdata/TestMarkdownGeneratorGetLines/empty.golden create mode 100644 pkg/changelog/testdata/TestMarkdownGeneratorGetLines/mixed.golden create mode 100644 pkg/changelog/types.go create mode 100644 pkg/convention/types.go diff --git a/cmd/main.go b/cmd/main.go index 92e91e5..5d3031e 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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...) diff --git a/pkg/changelog/markdown.go b/pkg/changelog/markdown.go new file mode 100644 index 0000000..b3f53b6 --- /dev/null +++ b/pkg/changelog/markdown.go @@ -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) +} diff --git a/pkg/changelog/markdown_test.go b/pkg/changelog/markdown_test.go new file mode 100644 index 0000000..ffd9d94 --- /dev/null +++ b/pkg/changelog/markdown_test.go @@ -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) + }) + } +} diff --git a/pkg/changelog/testdata/TestMarkdownGeneratorGetLines/1_commit_feat.golden b/pkg/changelog/testdata/TestMarkdownGeneratorGetLines/1_commit_feat.golden new file mode 100644 index 0000000..f9ecdc6 --- /dev/null +++ b/pkg/changelog/testdata/TestMarkdownGeneratorGetLines/1_commit_feat.golden @@ -0,0 +1,8 @@ +[ + "# CHANGELOG", + "# v1.0.0 (1-1-1)", + "## Added", + "feat: description", + "## Fixed", + "## Others" +] \ No newline at end of file diff --git a/pkg/changelog/testdata/TestMarkdownGeneratorGetLines/1_commit_fixed.golden b/pkg/changelog/testdata/TestMarkdownGeneratorGetLines/1_commit_fixed.golden new file mode 100644 index 0000000..fc549e0 --- /dev/null +++ b/pkg/changelog/testdata/TestMarkdownGeneratorGetLines/1_commit_fixed.golden @@ -0,0 +1,8 @@ +[ + "# CHANGELOG", + "# v1.0.0 (1-1-1)", + "## Added", + "## Fixed", + "fix: description", + "## Others" +] \ No newline at end of file diff --git a/pkg/changelog/testdata/TestMarkdownGeneratorGetLines/1_commit_other.golden b/pkg/changelog/testdata/TestMarkdownGeneratorGetLines/1_commit_other.golden new file mode 100644 index 0000000..8034677 --- /dev/null +++ b/pkg/changelog/testdata/TestMarkdownGeneratorGetLines/1_commit_other.golden @@ -0,0 +1,8 @@ +[ + "# CHANGELOG", + "# v1.0.0 (1-1-1)", + "## Added", + "## Fixed", + "## Others", + "ci: description" +] \ No newline at end of file diff --git a/pkg/changelog/testdata/TestMarkdownGeneratorGetLines/empty.golden b/pkg/changelog/testdata/TestMarkdownGeneratorGetLines/empty.golden new file mode 100644 index 0000000..ec747fa --- /dev/null +++ b/pkg/changelog/testdata/TestMarkdownGeneratorGetLines/empty.golden @@ -0,0 +1 @@ +null \ No newline at end of file diff --git a/pkg/changelog/testdata/TestMarkdownGeneratorGetLines/mixed.golden b/pkg/changelog/testdata/TestMarkdownGeneratorGetLines/mixed.golden new file mode 100644 index 0000000..a9d1e44 --- /dev/null +++ b/pkg/changelog/testdata/TestMarkdownGeneratorGetLines/mixed.golden @@ -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" +] \ No newline at end of file diff --git a/pkg/changelog/types.go b/pkg/changelog/types.go new file mode 100644 index 0000000..0166f0a --- /dev/null +++ b/pkg/changelog/types.go @@ -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 + } +} diff --git a/pkg/convention/types.go b/pkg/convention/types.go new file mode 100644 index 0000000..c7f6604 --- /dev/null +++ b/pkg/convention/types.go @@ -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" +)