2019-05-25 18:10:24 +02:00
|
|
|
package changelog
|
|
|
|
|
|
|
|
|
|
import (
|
2021-02-08 11:30:06 +01:00
|
|
|
"bufio"
|
2019-05-25 18:10:24 +02:00
|
|
|
"bytes"
|
2019-09-22 15:50:12 +02:00
|
|
|
"io/ioutil"
|
2019-06-10 16:24:44 +02:00
|
|
|
"strings"
|
2019-05-25 18:10:24 +02:00
|
|
|
"text/template"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/Nightapes/go-semantic-release/internal/analyzer"
|
2019-06-15 23:03:27 +02:00
|
|
|
"github.com/Nightapes/go-semantic-release/internal/shared"
|
2019-05-25 18:10:24 +02:00
|
|
|
"github.com/Nightapes/go-semantic-release/pkg/config"
|
2019-06-10 16:24:44 +02:00
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2019-05-25 18:10:24 +02:00
|
|
|
)
|
|
|
|
|
|
2019-09-22 15:50:12 +02:00
|
|
|
const defaultCommitList string = `{{ range $index,$commit := .BreakingChanges -}}
|
2020-01-26 17:08:57 +01:00
|
|
|
{{ if eq $index 0 -}}
|
2019-07-21 15:06:49 +02:00
|
|
|
## BREAKING CHANGES
|
2020-01-26 17:08:57 +01:00
|
|
|
{{ end -}}
|
2020-01-14 19:35:54 -08:00
|
|
|
* {{ if $commit.Scope }}**{{$.Backtick}}{{$commit.Scope}}{{$.Backtick}}**{{ end }} {{$commit.ParsedBreakingChangeMessage}}
|
2019-07-21 15:06:49 +02:00
|
|
|
introduced by commit:
|
2020-01-14 19:35:54 -08:00
|
|
|
{{$commit.ParsedMessage}} {{if $.HasURL}} ([{{ printf "%.7s" $commit.Commit.Hash}}]({{ replace $.URL "{{hash}}" $commit.Commit.Hash}})){{end}}
|
2019-07-21 15:06:49 +02:00
|
|
|
{{ end -}}
|
2020-01-26 17:08:57 +01:00
|
|
|
{{ range $key := .Order -}}
|
2020-01-14 19:35:54 -08:00
|
|
|
{{ $commits := index $.Commits $key -}}
|
2020-01-26 17:08:57 +01:00
|
|
|
{{ if $commits -}}
|
2019-05-25 18:10:24 +02:00
|
|
|
### {{ $key }}
|
2020-01-26 17:08:57 +01:00
|
|
|
{{ 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}}
|
2019-06-10 16:24:44 +02:00
|
|
|
{{ end -}}
|
2019-05-25 18:10:24 +02:00
|
|
|
{{ end -}}
|
2019-09-22 15:50:12 +02:00
|
|
|
{{ end -}}`
|
2021-02-08 11:15:27 +01:00
|
|
|
const defaultCommitListSubTemplate string = `{{ define "commitList" }}` + defaultCommitList + "{{ end }}"
|
2019-09-22 15:50:12 +02:00
|
|
|
const defaultChangelogTitle string = `v{{.Version}} ({{.Now.Format "2006-01-02"}})`
|
|
|
|
|
const defaultChangelog string = `# v{{$.Version}} ({{.Now.Format "2006-01-02"}})
|
2021-02-08 11:15:27 +01:00
|
|
|
{{ template "commitList" .CommitsContent -}}
|
|
|
|
|
|
2019-08-31 17:05:10 +02:00
|
|
|
{{ if .HasDocker}}
|
|
|
|
|
## Docker image
|
|
|
|
|
|
|
|
|
|
New docker image is released under {{$.Backtick}}{{.DockerRepository}}:{{.Version}}{{$.Backtick}}
|
|
|
|
|
|
|
|
|
|
### Usage
|
|
|
|
|
|
|
|
|
|
{{$.Backtick}}docker run {{.DockerRepository}}:{{.Version}}{{$.Backtick}}
|
|
|
|
|
{{ if .HasDockerLatest}}
|
|
|
|
|
or
|
|
|
|
|
|
|
|
|
|
{{$.Backtick}}docker run {{.DockerRepository}}:latest{{$.Backtick}}
|
|
|
|
|
{{ end -}}
|
|
|
|
|
{{ end -}}
|
2019-05-25 18:10:24 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
|
|
type changelogContent struct {
|
2021-02-08 11:15:27 +01:00
|
|
|
Commits string
|
|
|
|
|
CommitsContent commitsContent
|
2019-08-31 17:05:10 +02:00
|
|
|
Version string
|
|
|
|
|
Now time.Time
|
|
|
|
|
Backtick string
|
|
|
|
|
HasDocker bool
|
|
|
|
|
HasDockerLatest bool
|
|
|
|
|
DockerRepository string
|
2019-05-25 18:10:24 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-22 15:50:12 +02:00
|
|
|
type commitsContent struct {
|
|
|
|
|
Commits map[string][]shared.AnalyzedCommit
|
|
|
|
|
BreakingChanges []shared.AnalyzedCommit
|
|
|
|
|
Order []string
|
|
|
|
|
Version string
|
|
|
|
|
Now time.Time
|
|
|
|
|
Backtick string
|
|
|
|
|
HasURL bool
|
|
|
|
|
URL string
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-10 16:24:44 +02:00
|
|
|
//Changelog struct
|
2019-05-25 18:10:24 +02:00
|
|
|
type Changelog struct {
|
2019-07-21 15:06:49 +02:00
|
|
|
config *config.ReleaseConfig
|
|
|
|
|
rules []analyzer.Rule
|
|
|
|
|
releaseTime time.Time
|
2019-08-11 18:27:52 +02:00
|
|
|
log *log.Entry
|
2019-05-25 18:10:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//New Changelog struct for generating changelog from commits
|
2019-07-21 15:06:49 +02:00
|
|
|
func New(config *config.ReleaseConfig, rules []analyzer.Rule, releaseTime time.Time) *Changelog {
|
2019-05-25 18:10:24 +02:00
|
|
|
return &Changelog{
|
2019-07-21 15:06:49 +02:00
|
|
|
config: config,
|
|
|
|
|
rules: rules,
|
|
|
|
|
releaseTime: releaseTime,
|
2019-08-11 18:27:52 +02:00
|
|
|
log: log.WithField("changelog", config.CommitFormat),
|
2019-05-25 18:10:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GenerateChanglog from given commits
|
2019-08-11 18:27:52 +02:00
|
|
|
func (c *Changelog) GenerateChanglog(templateConfig shared.ChangelogTemplateConfig, analyzedCommits map[shared.Release][]shared.AnalyzedCommit) (*shared.GeneratedChangelog, error) {
|
2019-05-25 18:10:24 +02:00
|
|
|
|
2019-08-11 18:27:52 +02:00
|
|
|
commitsPerScope := map[string][]shared.AnalyzedCommit{}
|
|
|
|
|
commitsBreakingChange := []shared.AnalyzedCommit{}
|
2019-06-10 16:24:44 +02:00
|
|
|
order := make([]string, 0)
|
|
|
|
|
|
|
|
|
|
for _, rule := range c.rules {
|
2019-08-11 18:27:52 +02:00
|
|
|
c.log.Tracef("Add %s to list", rule.TagString)
|
2019-06-10 16:24:44 +02:00
|
|
|
if rule.Changelog || c.config.Changelog.PrintAll {
|
|
|
|
|
order = append(order, rule.TagString)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-25 18:10:24 +02:00
|
|
|
for _, commits := range analyzedCommits {
|
|
|
|
|
for _, commit := range commits {
|
|
|
|
|
if commit.Print {
|
2019-07-21 15:06:49 +02:00
|
|
|
if commit.ParsedBreakingChangeMessage != "" {
|
|
|
|
|
commitsBreakingChange = append(commitsBreakingChange, commit)
|
|
|
|
|
continue
|
|
|
|
|
}
|
2019-05-25 18:10:24 +02:00
|
|
|
if _, ok := commitsPerScope[commit.TagString]; !ok {
|
2019-08-11 18:27:52 +02:00
|
|
|
commitsPerScope[commit.TagString] = make([]shared.AnalyzedCommit, 0)
|
2019-05-25 18:10:24 +02:00
|
|
|
}
|
|
|
|
|
commitsPerScope[commit.TagString] = append(commitsPerScope[commit.TagString], commit)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-22 15:50:12 +02:00
|
|
|
commitsContent := commitsContent{
|
|
|
|
|
Version: templateConfig.Version,
|
|
|
|
|
Commits: commitsPerScope,
|
|
|
|
|
Now: c.releaseTime,
|
|
|
|
|
BreakingChanges: commitsBreakingChange,
|
|
|
|
|
Backtick: "`",
|
|
|
|
|
Order: order,
|
|
|
|
|
HasURL: templateConfig.CommitURL != "",
|
|
|
|
|
URL: templateConfig.CommitURL,
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-25 18:10:24 +02:00
|
|
|
changelogContent := changelogContent{
|
2021-02-08 11:15:27 +01:00
|
|
|
CommitsContent: commitsContent,
|
2019-08-31 17:05:10 +02:00
|
|
|
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,
|
2019-05-25 18:10:24 +02:00
|
|
|
}
|
2021-02-08 11:15:27 +01:00
|
|
|
template := defaultCommitListSubTemplate + defaultChangelog
|
2019-09-22 15:50:12 +02:00
|
|
|
if c.config.Changelog.TemplatePath != "" {
|
|
|
|
|
content, err := ioutil.ReadFile(c.config.Changelog.TemplatePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
template = string(content)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
templateTitle := defaultChangelogTitle
|
|
|
|
|
if c.config.Changelog.TemplateTitle != "" {
|
|
|
|
|
templateTitle = c.config.Changelog.TemplateTitle
|
|
|
|
|
}
|
2019-05-25 18:10:24 +02:00
|
|
|
|
2019-09-22 15:50:12 +02:00
|
|
|
log.Debugf("Render title")
|
|
|
|
|
renderedTitle, err := generateTemplate(templateTitle, changelogContent)
|
2019-06-10 16:24:44 +02:00
|
|
|
if err != nil {
|
2019-06-15 23:03:27 +02:00
|
|
|
return nil, err
|
2019-06-10 16:24:44 +02:00
|
|
|
}
|
2019-05-25 18:10:24 +02:00
|
|
|
|
2019-09-22 15:50:12 +02:00
|
|
|
log.Debugf("Render commits")
|
|
|
|
|
renderedCommitList, err := generateTemplate(defaultCommitList, commitsContent)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Tracef("Commits %s", renderedCommitList)
|
|
|
|
|
changelogContent.Commits = renderedCommitList
|
2021-02-08 11:15:27 +01:00
|
|
|
|
2019-09-22 15:50:12 +02:00
|
|
|
log.Debugf("Render changelog")
|
|
|
|
|
renderedContent, err := generateTemplate(template, changelogContent)
|
|
|
|
|
|
|
|
|
|
return &shared.GeneratedChangelog{Title: renderedTitle, Content: renderedContent}, err
|
2019-05-25 18:10:24 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-22 15:50:12 +02:00
|
|
|
func generateTemplate(text string, values interface{}) (string, error) {
|
2019-06-10 16:24:44 +02:00
|
|
|
|
|
|
|
|
funcMap := template.FuncMap{
|
|
|
|
|
"replace": replace,
|
2021-02-08 11:30:06 +01:00
|
|
|
"lower": lower,
|
|
|
|
|
"upper": upper,
|
|
|
|
|
"capitalize": capitalize,
|
|
|
|
|
"addPrefixToLines": addPrefixToLines,
|
2019-06-10 16:24:44 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-25 18:10:24 +02:00
|
|
|
var tpl bytes.Buffer
|
2019-06-10 16:24:44 +02:00
|
|
|
tmpl, err := template.New("template").Funcs(funcMap).Parse(text)
|
2019-05-25 18:10:24 +02:00
|
|
|
if err != nil {
|
2019-06-10 16:24:44 +02:00
|
|
|
return "", err
|
2019-05-25 18:10:24 +02:00
|
|
|
}
|
|
|
|
|
err = tmpl.Execute(&tpl, values)
|
|
|
|
|
if err != nil {
|
2019-06-10 16:24:44 +02:00
|
|
|
return "", err
|
2019-05-25 18:10:24 +02:00
|
|
|
}
|
|
|
|
|
return tpl.String(), nil
|
|
|
|
|
}
|
2019-06-10 16:24:44 +02:00
|
|
|
|
|
|
|
|
func replace(input, from, to string) string {
|
|
|
|
|
return strings.Replace(input, from, to, -1)
|
|
|
|
|
}
|
2021-02-08 11:30:06 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|