2019-11-17 16:17:24 +00:00
|
|
|
package sv
|
|
|
|
|
2021-02-13 18:40:09 +00:00
|
|
|
import "github.com/Masterminds/semver/v3"
|
2019-11-17 16:17:24 +00:00
|
|
|
|
|
|
|
type versionType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
none versionType = iota
|
|
|
|
patch
|
|
|
|
minor
|
|
|
|
major
|
|
|
|
)
|
|
|
|
|
2022-01-29 22:25:52 +00:00
|
|
|
// IsValidVersion return true when a version is valid.
|
|
|
|
func IsValidVersion(value string) bool {
|
|
|
|
_, err := semver.NewVersion(value)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2021-07-31 19:03:58 +00:00
|
|
|
// ToVersion parse string to semver.Version.
|
2021-12-29 21:02:50 +00:00
|
|
|
func ToVersion(value string) (*semver.Version, error) {
|
2019-11-17 16:17:24 +00:00
|
|
|
version := value
|
|
|
|
if version == "" {
|
|
|
|
version = "0.0.0"
|
|
|
|
}
|
2021-12-29 21:02:50 +00:00
|
|
|
return semver.NewVersion(version)
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 19:03:58 +00:00
|
|
|
// SemVerCommitsProcessor interface.
|
2019-11-17 16:17:24 +00:00
|
|
|
type SemVerCommitsProcessor interface {
|
2021-12-29 21:02:50 +00:00
|
|
|
NextVersion(version *semver.Version, commits []GitCommitLog) (*semver.Version, bool)
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 19:03:58 +00:00
|
|
|
// SemVerCommitsProcessorImpl process versions using commit log.
|
2019-11-17 16:17:24 +00:00
|
|
|
type SemVerCommitsProcessorImpl struct {
|
|
|
|
MajorVersionTypes map[string]struct{}
|
|
|
|
MinorVersionTypes map[string]struct{}
|
|
|
|
PatchVersionTypes map[string]struct{}
|
2021-02-15 05:41:43 +00:00
|
|
|
KnownTypes []string
|
2019-11-17 16:17:24 +00:00
|
|
|
IncludeUnknownTypeAsPatch bool
|
|
|
|
}
|
|
|
|
|
2021-07-31 19:03:58 +00:00
|
|
|
// NewSemVerCommitsProcessor SemanticVersionCommitsProcessorImpl constructor.
|
2021-02-15 05:41:43 +00:00
|
|
|
func NewSemVerCommitsProcessor(vcfg VersioningConfig, mcfg CommitMessageConfig) *SemVerCommitsProcessorImpl {
|
2019-11-17 16:17:24 +00:00
|
|
|
return &SemVerCommitsProcessorImpl{
|
2021-02-15 05:41:43 +00:00
|
|
|
IncludeUnknownTypeAsPatch: !vcfg.IgnoreUnknown,
|
|
|
|
MajorVersionTypes: toMap(vcfg.UpdateMajor),
|
|
|
|
MinorVersionTypes: toMap(vcfg.UpdateMinor),
|
|
|
|
PatchVersionTypes: toMap(vcfg.UpdatePatch),
|
|
|
|
KnownTypes: mcfg.Types,
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-31 19:03:58 +00:00
|
|
|
// NextVersion calculates next version based on commit log.
|
2021-12-29 21:02:50 +00:00
|
|
|
func (p SemVerCommitsProcessorImpl) NextVersion(version *semver.Version, commits []GitCommitLog) (*semver.Version, bool) {
|
2021-07-31 19:03:58 +00:00
|
|
|
versionToUpdate := none
|
2019-11-17 16:17:24 +00:00
|
|
|
for _, commit := range commits {
|
|
|
|
if v := p.versionTypeToUpdate(commit); v > versionToUpdate {
|
|
|
|
versionToUpdate = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 21:02:50 +00:00
|
|
|
updated := versionToUpdate != none
|
|
|
|
if version == nil {
|
|
|
|
return nil, updated
|
|
|
|
}
|
|
|
|
newVersion := updateVersion(*version, versionToUpdate)
|
|
|
|
return &newVersion, updated
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateVersion(version semver.Version, versionToUpdate versionType) semver.Version {
|
2019-11-17 16:17:24 +00:00
|
|
|
switch versionToUpdate {
|
|
|
|
case major:
|
2021-12-29 21:02:50 +00:00
|
|
|
return version.IncMajor()
|
2019-11-17 16:17:24 +00:00
|
|
|
case minor:
|
2021-12-29 21:02:50 +00:00
|
|
|
return version.IncMinor()
|
2019-11-17 16:17:24 +00:00
|
|
|
case patch:
|
2021-12-29 21:02:50 +00:00
|
|
|
return version.IncPatch()
|
2019-11-17 16:17:24 +00:00
|
|
|
default:
|
2021-12-29 21:02:50 +00:00
|
|
|
return version
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p SemVerCommitsProcessorImpl) versionTypeToUpdate(commit GitCommitLog) versionType {
|
2021-02-14 02:35:31 +00:00
|
|
|
if commit.Message.IsBreakingChange {
|
2019-11-17 16:17:24 +00:00
|
|
|
return major
|
|
|
|
}
|
2021-02-14 02:35:31 +00:00
|
|
|
if _, exists := p.MajorVersionTypes[commit.Message.Type]; exists {
|
2019-11-17 16:17:24 +00:00
|
|
|
return major
|
|
|
|
}
|
2021-02-14 02:35:31 +00:00
|
|
|
if _, exists := p.MinorVersionTypes[commit.Message.Type]; exists {
|
2019-11-17 16:17:24 +00:00
|
|
|
return minor
|
|
|
|
}
|
2021-02-14 02:35:31 +00:00
|
|
|
if _, exists := p.PatchVersionTypes[commit.Message.Type]; exists {
|
2019-11-17 16:17:24 +00:00
|
|
|
return patch
|
|
|
|
}
|
2021-02-15 05:41:43 +00:00
|
|
|
if !contains(commit.Message.Type, p.KnownTypes) && p.IncludeUnknownTypeAsPatch {
|
2019-11-17 16:17:24 +00:00
|
|
|
return patch
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
func toMap(values []string) map[string]struct{} {
|
|
|
|
result := make(map[string]struct{})
|
|
|
|
for _, v := range values {
|
|
|
|
result[v] = struct{}{}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|