diff --git a/.github/workflows/simver.yaml b/.github/workflows/simver.yaml index 13d8d87..2c504f4 100644 --- a/.github/workflows/simver.yaml +++ b/.github/workflows/simver.yaml @@ -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, diff --git a/.vscode/simver.code-workspace b/.vscode/simver.code-workspace index 15d6484..1b83529 100644 --- a/.vscode/simver.code-workspace +++ b/.vscode/simver.code-workspace @@ -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, diff --git a/calculate.go b/calculate.go index 5998aab..524bbdf 100644 --- a/calculate.go +++ b/calculate.go @@ -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 @@ -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 } @@ -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) diff --git a/calculate_test.go b/calculate_test.go index b9b5b9d..9a618f0 100644 --- a/calculate_test.go +++ b/calculate_test.go @@ -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", @@ -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", @@ -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", @@ -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{}, + }, }, { @@ -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", @@ -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", @@ -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)", @@ -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", @@ -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{}, }, { @@ -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") - } - }) - } } } diff --git a/cmd/simver_github_actions/main.go b/cmd/simver_github_actions/main.go index 6c4d440..40dec9b 100644 --- a/cmd/simver_github_actions/main.go +++ b/cmd/simver_github_actions/main.go @@ -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" diff --git a/execution.go b/execution.go index 8d6961d..fed645f 100644 --- a/execution.go +++ b/execution.go @@ -15,7 +15,7 @@ import ( type Execution interface { PR() int - IsMinor() bool + IsTargetingRoot() bool IsMerge() bool HeadCommitTags() Tags HeadBranchTags() Tags @@ -35,8 +35,6 @@ func Calculate(ctx context.Context, ex Execution) *Calculation { maxlr := MaxLiveOrReservedTag(mrlt, mrrt) - // maxmr := MaxMyOrReservedTag(mrrt, mmrt) - return &Calculation{ IsMerge: ex.IsMerge(), MostRecentLiveTag: mrlt, @@ -44,7 +42,7 @@ func Calculate(ctx context.Context, ex Execution) *Calculation { MyMostRecentTag: mmrt, MyMostRecentBuild: MyMostRecentBuildNumber(ex), PR: ex.PR(), - NextValidTag: GetNextValidTag(ctx, ex.IsMinor(), maxlr), + NextValidTag: GetNextValidTag(ctx, ex.IsTargetingRoot(), maxlr), } } diff --git a/execution_test.go b/execution_test.go index a5bebf3..ffd1ac4 100644 --- a/execution_test.go +++ b/execution_test.go @@ -244,25 +244,25 @@ const ( func TestNewTags(t *testing.T) { testCases := []struct { - name string - baseBranchTags simver.Tags - headBranchTags simver.Tags - rootBranchTags simver.Tags - headCommitTags simver.Tags - pr int - isMerge bool - isMinor bool - expectedTags simver.Tags + name string + baseBranchTags simver.Tags + headBranchTags simver.Tags + rootBranchTags simver.Tags + headCommitTags simver.Tags + pr int + isMerge bool + isTargetingRoot bool + expectedTags simver.Tags }{ { - name: "Normal Commit on Non-Main Base Branch", - baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3"}}, - headBranchTags: simver.Tags{}, - headCommitTags: simver.Tags{}, - rootBranchTags: simver.Tags{}, - pr: 0, - isMerge: false, - isMinor: false, + name: "Normal Commit on Non-Main Base Branch", + baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3"}}, + headBranchTags: simver.Tags{}, + headCommitTags: simver.Tags{}, + rootBranchTags: simver.Tags{}, + pr: 0, + isMerge: false, + isTargetingRoot: false, expectedTags: simver.Tags{ simver.Tag{Name: "v1.2.4", Ref: head_ref}, // simver.Tag{Name: "v1.2.4-reserved", Ref: root_ref}, @@ -270,14 +270,14 @@ func TestNewTags(t *testing.T) { }, }, { - name: "Normal Commit on Main Branch", - baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3"}}, - headBranchTags: simver.Tags{}, - headCommitTags: simver.Tags{}, - rootBranchTags: simver.Tags{}, - pr: 0, - isMerge: true, - isMinor: true, + name: "Normal Commit on Main Branch", + baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3"}}, + headBranchTags: simver.Tags{}, + headCommitTags: simver.Tags{}, + rootBranchTags: simver.Tags{}, + pr: 0, + isMerge: true, + isTargetingRoot: true, expectedTags: simver.Tags{ simver.Tag{Name: "v1.3.0", Ref: merge_ref}, // simver.Tag{Name: "v1.3.0-reserved", Ref: root_ref}, @@ -285,53 +285,53 @@ func TestNewTags(t *testing.T) { }, }, { - name: "PR Merge with Valid MMRT", - baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3"}}, - headBranchTags: simver.Tags{simver.Tag{Name: "v1.2.4-pr1+1002"}}, - headCommitTags: simver.Tags{}, - rootBranchTags: simver.Tags{simver.Tag{Name: "v1.2.4-reserved"}}, - pr: 1, - isMerge: false, - isMinor: false, + name: "PR Merge with Valid MMRT", + baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3"}}, + headBranchTags: simver.Tags{simver.Tag{Name: "v1.2.4-pr1+1002"}}, + headCommitTags: simver.Tags{}, + rootBranchTags: simver.Tags{simver.Tag{Name: "v1.2.4-reserved"}}, + pr: 1, + isMerge: false, + isTargetingRoot: false, expectedTags: simver.Tags{ simver.Tag{Name: "v1.2.4-pr1+1003", Ref: head_ref}, }, }, { - name: "PR Merge with No MMRT", - baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3"}}, - headBranchTags: simver.Tags{simver.Tag{Name: "v1.5.9-pr87+1002"}}, - headCommitTags: simver.Tags{}, - rootBranchTags: simver.Tags{simver.Tag{Name: "v1.5.9-reserved"}}, - pr: 87, - isMerge: false, - isMinor: false, + name: "PR Merge with No MMRT", + baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3"}}, + headBranchTags: simver.Tags{simver.Tag{Name: "v1.5.9-pr87+1002"}}, + headCommitTags: simver.Tags{}, + rootBranchTags: simver.Tags{simver.Tag{Name: "v1.5.9-reserved"}}, + pr: 87, + isMerge: false, + isTargetingRoot: false, expectedTags: simver.Tags{ simver.Tag{Name: "v1.5.9-pr87+1003", Ref: head_ref}, }, }, { - name: "PR Merge with Invalid MMRT", - baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3"}}, - headBranchTags: simver.Tags{simver.Tag{Name: "v1.2.4-pr2+5"}}, - headCommitTags: simver.Tags{}, - rootBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3-reserved"}}, - pr: 2, - isMerge: false, - isMinor: false, + name: "PR Merge with Invalid MMRT", + baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3"}}, + headBranchTags: simver.Tags{simver.Tag{Name: "v1.2.4-pr2+5"}}, + headCommitTags: simver.Tags{}, + rootBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3-reserved"}}, + pr: 2, + isMerge: false, + isTargetingRoot: false, expectedTags: simver.Tags{ simver.Tag{Name: "v1.2.4-pr2+6", Ref: head_ref}, }, }, { - name: "No Tags Available for PR Commit", - baseBranchTags: simver.Tags{}, - headBranchTags: simver.Tags{}, - headCommitTags: simver.Tags{}, - rootBranchTags: simver.Tags{}, - pr: 1, - isMerge: false, - isMinor: true, + name: "No Tags Available for PR Commit", + baseBranchTags: simver.Tags{}, + headBranchTags: simver.Tags{}, + headCommitTags: simver.Tags{}, + rootBranchTags: simver.Tags{}, + pr: 1, + isMerge: false, + isTargetingRoot: true, expectedTags: simver.Tags{ // we also need to reserve the next version tag // which should be v0.2.0 since the base branch is main @@ -343,14 +343,14 @@ func TestNewTags(t *testing.T) { }, }, { - name: "No Tags Available for PR Merge Commit", - baseBranchTags: simver.Tags{}, - headBranchTags: simver.Tags{}, - headCommitTags: simver.Tags{}, - rootBranchTags: simver.Tags{}, - pr: 2, - isMerge: false, - isMinor: true, + name: "No Tags Available for PR Merge Commit", + baseBranchTags: simver.Tags{}, + headBranchTags: simver.Tags{}, + headCommitTags: simver.Tags{}, + rootBranchTags: simver.Tags{}, + pr: 2, + isMerge: false, + isTargetingRoot: true, expectedTags: simver.Tags{ // since this is a merge we do not need to reserve anything // since the base branch is main, we set it to v0.2.0 @@ -371,9 +371,9 @@ func TestNewTags(t *testing.T) { simver.Tag{Name: "v1.5.0"}, simver.Tag{Name: "v1.5.9-pr84+base"}, }, - pr: 87, - isMerge: true, - isMinor: false, + pr: 87, + isMerge: true, + isTargetingRoot: false, expectedTags: simver.Tags{ simver.Tag{Name: "v1.5.10", Ref: merge_ref}, }, @@ -389,9 +389,9 @@ func TestNewTags(t *testing.T) { simver.Tag{Name: "v1.5.0"}, simver.Tag{Name: "v1.5.9-pr84+base"}, }, - pr: 84, - isMerge: false, - isMinor: false, + pr: 84, + isMerge: false, + isTargetingRoot: false, expectedTags: simver.Tags{ simver.Tag{Name: "v1.5.11-pr84+1003", Ref: head_ref}, simver.Tag{Name: "v1.5.11-reserved", Ref: root_ref}, @@ -405,37 +405,37 @@ func TestNewTags(t *testing.T) { simver.Tag{Name: "v1.2.4-pr2+4"}, simver.Tag{Name: "v1.2.4-pr2+5"}, }, - headCommitTags: simver.Tags{}, - rootBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3-reserved"}}, - pr: 2, - isMerge: false, - isMinor: false, + headCommitTags: simver.Tags{}, + rootBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3-reserved"}}, + pr: 2, + isMerge: false, + isTargetingRoot: false, expectedTags: simver.Tags{ simver.Tag{Name: "v1.2.4-pr2+6", Ref: head_ref}, }, }, { - name: "ignore other base", - baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3"}}, - headBranchTags: simver.Tags{simver.Tag{Name: "v1.2.4-pr2+5"}, simver.Tag{Name: "v1.2.99-base"}}, - headCommitTags: simver.Tags{}, - rootBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3-reserved"}}, - pr: 2, - isMerge: false, - isMinor: false, + name: "ignore other base", + baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3"}}, + headBranchTags: simver.Tags{simver.Tag{Name: "v1.2.4-pr2+5"}, simver.Tag{Name: "v1.2.99-base"}}, + headCommitTags: simver.Tags{}, + rootBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3-reserved"}}, + pr: 2, + isMerge: false, + isTargetingRoot: false, expectedTags: simver.Tags{ simver.Tag{Name: "v1.2.4-pr2+6", Ref: head_ref}, }, }, { - name: "reserved tag already exists", - baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3"}}, - headBranchTags: simver.Tags{}, - headCommitTags: simver.Tags{}, - rootBranchTags: simver.Tags{simver.Tag{Name: "v1.2.4-reserved"}}, - pr: 3, - isMerge: false, - isMinor: false, + name: "reserved tag already exists", + baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3"}}, + headBranchTags: simver.Tags{}, + headCommitTags: simver.Tags{}, + rootBranchTags: simver.Tags{simver.Tag{Name: "v1.2.4-reserved"}}, + pr: 3, + isMerge: false, + isTargetingRoot: false, expectedTags: simver.Tags{ // if the reserved tag did not exist, we would be using v1.2.4 // but since it exists, and pr0 does not know it owns it (via its own v1.2.4-pr0+base tag) @@ -457,9 +457,9 @@ func TestNewTags(t *testing.T) { rootBranchTags: simver.Tags{ simver.Tag{Name: "v0.17.3-reserved"}, }, - pr: 99, - isMerge: false, - isMinor: false, + pr: 99, + isMerge: false, + isTargetingRoot: false, expectedTags: simver.Tags{ // if the reserved tag did not exist, we would be using v1.2.4 // but since it exists, and pr99 does not know it owns it (via its own v1.2.4-pr99+base tag) @@ -469,6 +469,38 @@ func TestNewTags(t *testing.T) { simver.Tag{Name: "v0.17.4-pr99+base", Ref: base_ref}, }, }, + { + name: "when merging a branch that already is tagged correctly, don't do anything", + baseBranchTags: simver.Tags{ + simver.Tag{Name: "v0.2.0-pr1+1"}, + simver.Tag{Name: "v0.2.0"}, + simver.Tag{Name: "v0.3.0-pr1+base"}, + simver.Tag{Name: "v0.3.0-reserved"}, + simver.Tag{Name: "v0.3.0"}, + }, + headBranchTags: simver.Tags{ + simver.Tag{Name: "v0.2.0-pr1+1"}, + simver.Tag{Name: "v0.2.0"}, + simver.Tag{Name: "v0.3.0-pr1+base"}, + simver.Tag{Name: "v0.3.0-reserved"}, + simver.Tag{Name: "v0.3.0"}, + }, + headCommitTags: simver.Tags{ + simver.Tag{Name: "v0.3.0-reserved"}, + simver.Tag{Name: "v0.3.0"}, + }, + rootBranchTags: simver.Tags{ + simver.Tag{Name: "v0.2.0-pr1+1"}, + simver.Tag{Name: "v0.2.0"}, + simver.Tag{Name: "v0.3.0-pr1+base"}, + simver.Tag{Name: "v0.3.0-reserved"}, + simver.Tag{Name: "v0.3.0"}, + }, + pr: 1, + isMerge: true, + isTargetingRoot: true, + expectedTags: simver.Tags{}, + }, } ctx := context.Background() @@ -483,7 +515,7 @@ func TestNewTags(t *testing.T) { mockExec.EXPECT().HeadCommitTags().Return(tc.headCommitTags) mockExec.EXPECT().BaseBranchTags().Return(tc.baseBranchTags) mockExec.EXPECT().PR().Return(tc.pr) - mockExec.EXPECT().IsMinor().Return(tc.isMinor) + mockExec.EXPECT().IsTargetingRoot().Return(tc.isTargetingRoot) mockExec.EXPECT().IsMerge().Return(tc.isMerge) mockExec.EXPECT().RootBranchTags().Return(tc.rootBranchTags) diff --git a/gen/mockery/Execution.simver.mockery.go b/gen/mockery/Execution.simver.mockery.go index ee4f54d..df9c402 100644 --- a/gen/mockery/Execution.simver.mockery.go +++ b/gen/mockery/Execution.simver.mockery.go @@ -190,8 +190,8 @@ func (_c *MockExecution_simver_IsMerge_Call) RunAndReturn(run func() bool) *Mock return _c } -// IsMinor provides a mock function with given fields: -func (_m *MockExecution_simver) IsMinor() bool { +// IsTargetingRoot provides a mock function with given fields: +func (_m *MockExecution_simver) IsTargetingRoot() bool { ret := _m.Called() var r0 bool @@ -204,29 +204,29 @@ func (_m *MockExecution_simver) IsMinor() bool { return r0 } -// MockExecution_simver_IsMinor_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'IsMinor' -type MockExecution_simver_IsMinor_Call struct { +// MockExecution_simver_IsTargetingRoot_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'IsTargetingRoot' +type MockExecution_simver_IsTargetingRoot_Call struct { *mock.Call } -// IsMinor is a helper method to define mock.On call -func (_e *MockExecution_simver_Expecter) IsMinor() *MockExecution_simver_IsMinor_Call { - return &MockExecution_simver_IsMinor_Call{Call: _e.mock.On("IsMinor")} +// IsTargetingRoot is a helper method to define mock.On call +func (_e *MockExecution_simver_Expecter) IsTargetingRoot() *MockExecution_simver_IsTargetingRoot_Call { + return &MockExecution_simver_IsTargetingRoot_Call{Call: _e.mock.On("IsTargetingRoot")} } -func (_c *MockExecution_simver_IsMinor_Call) Run(run func()) *MockExecution_simver_IsMinor_Call { +func (_c *MockExecution_simver_IsTargetingRoot_Call) Run(run func()) *MockExecution_simver_IsTargetingRoot_Call { _c.Call.Run(func(args mock.Arguments) { run() }) return _c } -func (_c *MockExecution_simver_IsMinor_Call) Return(_a0 bool) *MockExecution_simver_IsMinor_Call { +func (_c *MockExecution_simver_IsTargetingRoot_Call) Return(_a0 bool) *MockExecution_simver_IsTargetingRoot_Call { _c.Call.Return(_a0) return _c } -func (_c *MockExecution_simver_IsMinor_Call) RunAndReturn(run func() bool) *MockExecution_simver_IsMinor_Call { +func (_c *MockExecution_simver_IsTargetingRoot_Call) RunAndReturn(run func() bool) *MockExecution_simver_IsTargetingRoot_Call { _c.Call.Return(run) return _c } diff --git a/simver.go b/simver.go index f8aaf21..743b0bb 100644 --- a/simver.go +++ b/simver.go @@ -10,22 +10,22 @@ var _ Execution = &rawExecution{} var _ RefProvider = &rawExecution{} type rawExecution struct { - pr *PRDetails - baseBranch string - headBranch string - rootBranch string - headCommit string - baseCommit string - rootCommit string - mergeCommit string - rootBranchTags Tags - rootCommitTags Tags - headCommitTags Tags - baseCommitTags Tags - baseBranchTags Tags - headBranchTags Tags - isMerged bool - isMinor bool + pr *PRDetails + baseBranch string + headBranch string + rootBranch string + headCommit string + baseCommit string + rootCommit string + mergeCommit string + rootBranchTags Tags + rootCommitTags Tags + headCommitTags Tags + baseCommitTags Tags + baseBranchTags Tags + headBranchTags Tags + isMerged bool + isTargetingRoot bool } func (e *rawExecution) Head() string { @@ -72,7 +72,7 @@ func (e *rawExecution) RootBranchTags() Tags { return e.rootBranchTags } -func (e *rawExecution) IsMinor() bool { +func (e *rawExecution) IsTargetingRoot() bool { return e.baseBranch == e.rootBranch } @@ -80,40 +80,36 @@ func (e *rawExecution) HeadCommitTags() Tags { return e.headCommitTags } -func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Execution, *PRDetails, bool, error) { +func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Execution, *PRDetails, error) { pr, err := prr.CurrentPR(ctx) if err != nil { - return nil, nil, false, err - } - - if pr.IsSimulatedPush() && pr.HeadBranch != "main" { - return nil, nil, false, nil + return nil, nil, err } _, err = tprov.FetchTags(ctx) if err != nil { - return nil, nil, false, err + return nil, nil, err } baseCommitTags, err := tprov.TagsFromCommit(ctx, pr.BaseCommit) if err != nil { - return nil, nil, false, err + return nil, nil, err } baseBranchTags, err := tprov.TagsFromBranch(ctx, pr.BaseBranch) if err != nil { - return nil, nil, false, err + return nil, nil, err } rootCommitTags, err := tprov.TagsFromCommit(ctx, pr.RootCommit) if err != nil { - return nil, nil, false, err + return nil, nil, err } rootBranchTags, err := tprov.TagsFromBranch(ctx, pr.RootBranch) if err != nil { - return nil, nil, false, err + return nil, nil, err } var headBranchTags Tags @@ -126,14 +122,14 @@ func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Exec headCommit = pr.HeadCommit branchTags, err := tprov.TagsFromBranch(ctx, pr.HeadBranch) if err != nil { - return nil, nil, false, err + return nil, nil, err } headBranchTags = branchTags } headTags, err := tprov.TagsFromCommit(ctx, headCommit) if err != nil { - return nil, nil, false, err + return nil, nil, err } zerolog.Ctx(ctx).Debug(). @@ -161,6 +157,6 @@ func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Exec rootBranchTags: rootBranchTags, rootCommitTags: rootCommitTags, mergeCommit: pr.MergeCommit, - }, pr, true, nil + }, pr, nil }