From 9594f39caae7c70bcc0be53d8b9ab570b891fb73 Mon Sep 17 00:00:00 2001 From: maulik13 Date: Mon, 8 Feb 2021 11:15:27 +0100 Subject: [PATCH 1/3] feat(changelog): allow using of TemplatePath file for full changelog text --- internal/changelog/changelog.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/internal/changelog/changelog.go b/internal/changelog/changelog.go index 5158466..d0dba1d 100644 --- a/internal/changelog/changelog.go +++ b/internal/changelog/changelog.go @@ -31,9 +31,11 @@ introduced by commit: {{ end -}} {{ end -}} {{ end -}}` +const defaultCommitListSubTemplate string = `{{ define "commitList" }}` + defaultCommitList + "{{ end }}" const defaultChangelogTitle string = `v{{.Version}} ({{.Now.Format "2006-01-02"}})` const defaultChangelog string = `# v{{$.Version}} ({{.Now.Format "2006-01-02"}}) -{{ .Commits -}} +{{ template "commitList" .CommitsContent -}} + {{ if .HasDocker}} ## Docker image @@ -51,7 +53,8 @@ or ` type changelogContent struct { - Commits string + Commits string + CommitsContent commitsContent Version string Now time.Time Backtick string @@ -130,6 +133,7 @@ func (c *Changelog) GenerateChanglog(templateConfig shared.ChangelogTemplateConf } changelogContent := changelogContent{ + CommitsContent: commitsContent, Version: templateConfig.Version, Now: c.releaseTime, Backtick: "`", @@ -137,7 +141,7 @@ func (c *Changelog) GenerateChanglog(templateConfig shared.ChangelogTemplateConf HasDockerLatest: c.config.Changelog.Docker.Latest, DockerRepository: c.config.Changelog.Docker.Repository, } - template := defaultChangelog + template := defaultCommitListSubTemplate + defaultChangelog if c.config.Changelog.TemplatePath != "" { content, err := ioutil.ReadFile(c.config.Changelog.TemplatePath) if err != nil { @@ -164,8 +168,8 @@ func (c *Changelog) GenerateChanglog(templateConfig shared.ChangelogTemplateConf } log.Tracef("Commits %s", renderedCommitList) - changelogContent.Commits = renderedCommitList + log.Debugf("Render changelog") renderedContent, err := generateTemplate(template, changelogContent) From 3a37a5e1db1efa200649283ac859ea806a3bc4d1 Mon Sep 17 00:00:00 2001 From: maulik13 Date: Mon, 8 Feb 2021 11:30:06 +0100 Subject: [PATCH 2/3] feat(changelog): add string functions for changelog template --- internal/changelog/changelog.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/internal/changelog/changelog.go b/internal/changelog/changelog.go index d0dba1d..a2698f0 100644 --- a/internal/changelog/changelog.go +++ b/internal/changelog/changelog.go @@ -1,6 +1,7 @@ package changelog import ( + "bufio" "bytes" "io/ioutil" "strings" @@ -180,6 +181,10 @@ func generateTemplate(text string, values interface{}) (string, error) { funcMap := template.FuncMap{ "replace": replace, + "lower": lower, + "upper": upper, + "capitalize": capitalize, + "addPrefixToLines": addPrefixToLines, } var tpl bytes.Buffer @@ -197,3 +202,30 @@ func generateTemplate(text string, values interface{}) (string, error) { func replace(input, from, to string) string { 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 +} From c485c3ee85ab790fd097af58da2bc73531ab651a Mon Sep 17 00:00:00 2001 From: maulik13 Date: Fri, 12 Feb 2021 13:55:08 +0100 Subject: [PATCH 3/3] docs(changelog): update changelog template example --- examples/changelog.tmpl | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/examples/changelog.tmpl b/examples/changelog.tmpl index f8f4486..4f4216d 100644 --- a/examples/changelog.tmpl +++ b/examples/changelog.tmpl @@ -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"}}) -{{ .Commits -}} +{{ template "commitList" .CommitsContent -}} + {{ if .HasDocker}} ## Docker image