Skip to content

Commit

Permalink
fix prerelease bump issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ptgoetz committed Nov 9, 2024
1 parent 1eeeacb commit 655b451
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cmd/versionbump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ var gitTagHistoryCmd = &cobra.Command{
}

var preReleaseNextCmd = &cobra.Command{
Use: "prerelease-next",
Use: "prerelease",
Short: `Bump the next pre-release version label (e.g. 1.2.3-alpha -> 1.2.3-beta).`,
Long: `Bump the patch version number (e.g. 1.2.3 -> 1.2.4).`,
RunE: bumpPreReleaseNext, // Use RunE for better error handling
Expand Down
27 changes: 19 additions & 8 deletions internal/semver/prerelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,27 +151,38 @@ func (v *PreReleaseVersion) Bump(versionPart int, preReleaseLabels []string) (*P
// sort pre-release labels
sort.Strings(preReleaseLabels)
// if the label is empty, this is the first pre-release version, so return the first label
label := v.Label
if v.Label == "" {
v.Label = preReleaseLabels[0]
return NewPrereleaseVersion(preReleaseLabels[0], 0, 0, 0), nil
label = preReleaseLabels[0]
//return NewPrereleaseVersion(preReleaseLabels[0], 0, 0, 0), nil
}

switch versionPart {
case prMajor:
return NewPrereleaseVersion(v.Label, v.Version.major+1, 0, 0), nil
if v.Label == "" {
return NewPrereleaseVersion(label, v.Version.major, 0, 0), nil
} else {
return NewPrereleaseVersion(label, v.Version.major+1, 0, 0), nil
}

case prMinor:
return NewPrereleaseVersion(v.Label, v.Version.major, v.Version.minor+1, 0), nil
return NewPrereleaseVersion(label, v.Version.major, v.Version.minor+1, 0), nil
case prPatch:
return NewPrereleaseVersion(v.Label, v.Version.major, v.Version.minor, v.Version.patch+1), nil
return NewPrereleaseVersion(label, v.Version.major, v.Version.minor, v.Version.patch+1), nil
case prNext:
// find the index of the current label
idx := indexOf(v.Label, preReleaseLabels)
idx := indexOf(label, preReleaseLabels)
// if the version being bumped has no label, return the first label
offset := 1
if v.Label == "" {
offset = 0
}
if idx == -1 {
return nil, fmt.Errorf("label %s not found in preReleaseLabels", v.Label)
return nil, fmt.Errorf("label %s not found in preReleaseLabels: %v", v.Label, preReleaseLabels)
} else if idx == len(preReleaseLabels)-1 {
return nil, fmt.Errorf("cannot bump beyond the last label %s", v.Label)
} else {
return NewPrereleaseVersion(preReleaseLabels[idx+1], 0, 0, 0), nil
return NewPrereleaseVersion(preReleaseLabels[idx+offset], 0, 0, 0), nil
}
default:
panic(fmt.Sprintf("invalid version part: %d.\n", versionPart))
Expand Down
10 changes: 5 additions & 5 deletions internal/semver/prerelease_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ func TestBumpPreRelease(t *testing.T) {
{"alpha", prNext, "beta", false},
{"alpha", prMinor, "alpha.0.1", false},
{"0.0.0", prMajor, "alpha", false},
{"1.0.0", prMinor, "alpha", false},
{"1.1.0", prPatch, "alpha", false},
{"2.5.1", prMajor, "alpha", false},
{"2.5.1", prMinor, "alpha", false},
{"2.5.1", prPatch, "alpha", false},
{"1.0.0", prMinor, "alpha.1.1", false},
{"1.1.0", prPatch, "alpha.1.1.1", false},
{"2.5.1", prMajor, "alpha.2", false},
{"2.5.1", prMinor, "alpha.2.6", false},
{"2.5.1", prPatch, "alpha.2.5.2", false},
}

for _, test := range tests {
Expand Down
10 changes: 9 additions & 1 deletion internal/semver/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ type SemVersion struct {

// String returns the version string
func (v *SemVersion) String() string {
if v == nil {
return ""
}
version := v.Version.String()
if v.PreReleaseVersion != nil && v.PreReleaseVersion.String() != "" {
version += "-" + v.PreReleaseVersion.String()
Expand All @@ -79,6 +82,7 @@ func (v *SemVersion) Bump(part VersionPart, preReleaseLabels []string, buildLabe
var version *Version
var preReleaseVersion *PreReleaseVersion
var build *Build
var err error
versionPart := versionPartInt(part)
if versionPart >= vMajor && versionPart <= vPatch {
// bump the root version
Expand All @@ -88,7 +92,11 @@ func (v *SemVersion) Bump(part VersionPart, preReleaseLabels []string, buildLabe
preReleaseVersion = NewPrereleaseVersion("", 0, 0, 0)
} else if versionPart >= prNext && versionPart <= prPatch {
version = NewVersion(v.Version.major, v.Version.minor, v.Version.patch)
preReleaseVersion, _ = v.PreReleaseVersion.Bump(versionPart, preReleaseLabels)
preReleaseVersion, err = v.PreReleaseVersion.Bump(versionPart, preReleaseLabels)
if err != nil {
fmt.Printf("Error: %v\n", err)
return nil, err
}

} else if versionPart == prBuild {
version = NewVersion(v.Version.major, v.Version.minor, v.Version.patch)
Expand Down
40 changes: 30 additions & 10 deletions internal/versionbump.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ func (vb *VersionBump) ShowVersion() {
fmt.Println(vb.Config.Version)
}

func checkBumpError(v *semver.SemVersion, err error) string {
if err != nil {
return "Not Possible"
} else {
return v.String()
}
}

func (vb *VersionBump) Show(versionStr string) error {
var curVersionStr string
isProject := false
Expand All @@ -100,14 +108,26 @@ func (vb *VersionBump) Show(versionStr string) error {
curVersion.String()))
}
// we now know we have a valid Version
majorVersion, _ := curVersion.Bump(semver.Major, vb.Config.PreReleaseLabels, vb.Config.BuildLabel)
majorVersion, err := curVersion.Bump(semver.Major, vb.Config.PreReleaseLabels, vb.Config.BuildLabel)
majorVersionStr := checkBumpError(majorVersion, err)
minorVersion, _ := curVersion.Bump(semver.Minor, vb.Config.PreReleaseLabels, vb.Config.BuildLabel)
minorVersionStr := checkBumpError(minorVersion, err)
patchVersion, _ := curVersion.Bump(semver.Patch, vb.Config.PreReleaseLabels, vb.Config.BuildLabel)
patchVersionStr := checkBumpError(patchVersion, err)
prNextVersion, _ := curVersion.Bump(semver.PreReleaseNext, vb.Config.PreReleaseLabels, vb.Config.BuildLabel)
prNextVersionStr := checkBumpError(prNextVersion, err)
prMajorVersion, _ := curVersion.Bump(semver.PreReleaseMajor, vb.Config.PreReleaseLabels, vb.Config.BuildLabel)
prMajorVersionStr := checkBumpError(prMajorVersion, err)
prMinorVersion, _ := curVersion.Bump(semver.PreReleaseMinor, vb.Config.PreReleaseLabels, vb.Config.BuildLabel)
prMinorVersionStr := checkBumpError(prMinorVersion, err)
prPatchVersion, _ := curVersion.Bump(semver.PreReleasePatch, vb.Config.PreReleaseLabels, vb.Config.BuildLabel)
prPatchVersionStr := checkBumpError(prPatchVersion, err)
prBuildVersion, _ := curVersion.Bump(semver.PreReleaseBuild, vb.Config.PreReleaseLabels, vb.Config.BuildLabel)
prBuildVersionStr := checkBumpError(prBuildVersion, err)

if prNextVersionStr == "" {
prNextVersionStr = "ERROR_NO_PATH"
}

padLen := len(curVersion.String())
padding := utils.PaddingString(padLen, " ")
Expand All @@ -116,28 +136,28 @@ func (vb *VersionBump) Show(versionStr string) error {
`%s ─┬─ major ─ %s
%s├─ minor ─ %s
%s├─ patch ─ %s
%s├─ prerelease-next ── %s
%s├─ prerelease ─ %s
%s├─ prerelease-major ─ %s
%s├─ prerelease-minor ─ %s
%s├─ prerelease-patch ─ %s
%s╰─ prerelease-build ─ %s
`,
curVersion.String(),
majorVersion.String(),
majorVersionStr,
padding,
minorVersion.String(),
minorVersionStr,
padding,
patchVersion.String(),
patchVersionStr,
padding,
prNextVersion.String(),
prNextVersionStr,
padding,
prMajorVersion.String(),
prMajorVersionStr,
padding,
prMinorVersion.String(),
prMinorVersionStr,
padding,
prPatchVersion.String(),
prPatchVersionStr,
padding,
prBuildVersion.String())
prBuildVersionStr)

printColorOpts(vb.Options, tree, ColorLightBlue)
return nil
Expand Down

0 comments on commit 655b451

Please sign in to comment.