-
Notifications
You must be signed in to change notification settings - Fork 787
/
semrel.go
137 lines (123 loc) · 3.43 KB
/
semrel.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package semrel
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/pkg/errors"
"github.com/Masterminds/semver"
"github.com/jenkins-x/jx/v2/pkg/gits"
)
var commitPattern = regexp.MustCompile("^(\\w*)(?:\\((.*)\\))?\\: (.*)$")
var breakingPattern = regexp.MustCompile("BREAKING CHANGES?")
type change struct {
Major, Minor, Patch bool
}
type conventionalCommit struct {
*gits.GitCommit
MessageLines []string
Type string
Scope string
MessageBody string
Change change
}
type release struct {
SHA string
Version *semver.Version
}
func calculateChange(commits []*conventionalCommit, latestRelease *release) change {
var change change
for _, commit := range commits {
if latestRelease.SHA == commit.SHA {
break
}
change.Major = change.Major || commit.Change.Major
change.Minor = change.Minor || commit.Change.Minor
change.Patch = change.Patch || commit.Change.Patch
}
return change
}
func applyChange(version *semver.Version, change change) *semver.Version {
log.Logger().Debugf("applying change %+v", change)
if version.Major() == 0 {
change.Major = true
}
if !change.Major && !change.Minor && !change.Patch {
log.Logger().Debugf("unable to determine change so defaulting to patch")
change.Patch = true
}
var newVersion semver.Version
preRel := version.Prerelease()
if preRel == "" {
switch {
case change.Major:
newVersion = version.IncMajor()
break
case change.Minor:
newVersion = version.IncMinor()
break
case change.Patch:
newVersion = version.IncPatch()
break
}
return &newVersion
}
preRelVer := strings.Split(preRel, ".")
if len(preRelVer) > 1 {
idx, err := strconv.ParseInt(preRelVer[1], 10, 32)
if err != nil {
idx = 0
}
preRel = fmt.Sprintf("%s.%d", preRelVer[0], idx+1)
} else {
preRel += ".1"
}
newVersion, _ = version.SetPrerelease(preRel)
return &newVersion
}
// GetNewVersion uses the conventional commits in the range of latestTagRev..endSha to increment the version from latestTag
func GetNewVersion(dir string, endSha string, gitter gits.Gitter, latestTag string, latestTagRev string) (*semver.Version, error) {
version, err := semver.NewVersion(strings.TrimPrefix(latestTag, "v"))
if err != nil {
return nil, errors.Wrapf(err, "parsing %s as semantic version", latestTag)
}
release := release{
SHA: latestTagRev,
Version: version,
}
if err != nil {
return nil, errors.WithStack(err)
}
rawCommits, err := gitter.GetCommits(dir, release.SHA, endSha)
if err != nil {
return nil, errors.Wrapf(err, "getting commits in range %s..%s", release.SHA, endSha)
}
commits := make([]*conventionalCommit, 0)
log.Logger().Debugf("got %d commits", len(rawCommits))
for _, c := range rawCommits {
commit := c
commits = append(commits, parseCommit(&commit))
}
return applyChange(release.Version, calculateChange(commits, &release)), nil
}
func parseCommit(commit *gits.GitCommit) *conventionalCommit {
c := &conventionalCommit{
GitCommit: commit,
}
log.Logger().Debugf("parsing message '%s'", commit.Message)
c.MessageLines = strings.Split(commit.Message, "\n")
found := commitPattern.FindAllStringSubmatch(c.MessageLines[0], -1)
if len(found) < 1 {
return c
}
c.Type = strings.ToLower(found[0][1])
c.Scope = found[0][2]
c.MessageBody = found[0][3]
c.Change = change{
Major: breakingPattern.MatchString(commit.Message),
Minor: c.Type == "feat",
Patch: c.Type == "fix",
}
return c
}