Files
go-semantic-release/internal/analyzer/analyzer.go

89 lines
2.4 KiB
Go
Raw Normal View History

2019-05-14 20:19:36 +02:00
// Package analyzer provides different commit analyzer
package analyzer
import (
"github.com/Nightapes/go-semantic-release/internal/gitutil"
"github.com/Nightapes/go-semantic-release/pkg/config"
log "github.com/sirupsen/logrus"
)
2019-05-14 20:19:36 +02:00
//Analyzer struct
type Analyzer struct {
CommitFormat string
Config config.ChangelogConfig
}
//Rule for commits
type Rule struct {
Tag string
TagString string
Release string
Changelog bool
}
2019-05-14 20:19:36 +02:00
type analyzeCommit interface {
analyze(commit gitutil.Commit, tag Rule) (AnalyzedCommit, bool, error)
getRules() []Rule
}
2019-05-14 20:19:36 +02:00
//AnalyzedCommit struct
type AnalyzedCommit struct {
Commit gitutil.Commit
ParsedMessage string
Scope string
ParsedBreakingChangeMessage string
Tag string
TagString string
Print bool
}
2019-05-14 20:19:36 +02:00
//New Analyzer struct for given commit format
func New(format string, config config.ChangelogConfig) *Analyzer {
return &Analyzer{
CommitFormat: format,
Config: config,
}
}
2019-05-14 20:19:36 +02:00
// Analyze commits and return commits splitted by major,minor,patch
func (a *Analyzer) Analyze(commits []gitutil.Commit) map[string][]AnalyzedCommit {
2019-05-14 20:19:36 +02:00
var commitAnalayzer analyzeCommit
switch a.CommitFormat {
case "angular":
log.Debugf("Commit format set to angular")
2019-05-14 20:19:36 +02:00
commitAnalayzer = newAngular()
}
analyzedCommits := make(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 {
2019-05-14 20:19:36 +02:00
for _, rule := range commitAnalayzer.getRules() {
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
}