2019-05-14 20:19:36 +02:00
|
|
|
// Package analyzer provides different commit analyzer
|
2019-05-13 22:36:24 +02:00
|
|
|
package analyzer
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/Nightapes/go-semantic-release/internal/gitutil"
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
)
|
|
|
|
|
|
2019-05-14 20:19:36 +02:00
|
|
|
//Analyzer struct
|
2019-05-13 22:36:24 +02:00
|
|
|
type Analyzer struct {
|
|
|
|
|
CommitFormat string
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 20:19:36 +02:00
|
|
|
//Rules for commits
|
2019-05-13 22:36:24 +02:00
|
|
|
type Rules struct {
|
|
|
|
|
Tag string
|
|
|
|
|
Release string
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 20:19:36 +02:00
|
|
|
type analyzeCommit interface {
|
|
|
|
|
analyze(commit gitutil.Commit, tag string) (AnalyzedCommit, bool)
|
|
|
|
|
getRules() []Rules
|
2019-05-13 22:36:24 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-14 20:19:36 +02:00
|
|
|
//AnalyzedCommit struct
|
2019-05-13 22:36:24 +02:00
|
|
|
type AnalyzedCommit struct {
|
|
|
|
|
Commit gitutil.Commit
|
|
|
|
|
ParsedMessage string
|
|
|
|
|
Scope string
|
|
|
|
|
ParsedBreakingChangeMessage string
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 20:19:36 +02:00
|
|
|
//New Analyzer struct for given commit format
|
2019-05-13 22:36:24 +02:00
|
|
|
func New(format string) *Analyzer {
|
|
|
|
|
return &Analyzer{
|
|
|
|
|
CommitFormat: format,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 20:19:36 +02:00
|
|
|
// Analyze commits and return commits splitted by major,minor,patch
|
2019-05-13 22:36:24 +02:00
|
|
|
func (a *Analyzer) Analyze(commits []gitutil.Commit) map[string][]AnalyzedCommit {
|
|
|
|
|
|
2019-05-14 20:19:36 +02:00
|
|
|
var commitAnalayzer analyzeCommit
|
2019-05-13 22:36:24 +02:00
|
|
|
switch a.CommitFormat {
|
|
|
|
|
case "angular":
|
|
|
|
|
log.Infof("analyze angular format")
|
2019-05-14 20:19:36 +02:00
|
|
|
commitAnalayzer = newAngular()
|
2019-05-13 22:36:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
analyzedCommits := make(map[string][]AnalyzedCommit)
|
|
|
|
|
analyzedCommits["major"] = make([]AnalyzedCommit, 0)
|
|
|
|
|
analyzedCommits["minor"] = make([]AnalyzedCommit, 0)
|
|
|
|
|
analyzedCommits["patch"] = make([]AnalyzedCommit, 0)
|
|
|
|
|
|
|
|
|
|
for _, commit := range commits {
|
2019-05-14 20:19:36 +02:00
|
|
|
for _, rule := range commitAnalayzer.getRules() {
|
|
|
|
|
analyzedCommit, hasBreakingChange := commitAnalayzer.analyze(commit, rule.Tag)
|
2019-05-13 22:36:24 +02:00
|
|
|
if hasBreakingChange {
|
|
|
|
|
analyzedCommits["major"] = append(analyzedCommits["major"], analyzedCommit)
|
|
|
|
|
} else {
|
|
|
|
|
analyzedCommits[rule.Release] = append(analyzedCommits[rule.Release], analyzedCommit)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return analyzedCommits
|
|
|
|
|
}
|