feat(init): increase version depending on commit (angular format)

This commit is contained in:
Nightapes
2019-05-13 22:36:24 +02:00
parent d313974d64
commit fe54370e7e
12 changed files with 664 additions and 1 deletions

View File

@@ -0,0 +1,63 @@
package analyzer
import (
"github.com/Nightapes/go-semantic-release/internal/gitutil"
log "github.com/sirupsen/logrus"
)
type Analyzer struct {
CommitFormat string
}
type Rules struct {
Tag string
Release string
}
type AnalyzeCommit interface {
Analyze(commit gitutil.Commit, tag string) (AnalyzedCommit, bool)
GetRules() []Rules
}
type AnalyzedCommit struct {
Commit gitutil.Commit
ParsedMessage string
Scope string
ParsedBreakingChangeMessage string
}
func New(format string) *Analyzer {
return &Analyzer{
CommitFormat: format,
}
}
func (a *Analyzer) Analyze(commits []gitutil.Commit) map[string][]AnalyzedCommit {
var commitAnalayzer AnalyzeCommit
switch a.CommitFormat {
case "angular":
log.Infof("analyze angular format")
commitAnalayzer = NewAngular()
}
analyzedCommits := make(map[string][]AnalyzedCommit)
analyzedCommits["major"] = make([]AnalyzedCommit, 0)
analyzedCommits["minor"] = make([]AnalyzedCommit, 0)
analyzedCommits["patch"] = 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)
}
}
}
return analyzedCommits
}

View File

@@ -0,0 +1,65 @@
package analyzer
import (
"regexp"
"strings"
"github.com/Nightapes/go-semantic-release/internal/gitutil"
)
type Angular struct {
rules []Rules
regex string
}
func NewAngular() *Angular {
return &Angular{
regex: `(TAG)(?:\((.*)\))?: (.*)`,
rules: []Rules{
{
Tag: "feat",
Release: "minor",
},
{
Tag: "fix",
Release: "patch",
}, {
Tag: "perf",
Release: "patch",
},
},
}
}
func (a *Angular) GetRules() []Rules {
return a.rules
}
func (a *Angular) Analyze(commit gitutil.Commit, tag string) (AnalyzedCommit, bool) {
analyzed := AnalyzedCommit{
Commit: commit,
}
re := regexp.MustCompile(strings.Replace(a.regex, "TAG", tag, -1))
matches := re.FindAllStringSubmatch(commit.Message+" "+commit.Message, -1)
if len(matches) >= 1 {
if len(matches[0]) >= 3 {
analyzed.Scope = matches[0][2]
message := strings.Join(matches[0][3:], "")
splitted := strings.SplitN(message, "BREAKING CHANGE:", 1)
if len(splitted) == 1 {
analyzed.ParsedMessage = splitted[0]
return analyzed, false
}
analyzed.ParsedMessage = splitted[0]
analyzed.ParsedBreakingChangeMessage = splitted[1]
return analyzed, true
}
}
return analyzed, false
}