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 (
|
2019-06-10 16:24:44 +02:00
|
|
|
"fmt"
|
|
|
|
|
|
2019-05-13 22:36:24 +02:00
|
|
|
"github.com/Nightapes/go-semantic-release/internal/gitutil"
|
2019-05-25 18:10:24 +02:00
|
|
|
"github.com/Nightapes/go-semantic-release/pkg/config"
|
2019-05-13 22:36:24 +02:00
|
|
|
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 {
|
2019-06-10 16:24:44 +02:00
|
|
|
analyzeCommit analyzeCommit
|
|
|
|
|
Config config.ChangelogConfig
|
2019-05-13 22:36:24 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-25 18:10:24 +02:00
|
|
|
//Rule for commits
|
|
|
|
|
type Rule struct {
|
2019-05-16 21:30:35 +02:00
|
|
|
Tag string
|
2019-05-25 18:10:24 +02:00
|
|
|
TagString string
|
2019-05-16 21:30:35 +02:00
|
|
|
Release string
|
|
|
|
|
Changelog bool
|
2019-05-13 22:36:24 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-14 20:19:36 +02:00
|
|
|
type analyzeCommit interface {
|
2019-05-25 18:10:24 +02:00
|
|
|
analyze(commit gitutil.Commit, tag Rule) (AnalyzedCommit, bool, error)
|
|
|
|
|
getRules() []Rule
|
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-16 21:30:35 +02:00
|
|
|
Tag string
|
2019-05-25 18:10:24 +02:00
|
|
|
TagString string
|
|
|
|
|
Print bool
|
2019-05-13 22:36:24 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-14 20:19:36 +02:00
|
|
|
//New Analyzer struct for given commit format
|
2019-06-10 16:24:44 +02:00
|
|
|
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)
|
2019-05-13 22:36:24 +02:00
|
|
|
}
|
2019-06-10 16:24:44 +02:00
|
|
|
return analyzer, nil
|
2019-05-13 22:36:24 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-10 16:24:44 +02:00
|
|
|
// GetRules from current mode
|
|
|
|
|
func (a *Analyzer) GetRules() []Rule {
|
|
|
|
|
return a.analyzeCommit.getRules()
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
|
|
analyzedCommits := make(map[string][]AnalyzedCommit)
|
|
|
|
|
analyzedCommits["major"] = make([]AnalyzedCommit, 0)
|
|
|
|
|
analyzedCommits["minor"] = make([]AnalyzedCommit, 0)
|
|
|
|
|
analyzedCommits["patch"] = make([]AnalyzedCommit, 0)
|
2019-05-16 21:30:35 +02:00
|
|
|
analyzedCommits["none"] = make([]AnalyzedCommit, 0)
|
2019-05-13 22:36:24 +02:00
|
|
|
|
|
|
|
|
for _, commit := range commits {
|
2019-06-10 16:24:44 +02:00
|
|
|
for _, rule := range a.analyzeCommit.getRules() {
|
|
|
|
|
analyzedCommit, hasBreakingChange, err := a.analyzeCommit.analyze(commit, rule)
|
2019-05-16 21:30:35 +02:00
|
|
|
if err == nil {
|
2019-05-25 18:10:24 +02:00
|
|
|
if a.Config.PrintAll {
|
|
|
|
|
analyzedCommit.Print = true
|
|
|
|
|
} else {
|
|
|
|
|
analyzedCommit.Print = rule.Changelog
|
|
|
|
|
}
|
2019-05-16 21:30:35 +02:00
|
|
|
if hasBreakingChange {
|
|
|
|
|
analyzedCommits["major"] = append(analyzedCommits["major"], analyzedCommit)
|
|
|
|
|
} else {
|
|
|
|
|
analyzedCommits[rule.Release] = append(analyzedCommits[rule.Release], analyzedCommit)
|
|
|
|
|
}
|
|
|
|
|
break
|
2019-05-13 22:36:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-16 21:30:35 +02:00
|
|
|
log.Debugf("Analyzed commits: major=%d minor=%d patch=%d none=%d", len(analyzedCommits["major"]), len(analyzedCommits["minor"]), len(analyzedCommits["patch"]), len(analyzedCommits["none"]))
|
|
|
|
|
|
2019-05-13 22:36:24 +02:00
|
|
|
return analyzedCommits
|
|
|
|
|
}
|