releaser-pleaser/internal/changelog/changelog.go
Julian Tölle 2621c48d75
feat(changelog): omit version heading in forge release notes
The forge ui usually shows the release name right above the description,
so this removes an unecessary duplicate bit of information.

In addition this also cleans up the changelog interface a bit and moves
functionality where it belongs. Prepares a bit for custom changelogs in
the future.

Closes #32
2024-09-22 14:00:30 +02:00

72 lines
1.5 KiB
Go

package changelog
import (
"bytes"
_ "embed"
"html/template"
"log"
"log/slog"
"github.com/apricote/releaser-pleaser/internal/commitparser"
"github.com/apricote/releaser-pleaser/internal/markdown"
)
var (
changelogTemplate *template.Template
)
//go:embed changelog.md.tpl
var rawChangelogTemplate string
func init() {
var err error
changelogTemplate, err = template.New("changelog").Parse(rawChangelogTemplate)
if err != nil {
log.Fatalf("failed to parse changelog template: %v", err)
}
}
func DefaultTemplate() *template.Template {
return changelogTemplate
}
type Data struct {
Commits map[string][]commitparser.AnalyzedCommit
Version string
VersionLink string
Prefix string
Suffix string
}
func New(commits map[string][]commitparser.AnalyzedCommit, version, versionLink, prefix, suffix string) Data {
return Data{
Commits: commits,
Version: version,
VersionLink: versionLink,
Prefix: prefix,
Suffix: suffix,
}
}
type Formatting struct {
HideVersionTitle bool
}
func Entry(logger *slog.Logger, tpl *template.Template, data Data, formatting Formatting) (string, error) {
var changelog bytes.Buffer
err := tpl.Execute(&changelog, map[string]any{
"Data": data,
"Formatting": formatting,
})
if err != nil {
return "", err
}
formatted, err := markdown.Format(changelog.String())
if err != nil {
logger.Warn("failed to format changelog entry, using unformatted", "error", err)
return changelog.String(), nil
}
return formatted, nil
}