-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
52 lines (42 loc) · 1.61 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"fmt"
"github.com/Masterminds/semver"
"github.com/xfxdev/xlog"
)
// GetNextVersion returns the next version to use based on the current version
// and a slice of commit messages
func GetNextVersion(currentVersion string, messages, patchTypes []string) (string, error) {
commits, errs := ParseMessages(messages)
for _, err := range errs {
xlog.Warn(err) // Log any errors, but dont actually terminate
}
return GetNextVersionFromCommits(currentVersion, commits, patchTypes)
}
// GetNextVersionFromCommits returns the next version to use based on the current
// version and a slice of `ConventionalCommit`s
func GetNextVersionFromCommits(currentVersion string, commits []ConventionalCommit, patchTypes []string) (string, error) {
current, err := semver.NewVersion(currentVersion)
if err != nil {
return "", fmt.Errorf("Unable to parse version %s", currentVersion)
}
var newVersion semver.Version
for _, commit := range commits {
if commit.BreakingChange {
xlog.Debugf("Commit message with description \"%s\" has resulted in a major version increment", commit.Description)
newVersion = current.IncMajor()
break
}
if !typeIsPatch(commit.CommitType, patchTypes) {
xlog.Debugf("Commit message with type \"%s\" and description \"%s\" has resulted in a minor version increment", commit.CommitType, commit.Description)
newVersion = current.IncMinor()
break
}
newVersion = current.IncPatch()
}
if newVersion.Equal(&semver.Version{}) {
xlog.Warn("No conventional commits parsed so defaulting to minor increment")
newVersion = current.IncMinor()
}
return newVersion.String(), nil
}