Skip to content

Commit

Permalink
Move tag increment to strategy package
Browse files Browse the repository at this point in the history
  • Loading branch information
gandarez committed Jan 24, 2024
1 parent 97734a9 commit c970762
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 171 deletions.
29 changes: 3 additions & 26 deletions cmd/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ func Tag(params Params, gc git.Git) (Result, error) {
return Result{}, fmt.Errorf("failed to decide branching strategy: %s", err)
}

log.Debugf("using branching strategy: %q\n", branchingStrategy.Name())

method, version := branchingStrategy.DetermineBumpStrategy(source, dest)

log.Debugf("method: %q, version: %q", method, version)
Expand Down Expand Up @@ -110,36 +112,11 @@ func Tag(params Params, gc git.Git) (Result, error) {
tag = params.BaseVersion
}

if (version == "major" && method == "build") || method == "major" {
log.Debug("incrementing major")

if err := tag.IncrementMajor(); err != nil {
return Result{}, fmt.Errorf("failed to increment major version: %s", err)
}
}

if (version == "minor" && method == "build") || method == "minor" {
log.Debug("incrementing minor")

if err := tag.IncrementMinor(); err != nil {
return Result{}, fmt.Errorf("failed to increment minor version: %s", err)
}
}

if (version == "patch" && method == "build") || method == "patch" || method == "hotfix" {
log.Debug("incrementing patch")

if err := tag.IncrementPatch(); err != nil {
return Result{}, fmt.Errorf("failed to increment patch version: %s", err)
}
}

