feat(changelog): show authors of commits and show body as header

Enable new features in `.release.yml` like

```yml

changelog:
  showAuthors: false  ## Show authors in changelog
  showBodyAsHeader: false  ## Show all bodies of the commits as header of changelog (useful for squash commit flow to show long text in release)

```
This commit is contained in:
Sebastian Beisch
2022-04-11 15:58:37 +02:00
committed by Felix Wiedmann
parent 03f2eeadaa
commit 0c7338ab13
13 changed files with 273 additions and 482 deletions

View File

@@ -22,7 +22,7 @@ var defaultTokenSeparators = [2]string{": ", " #"}
type Analyzer struct {
analyzeCommits analyzeCommits
ChangelogConfig config.ChangelogConfig
AnalyzerConfig config.AnalyzerConfig
AnalyzerConfig config.AnalyzerConfig
}
// Rule for commits
@@ -41,7 +41,7 @@ type analyzeCommits interface {
// New Analyzer struct for given commit format
func New(format string, analyzerConfig config.AnalyzerConfig, chglogConfig config.ChangelogConfig) (*Analyzer, error) {
analyzer := &Analyzer{
AnalyzerConfig: analyzerConfig,
AnalyzerConfig: analyzerConfig,
ChangelogConfig: chglogConfig,
}
@@ -112,9 +112,9 @@ func getRegexMatchedMap(regEx, url string) (paramsMap map[string]string) {
//
// getMessageBlocksFromTexts converts strings to an array of MessageBlock
//
func getMessageBlocksFromTexts(txtArray, separators []string) []shared.MessageBlock {
func getMessageBlocksFromTexts(txtArray, separators []string) []shared.MessageBlock {
blocks := make([]shared.MessageBlock, len(txtArray))
for i, line := range txtArray{
for i, line := range txtArray {
blocks[i] = parseMessageBlock(line, separators)
}
return blocks
@@ -128,9 +128,9 @@ func parseMessageBlock(msg string, separators []string) shared.MessageBlock {
Label: "",
Content: msg,
}
if token, sep := findFooterToken(msg, separators); len(token) > 0{
if token, sep := findFooterToken(msg, separators); len(token) > 0 {
msgBlock.Label = token
content := strings.Replace(msg, token + sep, "", 1)
content := strings.Replace(msg, token+sep, "", 1)
msgBlock.Content = strings.TrimSpace(content)
}
return msgBlock
@@ -158,7 +158,7 @@ func findFooterToken(text string, separators []string) (token string, sep string
// - A footer is detected when it starts with a token ending with a separator
// - A footer ends when another footer is found or text ends
//
func getDefaultMessageBlockMap(txtBlock string, tokenSep []string) map[string][]shared.MessageBlock{
func getDefaultMessageBlockMap(txtBlock string, tokenSep []string) map[string][]shared.MessageBlock {
msgBlockMap := make(map[string][]shared.MessageBlock)
footers := make([]string, 0)
body, footerBlock, line := "", "", ""
@@ -169,7 +169,7 @@ func getDefaultMessageBlockMap(txtBlock string, tokenSep []string) map[string][]
line = scanner.Text()
if token, _ := findFooterToken(line, tokenSep); len(token) > 0 {
// if footer was already found from before
if len(footerBlock) > 0{
if len(footerBlock) > 0 {
footers = append(footers, strings.TrimSpace(footerBlock))
}
footerFound = true
@@ -179,7 +179,7 @@ func getDefaultMessageBlockMap(txtBlock string, tokenSep []string) map[string][]
//'\n' is removed when reading from scanner
if !footerFound {
body += line + "\n"
}else{
} else {
footerBlock += line + "\n"
}
}
@@ -188,11 +188,11 @@ func getDefaultMessageBlockMap(txtBlock string, tokenSep []string) map[string][]
}
body = strings.TrimSpace(body)
if len(body) > 0{
msgBlockMap["body"] = []shared.MessageBlock {{
if len(body) > 0 {
msgBlockMap["body"] = []shared.MessageBlock{{
Label: "",
Content: body,
} }
}}
}
footerBlocks := getMessageBlocksFromTexts(footers, tokenSep)
@@ -201,4 +201,4 @@ func getDefaultMessageBlockMap(txtBlock string, tokenSep []string) map[string][]
}
return msgBlockMap
}
}

View File

@@ -11,14 +11,15 @@ import (
)
type angular struct {
rules []Rule
regex string
log *log.Entry
config config.AnalyzerConfig
rules []Rule
regex string
log *log.Entry
config config.AnalyzerConfig
}
// ANGULAR identifier
const ANGULAR = "angular"
var angularFooterTokenSep = defaultTokenSeparators
func newAngular() *angular {
@@ -99,17 +100,20 @@ func (a *angular) analyze(commit shared.Commit, rule Rule) *shared.AnalyzedCommi
}
matches := getRegexMatchedMap(a.regex, header)
if len(matches) == 0 || matches["type"] != rule.Tag{
if len(matches) == 0 || matches["type"] != rule.Tag {
a.log.Tracef("%s does not match %s, skip", commit.Message, rule.Tag)
return nil
}
msgBlockMap := getDefaultMessageBlockMap(body, tokenSep)
log.Debugf("Found commit from Author %s", commit.Author)
analyzed := &shared.AnalyzedCommit{
Commit: commit,
Tag: rule.Tag,
TagString: rule.TagString,
Commit: commit,
Author: commit.Author,
Tag: rule.Tag,
TagString: rule.TagString,
Scope: shared.Scope(matches["scope"]),
Subject: strings.TrimSpace(matches["subject"]),
MessageBlocks: msgBlockMap,

View File

@@ -11,21 +11,22 @@ import (
)
type conventional struct {
rules []Rule
regex string
log *log.Entry
config config.AnalyzerConfig
rules []Rule
regex string
log *log.Entry
config config.AnalyzerConfig
}
// CONVENTIONAL identifier
const CONVENTIONAL = "conventional"
var conventionalFooterTokenSep = defaultTokenSeparators
func newConventional(config config.AnalyzerConfig) *conventional {
return &conventional{
config: config,
regex: `^(?P<type>\w*)(?:\((?P<scope>.*)\))?(?P<breaking>\!)?: (?P<subject>.*)`,
log: log.WithField("analyzer", CONVENTIONAL),
regex: `^(?P<type>\w*)(?:\((?P<scope>.*)\))?(?P<breaking>\!)?: (?P<subject>.*)`,
log: log.WithField("analyzer", CONVENTIONAL),
rules: []Rule{
{
Tag: "feat",
@@ -101,7 +102,7 @@ func (a *conventional) analyze(commit shared.Commit, rule Rule) *shared.Analyzed
matches := getRegexMatchedMap(a.regex, header)
if len(matches) == 0 || matches["type"] != rule.Tag{
if len(matches) == 0 || matches["type"] != rule.Tag {
a.log.Tracef("%s does not match %s, skip", commit.Message, rule.Tag)
return nil
}
@@ -110,6 +111,7 @@ func (a *conventional) analyze(commit shared.Commit, rule Rule) *shared.Analyzed
analyzed := &shared.AnalyzedCommit{
Commit: commit,
Author: commit.Author,
Tag: rule.Tag,
TagString: rule.TagString,
Scope: shared.Scope(matches["scope"]),
@@ -140,4 +142,3 @@ func (a *conventional) analyze(commit shared.Commit, rule Rule) *shared.Analyzed
return analyzed
}

View File

@@ -29,6 +29,7 @@ introduced by commit:
### {{ $key }}
{{ range $index,$commit := $commits -}}
* {{ if $commit.Scope }}**{{$.Backtick}}{{$commit.Scope}}{{$.Backtick}}** {{end}}{{$commit.Subject}}{{if $.HasURL}} ([{{ printf "%.7s" $commit.Commit.Hash}}]({{ replace $.URL "{{hash}}" $commit.Commit.Hash}})){{end}}
{{ if not $.ShowBodyAsHeader -}}
{{ if $commit.MessageBlocks.body -}}
{{ range $indexBlock,$bodyBlock := $commit.MessageBlocks.body -}}
{{ addPrefixToLines $bodyBlock.Content " > "}}
@@ -36,10 +37,27 @@ introduced by commit:
{{ end -}}
{{ end -}}
{{ end -}}
{{ end -}}
{{ end -}}`
const defaultCommitListSubTemplate = `{{ define "commitList" }}` + defaultCommitList + "{{ end }}"
const defaultChangelogTitle = `v{{.Version}} ({{.Now.Format "2006-01-02"}})`
const defaultChangelog = `# v{{$.Version}} ({{.Now.Format "2006-01-02"}})
{{ if .ShowBodyAsHeader -}}
{{ range $key := .CommitsContent.Order -}}
{{ $commits := index $.CommitsContent.Commits $key -}}
{{ if $commits -}}
{{ range $index,$commit := $commits -}}
{{ if $commit.MessageBlocks.body -}}
{{ range $indexBlock,$bodyBlock := $commit.MessageBlocks.body -}}
{{ $bodyBlock.Content }}
{{ end -}}
{{ end -}}
{{ end -}}
{{ end -}}
{{ end -}}
{{ end -}}
{{ template "commitList" .CommitsContent -}}
{{ if .HasDocker}}
@@ -70,6 +88,12 @@ or
{{$.Backtick}}npm install -save {{.NPMPackageName}}@{{.Version}}{{$.Backtick}}
{{ end -}}
{{ if .ShowAuthors -}}
# Special Thanks
{{range $i,$a := .Authors}}{{if gt $i 0 }}, {{end}}{{$a}}{{end}}
{{ end -}}
`
@@ -79,6 +103,7 @@ type changelogContent struct {
Version string
Now time.Time
Backtick string
ShowBodyAsHeader bool
HasDocker bool
HasDockerLatest bool
DockerRepository string
@@ -86,15 +111,18 @@ type changelogContent struct {
IsYarn bool
NPMRepository string
NPMPackageName string
Authors []string
ShowAuthors bool
}
type commitsContent struct {
Commits map[string][]shared.AnalyzedCommit
BreakingChanges []shared.AnalyzedCommit
Order []string
Backtick string
HasURL bool
URL string
Commits map[string][]shared.AnalyzedCommit
BreakingChanges []shared.AnalyzedCommit
Order []string
ShowBodyAsHeader bool
Backtick string
HasURL bool
URL string
}
//Changelog struct
@@ -129,8 +157,17 @@ func (c *Changelog) GenerateChangelog(templateConfig shared.ChangelogTemplateCon
}
}
authors := map[string]int{}
for _, commits := range analyzedCommits {
for _, commit := range commits {
_, ok := authors[commit.Author]
if !ok {
authors[commit.Author] = 0
}
authors[commit.Author] = authors[commit.Author] + 1
if commit.Print {
if commit.IsBreaking {
commitsBreakingChange = append(commitsBreakingChange, commit)
@@ -145,12 +182,20 @@ func (c *Changelog) GenerateChangelog(templateConfig shared.ChangelogTemplateCon
}
commitsContent := commitsContent{
Commits: commitsPerScope,
BreakingChanges: commitsBreakingChange,
Backtick: "`",
Order: order,
HasURL: templateConfig.CommitURL != "",
URL: templateConfig.CommitURL,
Commits: commitsPerScope,
BreakingChanges: commitsBreakingChange,
Backtick: "`",
Order: order,
ShowBodyAsHeader: c.config.Changelog.ShowBodyAsHeader,
HasURL: templateConfig.CommitURL != "",
URL: templateConfig.CommitURL,
}
authorsNames := make([]string, len(authors))
i := 0
for k := range authors {
authorsNames[i] = k
i++
}
changelogContent := changelogContent{
@@ -164,6 +209,9 @@ func (c *Changelog) GenerateChangelog(templateConfig shared.ChangelogTemplateCon
HasNPM: c.config.Changelog.NPM.PackageName != "",
NPMPackageName: c.config.Changelog.NPM.PackageName,
NPMRepository: c.config.Changelog.NPM.Repository,
ShowBodyAsHeader: c.config.Changelog.ShowBodyAsHeader,
ShowAuthors: c.config.Changelog.ShowAuthors && len(authors) > 0,
Authors: authorsNames,
}
chglogTemplate := defaultCommitListSubTemplate + defaultChangelog

View File

@@ -25,6 +25,7 @@ func TestChangelog(t *testing.T) {
analyzedCommits map[shared.Release][]shared.AnalyzedCommit
result *shared.GeneratedChangelog
hasError bool
showAuthors bool
}{
{
testCase: "feat",
@@ -52,6 +53,48 @@ func TestChangelog(t *testing.T) {
},
hasError: false,
},
{
testCase: "feat with authors",
showAuthors: true,
analyzedCommits: map[shared.Release][]shared.AnalyzedCommit{
"minor": {
{
Author: "me",
Commit: shared.Commit{
Message: "feat(internal/changelog): my first commit",
Author: "me",
Hash: "12345667",
},
Scope: "internal/changelog",
ParsedMessage: "my first commit",
Tag: "feat",
TagString: "Features",
Print: true,
Subject: "my first commit",
MessageBlocks: map[string][]shared.MessageBlock{},
},
{
Author: "secondAuthor",
Commit: shared.Commit{
Message: "feat(internal/changelog): my second commit",
Author: "secondAuthor",
Hash: "12345667",
},
Scope: "internal/changelog",
ParsedMessage: "my second commit",
Tag: "feat",
TagString: "Features",
Print: true,
Subject: "my second commit",
MessageBlocks: map[string][]shared.MessageBlock{},
},
},
},
result: &shared.GeneratedChangelog{
Title: "v1.0.0 (2019-07-19)",
Content: "# v1.0.0 (2019-07-19)\n### Features\n* **`internal/changelog`** my first commit ([1234566](https://commit.url))\n* **`internal/changelog`** my second commit ([1234566](https://commit.url))\n# Special Thanks\n\nme, secondAuthor\n"},
hasError: false,
},
{
testCase: "feat no scope",
analyzedCommits: map[shared.Release][]shared.AnalyzedCommit{
@@ -200,6 +243,27 @@ func TestChangelog(t *testing.T) {
}},
},
},
{
Commit: shared.Commit{
Message: "feat: my awesome features \n\n * Feature1: Lists in changelog \n* Feature2: Lists in changelog2",
Author: "me",
Hash: "12345668",
},
Scope: "",
ParsedMessage: "my awesome features",
Tag: "feat",
TagString: "Features",
Print: true,
ParsedBreakingChangeMessage: "",
IsBreaking: false,
Subject: "my awesome features",
MessageBlocks: map[string][]shared.MessageBlock{
"body": {shared.MessageBlock{
Label: "",
Content: "* Feature1: Lists in changelog \n* Feature2: Lists in changelog2",
}},
},
},
{
Commit: shared.Commit{
Message: "feat!: my next commit",
@@ -220,37 +284,42 @@ func TestChangelog(t *testing.T) {
},
result: &shared.GeneratedChangelog{
Title: "v1.0.0 (2019-07-19)",
Content: "# v1.0.0 (2019-07-19)\n## BREAKING CHANGES\n* hey from the change \nintroduced by commit: \nmy first break ([1234566](https://commit.url))\n* change api to v2 \nintroduced by commit: \nmy first break ([1234566](https://commit.url))\n* my next commit \nintroduced by commit: \nmy next commit ([1234566](https://commit.url))\n### Features\n* **`internal/changelog`** my first commit ([1234566](https://commit.url))\n* my second commit ([1234566](https://commit.url))\n"},
Content: "# v1.0.0 (2019-07-19)\n## BREAKING CHANGES\n* hey from the change \nintroduced by commit: \nmy first break ([1234566](https://commit.url))\n* change api to v2 \nintroduced by commit: \nmy first break ([1234566](https://commit.url))\n* my next commit \nintroduced by commit: \nmy next commit ([1234566](https://commit.url))\n### Features\n* **`internal/changelog`** my first commit ([1234566](https://commit.url))\n* my second commit ([1234566](https://commit.url))\n* my awesome features ([1234566](https://commit.url))\n > * Feature1: Lists in changelog \n > * Feature2: Lists in changelog2\n"},
hasError: false,
},
}
cl := changelog.New(&config.ReleaseConfig{}, []analyzer.Rule{
{
Tag: "feat",
TagString: "Features",
Release: "minor",
Changelog: true,
},
{
Tag: "fix",
TagString: "Bug fixes",
Release: "patch",
Changelog: true,
},
{
Tag: "build",
TagString: "Build",
Release: "none",
Changelog: false,
},
}, time.Date(2019, 7, 19, 0, 0, 0, 0, time.UTC))
for _, testConfig := range testConfigs {
t.Run(testConfig.testCase, func(t *testing.T) {
cl := changelog.New(&config.ReleaseConfig{
Changelog: config.ChangelogConfig{
ShowBodyAsHeader: false,
ShowAuthors: testConfig.showAuthors,
},
}, []analyzer.Rule{
{
Tag: "feat",
TagString: "Features",
Release: "minor",
Changelog: true,
},
{
Tag: "fix",
TagString: "Bug fixes",
Release: "patch",
Changelog: true,
},
{
Tag: "build",
TagString: "Build",
Release: "none",
Changelog: false,
},
}, time.Date(2019, 7, 19, 0, 0, 0, 0, time.UTC))
for _, config := range testConfigs {
t.Run(config.testCase, func(t *testing.T) {
generatedChangelog, err := cl.GenerateChangelog(templateConfig, config.analyzedCommits)
assert.Equalf(t, config.hasError, err != nil, "Testcase %s should have error: %t -> %s", config.testCase, config.hasError, err)
assert.Equalf(t, config.result, generatedChangelog, "Testcase %s should have generated changelog", config.testCase)
generatedChangelog, err := cl.GenerateChangelog(templateConfig, testConfig.analyzedCommits)
assert.Equalf(t, testConfig.hasError, err != nil, "Testcase %s should have error: %t -> %s", testConfig.testCase, testConfig.hasError, err)
assert.Equalf(t, testConfig.result, generatedChangelog, "Testcase %s should have generated changelog", testConfig.testCase)
})
}

View File

@@ -3,9 +3,10 @@ package gitutil
import (
"fmt"
"github.com/pkg/errors"
"sort"
"github.com/pkg/errors"
"github.com/Masterminds/semver"
"github.com/Nightapes/go-semantic-release/internal/shared"
"github.com/go-git/go-git/v5"
@@ -176,10 +177,10 @@ func (g *GitUtil) GetCommits(lastTagHash *plumbing.Reference) ([]shared.Commit,
commits := make(map[string]shared.Commit)
err = cIter.ForEach(func(c *object.Commit) error {
log.Debugf("Found commit with hash %s", c.Hash.String())
log.Debugf("Found commit with hash %s from %s", c.Hash.String(), c.Author.Name)
commits[c.Hash.String()] = shared.Commit{
Message: c.Message,
Author: c.Committer.Name,
Author: c.Author.Name,
Hash: c.Hash.String(),
}
return nil

View File

@@ -35,21 +35,22 @@ type ChangelogTemplateConfig struct {
//AnalyzedCommit struct
type AnalyzedCommit struct {
Commit Commit `yaml:"commit"`
ParsedMessage string `yaml:"parsedMessage"`
ParsedBreakingChangeMessage string `yaml:"parsedBreakingChangeMessage"`
Tag string `yaml:"tag"`
TagString string `yaml:"tagString"`
Scope Scope `yaml:"scope"`
Subject string `yaml:"subject"`
MessageBlocks map[string][]MessageBlock `yaml:"messageBlocks"`
IsBreaking bool `yaml:"isBreaking"`
Print bool `yaml:"print"`
Commit Commit `yaml:"commit"`
ParsedMessage string `yaml:"parsedMessage"`
Author string `yaml:"author"`
ParsedBreakingChangeMessage string `yaml:"parsedBreakingChangeMessage"`
Tag string `yaml:"tag"`
TagString string `yaml:"tagString"`
Scope Scope `yaml:"scope"`
Subject string `yaml:"subject"`
MessageBlocks map[string][]MessageBlock `yaml:"messageBlocks"`
IsBreaking bool `yaml:"isBreaking"`
Print bool `yaml:"print"`
}
// MessageBlock represents a block in the body section of a commit message
type MessageBlock struct {
Label string `yaml:"label"`
Label string `yaml:"label"`
Content string `yaml:"content"`
}