2020-02-02 01:40:31 +00:00
|
|
|
package sv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-02-06 21:15:36 +00:00
|
|
|
"io/fs"
|
2022-02-07 01:06:46 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2020-02-02 01:40:31 +00:00
|
|
|
"text/template"
|
2022-02-07 01:06:46 +00:00
|
|
|
|
|
|
|
"github.com/Masterminds/semver/v3"
|
2020-02-02 01:40:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type releaseNoteTemplateVariables struct {
|
2021-12-29 21:02:50 +00:00
|
|
|
Release string
|
2022-02-07 01:06:46 +00:00
|
|
|
Version *semver.Version
|
2020-02-02 01:40:31 +00:00
|
|
|
Date string
|
2022-02-07 01:06:46 +00:00
|
|
|
Sections []ReleaseNoteSection
|
2021-02-15 04:47:20 +00:00
|
|
|
BreakingChanges BreakingChangeSection
|
2022-02-07 01:06:46 +00:00
|
|
|
AuthorNames []string
|
2020-02-02 01:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OutputFormatter output formatter interface.
|
|
|
|
type OutputFormatter interface {
|
2021-07-31 20:34:40 +00:00
|
|
|
FormatReleaseNote(releasenote ReleaseNote) (string, error)
|
|
|
|
FormatChangelog(releasenotes []ReleaseNote) (string, error)
|
2020-02-02 01:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OutputFormatterImpl formater for release note and changelog.
|
|
|
|
type OutputFormatterImpl struct {
|
2022-01-30 21:45:16 +00:00
|
|
|
templates *template.Template
|
2020-02-02 01:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOutputFormatter TemplateProcessor constructor.
|
2022-02-06 21:15:36 +00:00
|
|
|
func NewOutputFormatter(templatesFS fs.FS) *OutputFormatterImpl {
|
|
|
|
tpls := template.Must(template.New("templates").ParseFS(templatesFS, "*"))
|
2022-01-30 21:45:16 +00:00
|
|
|
return &OutputFormatterImpl{templates: tpls}
|
2020-02-02 01:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FormatReleaseNote format a release note.
|
2021-07-31 20:34:40 +00:00
|
|
|
func (p OutputFormatterImpl) FormatReleaseNote(releasenote ReleaseNote) (string, error) {
|
2020-02-02 03:49:54 +00:00
|
|
|
var b bytes.Buffer
|
2022-01-30 21:45:16 +00:00
|
|
|
if err := p.templates.ExecuteTemplate(&b, "releasenotes-md.tpl", releaseNoteVariables(releasenote)); err != nil {
|
2021-07-31 20:34:40 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return b.String(), nil
|
2020-02-02 03:49:54 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 19:03:58 +00:00
|
|
|
// FormatChangelog format a changelog.
|
2021-07-31 20:34:40 +00:00
|
|
|
func (p OutputFormatterImpl) FormatChangelog(releasenotes []ReleaseNote) (string, error) {
|
2021-07-31 19:35:28 +00:00
|
|
|
templateVars := make([]releaseNoteTemplateVariables, len(releasenotes))
|
|
|
|
for i, v := range releasenotes {
|
|
|
|
templateVars[i] = releaseNoteVariables(v)
|
2020-02-02 03:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
2022-01-30 21:45:16 +00:00
|
|
|
if err := p.templates.ExecuteTemplate(&b, "changelog-md.tpl", templateVars); err != nil {
|
2021-07-31 20:34:40 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return b.String(), nil
|
2020-02-02 03:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func releaseNoteVariables(releasenote ReleaseNote) releaseNoteTemplateVariables {
|
2021-07-31 19:03:58 +00:00
|
|
|
date := ""
|
2020-05-01 03:45:08 +00:00
|
|
|
if !releasenote.Date.IsZero() {
|
|
|
|
date = releasenote.Date.Format("2006-01-02")
|
|
|
|
}
|
2021-01-25 20:16:56 +00:00
|
|
|
|
2022-02-07 01:06:46 +00:00
|
|
|
release := releasenote.Tag
|
2021-01-25 20:16:56 +00:00
|
|
|
if releasenote.Version != nil {
|
2021-12-29 21:02:50 +00:00
|
|
|
release = "v" + releasenote.Version.String()
|
2021-01-25 20:16:56 +00:00
|
|
|
}
|
2020-02-02 03:49:54 +00:00
|
|
|
return releaseNoteTemplateVariables{
|
2021-12-29 21:02:50 +00:00
|
|
|
Release: release,
|
2022-02-07 01:06:46 +00:00
|
|
|
Version: releasenote.Version,
|
2020-05-01 03:45:08 +00:00
|
|
|
Date: date,
|
2022-02-07 01:06:46 +00:00
|
|
|
Sections: toTemplateSections(releasenote.Sections),
|
2020-02-02 01:40:31 +00:00
|
|
|
BreakingChanges: releasenote.BreakingChanges,
|
2022-02-07 01:06:46 +00:00
|
|
|
AuthorNames: toSortedArray(releasenote.AuthorsNames),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toTemplateSections(sections map[string]ReleaseNoteSection) []ReleaseNoteSection {
|
|
|
|
result := make([]ReleaseNoteSection, len(sections))
|
|
|
|
i := 0
|
|
|
|
for _, section := range sections {
|
|
|
|
result[i] = section
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
order := map[string]int{"feat": 0, "fix": 1, "refactor": 2, "perf": 3, "test": 4, "build": 5, "ci": 6, "chore": 7, "docs": 8, "style": 9}
|
|
|
|
sort.SliceStable(result, func(i, j int) bool {
|
|
|
|
priority1, disambiguity1 := priority(result[i].Types, order)
|
|
|
|
priority2, disambiguity2 := priority(result[j].Types, order)
|
|
|
|
if priority1 == priority2 {
|
|
|
|
return disambiguity1 < disambiguity2
|
|
|
|
}
|
|
|
|
return priority1 < priority2
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func priority(types []string, order map[string]int) (int, string) {
|
|
|
|
sort.Strings(types)
|
|
|
|
p := -1
|
|
|
|
for _, t := range types {
|
|
|
|
if p == -1 || order[t] < p {
|
|
|
|
p = order[t]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p, strings.Join(types, "-")
|
|
|
|
}
|
|
|
|
|
|
|
|
func toSortedArray(input map[string]struct{}) []string {
|
|
|
|
result := make([]string, len(input))
|
|
|
|
i := 0
|
|
|
|
for k := range input {
|
|
|
|
result[i] = k
|
|
|
|
i++
|
2020-02-02 01:40:31 +00:00
|
|
|
}
|
2022-02-07 01:06:46 +00:00
|
|
|
sort.Strings(result)
|
|
|
|
return result
|
2020-02-02 01:40:31 +00:00
|
|
|
}
|