From c97076259996613427a7dbfdee6423a10642ab4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Henrique=20Guard=C3=A3o=20Gandarez?= Date: Wed, 24 Jan 2024 17:31:09 -0300 Subject: [PATCH] Move tag increment to strategy package --- cmd/generate/generate.go | 29 +----- internal/strategy/gitflow.go | 31 ++++++- internal/strategy/gitflow_test.go | 105 ++++++++++------------ internal/strategy/strategy.go | 3 +- internal/strategy/trunkbased.go | 46 ++++++++-- internal/strategy/trunkbased_test.go | 130 +++++++++++---------------- 6 files changed, 173 insertions(+), 171 deletions(-) diff --git a/cmd/generate/generate.go b/cmd/generate/generate.go index 23b7fe5..9d61324 100644 --- a/cmd/generate/generate.go +++ b/cmd/generate/generate.go @@ -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) @@ -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) @@ -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, diff --git a/internal/strategy/gitflow.go b/internal/strategy/gitflow.go index 5482dca..b8570d6 100644 --- a/internal/strategy/gitflow.go +++ b/internal/strategy/gitflow.go @@ -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" @@ -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": { @@ -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" +} diff --git a/internal/strategy/gitflow_test.go b/internal/strategy/gitflow_test.go index 4c0b394..5f59ecf 100644 --- a/internal/strategy/gitflow_test.go +++ b/internal/strategy/gitflow_test.go @@ -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, }, }, @@ -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) diff --git a/internal/strategy/strategy.go b/internal/strategy/strategy.go index 7342a3d..a3c084b 100644 --- a/internal/strategy/strategy.go +++ b/internal/strategy/strategy.go @@ -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. @@ -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 diff --git a/internal/strategy/trunkbased.go b/internal/strategy/trunkbased.go index 855f824..9640368 100644 --- a/internal/strategy/trunkbased.go +++ b/internal/strategy/trunkbased.go @@ -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" @@ -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 @@ -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" +} diff --git a/internal/strategy/trunkbased_test.go b/internal/strategy/trunkbased_test.go index 8ca4907..474dfe0 100644 --- a/internal/strategy/trunkbased_test.go +++ b/internal/strategy/trunkbased_test.go @@ -13,57 +13,49 @@ import ( func TestDetermineBumpStrategy_TrunkBased(t *testing.T) { tests := map[string]struct { - SourceBranch string - DestBranch string - Bump string - ExcludePattern regex.Regex - ExpectedMethod string - ExpectedVersion string + SourceBranch string + DestBranch string + Bump string + ExcludePattern regex.Regex + ExpectedMethod string }{ "source branch bugfix, dest branch master and auto bump": { - SourceBranch: "bugfix/some", - DestBranch: "master", - Bump: "auto", - ExpectedMethod: "build", - ExpectedVersion: "patch", + SourceBranch: "bugfix/some", + DestBranch: "master", + Bump: "auto", + ExpectedMethod: "patch", }, "source branch feature, dest branch master and auto bump": { - SourceBranch: "feature/some", - DestBranch: "master", - Bump: "auto", - ExpectedMethod: "build", - ExpectedVersion: "minor", + SourceBranch: "feature/some", + DestBranch: "master", + Bump: "auto", + ExpectedMethod: "minor", }, "source branch major, dest branch master and auto bump": { - SourceBranch: "major/some", - DestBranch: "master", - Bump: "auto", - ExpectedMethod: "build", - ExpectedVersion: "major", + SourceBranch: "major/some", + DestBranch: "master", + Bump: "auto", + ExpectedMethod: "major", }, "source branch build, dest branch master and auto bump": { - SourceBranch: "doc/some", - DestBranch: "master", - Bump: "auto", - ExpectedMethod: "build", - ExpectedVersion: "", + SourceBranch: "doc/some", + DestBranch: "master", + Bump: "auto", + ExpectedMethod: "build", }, "source branch ignore": { - SourceBranch: "ignore/some", - ExcludePattern: regex.MustCompile(`(?i)^ignore/.+`), - ExpectedMethod: "", - ExpectedVersion: "", + SourceBranch: "ignore/some", + ExcludePattern: regex.MustCompile(`(?i)^ignore/.+`), + ExpectedMethod: "", }, "source branch ignore, no exclude pattern": { - SourceBranch: "ignore/some", - ExpectedMethod: "", - ExpectedVersion: "", + SourceBranch: "ignore/some", + ExpectedMethod: "", }, "source branch ignore, no exclude pattern and auto bump": { - SourceBranch: "ignore/some", - Bump: "auto", - ExpectedMethod: "build", - ExpectedVersion: "", + SourceBranch: "ignore/some", + Bump: "auto", + ExpectedMethod: "build", }, "not a valid source branch prefix and auto bump": { SourceBranch: "some-branch", @@ -101,76 +93,63 @@ func TestDetermineBumpStrategy_TrunkBased(t *testing.T) { method, version := branchingStrategy.DetermineBumpStrategy(test.SourceBranch, test.DestBranch) assert.Equal(t, test.ExpectedMethod, method) - assert.Equal(t, test.ExpectedVersion, version) + assert.Empty(t, version) }) } } func TestTag_Trunkbased(t *testing.T) { tests := map[string]struct { - Method string - PreviousTag string - Tag *semver.Version - Version string - Expected strategy.Result + Method string + Tag *semver.Version + Version string + Expected strategy.Result }{ - "method build": { - Method: "build", - Tag: newSemVerPtr(t, "1.2.3"), - PreviousTag: "v1.2.3", + "build": { + Method: "build", + Tag: newSemVerPtr(t, "1.2.3"), Expected: strategy.Result{ - PreviousTag: "v1.2.3", SemverTag: "v1.2.3+1", IsPrerelease: false, }, }, - "method build and previous tag contains build": { - Method: "build", - Tag: newSemVerPtr(t, "1.2.3+1"), - PreviousTag: "v1.2.3+1", + "build with previous tag containing build": { + Method: "build", + Tag: newSemVerPtr(t, "1.2.3+1"), Expected: strategy.Result{ - PreviousTag: "v1.2.3+1", SemverTag: "v1.2.3+2", IsPrerelease: false, }, }, - "method major": { - Method: "major", - Tag: newSemVerPtr(t, "1.2.3"), - PreviousTag: "v1.2.2", + "major": { + Method: "major", + Tag: newSemVerPtr(t, "1.2.3"), Expected: strategy.Result{ - PreviousTag: "v1.2.2", - SemverTag: "v1.2.3", + SemverTag: "v2.0.0", IsPrerelease: false, }, }, - "method minor": { - Method: "minor", - Tag: newSemVerPtr(t, "1.3.0"), - PreviousTag: "v1.2.3", + "minor": { + Method: "minor", + Tag: newSemVerPtr(t, "1.2.3"), Expected: strategy.Result{ - PreviousTag: "v1.2.3", SemverTag: "v1.3.0", IsPrerelease: false, }, }, - "method patch": { - Method: "patch", - Tag: newSemVerPtr(t, "1.2.4"), - PreviousTag: "v1.2.3", + "patch": { + Method: "patch", + Tag: newSemVerPtr(t, "1.2.3"), Expected: strategy.Result{ - PreviousTag: "v1.2.3", SemverTag: "v1.2.4", IsPrerelease: false, }, }, - "method final (not used in trunk based)": { - Method: "final", - Tag: newSemVerPtr(t, "1.2.4"), - PreviousTag: "v1.2.3", + "default": { + Method: "not-in-use", + Tag: newSemVerPtr(t, "1.2.3"), Expected: strategy.Result{ - PreviousTag: "v1.2.3", - SemverTag: "v1.2.4", + SemverTag: "v1.2.3", IsPrerelease: false, }, }, @@ -189,7 +168,6 @@ func TestTag_Trunkbased(t *testing.T) { Prefix: "v", PrereleaseID: "alpha", Method: test.Method, - PreviousTag: test.PreviousTag, Tag: test.Tag, Version: test.Version, }, gc)