Merge pull request #58 from maulik13/changelog-full-template

Changelog full template
This commit is contained in:
Sebastian
2021-02-12 17:23:32 +01:00
committed by GitHub
2 changed files with 62 additions and 5 deletions

View File

@@ -1,5 +1,26 @@
{{ define "commitList" }}
{{ range $index,$commit := .BreakingChanges -}}
{{ if eq $index 0 -}}
## BREAKING CHANGES
{{ end -}}
* {{ if $commit.Scope }}**{{$.Backtick}}{{$commit.Scope}}{{$.Backtick}}**{{ end }} {{$commit.ParsedBreakingChangeMessage}}
introduced by commit:
{{$commit.ParsedMessage}} {{if $.HasURL}} ([{{ printf "%.7s" $commit.Commit.Hash}}]({{ replace $.URL "{{hash}}" $commit.Commit.Hash}})){{end}}
{{ end -}}
{{ range $key := .Order -}}
{{ $commits := index $.Commits $key -}}
{{ if $commits -}}
### {{ $key }}
{{ range $index,$commit := $commits -}}
* {{ if $commit.Scope }}**{{$.Backtick}}{{$commit.Scope}}{{$.Backtick}}** {{end}}{{$commit.ParsedMessage}}{{if $.HasURL}} ([{{ printf "%.7s" $commit.Commit.Hash}}]({{ replace $.URL "{{hash}}" $commit.Commit.Hash}})){{end}}
{{ end -}}
{{ end -}}
{{ end -}}
{{ end -}}
# My custom release template v{{$.Version}} ({{.Now.Format "2006-01-02"}}) # My custom release template v{{$.Version}} ({{.Now.Format "2006-01-02"}})
{{ .Commits -}} {{ template "commitList" .CommitsContent -}}
{{ if .HasDocker}} {{ if .HasDocker}}
## Docker image ## Docker image

View File

@@ -1,6 +1,7 @@
package changelog package changelog
import ( import (
"bufio"
"bytes" "bytes"
"io/ioutil" "io/ioutil"
"strings" "strings"
@@ -31,9 +32,11 @@ introduced by commit:
{{ end -}} {{ end -}}
{{ end -}} {{ end -}}
{{ end -}}` {{ end -}}`
const defaultCommitListSubTemplate string = `{{ define "commitList" }}` + defaultCommitList + "{{ end }}"
const defaultChangelogTitle string = `v{{.Version}} ({{.Now.Format "2006-01-02"}})` const defaultChangelogTitle string = `v{{.Version}} ({{.Now.Format "2006-01-02"}})`
const defaultChangelog string = `# v{{$.Version}} ({{.Now.Format "2006-01-02"}}) const defaultChangelog string = `# v{{$.Version}} ({{.Now.Format "2006-01-02"}})
{{ .Commits -}} {{ template "commitList" .CommitsContent -}}
{{ if .HasDocker}} {{ if .HasDocker}}
## Docker image ## Docker image
@@ -51,7 +54,8 @@ or
` `
type changelogContent struct { type changelogContent struct {
Commits string Commits string
CommitsContent commitsContent
Version string Version string
Now time.Time Now time.Time
Backtick string Backtick string
@@ -130,6 +134,7 @@ func (c *Changelog) GenerateChanglog(templateConfig shared.ChangelogTemplateConf
} }
changelogContent := changelogContent{ changelogContent := changelogContent{
CommitsContent: commitsContent,
Version: templateConfig.Version, Version: templateConfig.Version,
Now: c.releaseTime, Now: c.releaseTime,
Backtick: "`", Backtick: "`",
@@ -137,7 +142,7 @@ func (c *Changelog) GenerateChanglog(templateConfig shared.ChangelogTemplateConf
HasDockerLatest: c.config.Changelog.Docker.Latest, HasDockerLatest: c.config.Changelog.Docker.Latest,
DockerRepository: c.config.Changelog.Docker.Repository, DockerRepository: c.config.Changelog.Docker.Repository,
} }
template := defaultChangelog template := defaultCommitListSubTemplate + defaultChangelog
if c.config.Changelog.TemplatePath != "" { if c.config.Changelog.TemplatePath != "" {
content, err := ioutil.ReadFile(c.config.Changelog.TemplatePath) content, err := ioutil.ReadFile(c.config.Changelog.TemplatePath)
if err != nil { if err != nil {
@@ -164,8 +169,8 @@ func (c *Changelog) GenerateChanglog(templateConfig shared.ChangelogTemplateConf
} }
log.Tracef("Commits %s", renderedCommitList) log.Tracef("Commits %s", renderedCommitList)
changelogContent.Commits = renderedCommitList changelogContent.Commits = renderedCommitList
log.Debugf("Render changelog") log.Debugf("Render changelog")
renderedContent, err := generateTemplate(template, changelogContent) renderedContent, err := generateTemplate(template, changelogContent)
@@ -176,6 +181,10 @@ func generateTemplate(text string, values interface{}) (string, error) {
funcMap := template.FuncMap{ funcMap := template.FuncMap{
"replace": replace, "replace": replace,
"lower": lower,
"upper": upper,
"capitalize": capitalize,
"addPrefixToLines": addPrefixToLines,
} }
var tpl bytes.Buffer var tpl bytes.Buffer
@@ -193,3 +202,30 @@ func generateTemplate(text string, values interface{}) (string, error) {
func replace(input, from, to string) string { func replace(input, from, to string) string {
return strings.Replace(input, from, to, -1) return strings.Replace(input, from, to, -1)
} }
func lower(input string) string {
return strings.ToLower(input)
}
func upper(input string) string {
return strings.ToUpper(input)
}
func capitalize(input string) string {
if len(input) > 0 {
return strings.ToUpper(string(input[0])) + input[1:]
}
return ""
}
// Adds a prefix to each line of the given text block
// this can be helpful in rendering correct indentation or bullets for multi-line texts
func addPrefixToLines(input, prefix string) string {
output := ""
scanner := bufio.NewScanner(strings.NewReader(input))
for scanner.Scan() {
output += prefix + scanner.Text() + "\n"
}
output = strings.TrimRight(output, "\n")
return output
}