2016-07-14 11:44:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2023-11-11 14:27:37 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Masterminds/semver/v3"
|
2016-07-14 11:44:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2023-11-11 14:27:37 +00:00
|
|
|
*semver.Version
|
2016-07-14 11:44:21 +00:00
|
|
|
}
|
|
|
|
|
2023-11-11 14:27:37 +00:00
|
|
|
func (s *semVer) SetMetadata(metadata string) error {
|
|
|
|
nv, err := s.Version.SetMetadata(metadata)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("setting metadata: %w", err)
|
2016-07-14 11:44:21 +00:00
|
|
|
}
|
|
|
|
|
2023-11-11 14:27:37 +00:00
|
|
|
s.Version = &nv
|
|
|
|
return nil
|
2016-07-14 11:44:21 +00:00
|
|
|
}
|
|
|
|
|
2023-11-11 14:27:37 +00:00
|
|
|
func (s *semVer) SetPrerelease(prerelease string) error {
|
|
|
|
nv, err := s.Version.SetPrerelease(prerelease)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("setting prerelease: %w", err)
|
2016-07-14 11:44:21 +00:00
|
|
|
}
|
|
|
|
|
2023-11-11 14:27:37 +00:00
|
|
|
s.Version = &nv
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-14 11:44:21 +00:00
|
|
|
|
2023-11-11 14:27:37 +00:00
|
|
|
func parseSemVer(version string) (*semVer, error) {
|
|
|
|
v, err := semver.NewVersion(version)
|
2016-07-14 11:44:21 +00:00
|
|
|
if err != nil {
|
2023-11-11 14:27:37 +00:00
|
|
|
return nil, fmt.Errorf("parsing semver: %w", err)
|
2016-07-14 11:44:21 +00:00
|
|
|
}
|
2023-11-11 14:27:37 +00:00
|
|
|
return &semVer{v}, nil
|
2016-07-14 11:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *semVer) Bump(bumpType semVerBump) {
|
2023-11-11 14:27:37 +00:00
|
|
|
var nv semver.Version
|
|
|
|
|
2016-07-14 11:44:21 +00:00
|
|
|
switch bumpType {
|
|
|
|
case semVerBumpPatch:
|
2023-11-11 14:27:37 +00:00
|
|
|
nv = s.Version.IncPatch()
|
|
|
|
|
2016-07-14 11:44:21 +00:00
|
|
|
case semVerBumpMinor:
|
2023-11-11 14:27:37 +00:00
|
|
|
nv = s.Version.IncMinor()
|
|
|
|
|
2016-07-14 11:44:21 +00:00
|
|
|
case semVerBumpMajor:
|
2023-11-11 14:27:37 +00:00
|
|
|
nv = s.Version.IncMajor()
|
2016-07-14 11:44:21 +00:00
|
|
|
}
|
2023-11-11 14:27:37 +00:00
|
|
|
|
|
|
|
s.Version = &nv
|
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
|
2023-11-11 14:27: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
|
|
|
|
}
|