2016-07-14 11:44:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type semVerBump uint
|
|
|
|
|
|
|
|
const (
|
2016-07-21 15:51:08 +00:00
|
|
|
semVerBumpUndecided semVerBump = iota
|
2016-07-14 11:44:21 +00:00
|
|
|
semVerBumpPatch
|
2016-07-21 15:51:08 +00:00
|
|
|
semVerBumpMinor
|
2016-07-14 11:44:21 +00:00
|
|
|
semVerBumpMajor
|
|
|
|
)
|
|
|
|
|
|
|
|
type semVer struct {
|
|
|
|
Major, Minor, Patch int
|
|
|
|
PreReleaseInformation string
|
|
|
|
MetaData string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *semVer) String() string {
|
|
|
|
v := []string{strings.Join([]string{
|
|
|
|
strconv.Itoa(s.Major),
|
|
|
|
strconv.Itoa(s.Minor),
|
|
|
|
strconv.Itoa(s.Patch),
|
|
|
|
}, ".")}
|
|
|
|
|
|
|
|
if s.PreReleaseInformation != "" {
|
|
|
|
v = append(v, "-"+s.PreReleaseInformation)
|
|
|
|
}
|
|
|
|
if s.MetaData != "" {
|
|
|
|
v = append(v, "+"+s.MetaData)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(v, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseSemVer(version string) (*semVer, error) {
|
|
|
|
var (
|
|
|
|
s semVer
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
version = strings.TrimLeft(version, "v") // Ensure the version is not prefixed like v0.1.0
|
|
|
|
|
|
|
|
t := strings.SplitN(version, "+", 2)
|
|
|
|
if len(t) == 2 {
|
|
|
|
s.MetaData = t[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
t = strings.SplitN(t[0], "-", 2)
|
|
|
|
if len(t) == 2 {
|
|
|
|
s.PreReleaseInformation = t[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
elements := strings.Split(t[0], ".")
|
|
|
|
if len(elements) != 3 {
|
|
|
|
return nil, errors.New("Version does not match semantic versioning format")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Major, err = strconv.Atoi(elements[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.Minor, err = strconv.Atoi(elements[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.Patch, err = strconv.Atoi(elements[2])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *semVer) Bump(bumpType semVerBump) {
|
|
|
|
switch bumpType {
|
|
|
|
case semVerBumpPatch:
|
2018-07-03 07:46:37 +00:00
|
|
|
s.Patch++
|
2016-07-14 11:44:21 +00:00
|
|
|
case semVerBumpMinor:
|
|
|
|
s.Patch = 0
|
2018-07-03 07:46:37 +00:00
|
|
|
s.Minor++
|
2016-07-14 11:44:21 +00:00
|
|
|
case semVerBumpMajor:
|
|
|
|
s.Patch = 0
|
|
|
|
s.Minor = 0
|
2018-07-03 07:46:37 +00:00
|
|
|
s.Major++
|
2016-07-14 11:44:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func selectBumpType(logs []commit) (semVerBump, error) {
|
2016-07-21 15:51:08 +00:00
|
|
|
bump := semVerBumpUndecided
|
2016-07-14 11:44:21 +00:00
|
|
|
|
2016-07-21 15:51:08 +00:00
|
|
|
for _, l := range logs {
|
|
|
|
if l.BumpType > bump {
|
|
|
|
bump = l.BumpType
|
2016-07-14 11:44:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 15:51:08 +00:00
|
|
|
if bump == semVerBumpUndecided {
|
|
|
|
// Impossible to reach
|
2018-07-03 07:46:37 +00:00
|
|
|
return semVerBumpUndecided, errors.New("Could not decide for any bump type")
|
2016-07-14 11:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bump, nil
|
|
|
|
}
|