feat(changelog): add order and hash url

This commit is contained in:
Nightapes
2019-06-10 16:24:44 +02:00
parent 4011cd5a8c
commit a1498f3e32
8 changed files with 110 additions and 40 deletions

View File

@@ -2,6 +2,8 @@
package analyzer
import (
"fmt"
"github.com/Nightapes/go-semantic-release/internal/gitutil"
"github.com/Nightapes/go-semantic-release/pkg/config"
log "github.com/sirupsen/logrus"
@@ -9,8 +11,8 @@ import (
//Analyzer struct
type Analyzer struct {
CommitFormat string
Config config.ChangelogConfig
analyzeCommit analyzeCommit
Config config.ChangelogConfig
}
//Rule for commits
@@ -38,24 +40,30 @@ type AnalyzedCommit struct {
}
//New Analyzer struct for given commit format
func New(format string, config config.ChangelogConfig) *Analyzer {
return &Analyzer{
CommitFormat: format,
Config: config,
func New(format string, config config.ChangelogConfig) (*Analyzer, error) {
analyzer := &Analyzer{
Config: config,
}
switch format {
case "angular":
log.Debugf("Commit format set to angular")
analyzer.analyzeCommit = newAngular()
default:
return nil, fmt.Errorf("Invalid commit format: %s", format)
}
return analyzer, nil
}
// GetRules from current mode
func (a *Analyzer) GetRules() []Rule {
return a.analyzeCommit.getRules()
}
// Analyze commits and return commits splitted by major,minor,patch
func (a *Analyzer) Analyze(commits []gitutil.Commit) map[string][]AnalyzedCommit {
var commitAnalayzer analyzeCommit
switch a.CommitFormat {
case "angular":
log.Debugf("Commit format set to angular")
commitAnalayzer = newAngular()
}
analyzedCommits := make(map[string][]AnalyzedCommit)
analyzedCommits["major"] = make([]AnalyzedCommit, 0)
analyzedCommits["minor"] = make([]AnalyzedCommit, 0)
@@ -63,8 +71,8 @@ func (a *Analyzer) Analyze(commits []gitutil.Commit) map[string][]AnalyzedCommit
analyzedCommits["none"] = make([]AnalyzedCommit, 0)
for _, commit := range commits {
for _, rule := range commitAnalayzer.getRules() {
analyzedCommit, hasBreakingChange, err := commitAnalayzer.analyze(commit, rule)
for _, rule := range a.analyzeCommit.getRules() {
analyzedCommit, hasBreakingChange, err := a.analyzeCommit.analyze(commit, rule)
if err == nil {
if a.Config.PrintAll {
analyzedCommit.Print = true

View File

@@ -64,7 +64,7 @@ func newAngular() *angular {
Changelog: false,
}, {
Tag: "build",
TagString: "Changes to ci config",
TagString: "Changes to CI/CD",
Release: "none",
Changelog: false,
},

View File

@@ -2,48 +2,65 @@ package changelog
import (
"bytes"
"strings"
"text/template"
"time"
"github.com/Nightapes/go-semantic-release/internal/analyzer"
"github.com/Nightapes/go-semantic-release/pkg/config"
log "github.com/sirupsen/logrus"
)
const defaultChangelogTitle string = `v{{.Version}} ({{.Now.Format "2006-01-02"}})`
const defaultChangelog string = `{{ $version := .Version -}}
{{ $backtick := .Backtick -}}
# v{{.Version}} ({{.Now.Format "2006-01-02"}})
{{ range $key, $commits := .Commits }}
const defaultChangelog string = `# v{{$.Version}} ({{.Now.Format "2006-01-02"}})
{{ range $key := .Order }}
{{ $commits := index $.Commits $key}} {{if $commits -}}
### {{ $key }}
{{range $index,$commit := $commits}}* **{{$backtick}}{{$commit.Scope}}:{{$backtick}}** {{$commit.ParsedMessage}}
{{ range $index,$commit := $commits -}}
* **{{$.Backtick}}{{$commit.Scope}}{{$.Backtick}}** {{$commit.ParsedMessage}} {{if $.HasURL}} ([{{ printf "%.7s" $commit.Commit.Hash}}]({{ replace $.URL "{{hash}}" $commit.Commit.Hash}})) {{end}}
{{ end -}}
{{ end -}}
{{ end -}}
`
type changelogContent struct {
Commits map[string][]analyzer.AnalyzedCommit
Order []string
Version string
Now time.Time
Backtick string
HasURL bool
URL string
}
//CommitFormat struct
//Changelog struct
type Changelog struct {
config *config.ReleaseConfig
rules []analyzer.Rule
}
//New Changelog struct for generating changelog from commits
func New(config *config.ReleaseConfig) *Changelog {
func New(config *config.ReleaseConfig, rules []analyzer.Rule) *Changelog {
return &Changelog{
config: config,
rules: rules,
}
}
// GenerateChanglog from given commits
func (c *Changelog) GenerateChanglog(version string, analyzedCommits map[string][]analyzer.AnalyzedCommit) (string, string, error) {
func (c *Changelog) GenerateChanglog(version, url string, analyzedCommits map[string][]analyzer.AnalyzedCommit) (string, string, error) {
commitsPerScope := map[string][]analyzer.AnalyzedCommit{}
order := make([]string, 0)
for _, rule := range c.rules {
log.Debugf("Add %s to list", rule.TagString)
if rule.Changelog || c.config.Changelog.PrintAll {
order = append(order, rule.TagString)
}
}
for _, commits := range analyzedCommits {
for _, commit := range commits {
if commit.Print {
@@ -60,23 +77,38 @@ func (c *Changelog) GenerateChanglog(version string, analyzedCommits map[string]
Commits: commitsPerScope,
Now: time.Now(),
Backtick: "`",
Order: order,
HasURL: url != "",
URL: url,
}
title, err := generateTemplate(defaultChangelogTitle, changelogContent)
if err != nil {
return "", "", err
}
content, err := generateTemplate(defaultChangelog, changelogContent)
return title, content, err
}
func generateTemplate(text string, values changelogContent) (string, error) {
funcMap := template.FuncMap{
"replace": replace,
}
var tpl bytes.Buffer
tmpl, err := template.New("template").Parse(text)
tmpl, err := template.New("template").Funcs(funcMap).Parse(text)
if err != nil {
return "", nil
return "", err
}
err = tmpl.Execute(&tpl, values)
if err != nil {
return "", nil
return "", err
}
return tpl.String(), nil
}
func replace(input, from, to string) string {
return strings.Replace(input, from, to, -1)
}