Skip to content

Commit

Permalink
Add release bump strategy and related unit tests (#9)
Browse files Browse the repository at this point in the history
* Add `release` bump strategy and related unit tests

Also add unit tests for pre-new-* type strategies

* bump version 1.0.0-alpha --> 0.7.0-alpha

* Update release workflow to include 'release' bump type and install versionbump v0.7.0-alpha

---------

Co-authored-by: kylie-bee <[email protected]>
  • Loading branch information
kylie-bee and kylie-bee authored Jan 14, 2025
1 parent 0ac04ee commit d70b5d7
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 21 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ on:
workflow_dispatch: # Manual trigger, adjust as needed
inputs:
bumpType:
description: 'Version bump type'
description: "Version bump type"
required: true
type: choice
options:
- major
- minor
- patch
- release
- pre
- new-pre-major
- new-pre-minor
Expand All @@ -34,11 +35,11 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22.2'
go-version: "1.22.2"

- name: Install versionbump
run: |
go install github.com/ptgoetz/go-versionbump/cmd/versionbump@v0.6.0-alpha.2
go install github.com/ptgoetz/go-versionbump/cmd/versionbump@v0.7.0-alpha
echo "Versionbump installed successfully."
- name: Bump version
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Variables
APP_NAME := "versionbump"
VERSION := "v1.0.0-alpha"
VERSION := "v0.7.0-alpha"
GO_FILES := $(shell find . -name '*.go' | grep -v /vendor/)
PKG := ./...
INTEGRATION_TEST_DIR := ./test/integration
Expand Down
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![VersionBump Gopher](assets/versionbump_gopher-250.png)

**Latest Version:** v1.0.0-alpha ([Install](#installation))
**Latest Version:** v0.7.0-alpha ([Install](#installation))

VersionBump is a powerful command-line tool designed to streamline the process of version management in your projects.
By automating version bumping, VersionBump ensures that your project’s version numbers are always up-to-date across all
Expand Down Expand Up @@ -43,6 +43,7 @@ relevant files, reducing the risk of human error and saving you valuable time.
0.0.0 ─┬─ major ─ 1.0.0
├─ minor ─ 0.1.0
├─ patch ─ 0.0.1
├─ release ─ 0.0.0
├─ new-pre-major ─ 1.0.0-alpha
├─ new-pre-minor ─ 0.1.0-alpha
├─ new-pre-patch ─ 0.0.1-alpha
Expand Down Expand Up @@ -104,6 +105,7 @@ relevant files, reducing the risk of human error and saving you valuable time.
0.0.1-alpha ─┬─ major ─ 1.0.0
├─ minor ─ 0.1.0
├─ patch ─ 0.0.2
├─ release ─ 0.0.1
├─ new-pre-major ─ 1.0.0-alpha
├─ new-pre-minor ─ 0.1.0-alpha
├─ new-pre-patch ─ 0.0.2-alpha
Expand Down Expand Up @@ -202,7 +204,7 @@ go install github.com/ptgoetz/go-versionbump/cmd/versionbump@latest

### Without Go
If you don't have Go installed and just want the binary executable, you can download a prebuilt binaries from
[here](https://github.com/ptgoetz/go-versionbump/releases/tag/v1.0.0-alpha).
[here](https://github.com/ptgoetz/go-versionbump/releases/tag/v0.7.0-alpha).

VersionBump binary distribution archives include the `README.md` and `versionbump[.exe]` files:

Expand Down Expand Up @@ -259,6 +261,7 @@ Available Commands:
new-pre-minor Bump the minor version and apply the first pre-release label (e.g. 1.2.3 -> 1.3.0-alpha).
new-pre-patch Bump the patch version and apply the first pre-release label (e.g. 1.2.3 -> 1.2.4-alpha).
patch Bump the patch version number (e.g. 1.2.3 -> 1.2.4).
release Bump the pre-release version to a release version (e.g. 1.2.3-alpha -> 1.2.3).
pre Bump the next pre-release version label (e.g. 1.2.3-alpha -> 1.2.3-beta).
pre-build Bump the pre-release build version number (e.g. 1.2.3 -> 1.2.3+build.1).
pre-major Bump the pre-release major version number (e.g. 1.2.3-alpha -> 1.2.3-alpha.1).
Expand All @@ -270,7 +273,7 @@ Available Commands:

```

The commands `major`, `minor` `patch`, `set`, `new-pre-major`, `new-pre-minor`, `new-pre-patch`, `pre`, `pre-major`,
The commands `major`, `minor` `patch`, `release`, `set`, `new-pre-major`, `new-pre-minor`, `new-pre-patch`, `pre`, `pre-major`,
`pre-minor`, `pre-patch` and `pre-build` support the following flags:'
- `-c`, `-config`: Path to the configuration file (default: `./versionbump.yaml`).
- `-no-prompt`: Do not prompt the user for confirmation before making changes.
Expand Down Expand Up @@ -370,7 +373,7 @@ and perform an initial commit before continuing.

```console
$ versionbump patch
VersionBump v1.0.0-alpha
VersionBump v0.7.0-alpha
Configuration file: versionbump.yaml
Project root directory: /Users/tgoetz/Projects/ptgoetz/test-project
Checking git configuration...
Expand Down Expand Up @@ -495,6 +498,7 @@ Without parameters, the `show` command will display the potential versioning pat
$ 0.6.0-alpha.1 ─┬─ major ─ 1.0.0
├─ minor ─ 0.7.0
├─ patch ─ 0.6.1
├─ release ─ 0.6.0
├─ pre-new-major ─ 1.0.0-alpha
├─ pre-new-minor ─ 0.7.0-alpha
├─ pre-new-patch ─ 0.6.1-alpha
Expand All @@ -512,6 +516,7 @@ Potential versioning paths for version: 0.4.1-alpha
0.4.1-alpha ─┬─ major ─ 1.0.0
├─ minor ─ 0.5.0
├─ patch ─ 0.4.2
├─ release ─ 0.4.1
├─ pre-new-major ─ 1.0.0-alpha
├─ pre-new-minor ─ 0.5.0-alpha
├─ pre-new-patch ─ 0.4.2-alpha
Expand All @@ -531,6 +536,9 @@ considered valid semantic version numbers.
```console
$ versionbump history
version History:
- 0.7.0
- 0.7.0-alpha
- 0.6.0
- 0.6.0-alpha.1
- 0.6.0-alpha
- 0.5.4
Expand Down
18 changes: 16 additions & 2 deletions cmd/versionbump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package main

import (
"fmt"
"os"

"github.com/ptgoetz/go-versionbump/internal"
vbc "github.com/ptgoetz/go-versionbump/internal/config"
"github.com/ptgoetz/go-versionbump/pkg/semver"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"os"
)

func main() {
Expand Down Expand Up @@ -131,7 +132,7 @@ var gitTagHistoryCmd = &cobra.Command{
var preReleaseNextCmd = &cobra.Command{
Use: semver.PreRelease.String(),
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).`,
Long: `Bump the next pre-release version label (e.g. 1.2.3-alpha -> 1.2.3-beta).`,
RunE: bumpPreReleaseNext, // Use RunE for better error handling
}

Expand Down Expand Up @@ -177,6 +178,13 @@ var preReleaseNewPatchCmd = &cobra.Command{
RunE: bumpNewPreReleasePatch, // Use RunE for better error handling
}

var releaseCmd = &cobra.Command{
Use: semver.Release.String(),
Short: `Bump the pre-release version to a release version (e.g. 1.2.3-alpha -> 1.2.3).`,
Long: `Bump the pre-release version to a release version (e.g. 1.2.3-alpha -> 1.2.3).`,
RunE: bumpRelease, // Use RunE for better error handling
}

var preReleaseBuildCmd = &cobra.Command{
Use: semver.PreReleaseBuild.String(),
Short: `Bump the pre-release build version number (e.g. 1.2.3 -> 1.2.3+build.1).`,
Expand Down Expand Up @@ -216,6 +224,7 @@ func init() {
preReleaseNewMajorCmd.Flags().AddFlagSet(prereleaserFlags)
preReleaseNewMinorCmd.Flags().AddFlagSet(prereleaserFlags)
preReleaseNewPatchCmd.Flags().AddFlagSet(prereleaserFlags)
releaseCmd.Flags().AddFlagSet(prereleaserFlags)

showCmd.Flags().AddFlagSet(configColorFlags)
showVersionCmd.Flags().AddFlagSet(commonFlags)
Expand All @@ -239,6 +248,7 @@ func init() {
rootCmd.AddCommand(preReleaseNewMajorCmd)
rootCmd.AddCommand(preReleaseNewMinorCmd)
rootCmd.AddCommand(preReleaseNewPatchCmd)
rootCmd.AddCommand(releaseCmd)
rootCmd.AddCommand(showCmd)
rootCmd.AddCommand(showVersionCmd)
rootCmd.AddCommand(showLatestCmd)
Expand Down Expand Up @@ -299,6 +309,10 @@ func bumpNewPreReleasePatch(cmd *cobra.Command, args []string) error {
return runVersionBump(semver.PreReleaseNewPatch)
}

func bumpRelease(cmd *cobra.Command, args []string) error {
return runVersionBump(semver.Release)
}

func runResetCmd(cmd *cobra.Command, args []string) error {
opts.ResetVersion = args[0]
vb, err := internal.NewVersionBump(opts)
Expand Down
17 changes: 12 additions & 5 deletions internal/versionbump.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package internal
import (
"bufio"
"fmt"
"os"
"path"
"strings"
"text/template"

"github.com/ptgoetz/go-versionbump/internal/config"
"github.com/ptgoetz/go-versionbump/internal/git"
"github.com/ptgoetz/go-versionbump/internal/utils"
vbu "github.com/ptgoetz/go-versionbump/internal/utils"
"github.com/ptgoetz/go-versionbump/pkg/semver"
"gopkg.in/yaml.v2"
"os"
"path"
"strings"
"text/template"
)

const Version = "1.0.0-alpha"
const Version = "0.7.0-alpha"

// VersionBump represents the Version bump operation.
type VersionBump struct {
Expand Down Expand Up @@ -116,6 +117,9 @@ func (vb *VersionBump) Show(versionStr string) error {
patchVersion, err := curVersion.Bump(semver.Patch, vb.Config.PreReleaseLabels, vb.Config.BuildLabel)
patchVersionStr := checkBumpError(vb, patchVersion, err)

releaseVersion, err := curVersion.Bump(semver.Release, vb.Config.PreReleaseLabels, vb.Config.BuildLabel)
releaseVersionStr := checkBumpError(vb, releaseVersion, err)

prNextVersion, err := curVersion.Bump(semver.PreRelease, vb.Config.PreReleaseLabels, vb.Config.BuildLabel)
prNextVersionStr := checkBumpError(vb, prNextVersion, err)

Expand Down Expand Up @@ -147,6 +151,7 @@ func (vb *VersionBump) Show(versionStr string) error {
`%s ─┬─ major ─ %s
%s├─ minor ─ %s
%s├─ patch ─ %s
%s├─ release ─ %s
%s├─ new-pre-major ─ %s
%s├─ new-pre-minor ─ %s
%s├─ new-pre-patch ─ %s
Expand All @@ -163,6 +168,8 @@ func (vb *VersionBump) Show(versionStr string) error {
padding,
patchVersionStr,
padding,
releaseVersionStr,
padding,
prNewMajorVersionStr,
padding,
prNewMinorVersionStr,
Expand Down
13 changes: 11 additions & 2 deletions pkg/semver/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ package semver

import (
"fmt"
"github.com/ptgoetz/go-versionbump/internal/utils"
"sort"
"strings"

"github.com/ptgoetz/go-versionbump/internal/utils"
)

const (
vMajor = iota
vMinor
vPatch
vRelease
prNext
prMajor
prNewMajor
Expand All @@ -30,6 +32,7 @@ const (
Major BumpStrategy = "major"
Minor BumpStrategy = "minor"
Patch BumpStrategy = "patch"
Release BumpStrategy = "release"
PreRelease BumpStrategy = "pre"
PreReleaseMajor BumpStrategy = "pre-major"
PreReleaseMinor BumpStrategy = "pre-minor"
Expand All @@ -52,6 +55,8 @@ func versionPartInt(part BumpStrategy) int {
return vMinor
case Patch:
return vPatch
case Release:
return vRelease
case PreRelease:
return prNext
case PreReleaseMajor:
Expand Down Expand Up @@ -128,7 +133,7 @@ func (v *SemanticVersion) Bump(strategy BumpStrategy, preReleaseLabels []string,
preReleaseVersion = newPrereleaseVersion("", 0, 0, 0)
case versionPart >= prNewMajor && versionPart <= prNewPatch:
// bump the root version
version = v.rootVersion.bump(versionPart - 5)
version = v.rootVersion.bump(versionPart - 6)
// reset all pre-release versions
preReleaseVersion = newPrereleaseVersion(preReleaseLabels[0], 0, 0, 0)
case versionPart >= prNext && versionPart <= prPatch:
Expand All @@ -145,6 +150,10 @@ func (v *SemanticVersion) Bump(strategy BumpStrategy, preReleaseLabels []string,
} else {
build = newBuild(buildLabel, 1)
}
case versionPart == vRelease:
version = v.rootVersion
preReleaseVersion = nil
build = nil
default:
return nil, fmt.Errorf("invalid version strategy: %d", versionPart)
}
Expand Down
14 changes: 11 additions & 3 deletions pkg/semver/semver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package semver

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
sv "golang.org/x/mod/semver"
"testing"
)

func TestParseSemVersion(t *testing.T) {
Expand Down Expand Up @@ -57,13 +58,20 @@ func TestSemVersion_Bump(t *testing.T) {
{"0.0.1", Minor, "0.1.0", false},
{"0.1.0", Major, "1.0.0", false},
{"1.0.0", PreRelease, "1.0.0-alpha", false},
{"1.0.0-alpha+ptgoetz.1", PreRelease, "1.0.0-beta", false},
{"1.0.0", PreReleaseMajor, "1.0.0-alpha", false},
{"1.0.0-beta", PreReleaseMajor, "1.0.0-beta.1", false},
{"1.0.0-alpha", PreReleaseBuild, "1.0.0-alpha+ptgoetz.1", false},
{"1.0.0-alpha+ptgoetz.1", PreReleaseBuild, "1.0.0-alpha+ptgoetz.2", false},
{"1.0.0-alpha+ptgoetz.1", PreRelease, "1.0.0-beta", false},
{"1.0.0-beta", PreReleasePatch, "1.0.0-beta.0.0.1", false},
{"1.0.0-beta", PreReleaseMinor, "1.0.0-beta.0.1", false},
{"1.0.0-beta", PreReleaseMajor, "1.0.0-beta.1", false},
{"1.0.0", PreReleaseNewMajor, "2.0.0-alpha", false},
{"1.0.0-alpha", PreReleaseNewMajor, "2.0.0-alpha", false},
{"1.0.0", PreReleaseNewMinor, "1.1.0-alpha", false},
{"1.0.0-alpha", PreReleaseNewMinor, "1.1.0-alpha", false},
{"1.0.0", PreReleaseNewPatch, "1.0.1-alpha", false},
{"1.0.0-alpha", PreReleaseNewPatch, "1.0.1-alpha", false},
{"1.0.0-beta", Release, "1.0.0", false},
}

for _, test := range tests {
Expand Down
2 changes: 1 addition & 1 deletion versionbump.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "1.0.0-alpha" # The current version of the project.
version: "0.7.0-alpha" # The current version of the project.
git-commit: true # Whether to create a git commit for the version bump.
git-tag: true # Whether to create a git tag for the version bump.
git-sign: false # Whether to sign the git tag.
Expand Down

0 comments on commit d70b5d7

Please sign in to comment.