mirror of
https://github.com/thegeeklab/git-sv.git
synced 2024-11-21 12:00:40 +00:00
parent
6b4948b103
commit
b5d9f9c535
@ -2,6 +2,7 @@ package sv
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"embed"
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -13,40 +14,9 @@ type releaseNoteTemplateVariables struct {
|
|||||||
BreakingChanges BreakingChangeSection
|
BreakingChanges BreakingChangeSection
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
var (
|
||||||
cglTemplate = `# Changelog
|
//go:embed resources/templates/*
|
||||||
{{- range .}}
|
defaultTemplatesFS embed.FS
|
||||||
|
|
||||||
{{template "rnTemplate" .}}
|
|
||||||
---
|
|
||||||
{{- end}}
|
|
||||||
`
|
|
||||||
|
|
||||||
rnSectionItem = "- {{if .Message.Scope}}**{{.Message.Scope}}:** {{end}}{{.Message.Description}} ({{.Hash}}){{if .Message.Metadata.issue}} ({{.Message.Metadata.issue}}){{end}}"
|
|
||||||
|
|
||||||
rnSection = `{{- if .}}{{- if ne .Name ""}}
|
|
||||||
|
|
||||||
### {{.Name}}
|
|
||||||
{{range $k,$v := .Items}}
|
|
||||||
{{template "rnSectionItem" $v}}
|
|
||||||
{{- end}}
|
|
||||||
{{- end}}{{- end}}`
|
|
||||||
|
|
||||||
rnSectionBreakingChanges = `{{- if ne .Name ""}}
|
|
||||||
|
|
||||||
### {{.Name}}
|
|
||||||
{{range $k,$v := .Messages}}
|
|
||||||
- {{$v}}
|
|
||||||
{{- end}}
|
|
||||||
{{- end}}`
|
|
||||||
|
|
||||||
rnTemplate = `## {{if .Release}}{{.Release}}{{end}}{{if and .Date .Release}} ({{end}}{{.Date}}{{if and .Date .Release}}){{end}}
|
|
||||||
{{- $sections := .Sections }}
|
|
||||||
{{- range $key := .Order }}
|
|
||||||
{{- template "rnSection" (index $sections $key) }}
|
|
||||||
{{- end}}
|
|
||||||
{{- template "rnSectionBreakingChanges" .BreakingChanges}}
|
|
||||||
`
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// OutputFormatter output formatter interface.
|
// OutputFormatter output formatter interface.
|
||||||
@ -57,24 +27,19 @@ type OutputFormatter interface {
|
|||||||
|
|
||||||
// OutputFormatterImpl formater for release note and changelog.
|
// OutputFormatterImpl formater for release note and changelog.
|
||||||
type OutputFormatterImpl struct {
|
type OutputFormatterImpl struct {
|
||||||
releasenoteTemplate *template.Template
|
templates *template.Template
|
||||||
changelogTemplate *template.Template
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewOutputFormatter TemplateProcessor constructor.
|
// NewOutputFormatter TemplateProcessor constructor.
|
||||||
func NewOutputFormatter() *OutputFormatterImpl {
|
func NewOutputFormatter() *OutputFormatterImpl {
|
||||||
cgl := template.Must(template.New("cglTemplate").Parse(cglTemplate))
|
tpls := template.Must(template.New("templates").ParseFS(defaultTemplatesFS, "resources/templates/*"))
|
||||||
rn := template.Must(cgl.New("rnTemplate").Parse(rnTemplate))
|
return &OutputFormatterImpl{templates: tpls}
|
||||||
template.Must(rn.New("rnSectionItem").Parse(rnSectionItem))
|
|
||||||
template.Must(rn.New("rnSection").Parse(rnSection))
|
|
||||||
template.Must(rn.New("rnSectionBreakingChanges").Parse(rnSectionBreakingChanges))
|
|
||||||
return &OutputFormatterImpl{releasenoteTemplate: rn, changelogTemplate: cgl}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FormatReleaseNote format a release note.
|
// FormatReleaseNote format a release note.
|
||||||
func (p OutputFormatterImpl) FormatReleaseNote(releasenote ReleaseNote) (string, error) {
|
func (p OutputFormatterImpl) FormatReleaseNote(releasenote ReleaseNote) (string, error) {
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
if err := p.releasenoteTemplate.Execute(&b, releaseNoteVariables(releasenote)); err != nil {
|
if err := p.templates.ExecuteTemplate(&b, "releasenotes-md.tpl", releaseNoteVariables(releasenote)); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return b.String(), nil
|
return b.String(), nil
|
||||||
@ -88,7 +53,7 @@ func (p OutputFormatterImpl) FormatChangelog(releasenotes []ReleaseNote) (string
|
|||||||
}
|
}
|
||||||
|
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
if err := p.changelogTemplate.Execute(&b, templateVars); err != nil {
|
if err := p.templates.ExecuteTemplate(&b, "changelog-md.tpl", templateVars); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return b.String(), nil
|
return b.String(), nil
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package sv
|
package sv
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Masterminds/semver/v3"
|
"github.com/Masterminds/semver/v3"
|
||||||
@ -85,3 +87,69 @@ func fullReleaseNote(tag string, date time.Time) ReleaseNote {
|
|||||||
}
|
}
|
||||||
return releaseNote(v, tag, date, sections, []string{"break change message"})
|
return releaseNote(v, tag, date, sections, []string{"break change message"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func releaseNotesVariables(release string) releaseNoteTemplateVariables {
|
||||||
|
return releaseNoteTemplateVariables{
|
||||||
|
Release: release,
|
||||||
|
Date: "2006-01-02",
|
||||||
|
Sections: map[string]ReleaseNoteSection{
|
||||||
|
"build": newReleaseNoteSection("Build", []GitCommitLog{commitlog("build", map[string]string{})}),
|
||||||
|
"feat": newReleaseNoteSection("Features", []GitCommitLog{commitlog("feat", map[string]string{})}),
|
||||||
|
"fix": newReleaseNoteSection("Bug Fixes", []GitCommitLog{commitlog("fix", map[string]string{})}),
|
||||||
|
},
|
||||||
|
Order: []string{"feat", "fix", "refactor", "perf", "test", "build", "ci", "chore", "docs", "style"},
|
||||||
|
BreakingChanges: BreakingChangeSection{"Breaking Changes", []string{"break change message"}},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func changelogVariables(releases ...string) []releaseNoteTemplateVariables {
|
||||||
|
var variables []releaseNoteTemplateVariables
|
||||||
|
for _, r := range releases {
|
||||||
|
variables = append(variables, releaseNotesVariables(r))
|
||||||
|
}
|
||||||
|
return variables
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_checkTemplatesFiles(t *testing.T) {
|
||||||
|
tests := []string{
|
||||||
|
"resources/templates/changelog-md.tpl",
|
||||||
|
"resources/templates/releasenotes-md.tpl",
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt, func(t *testing.T) {
|
||||||
|
got, err := defaultTemplatesFS.ReadFile(tt)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("missing template error = %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(got) <= 0 {
|
||||||
|
t.Errorf("empty template")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_checkTemplatesExecution(t *testing.T) {
|
||||||
|
tpls := template.Must(template.New("templates").ParseFS(defaultTemplatesFS, "resources/templates/*"))
|
||||||
|
tests := []struct {
|
||||||
|
template string
|
||||||
|
variables interface{}
|
||||||
|
}{
|
||||||
|
{"changelog-md.tpl", changelogVariables("v1.0.0", "v1.0.1")},
|
||||||
|
{"releasenotes-md.tpl", releaseNotesVariables("v1.0.0")},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.template, func(t *testing.T) {
|
||||||
|
var b bytes.Buffer
|
||||||
|
err := tpls.ExecuteTemplate(&b, tt.template, tt.variables)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("invalid template err = %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(b.Bytes()) <= 0 {
|
||||||
|
t.Errorf("empty template")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
6
sv/resources/templates/changelog-md.tpl
Normal file
6
sv/resources/templates/changelog-md.tpl
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
# Changelog
|
||||||
|
{{- range .}}
|
||||||
|
|
||||||
|
{{template "releasenotes-md.tpl" .}}
|
||||||
|
---
|
||||||
|
{{- end}}
|
6
sv/resources/templates/releasenotes-md.tpl
Normal file
6
sv/resources/templates/releasenotes-md.tpl
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
## {{if .Release}}{{.Release}}{{end}}{{if and .Date .Release}} ({{end}}{{.Date}}{{if and .Date .Release}}){{end}}
|
||||||
|
{{- $sections := .Sections }}
|
||||||
|
{{- range $key := .Order }}
|
||||||
|
{{- template "rn-md-section.tpl" (index $sections $key) }}
|
||||||
|
{{- end}}
|
||||||
|
{{- template "rn-md-section-breaking-changes.tpl" .BreakingChanges}}
|
@ -0,0 +1,7 @@
|
|||||||
|
{{- if ne .Name ""}}
|
||||||
|
|
||||||
|
### {{.Name}}
|
||||||
|
{{range $k,$v := .Messages}}
|
||||||
|
- {{$v}}
|
||||||
|
{{- end}}
|
||||||
|
{{- end}}
|
1
sv/resources/templates/rn-md-section-item.tpl
Normal file
1
sv/resources/templates/rn-md-section-item.tpl
Normal file
@ -0,0 +1 @@
|
|||||||
|
- {{if .Message.Scope}}**{{.Message.Scope}}:** {{end}}{{.Message.Description}} ({{.Hash}}){{if .Message.Metadata.issue}} ({{.Message.Metadata.issue}}){{end}}
|
7
sv/resources/templates/rn-md-section.tpl
Normal file
7
sv/resources/templates/rn-md-section.tpl
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{{- if .}}{{- if ne .Name ""}}
|
||||||
|
|
||||||
|
### {{.Name}}
|
||||||
|
{{range $k,$v := .Items}}
|
||||||
|
{{template "rn-md-section-item.tpl" $v}}
|
||||||
|
{{- end}}
|
||||||
|
{{- end}}{{- end}}
|
Loading…
Reference in New Issue
Block a user