2023-10-15 19:29:29 +00:00
|
|
|
package formatter
|
2020-02-02 01:40:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-02-07 01:06:46 +00:00
|
|
|
"sort"
|
2020-02-02 01:40:31 +00:00
|
|
|
"text/template"
|
2022-02-08 01:49:32 +00:00
|
|
|
"time"
|
2022-02-07 01:06:46 +00:00
|
|
|
|
|
|
|
"github.com/Masterminds/semver/v3"
|
2023-10-16 19:41:33 +00:00
|
|
|
"github.com/thegeeklab/git-sv/sv"
|
2020-02-02 01:40:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type releaseNoteTemplateVariables struct {
|
2022-02-08 01:49:32 +00:00
|
|
|
Release string
|
|
|
|
Tag string
|
|
|
|
Version *semver.Version
|
|
|
|
Date time.Time
|
2023-10-15 19:29:29 +00:00
|
|
|
Sections []sv.ReleaseNoteSection
|
2022-02-08 01:49:32 +00:00
|
|
|
AuthorNames []string
|
2020-02-02 01:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OutputFormatter output formatter interface.
|
|
|
|
type OutputFormatter interface {
|
2023-10-15 22:29:02 +00:00
|
|
|
FormatReleaseNote(releasenote sv.ReleaseNote) ([]byte, error)
|
|
|
|
FormatChangelog(releasenotes []sv.ReleaseNote) ([]byte, error)
|
2020-02-02 01:40:31 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
// BaseOutputFormatter formater for release note and changelog.
|
|
|
|
type BaseOutputFormatter struct {
|
2022-01-30 21:45:16 +00:00
|
|
|
templates *template.Template
|
2020-02-02 01:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOutputFormatter TemplateProcessor constructor.
|
2023-10-15 19:29:29 +00:00
|
|
|
func NewOutputFormatter(tpls *template.Template) *BaseOutputFormatter {
|
|
|
|
return &BaseOutputFormatter{templates: tpls}
|
2020-02-02 01:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FormatReleaseNote format a release note.
|
2023-10-15 22:29:02 +00:00
|
|
|
func (p BaseOutputFormatter) FormatReleaseNote(releasenote sv.ReleaseNote) ([]byte, 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 {
|
2023-10-15 22:29:02 +00:00
|
|
|
return b.Bytes(), err
|
2021-07-31 20:34:40 +00:00
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2023-10-15 22:29:02 +00:00
|
|
|
return b.Bytes(), nil
|
2020-02-02 03:49:54 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 19:03:58 +00:00
|
|
|
// FormatChangelog format a changelog.
|
2023-10-15 22:29:02 +00:00
|
|
|
func (p BaseOutputFormatter) FormatChangelog(releasenotes []sv.ReleaseNote) ([]byte, 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 {
|
2023-10-15 22:29:02 +00:00
|
|
|
return b.Bytes(), err
|
2021-07-31 20:34:40 +00:00
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2023-10-15 22:29:02 +00:00
|
|
|
return b.Bytes(), nil
|
2020-02-02 03:49:54 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
func releaseNoteVariables(releasenote sv.ReleaseNote) releaseNoteTemplateVariables {
|
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
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2020-02-02 03:49:54 +00:00
|
|
|
return releaseNoteTemplateVariables{
|
2022-02-08 01:49:32 +00:00
|
|
|
Release: release,
|
|
|
|
Tag: releasenote.Tag,
|
|
|
|
Version: releasenote.Version,
|
|
|
|
Date: releasenote.Date,
|
|
|
|
Sections: releasenote.Sections,
|
|
|
|
AuthorNames: toSortedArray(releasenote.AuthorsNames),
|
2022-02-07 01:06:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toSortedArray(input map[string]struct{}) []string {
|
|
|
|
result := make([]string, len(input))
|
|
|
|
i := 0
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2022-02-07 01:06:46 +00:00
|
|
|
for k := range input {
|
|
|
|
result[i] = k
|
|
|
|
i++
|
2020-02-02 01:40:31 +00:00
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2022-02-07 01:06:46 +00:00
|
|
|
sort.Strings(result)
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2022-02-07 01:06:46 +00:00
|
|
|
return result
|
2020-02-02 01:40:31 +00:00
|
|
|
}
|