Skip to content

Commit

Permalink
fix bug in version 2 (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
walteh authored Nov 18, 2023
2 parents c0c4b7e + 79f7f78 commit ec1f774
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 237 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/simver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
{
name: install simver,
run: "go install github.com/walteh/simver/cmd/simver_github_actions@v0.2.0",
run: "go install github.com/walteh/simver/cmd/simver_github_actions@v0.4.1-pr2+1",
},
{
name: run simver,
Expand Down
2 changes: 1 addition & 1 deletion .vscode/simver.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
VSCODE
////////////////////////////////////////////// */
"workbench.tree.indent": 16,
"editor.defaultFormatter": "esbenp.prettier-vscode",
// "editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"search.useIgnoreFiles": true,
"debug.console.wordWrap": false,
Expand Down
10 changes: 7 additions & 3 deletions calculate.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func (me *Calculation) CalculateNewTagsRaw(ctx context.Context) *CalculationOutp

mrlt := string(me.MostRecentLiveTag)

matching := mmrt == mrlt

// first we check to see if mrlt exists, if not we set it to the base
if mrlt == "" {
mrlt = baseTag
Expand All @@ -70,7 +72,7 @@ func (me *Calculation) CalculateNewTagsRaw(ctx context.Context) *CalculationOutp
validMmrt := false

// first we validate that mmrt is still valid, which means it is greater than or equal to mrlt
if mmrt != "" && semver.Compare(mmrt, mrlt) > 0 {
if mmrt != "" && semver.Compare(mmrt, mrlt) >= 0 {
validMmrt = true
}

Expand All @@ -84,14 +86,16 @@ func (me *Calculation) CalculateNewTagsRaw(ctx context.Context) *CalculationOutp
if !validMmrt {
mmrt = nvt
// pr will be 0 if this is not a and is a push to the root branch
if me.PR != 0 {
if me.PR != 0 && !me.IsMerge {
out.RootTags = append(out.RootTags, nvt+"-reserved")
out.BaseTags = append(out.BaseTags, nvt+fmt.Sprintf("-pr%d+base", me.PR))
}
}

if me.IsMerge {
out.MergeTags = append(out.MergeTags, mmrt)
if !matching {
out.MergeTags = append(out.MergeTags, mmrt)
}
} else {
if me.PR == 0 {
out.HeadTags = append(out.HeadTags, mmrt)
Expand Down
195 changes: 109 additions & 86 deletions calculate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ import (
"context"
"testing"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
"github.com/walteh/simver"
)

func TestNewCalculationAndCalculateNewTags(t *testing.T) {
testCases := []struct {
name string
calculation *simver.Calculation
expectedBaseTags []string
expectedHeadTags []string
expectedRootTags []string
expectedMergeTags []string
name string
calculation *simver.Calculation
output *simver.CalculationOutput
}{
{
name: "expired mmrt",
Expand All @@ -28,16 +25,18 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
IsMerge: false,
ForcePatch: false,
},
expectedBaseTags: []string{
"v99.99.99-pr85+base",
output: &simver.CalculationOutput{
BaseTags: []string{
"v99.99.99-pr85+base",
},
HeadTags: []string{
"v99.99.99-pr85+34",
},
RootTags: []string{
"v99.99.99-reserved",
},
MergeTags: []string{},
},
expectedHeadTags: []string{
"v99.99.99-pr85+34",
},
expectedRootTags: []string{
"v99.99.99-reserved",
},
expectedMergeTags: []string{},
},
{
name: "missing all",
Expand All @@ -50,16 +49,19 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
IsMerge: false,
ForcePatch: false,
},
expectedBaseTags: []string{
"v3.3.3-pr1+base",
},
expectedHeadTags: []string{
"v3.3.3-pr1+2",
},
expectedRootTags: []string{
"v3.3.3-reserved",

output: &simver.CalculationOutput{
BaseTags: []string{
"v3.3.3-pr1+base",
},
HeadTags: []string{
"v3.3.3-pr1+2",
},
RootTags: []string{
"v3.3.3-reserved",
},
MergeTags: []string{},
},
expectedMergeTags: []string{},
},
{
name: "valid mmrt",
Expand All @@ -72,9 +74,13 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
IsMerge: false,
ForcePatch: false,
},
expectedBaseTags: []string{},
expectedHeadTags: []string{"v1.2.4-pr1+34"},
expectedRootTags: []string{},

output: &simver.CalculationOutput{
BaseTags: []string{},
HeadTags: []string{"v1.2.4-pr1+34"},
RootTags: []string{},
MergeTags: []string{},
},
},

{
Expand All @@ -88,16 +94,13 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
IsMerge: false,
ForcePatch: false,
},
expectedBaseTags: []string{
"v1.2.6-pr1+base",
},
expectedHeadTags: []string{
"v1.2.6-pr1+34",
},
expectedRootTags: []string{
"v1.2.6-reserved",

output: &simver.CalculationOutput{
BaseTags: []string{"v1.2.6-pr1+base"},
HeadTags: []string{"v1.2.6-pr1+34"},
RootTags: []string{"v1.2.6-reserved"},
MergeTags: []string{},
},
expectedMergeTags: []string{},
},
{
name: "valid mmrt with merge",
Expand All @@ -110,10 +113,12 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
IsMerge: true,
ForcePatch: false,
},
expectedBaseTags: []string{},
expectedHeadTags: []string{},
expectedRootTags: []string{},
expectedMergeTags: []string{"v1.2.4"},
output: &simver.CalculationOutput{
BaseTags: []string{},
HeadTags: []string{},
RootTags: []string{},
MergeTags: []string{"v1.2.4"},
},
},
{
name: "valid mmrt with force patch",
Expand All @@ -126,10 +131,12 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
IsMerge: false,
ForcePatch: true,
},
expectedBaseTags: []string{"v1.2.5-pr1+base"},
expectedHeadTags: []string{"v1.2.5-pr1+34"},
expectedRootTags: []string{"v1.2.5-reserved"},
expectedMergeTags: []string{},
output: &simver.CalculationOutput{
BaseTags: []string{"v1.2.5-pr1+base"},
HeadTags: []string{"v1.2.5-pr1+34"},
RootTags: []string{"v1.2.5-reserved"},
MergeTags: []string{},
},
},
{
name: "valid mmrt with force patch (merge override)",
Expand All @@ -142,10 +149,12 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
IsMerge: true,
ForcePatch: true,
},
expectedBaseTags: []string{},
expectedHeadTags: []string{},
expectedRootTags: []string{},
expectedMergeTags: []string{"v1.2.4"},
output: &simver.CalculationOutput{
BaseTags: []string{},
HeadTags: []string{},
RootTags: []string{},
MergeTags: []string{"v1.2.4"},
},
},
{
name: "expired mmrt with force patch",
Expand All @@ -158,16 +167,12 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
IsMerge: false,
ForcePatch: true,
},
expectedBaseTags: []string{
"v1.9.10-pr85+base",
},
expectedHeadTags: []string{
"v1.9.10-pr85+34",
},
expectedRootTags: []string{
"v1.9.10-reserved",
output: &simver.CalculationOutput{
BaseTags: []string{"v1.9.10-pr85+base"},
HeadTags: []string{"v1.9.10-pr85+34"},
RootTags: []string{"v1.9.10-reserved"},
MergeTags: []string{},
},
expectedMergeTags: []string{},
},

{
Expand All @@ -181,43 +186,61 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
NextValidTag: "v0.18.0",
PR: 13.000000,
},
expectedBaseTags: []string{
// "v0.18.0-pr13+base",
output: &simver.CalculationOutput{
BaseTags: []string{},
HeadTags: []string{"v0.17.3-pr13+2"},
RootTags: []string{},
MergeTags: []string{},
},
expectedHeadTags: []string{
"v0.17.3-pr13+2",
},
{
name: "when merging a branch that already is tagged correctly, don't do anything",
calculation: &simver.Calculation{
ForcePatch: false,
IsMerge: true,
MostRecentLiveTag: "v0.3.0",
MyMostRecentBuild: 1.000000,
MyMostRecentTag: "v0.3.0",
NextValidTag: "v0.4.0",
PR: 1.000000,
},
output: &simver.CalculationOutput{
BaseTags: []string{},
HeadTags: []string{},
RootTags: []string{},
MergeTags: []string{},
},
},
{
name: "when merging a branch that already is tagged correctly, don't do anything (ignoring force patch)",
calculation: &simver.Calculation{
ForcePatch: true,
IsMerge: true,
MostRecentLiveTag: "v0.2.0",
MyMostRecentBuild: 1.000000,
MyMostRecentTag: "v0.2.0",
NextValidTag: "v0.3.0",
PR: 1.000000,
},
expectedRootTags: []string{
// "v0.18.0-reserved",
output: &simver.CalculationOutput{
BaseTags: []string{},
HeadTags: []string{},
RootTags: []string{},
MergeTags: []string{},
},
expectedMergeTags: []string{},
},
// Add more test cases here...
}

ctx := context.Background()

for _, tc := range testCases {
for _, i := range []string{"base", "head", "root", "merge"} {
t.Run(tc.name+"_"+i, func(t *testing.T) {
out := tc.calculation.CalculateNewTagsRaw(ctx)
t.Run(tc.name, func(t *testing.T) {
out := tc.calculation.CalculateNewTagsRaw(ctx)
assert.ElementsMatch(t, tc.output.BaseTags, out.BaseTags, "base tags do not match")
assert.ElementsMatch(t, tc.output.HeadTags, out.HeadTags, "head tags do not match")
assert.ElementsMatch(t, tc.output.RootTags, out.RootTags, "root tags do not match")
assert.ElementsMatch(t, tc.output.MergeTags, out.MergeTags, "merge tags do not match")
})

if i == "base" {
require.NotContains(t, out.BaseTags, "", "Base tags contain empty string")
require.ElementsMatch(t, tc.expectedBaseTags, out.BaseTags, "Base tags do not match")
} else if i == "head" {
require.NotContains(t, out.HeadTags, "", "Head tags contain empty string")
require.ElementsMatch(t, tc.expectedHeadTags, out.HeadTags, "Head tags do not match")
} else if i == "root" {
require.NotContains(t, out.RootTags, "", "Root tags contain empty string")
require.ElementsMatch(t, tc.expectedRootTags, out.RootTags, "Root tags do not match")
} else if i == "merge" {
require.NotContains(t, out.MergeTags, "", "Merge tags contain empty string")
require.ElementsMatch(t, tc.expectedMergeTags, out.MergeTags, "Merge tags do not match")
} else {
require.Fail(t, "invalid test case")
}
})
}
}
}
10 changes: 5 additions & 5 deletions cmd/simver_github_actions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,17 @@ func main() {
os.Exit(1)
}

ee, _, keepgoing, err := simver.LoadExecution(ctx, tagprov, prr)
ee, _, err := simver.LoadExecution(ctx, tagprov, prr)
if err != nil {
zerolog.Ctx(ctx).Error().Err(err).Msgf("error loading execution")
fmt.Println(terrors.FormatErrorCaller(err))
os.Exit(1)
}

if !keepgoing {
zerolog.Ctx(ctx).Debug().Msg("execution is complete, likely because this is a push to a branch that is not main and not related to a PR")
os.Exit(0)
}
// if !keepgoing {
// zerolog.Ctx(ctx).Debug().Msg("execution is complete, likely because this is a push to a branch that is not main and not related to a PR")
// os.Exit(0)
// }

// isPush := os.Getenv("GITHUB_EVENT_NAME") == "push"

Expand Down
6 changes: 2 additions & 4 deletions execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

type Execution interface {
PR() int
IsMinor() bool
IsTargetingRoot() bool
IsMerge() bool
HeadCommitTags() Tags
HeadBranchTags() Tags
Expand All @@ -35,16 +35,14 @@ func Calculate(ctx context.Context, ex Execution) *Calculation {

maxlr := MaxLiveOrReservedTag(mrlt, mrrt)

// maxmr := MaxMyOrReservedTag(mrrt, mmrt)

return &Calculation{
IsMerge: ex.IsMerge(),
MostRecentLiveTag: mrlt,
ForcePatch: ForcePatch(ctx, ex, mmrt),
MyMostRecentTag: mmrt,
MyMostRecentBuild: MyMostRecentBuildNumber(ex),
PR: ex.PR(),
NextValidTag: GetNextValidTag(ctx, ex.IsMinor(), maxlr),
NextValidTag: GetNextValidTag(ctx, ex.IsTargetingRoot(), maxlr),
}
}

Expand Down
Loading

0 comments on commit ec1f774

Please sign in to comment.