Skip to content

Commit

Permalink
Merge pull request #58 from maulik13/changelog-full-template
Browse files Browse the repository at this point in the history
Changelog full template
  • Loading branch information
Nightapes authored Feb 12, 2021
2 parents 86c9512 + c485c3e commit 81bdb68
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
23 changes: 22 additions & 1 deletion examples/changelog.tmpl
Original file line number Diff line number Diff line change
@@ -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

Expand Down
44 changes: 40 additions & 4 deletions internal/changelog/changelog.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package changelog

import (
"bufio"
"bytes"
"io/ioutil"
"strings"
Expand Down Expand Up @@ -31,9 +32,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
Expand All @@ -51,7 +54,8 @@ or
`

type changelogContent struct {
Commits string
Commits string
CommitsContent commitsContent
Version string
Now time.Time
Backtick string
Expand Down Expand Up @@ -130,14 +134,15 @@ func (c *Changelog) GenerateChanglog(templateConfig shared.ChangelogTemplateConf
}

changelogContent := changelogContent{
CommitsContent: commitsContent,
Version: templateConfig.Version,
Now: c.releaseTime,
Backtick: "`",
HasDocker: c.config.Changelog.Docker.Repository != "",
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 {
Expand All @@ -164,8 +169,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)

Expand All @@ -176,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
Expand All @@ -193,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
}

0 comments on commit 81bdb68

Please sign in to comment.