You've already forked go-semantic-release
Merge remote-tracking branch 'origin/travis' into add_git_releases
This commit is contained in:
@@ -3,24 +3,27 @@ package analyzer
|
||||
|
||||
import (
|
||||
"github.com/Nightapes/go-semantic-release/internal/gitutil"
|
||||
"github.com/Nightapes/go-semantic-release/pkg/config"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
//Analyzer struct
|
||||
type Analyzer struct {
|
||||
CommitFormat string
|
||||
Config config.ChangelogConfig
|
||||
}
|
||||
|
||||
//Rules for commits
|
||||
type Rules struct {
|
||||
Tag string
|
||||
Release string
|
||||
Enabled bool
|
||||
//Rule for commits
|
||||
type Rule struct {
|
||||
Tag string
|
||||
TagString string
|
||||
Release string
|
||||
Changelog bool
|
||||
}
|
||||
|
||||
type analyzeCommit interface {
|
||||
analyze(commit gitutil.Commit, tag string) (AnalyzedCommit, bool)
|
||||
getRules() []Rules
|
||||
analyze(commit gitutil.Commit, tag Rule) (AnalyzedCommit, bool, error)
|
||||
getRules() []Rule
|
||||
}
|
||||
|
||||
//AnalyzedCommit struct
|
||||
@@ -29,12 +32,16 @@ type AnalyzedCommit struct {
|
||||
ParsedMessage string
|
||||
Scope string
|
||||
ParsedBreakingChangeMessage string
|
||||
Tag string
|
||||
TagString string
|
||||
Print bool
|
||||
}
|
||||
|
||||
//New Analyzer struct for given commit format
|
||||
func New(format string) *Analyzer {
|
||||
func New(format string, config config.ChangelogConfig) *Analyzer {
|
||||
return &Analyzer{
|
||||
CommitFormat: format,
|
||||
Config: config,
|
||||
}
|
||||
|
||||
}
|
||||
@@ -45,7 +52,7 @@ func (a *Analyzer) Analyze(commits []gitutil.Commit) map[string][]AnalyzedCommit
|
||||
var commitAnalayzer analyzeCommit
|
||||
switch a.CommitFormat {
|
||||
case "angular":
|
||||
log.Infof("analyze angular format")
|
||||
log.Debugf("Commit format set to angular")
|
||||
commitAnalayzer = newAngular()
|
||||
}
|
||||
|
||||
@@ -53,18 +60,29 @@ func (a *Analyzer) Analyze(commits []gitutil.Commit) map[string][]AnalyzedCommit
|
||||
analyzedCommits["major"] = make([]AnalyzedCommit, 0)
|
||||
analyzedCommits["minor"] = make([]AnalyzedCommit, 0)
|
||||
analyzedCommits["patch"] = make([]AnalyzedCommit, 0)
|
||||
analyzedCommits["none"] = make([]AnalyzedCommit, 0)
|
||||
|
||||
for _, commit := range commits {
|
||||
for _, rule := range commitAnalayzer.getRules() {
|
||||
analyzedCommit, hasBreakingChange := commitAnalayzer.analyze(commit, rule.Tag)
|
||||
if hasBreakingChange {
|
||||
analyzedCommits["major"] = append(analyzedCommits["major"], analyzedCommit)
|
||||
} else {
|
||||
analyzedCommits[rule.Release] = append(analyzedCommits[rule.Release], analyzedCommit)
|
||||
analyzedCommit, hasBreakingChange, err := commitAnalayzer.analyze(commit, rule)
|
||||
if err == nil {
|
||||
if a.Config.PrintAll {
|
||||
analyzedCommit.Print = true
|
||||
} else {
|
||||
analyzedCommit.Print = rule.Changelog
|
||||
}
|
||||
if hasBreakingChange {
|
||||
analyzedCommits["major"] = append(analyzedCommits["major"], analyzedCommit)
|
||||
} else {
|
||||
analyzedCommits[rule.Release] = append(analyzedCommits[rule.Release], analyzedCommit)
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
log.Debugf("Analyzed commits: major=%d minor=%d patch=%d none=%d", len(analyzedCommits["major"]), len(analyzedCommits["minor"]), len(analyzedCommits["patch"]), len(analyzedCommits["none"]))
|
||||
|
||||
return analyzedCommits
|
||||
}
|
||||
|
||||
@@ -2,53 +2,93 @@
|
||||
package analyzer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/Nightapes/go-semantic-release/internal/gitutil"
|
||||
)
|
||||
|
||||
type angular struct {
|
||||
rules []Rules
|
||||
rules []Rule
|
||||
regex string
|
||||
}
|
||||
|
||||
func newAngular() *angular {
|
||||
return &angular{
|
||||
regex: `(TAG)(?:\((.*)\))?: (.*)`,
|
||||
rules: []Rules{
|
||||
rules: []Rule{
|
||||
{
|
||||
Tag: "feat",
|
||||
Release: "minor",
|
||||
Enabled: true,
|
||||
Tag: "feat",
|
||||
TagString: "Features",
|
||||
Release: "minor",
|
||||
Changelog: true,
|
||||
},
|
||||
{
|
||||
Tag: "fix",
|
||||
Release: "patch",
|
||||
Enabled: true,
|
||||
Tag: "fix",
|
||||
TagString: "Bug fixes",
|
||||
Release: "patch",
|
||||
Changelog: true,
|
||||
}, {
|
||||
Tag: "perf",
|
||||
Release: "patch",
|
||||
Enabled: true,
|
||||
Tag: "perf",
|
||||
TagString: "Performance improvments",
|
||||
Release: "patch",
|
||||
Changelog: true,
|
||||
}, {
|
||||
Tag: "docs",
|
||||
TagString: "Documentation changes",
|
||||
Release: "none",
|
||||
Changelog: false,
|
||||
},
|
||||
{
|
||||
Tag: "style",
|
||||
TagString: "Style",
|
||||
Release: "none",
|
||||
Changelog: false,
|
||||
}, {
|
||||
Tag: "refactor",
|
||||
TagString: "Code refactor",
|
||||
Release: "none",
|
||||
Changelog: false,
|
||||
}, {
|
||||
Tag: "test",
|
||||
TagString: "Testing",
|
||||
Release: "none",
|
||||
Changelog: false,
|
||||
}, {
|
||||
Tag: "chore",
|
||||
TagString: "Changes to the build process or auxiliary tools and libraries such as documentation generation",
|
||||
Release: "none",
|
||||
Changelog: false,
|
||||
}, {
|
||||
Tag: "build",
|
||||
TagString: "Changes to ci config",
|
||||
Release: "none",
|
||||
Changelog: false,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (a *angular) getRules() []Rules {
|
||||
func (a *angular) getRules() []Rule {
|
||||
return a.rules
|
||||
}
|
||||
|
||||
func (a *angular) analyze(commit gitutil.Commit, tag string) (AnalyzedCommit, bool) {
|
||||
func (a *angular) analyze(commit gitutil.Commit, rule Rule) (AnalyzedCommit, bool, error) {
|
||||
|
||||
analyzed := AnalyzedCommit{
|
||||
Commit: commit,
|
||||
Commit: commit,
|
||||
Tag: rule.Tag,
|
||||
TagString: rule.TagString,
|
||||
}
|
||||
|
||||
re := regexp.MustCompile(strings.Replace(a.regex, "TAG", tag, -1))
|
||||
matches := re.FindAllStringSubmatch(commit.Message+" "+commit.Message, -1)
|
||||
re := regexp.MustCompile(strings.Replace(a.regex, "TAG", rule.Tag, -1))
|
||||
matches := re.FindAllStringSubmatch(commit.Message, -1)
|
||||
if len(matches) >= 1 {
|
||||
if len(matches[0]) >= 3 {
|
||||
|
||||
analyzed.Scope = matches[0][2]
|
||||
|
||||
message := strings.Join(matches[0][3:], "")
|
||||
@@ -56,14 +96,17 @@ func (a *angular) analyze(commit gitutil.Commit, tag string) (AnalyzedCommit, bo
|
||||
|
||||
if len(splitted) == 1 {
|
||||
analyzed.ParsedMessage = splitted[0]
|
||||
return analyzed, false
|
||||
log.Tracef("%s: found %s", commit.Message, rule.Tag)
|
||||
return analyzed, false, nil
|
||||
}
|
||||
analyzed.ParsedMessage = splitted[0]
|
||||
analyzed.ParsedBreakingChangeMessage = splitted[1]
|
||||
return analyzed, true
|
||||
log.Tracef(" %s, BREAKING CHANGE found", commit.Message)
|
||||
return analyzed, true, nil
|
||||
|
||||
}
|
||||
}
|
||||
return analyzed, false
|
||||
log.Tracef("%s does not match %s, skip", commit.Message, rule.Tag)
|
||||
return analyzed, false, fmt.Errorf("Not found")
|
||||
|
||||
}
|
||||
|
||||
82
internal/changelog/changelog.go
Normal file
82
internal/changelog/changelog.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package changelog
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/Nightapes/go-semantic-release/internal/analyzer"
|
||||
"github.com/Nightapes/go-semantic-release/pkg/config"
|
||||
)
|
||||
|
||||
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 }}
|
||||
### {{ $key }}
|
||||
|
||||
{{range $index,$commit := $commits}}* **{{$backtick}}{{$commit.Scope}}:{{$backtick}}** {{$commit.ParsedMessage}}
|
||||
{{ end -}}
|
||||
{{ end -}}
|
||||
`
|
||||
|
||||
type changelogContent struct {
|
||||
Commits map[string][]analyzer.AnalyzedCommit
|
||||
Version string
|
||||
Now time.Time
|
||||
Backtick string
|
||||
}
|
||||
|
||||
//CommitFormat struct
|
||||
type Changelog struct {
|
||||
config *config.ReleaseConfig
|
||||
}
|
||||
|
||||
//New Changelog struct for generating changelog from commits
|
||||
func New(config *config.ReleaseConfig) *Changelog {
|
||||
return &Changelog{
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
// GenerateChanglog from given commits
|
||||
func (c *Changelog) GenerateChanglog(version string, analyzedCommits map[string][]analyzer.AnalyzedCommit) (string, string, error) {
|
||||
|
||||
commitsPerScope := map[string][]analyzer.AnalyzedCommit{}
|
||||
for _, commits := range analyzedCommits {
|
||||
for _, commit := range commits {
|
||||
if commit.Print {
|
||||
if _, ok := commitsPerScope[commit.TagString]; !ok {
|
||||
commitsPerScope[commit.TagString] = make([]analyzer.AnalyzedCommit, 0)
|
||||
}
|
||||
commitsPerScope[commit.TagString] = append(commitsPerScope[commit.TagString], commit)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
changelogContent := changelogContent{
|
||||
Version: version,
|
||||
Commits: commitsPerScope,
|
||||
Now: time.Now(),
|
||||
Backtick: "`",
|
||||
}
|
||||
|
||||
title, err := generateTemplate(defaultChangelogTitle, changelogContent)
|
||||
content, err := generateTemplate(defaultChangelog, changelogContent)
|
||||
|
||||
return title, content, err
|
||||
}
|
||||
|
||||
func generateTemplate(text string, values changelogContent) (string, error) {
|
||||
var tpl bytes.Buffer
|
||||
tmpl, err := template.New("template").Parse(text)
|
||||
if err != nil {
|
||||
return "", nil
|
||||
}
|
||||
err = tmpl.Execute(&tpl, values)
|
||||
if err != nil {
|
||||
return "", nil
|
||||
}
|
||||
return tpl.String(), nil
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/Masterminds/semver"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gopkg.in/src-d/go-git.v4"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/object"
|
||||
)
|
||||
|
||||
@@ -62,23 +63,27 @@ func (g *GitUtil) GetBranch() (string, error) {
|
||||
// GetLastVersion from git tags
|
||||
func (g *GitUtil) GetLastVersion() (*semver.Version, string, error) {
|
||||
|
||||
log.Debugf("GetLastVersion")
|
||||
var tags []*semver.Version
|
||||
|
||||
gitTags, err := g.Repository.Tags()
|
||||
|
||||
tagObjects, err := g.Repository.TagObjects()
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
var tags []*semver.Version
|
||||
|
||||
err = tagObjects.ForEach(func(t *object.Tag) error {
|
||||
v, err := semver.NewVersion(t.Name)
|
||||
|
||||
if err != nil {
|
||||
log.Debugf("Tag %s is not a valid version, skip", t.Name)
|
||||
err = gitTags.ForEach(func(p *plumbing.Reference) error {
|
||||
v, err := semver.NewVersion(p.Name().Short())
|
||||
log.Tracef("%+v", p.Name().Short())
|
||||
if err == nil {
|
||||
_, err := g.Repository.TagObject(p.Hash())
|
||||
if err == nil {
|
||||
log.Debugf("Add tag %s", p.Name().Short())
|
||||
tags = append(tags, v)
|
||||
} else {
|
||||
log.Debugf("Found tag %s, but is not annotated, skip", err.Error())
|
||||
}
|
||||
} else {
|
||||
log.Debugf("Add tag %s", t.Name)
|
||||
tags = append(tags, v)
|
||||
log.Debugf("Tag %s is not a valid version, skip", p.Name().Short())
|
||||
}
|
||||
return nil
|
||||
})
|
||||
@@ -113,7 +118,6 @@ func (g *GitUtil) GetLastVersion() (*semver.Version, string, error) {
|
||||
// GetCommits from git hash to HEAD
|
||||
func (g *GitUtil) GetCommits(lastTagHash string) ([]Commit, error) {
|
||||
|
||||
log.Printf("Read head")
|
||||
ref, err := g.Repository.Head()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -129,7 +133,7 @@ func (g *GitUtil) GetCommits(lastTagHash string) ([]Commit, error) {
|
||||
|
||||
err = cIter.ForEach(func(c *object.Commit) error {
|
||||
if c.Hash.String() == lastTagHash {
|
||||
log.Infof("%s == %s", c.Hash.String(), lastTagHash)
|
||||
log.Debugf("Found commit with hash %s, will stop here", c.Hash.String())
|
||||
foundEnd = true
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user