result, err := branchingStrategy.Tag(strategy.TagParams{
DestBranch: dest,
Method: method,
Prefix: params.Prefix,
PrereleaseID: params.PrereleaseID,
PreviousTag: previousTag,
Tag: tag,
Version: version,
}, gc)
Expand All @@ -150,7 +127,7 @@ func Tag(params Params, gc git.Git) (Result, error) {
log.Debugf("result: %+v\n", result)

return Result{
PreviousTag: result.PreviousTag,
PreviousTag: previousTag,
AncestorTag: result.AncestorTag,
SemverTag: result.SemverTag,
IsPrerelease: result.IsPrerelease,
Expand Down
31 changes: 30 additions & 1 deletion internal/strategy/gitflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strconv"

"github.com/apex/log"
"github.com/gandarez/semver-action/internal/regex"
"github.com/gandarez/semver-action/pkg/git"

Expand Down Expand Up @@ -77,6 +78,30 @@ func (g *GitFlow) Tag(params TagParams, gc git.Git) (Result, error) {
isPrerelease bool
)

if (params.Version == "major" && params.Method == "build") || params.Method == "major" {
log.Debug("incrementing major")

if err := params.Tag.IncrementMajor(); err != nil {
return Result{}, fmt.Errorf("failed to increment major version: %s", err)
}
}

if (params.Version == "minor" && params.Method == "build") || params.Method == "minor" {
log.Debug("incrementing minor")

if err := params.Tag.IncrementMinor(); err != nil {
return Result{}, fmt.Errorf("failed to increment minor version: %s", err)
}
}

if (params.Version == "patch" && params.Method == "build") || params.Method == "patch" || params.Method == "hotfix" {
log.Debug("incrementing patch")

if err := params.Tag.IncrementPatch(); err != nil {
return Result{}, fmt.Errorf("failed to increment patch version: %s", err)
}
}

switch params.Method {
case "build":
{
Expand Down Expand Up @@ -124,9 +149,13 @@ func (g *GitFlow) Tag(params TagParams, gc git.Git) (Result, error) {
}

return Result{
PreviousTag: params.PreviousTag,
AncestorTag: gc.AncestorTag(includePattern, excludePattern, params.DestBranch),
SemverTag: finalTag,
IsPrerelease: isPrerelease,
}, nil
}

// Name returns the name of the strategy.
func (GitFlow) Name() string {
return "git-flow"
}
105 changes: 45 additions & 60 deletions internal/strategy/gitflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,105 +123,91 @@ func TestDetermineBumpStrategy_Gitflow(t *testing.T) {
func TestTag_Gitflow(t *testing.T) {
tests := map[string]struct {
Method string
PreviousTag string
AncestorTag string
Tag *semver.Version
Version string
Expected strategy.Result
}{
"method build": {
"build": {
Method: "build",
Tag: newSemVerPtr(t, "1.2.3-alpha.0"),
PreviousTag: "v1.2.2-alpha.0",
AncestorTag: "v1.2.3-alpha.0",
Tag: newSemVerPtr(t, "1.2.3-alpha.2"),
AncestorTag: "v1.2.3-alpha.1",
Expected: strategy.Result{
PreviousTag: "v1.2.2-alpha.0",
AncestorTag: "v1.2.3-alpha.0",
SemverTag: "v1.2.3-alpha.1",
AncestorTag: "v1.2.3-alpha.1",
SemverTag: "v1.2.3-alpha.3",
IsPrerelease: true,
},
},
"method major with pre release tag": {
Method: "major",
Tag: newSemVerPtr(t, "2.0.0-alpha.0"),
PreviousTag: "v1.2.3-alpha.0",
AncestorTag: "v2.0.0-alpha.1",
"major with pre release tag": {
Method: "build",
Version: "major",
Tag: newSemVerPtr(t, "1.2.3-alpha.1"),
AncestorTag: "v1.2.2-alpha.1",
Expected: strategy.Result{
PreviousTag: "v1.2.3-alpha.0",
AncestorTag: "v2.0.0-alpha.1",
SemverTag: "v2.0.0-alpha.0",
AncestorTag: "v1.2.2-alpha.1",
SemverTag: "v2.0.0-alpha.1",
IsPrerelease: true,
},
},
"method major without pre release tag": {
"major without pre release tag": {
Method: "major",
Tag: newSemVerPtr(t, "2.0.0"),
PreviousTag: "v1.2.3",
AncestorTag: "v2.0.0",
Tag: newSemVerPtr(t, "1.2.3"),
AncestorTag: "v1.2.2",
Expected: strategy.Result{
PreviousTag: "v1.2.3",
AncestorTag: "v2.0.0",
AncestorTag: "v1.2.2",
SemverTag: "v2.0.0",
IsPrerelease: false,
},
},
"method minor with pre release tag": {
Method: "minor",
Tag: newSemVerPtr(t, "1.3.0-alpha.0"),
PreviousTag: "v1.2.3-alpha.0",
AncestorTag: "v1.3.0-alpha.0",
"minor with pre release tag": {
Method: "build",
Version: "minor",
Tag: newSemVerPtr(t, "1.2.3-alpha.1"),
AncestorTag: "v1.2.2-alpha.1",
Expected: strategy.Result{
PreviousTag: "v1.2.3-alpha.0",
AncestorTag: "v1.3.0-alpha.0",
SemverTag: "v1.3.0-alpha.0",
AncestorTag: "v1.2.2-alpha.1",
SemverTag: "v1.3.0-alpha.1",
IsPrerelease: true,
},
},
"method ninor without pre release tag": {
Method: "major",
Tag: newSemVerPtr(t, "1.3.0"),
PreviousTag: "v1.2.3",
AncestorTag: "v1.3.0",
"minor without pre release tag": {
Method: "minor",
Tag: newSemVerPtr(t, "1.2.3"),
AncestorTag: "v1.2.2",
Expected: strategy.Result{
PreviousTag: "v1.2.3",
AncestorTag: "v1.3.0",
AncestorTag: "v1.2.2",
SemverTag: "v1.3.0",
IsPrerelease: false,
},
},
"method patch with pre release tag": {
Method: "patch",
Tag: newSemVerPtr(t, "1.2.1-alpha.0"),
PreviousTag: "v1.2.0-alpha.0",
AncestorTag: "v1.2.1-alpha.0",
"patch with pre release tag": {
Method: "build",
Version: "patch",
Tag: newSemVerPtr(t, "1.2.3-alpha.1"),
AncestorTag: "v1.2.2-alpha.1",
Expected: strategy.Result{
PreviousTag: "v1.2.0-alpha.0",
AncestorTag: "v1.2.1-alpha.0",
SemverTag: "v1.2.1-alpha.0",
AncestorTag: "v1.2.2-alpha.1",
SemverTag: "v1.2.4-alpha.1",
IsPrerelease: true,
},
},
"method patch without pre release tag": {
"patch without pre release tag": {
Method: "patch",
Tag: newSemVerPtr(t, "1.2.1"),
PreviousTag: "v1.2.0",
AncestorTag: "v1.2.1",
Tag: newSemVerPtr(t, "1.2.3"),
AncestorTag: "v1.2.2",
Expected: strategy.Result{
PreviousTag: "v1.2.0",
AncestorTag: "v1.2.1",
SemverTag: "v1.2.1",
AncestorTag: "v1.2.2",
SemverTag: "v1.2.4",
IsPrerelease: false,
},
},
"method final": {
"final version": {
Method: "final",
Tag: newSemVerPtr(t, "1.3.0-alpha.1"),
PreviousTag: "v1.2.1-alpha.0",
AncestorTag: "v1.3.0-alpha.0",
Tag: newSemVerPtr(t, "1.2.3-alpha.1"),
AncestorTag: "v1.2.2-alpha.1",
Expected: strategy.Result{
PreviousTag: "v1.2.1-alpha.0",
AncestorTag: "v1.3.0-alpha.0",
SemverTag: "v1.3.0",
AncestorTag: "v1.2.2-alpha.1",
SemverTag: "v1.2.3",
IsPrerelease: false,
},
},
Expand All @@ -240,7 +226,6 @@ func TestTag_Gitflow(t *testing.T) {
Prefix: "v",
PrereleaseID: "alpha",
Method: test.Method,
PreviousTag: test.PreviousTag,
Tag: test.Tag,
Version: test.Version,
}, gc)
Expand Down
3 changes: 1 addition & 2 deletions internal/strategy/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type (
Strategy interface {
DetermineBumpStrategy(sourceBranch, destBranch string) (string, string)
Tag(params TagParams, gc git.Git) (Result, error)
Name() string
}

// Configuration contains the strategy configuration.
Expand All @@ -36,14 +37,12 @@ type (
Method string
Prefix string
PrereleaseID string
PreviousTag string
Tag *semver.Version
Version string
}

// Result contains the result of strategy execution.
Result struct {
PreviousTag string
AncestorTag string
SemverTag string
IsPrerelease bool
Expand Down
46 changes: 40 additions & 6 deletions internal/strategy/trunkbased.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package strategy

import (
"fmt"
"strconv"

"github.com/apex/log"
"github.com/gandarez/semver-action/internal/regex"
"github.com/gandarez/semver-action/pkg/git"

Expand Down Expand Up @@ -34,17 +36,17 @@ func (t *TrunkBased) DetermineBumpStrategy(sourceBranch, destBranch string) (str

// bugfix into main branch
if t.patchPattern.MatchString(sourceBranch) && destBranch == t.branchName {
return "build", "patch"
return "patch", ""
}

// feature into main branch
if t.minorPattern.MatchString(sourceBranch) && destBranch == t.branchName {
return "build", "minor"
return "minor", ""
}

// major into main branch
if t.majorPattern.MatchString(sourceBranch) && destBranch == t.branchName {
return "build", "major"
return "major", ""
}

// build into main branch
Expand Down Expand Up @@ -75,16 +77,48 @@ func (t *TrunkBased) Tag(params TagParams, gc git.Git) (Result, error) {

finalTag = params.Prefix + params.Tag.String()
}
case "major", "minor", "patch":
finalTag = params.Prefix + params.Tag.FinalizeVersion()
case "major":
{
log.Debug("incrementing major")

if err := params.Tag.IncrementMajor(); err != nil {
return Result{}, fmt.Errorf("failed to increment major version: %s", err)
}

finalTag = params.Prefix + params.Tag.FinalizeVersion()
}
case "minor":
{
log.Debug("incrementing minor")

if err := params.Tag.IncrementMinor(); err != nil {
return Result{}, fmt.Errorf("failed to increment minor version: %s", err)
}

finalTag = params.Prefix + params.Tag.FinalizeVersion()
}
case "patch":
{
log.Debug("incrementing patch")

if err := params.Tag.IncrementPatch(); err != nil {
return Result{}, fmt.Errorf("failed to increment patch version: %s", err)
}

finalTag = params.Prefix + params.Tag.FinalizeVersion()
}
default:
finalTag = params.Prefix + params.Tag.FinalizeVersion()
}

return Result{
PreviousTag: params.PreviousTag,
AncestorTag: "",
SemverTag: finalTag,
IsPrerelease: false,
}, nil
}

// Name returns the name of the strategy.
func (TrunkBased) Name() string {
return "trunk-based"
}
Loading

0 comments on commit c970762

Please sign in to comment.