From 5eb1b3579d7fff91ffce06917c4c006d0ef79c6d Mon Sep 17 00:00:00 2001 From: walter Date: Mon, 13 Nov 2023 20:52:41 -0600 Subject: [PATCH 01/38] rollin --- README.md | 7 ++++++- exec/tag.go | 10 ---------- execution.go | 9 ++++++--- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index e5b7576..ebd4374 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ simple, pr based, semver git tagging logic # definitions -## when is mmrt valid? +## when is mmrt valid? 1. it exists 2. it is higher than the mrlt @@ -35,6 +35,11 @@ the mmrbn is an integer, so "N" ----------- +two bugs: +- need to make sure merges do not have build numbers +- need to make sure that build nums are picked up + + # process ## when to run the workflow diff --git a/exec/tag.go b/exec/tag.go index cf6b51d..cb8ead3 100644 --- a/exec/tag.go +++ b/exec/tag.go @@ -48,16 +48,6 @@ func (p *gitProvider) TagsFromBranch(ctx context.Context, branch string) ([]simv ctx = zerolog.Ctx(ctx).With().Str("branch", branch).Logger().WithContext(ctx) - // zerolog.Ctx(ctx).Debug().Msg("getting tags from branch") - - // cmd := p.git(ctx, "pull", "--ff-only", "--tags", "origin", branch) - // cmd.Stdout = os.Stdout - // cmd.Stderr = os.Stderr - // err := cmd.Run() - // if err != nil { - // return nil, ErrExecGit.Trace(err) - // } - cmd := p.git(ctx, "tag", "--merged", "origin/"+branch, "--format='{\"sha\":\"%(objectname)\",\"type\": \"%(objecttype)\", \"ref\": \"%(refname)\"}'") out, err := cmd.Output() if err != nil { diff --git a/execution.go b/execution.go index 6cee8f4..8c7002f 100644 --- a/execution.go +++ b/execution.go @@ -7,6 +7,7 @@ import ( "strconv" "strings" + "github.com/rs/zerolog" "golang.org/x/mod/semver" ) @@ -229,21 +230,23 @@ func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Exec return nil, err } - headTags, err := tprov.TagsFromCommit(ctx, pr.HeadCommit) + baseBranchTags, err := tprov.TagsFromBranch(ctx, pr.BaseBranch) if err != nil { return nil, err } - branchTags, err := tprov.TagsFromBranch(ctx, pr.HeadBranch) + headTags, err := tprov.TagsFromCommit(ctx, pr.HeadCommit) if err != nil { return nil, err } - baseBranchTags, err := tprov.TagsFromBranch(ctx, pr.BaseBranch) + branchTags, err := tprov.TagsFromBranch(ctx, pr.HeadBranch) if err != nil { return nil, err } + zerolog.Ctx(ctx).Debug().Any("pr", pr).Any("baseCommitTags", baseCommitTags).Any("baseBranchTags", baseBranchTags).Any("headTags", headTags).Any("branchTags", branchTags).Msg("loaded execution") + return &rawExecution{ pr: pr, baseBranch: pr.BaseBranch, From 9a6ed7a994515b3b2c610d4aed4ff85b6101615a Mon Sep 17 00:00:00 2001 From: walter Date: Mon, 13 Nov 2023 20:59:40 -0600 Subject: [PATCH 02/38] adjust branch tags --- exec/exec.go | 1 - execution.go | 7 ++----- 2 files changed, 2 insertions(+), 6 deletions(-) delete mode 100644 exec/exec.go diff --git a/exec/exec.go b/exec/exec.go deleted file mode 100644 index 81c33c4..0000000 --- a/exec/exec.go +++ /dev/null @@ -1 +0,0 @@ -package exec diff --git a/execution.go b/execution.go index 8c7002f..b1c995f 100644 --- a/execution.go +++ b/execution.go @@ -7,7 +7,6 @@ import ( "strconv" "strings" - "github.com/rs/zerolog" "golang.org/x/mod/semver" ) @@ -245,8 +244,6 @@ func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Exec return nil, err } - zerolog.Ctx(ctx).Debug().Any("pr", pr).Any("baseCommitTags", baseCommitTags).Any("baseBranchTags", baseBranchTags).Any("headTags", headTags).Any("branchTags", branchTags).Msg("loaded execution") - return &rawExecution{ pr: pr, baseBranch: pr.BaseBranch, @@ -255,8 +252,8 @@ func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Exec baseCommit: pr.BaseCommit, headCommitTags: headTags, baseCommitTags: baseCommitTags, - baseBranchTags: branchTags, - headBranchTags: baseBranchTags, + baseBranchTags: baseBranchTags, + headBranchTags: branchTags, }, nil } From 1fe58b9ea3bc98e3d49d47ec61d98cccd662a9dd Mon Sep 17 00:00:00 2001 From: walter Date: Mon, 13 Nov 2023 21:14:41 -0600 Subject: [PATCH 03/38] rollin --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ebd4374..58ce0be 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ inside this commits tree, the highest '*-pr.N+(this)' is the mmrbn. note each of the nvt, mrrt, mrlt, and mmrt are saved as valid semvers, so "X.Y.Z" the mmrbn is an integer, so "N" ------------ +------------ two bugs: - need to make sure merges do not have build numbers From 5df8632c2d082b9bc18496744235d8a6a709f90f Mon Sep 17 00:00:00 2001 From: walter Date: Mon, 13 Nov 2023 21:21:14 -0600 Subject: [PATCH 04/38] get ready for simple merge --- cmd/simver_github_actions/main.go | 16 ++++++++++++++-- execution.go | 16 ++++++++-------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/cmd/simver_github_actions/main.go b/cmd/simver_github_actions/main.go index 26835df..e05b0f8 100644 --- a/cmd/simver_github_actions/main.go +++ b/cmd/simver_github_actions/main.go @@ -171,7 +171,7 @@ func main() { os.Exit(1) } - ee, err := simver.LoadExecution(ctx, tagprov, prr) + ee, prd, err := simver.LoadExecution(ctx, tagprov, prr) if err != nil { zerolog.Ctx(ctx).Error().Err(err).Msgf("error loading execution") fmt.Println(terrors.FormatErrorCaller(err)) @@ -197,13 +197,15 @@ func main() { } time.Sleep(1 * time.Second) - ee, err := simver.LoadExecution(ctx, tagprov, prr) + eez, prz, err := simver.LoadExecution(ctx, tagprov, prr) if err != nil { zerolog.Ctx(ctx).Error().Err(err).Msgf("error loading execution: %v", err) fmt.Println(terrors.FormatErrorCaller(err)) os.Exit(1) } + ee = eez + prd = prz tags := simver.NewTags(ee) reservedTag, reserved = tags.GetReserved() } else { @@ -211,11 +213,21 @@ func main() { } } + havMergedTag := false + for _, tag := range tags { if tag.Name == reservedTag.Name && tag.Ref == reservedTag.Ref { continue } + if prd.Merged { + if havMergedTag { + continue + } + havMergedTag = true + tag.Name = strings.Split(tag.Name, "-")[0] + } + err := tagprov.CreateTag(ctx, tag) if err != nil { zerolog.Ctx(ctx).Error().Err(err).Msgf("error creating tag: %v", err) diff --git a/execution.go b/execution.go index b1c995f..75c044d 100644 --- a/execution.go +++ b/execution.go @@ -212,36 +212,36 @@ func (e *rawExecution) HeadBranch() string { return e.headBranch } -func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Execution, error) { +func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Execution, *PRDetails, error) { pr, err := prr.CurrentPR(ctx) if err != nil { - return nil, err + return nil, nil, err } _, err = tprov.FetchTags(ctx) if err != nil { - return nil, err + return nil, nil, err } baseCommitTags, err := tprov.TagsFromCommit(ctx, pr.BaseCommit) if err != nil { - return nil, err + return nil, nil, err } baseBranchTags, err := tprov.TagsFromBranch(ctx, pr.BaseBranch) if err != nil { - return nil, err + return nil, nil, err } headTags, err := tprov.TagsFromCommit(ctx, pr.HeadCommit) if err != nil { - return nil, err + return nil, nil, err } branchTags, err := tprov.TagsFromBranch(ctx, pr.HeadBranch) if err != nil { - return nil, err + return nil, nil, err } return &rawExecution{ @@ -254,6 +254,6 @@ func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Exec baseCommitTags: baseCommitTags, baseBranchTags: baseBranchTags, headBranchTags: branchTags, - }, nil + }, pr, nil } From 7eaa3f9c5f7f94f920abdcc217451b0b5ecffbc6 Mon Sep 17 00:00:00 2001 From: walter Date: Mon, 13 Nov 2023 21:32:10 -0600 Subject: [PATCH 05/38] role --- cmd/simver_github_actions/main.go | 37 +++++++++---------------------- execution.go | 20 ++++++++++------- 2 files changed, 23 insertions(+), 34 deletions(-) diff --git a/cmd/simver_github_actions/main.go b/cmd/simver_github_actions/main.go index e05b0f8..4460102 100644 --- a/cmd/simver_github_actions/main.go +++ b/cmd/simver_github_actions/main.go @@ -59,14 +59,6 @@ func (p *PullRequestResolver) CurrentPR(ctx context.Context) (*simver.PRDetails, sha := os.Getenv("GITHUB_SHA") - // // this is a push event, we need to find the PR (if any) that this push is for - - // // get the commit hash - // commit, err := p.git.GetHeadRef(ctx) - // if err != nil { - // return nil, Err.Trace(err, "error getting commit hash") - // } - pr, exists, err := p.gh.PRDetailsByCommit(ctx, sha) if err != nil { return nil, Err.Trace(err, "error getting PR details by commit") @@ -76,21 +68,6 @@ func (p *PullRequestResolver) CurrentPR(ctx context.Context) (*simver.PRDetails, return pr, nil } - // // get the branch - // branch, err := p.git.Branch(ctx) - // if err != nil { - // return nil, Err.Trace(err, "error getting branch") - // } - - // pr, exists, err = p.gh.PRDetailsByBranch(ctx, branch) - // if err != nil { - // return nil, Err.Trace(err, "error getting PR details by branch") - // } - - if exists { - return pr, nil - } - // get the parent commit parent, err := p.git.CommitFromRef(ctx, "HEAD^") if err != nil { @@ -171,7 +148,7 @@ func main() { os.Exit(1) } - ee, prd, err := simver.LoadExecution(ctx, tagprov, prr) + ee, prd, keepgoing, err := simver.LoadExecution(ctx, tagprov, prr) if err != nil { zerolog.Ctx(ctx).Error().Err(err).Msgf("error loading execution") fmt.Println(terrors.FormatErrorCaller(err)) @@ -179,6 +156,11 @@ func main() { os.Exit(1) } + if !keepgoing { + zerolog.Ctx(ctx).Debug().Msg("execution is complete, exiting") + os.Exit(0) + } + tags := simver.NewTags(ee) reservedTag, reserved := tags.GetReserved() @@ -197,13 +179,16 @@ func main() { } time.Sleep(1 * time.Second) - eez, prz, err := simver.LoadExecution(ctx, tagprov, prr) + eez, prz, keepgoing, err := simver.LoadExecution(ctx, tagprov, prr) if err != nil { zerolog.Ctx(ctx).Error().Err(err).Msgf("error loading execution: %v", err) fmt.Println(terrors.FormatErrorCaller(err)) - os.Exit(1) } + if !keepgoing { + zerolog.Ctx(ctx).Debug().Msg("execution is complete, exiting") + os.Exit(0) + } ee = eez prd = prz tags := simver.NewTags(ee) diff --git a/execution.go b/execution.go index 75c044d..85d8aee 100644 --- a/execution.go +++ b/execution.go @@ -212,36 +212,40 @@ func (e *rawExecution) HeadBranch() string { return e.headBranch } -func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Execution, *PRDetails, error) { +func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Execution, *PRDetails, bool, error) { pr, err := prr.CurrentPR(ctx) if err != nil { - return nil, nil, err + return nil, nil, false, err + } + + if pr.Number == 0 && pr.HeadBranch != "main" { + return nil, nil, false, nil } _, err = tprov.FetchTags(ctx) if err != nil { - return nil, nil, err + return nil, nil, false, err } baseCommitTags, err := tprov.TagsFromCommit(ctx, pr.BaseCommit) if err != nil { - return nil, nil, err + return nil, nil, false, err } baseBranchTags, err := tprov.TagsFromBranch(ctx, pr.BaseBranch) if err != nil { - return nil, nil, err + return nil, nil, false, err } headTags, err := tprov.TagsFromCommit(ctx, pr.HeadCommit) if err != nil { - return nil, nil, err + return nil, nil, false, err } branchTags, err := tprov.TagsFromBranch(ctx, pr.HeadBranch) if err != nil { - return nil, nil, err + return nil, nil, false, err } return &rawExecution{ @@ -254,6 +258,6 @@ func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Exec baseCommitTags: baseCommitTags, baseBranchTags: baseBranchTags, headBranchTags: branchTags, - }, pr, nil + }, pr, true, nil } From aaf7e747143a8b176ed36a5f076e1193323ea434 Mon Sep 17 00:00:00 2001 From: walter Date: Mon, 13 Nov 2023 21:37:16 -0600 Subject: [PATCH 06/38] logic --- cmd/simver_github_actions/main.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cmd/simver_github_actions/main.go b/cmd/simver_github_actions/main.go index 4460102..2df07cc 100644 --- a/cmd/simver_github_actions/main.go +++ b/cmd/simver_github_actions/main.go @@ -152,11 +152,14 @@ func main() { if err != nil { zerolog.Ctx(ctx).Error().Err(err).Msgf("error loading execution") fmt.Println(terrors.FormatErrorCaller(err)) - os.Exit(1) } - if !keepgoing { + isPush := os.Getenv("GITHUB_EVENT_NAME") == "push" + + isMain := prd.HeadBranch == "main" + + if !keepgoing || (isPush && !isMain) { zerolog.Ctx(ctx).Debug().Msg("execution is complete, exiting") os.Exit(0) } From aa0413ac8dcc0055e412152c73b4156787b0869d Mon Sep 17 00:00:00 2001 From: walter Date: Mon, 13 Nov 2023 21:44:25 -0600 Subject: [PATCH 07/38] hold up --- execution.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/execution.go b/execution.go index 85d8aee..3010fd7 100644 --- a/execution.go +++ b/execution.go @@ -84,7 +84,7 @@ func MyMostRecentTag(e Execution) MMRT { func MostRecentReservedTag(e Execution) MRRT { reg := regexp.MustCompile(`^v\d+\.\d+\.\d+-reserved$`) - highest, err := e.BaseCommitTags().HighestSemverMatching(reg) + highest, err := e.BaseBranchTags().HighestSemverMatching(reg) if err != nil { return "" } From f866d7d7461e2dee4a4d27471ae123bb714b8e4b Mon Sep 17 00:00:00 2001 From: walter Date: Mon, 13 Nov 2023 21:54:26 -0600 Subject: [PATCH 08/38] check it --- execution.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/execution.go b/execution.go index 3010fd7..0b520ab 100644 --- a/execution.go +++ b/execution.go @@ -63,7 +63,7 @@ type MMRT string // my most recent tag type MMRBN int // my most recent build number func MostRecentLiveTag(e Execution) MRLT { - reg := regexp.MustCompile(`^v\d+\.\d+\.\d+$`) + reg := regexp.MustCompile(`^v\d+\.\d+\.\d+(|-\S+\+\d+)$`) highest, err := e.BaseBranchTags().HighestSemverMatching(reg) if err != nil { return "" From e22c5c412d4838552fcb3bb6de70da2140e23a7c Mon Sep 17 00:00:00 2001 From: walter Date: Mon, 13 Nov 2023 22:46:14 -0600 Subject: [PATCH 09/38] bump it up --- cmd/simver_github_actions/main.go | 4 ++-- execution.go | 35 ++++++++++++++++++++++++------- execution_test.go | 9 ++++++-- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/cmd/simver_github_actions/main.go b/cmd/simver_github_actions/main.go index 2df07cc..897ef64 100644 --- a/cmd/simver_github_actions/main.go +++ b/cmd/simver_github_actions/main.go @@ -164,7 +164,7 @@ func main() { os.Exit(0) } - tags := simver.NewTags(ee) + tags := simver.NewTags(ctx, ee) reservedTag, reserved := tags.GetReserved() @@ -194,7 +194,7 @@ func main() { } ee = eez prd = prz - tags := simver.NewTags(ee) + tags := simver.NewTags(ctx, ee) reservedTag, reserved = tags.GetReserved() } else { reserved = false diff --git a/execution.go b/execution.go index 0b520ab..8b03a3c 100644 --- a/execution.go +++ b/execution.go @@ -7,6 +7,7 @@ import ( "strconv" "strings" + "github.com/rs/zerolog" "golang.org/x/mod/semver" ) @@ -27,7 +28,7 @@ type Execution interface { const baseTag = "v0.1.0" -func NewCaclulation(ex Execution) *Calculation { +func NewCaclulation(ctx context.Context, ex Execution) *Calculation { mrlt := MostRecentLiveTag(ex) mrrt := MostRecentReservedTag(ex) return &Calculation{ @@ -36,21 +37,21 @@ func NewCaclulation(ex Execution) *Calculation { MyMostRecentTag: MyMostRecentTag(ex), MyMostRecentBuild: MyMostRecentBuildNumber(ex), PR: ex.PR(), - NextValidTag: GetNextValidTag(ex.BaseBranch() == "main", mrlt, mrrt), + NextValidTag: GetNextValidTag(ctx, ex.BaseBranch() == "main", mrlt, mrrt), } } -func NewTags(me Execution) Tags { - calc := NewCaclulation(me) +func NewTags(ctx context.Context, ex Execution) Tags { + calc := NewCaclulation(ctx, ex) baseTags, headTags := calc.CalculateNewTagsRaw() tags := make(Tags, 0) for _, tag := range baseTags { - tags = append(tags, TagInfo{Name: tag, Ref: me.BaseCommit()}) + tags = append(tags, TagInfo{Name: tag, Ref: ex.BaseCommit()}) } for _, tag := range headTags { - tags = append(tags, TagInfo{Name: tag, Ref: me.HeadCommit()}) + tags = append(tags, TagInfo{Name: tag, Ref: ex.HeadCommit()}) } return tags @@ -112,7 +113,7 @@ func MyMostRecentBuildNumber(e Execution) MMRBN { return MMRBN(n) } -func GetNextValidTag(minor bool, mrlt MRLT, mrrt MRRT) NVT { +func GetNextValidTag(ctx context.Context, minor bool, mrlt MRLT, mrrt MRRT) NVT { var max string @@ -158,6 +159,18 @@ func GetNextValidTag(minor bool, mrlt MRLT, mrrt MRRT) NVT { patchnum++ } + zerolog.Ctx(ctx).Debug(). + Str("max", max). + Str("maj", maj). + Str("majmin", majmin). + Str("patch", patch). + Str("min", min). + Str("mrlt", string(mrlt)). + Str("mrrt", string(mrrt)). + Int("minornum", minornum). + Int("patchnum", patchnum). + Msg("calculated next valid tag") + return NVT(fmt.Sprintf("%s.%d.%d", semver.Major(max), minornum, patchnum)) } @@ -248,6 +261,14 @@ func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Exec return nil, nil, false, err } + zerolog.Ctx(ctx).Debug(). + Any("baseCommitTags", baseCommitTags). + Any("baseBranchTags", baseBranchTags). + Any("headTags", headTags). + Any("branchTags", branchTags). + Any("pr", pr). + Msg("loaded tags") + return &rawExecution{ pr: pr, baseBranch: pr.BaseBranch, diff --git a/execution_test.go b/execution_test.go index 05fc743..49022b9 100644 --- a/execution_test.go +++ b/execution_test.go @@ -1,6 +1,7 @@ package simver_test import ( + "context" "testing" "github.com/stretchr/testify/assert" @@ -173,9 +174,11 @@ func TestNvt(t *testing.T) { }, } + ctx := context.Background() + for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - result := simver.GetNextValidTag(tc.minor, tc.mrlt, tc.mrrt) + result := simver.GetNextValidTag(ctx, tc.minor, tc.mrlt, tc.mrrt) assert.Equal(t, tc.expectedNvt, result) }) } @@ -311,6 +314,8 @@ func TestNewTags(t *testing.T) { }, } + ctx := context.Background() + for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { mockExec := new(mockery.MockExecution_simver) @@ -324,7 +329,7 @@ func TestNewTags(t *testing.T) { mockExec.EXPECT().HeadBranch().Return(tc.headBranch) mockExec.EXPECT().BaseBranch().Return(tc.baseBranch) - result := simver.NewTags(mockExec) + result := simver.NewTags(ctx, mockExec) assert.ElementsMatch(t, tc.expectedTags, result) }) } From 115d0bc5eba01b029e4e54c6f9d53f5feed15aa6 Mon Sep 17 00:00:00 2001 From: walter Date: Mon, 13 Nov 2023 22:49:07 -0600 Subject: [PATCH 10/38] save --- cmd/simver_github_actions/main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/simver_github_actions/main.go b/cmd/simver_github_actions/main.go index 897ef64..8422216 100644 --- a/cmd/simver_github_actions/main.go +++ b/cmd/simver_github_actions/main.go @@ -171,7 +171,6 @@ func main() { tries := 0 for reserved { - err := tagprov.CreateTag(ctx, reservedTag) if err != nil { if tries > 5 { From 87e59f72c5dd72ad4c3c715fe2f7aa633c65997f Mon Sep 17 00:00:00 2001 From: walter Date: Mon, 13 Nov 2023 23:00:47 -0600 Subject: [PATCH 11/38] 3 --- execution.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/execution.go b/execution.go index 8b03a3c..d5ac34b 100644 --- a/execution.go +++ b/execution.go @@ -251,7 +251,13 @@ func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Exec return nil, nil, false, err } - headTags, err := tprov.TagsFromCommit(ctx, pr.HeadCommit) + hc := pr.HeadCommit + + if pr.Merged { + hc = pr.MergeCommit + } + + headTags, err := tprov.TagsFromCommit(ctx, hc) if err != nil { return nil, nil, false, err } From 09c697c3988f229e77bc8d7dd5b977d49f74e4a1 Mon Sep 17 00:00:00 2001 From: walter Date: Tue, 14 Nov 2023 06:30:50 -0600 Subject: [PATCH 12/38] basic root logic --- README.md | 11 + cmd/simver_github_actions/main.go | 30 ++- exec/gh.go | 56 ++++- execution.go | 67 +++++- execution_test.go | 82 +++++-- gen/mockery/Execution.simver.mockery.go | 270 ++++++++++++++++-------- pr.go | 7 +- 7 files changed, 386 insertions(+), 137 deletions(-) diff --git a/README.md b/README.md index 58ce0be..8b48089 100644 --- a/README.md +++ b/README.md @@ -91,3 +91,14 @@ two bugs: 1. find the mrrt and mrlt, calculate the nvt 2. create a new tag (based on nvt) on the head commit with no build number or prerelease + + + + + +when you merge a pr: +- find the mmrt or the pr branch, and we need to start using that for this branch +- the base branch should inherit the mmrt from the pr branch +- so we need to create: + 1. a new "base" tag for the base branch with the mmrt of the pr branch + 2. create a new build tag using the mmrt diff --git a/cmd/simver_github_actions/main.go b/cmd/simver_github_actions/main.go index 8422216..60f4299 100644 --- a/cmd/simver_github_actions/main.go +++ b/cmd/simver_github_actions/main.go @@ -26,6 +26,20 @@ type PullRequestResolver struct { } func (p *PullRequestResolver) CurrentPR(ctx context.Context) (*simver.PRDetails, error) { + // is the an open, closed, or synchronized PR event? + + // event := os.Getenv("GITHUB_EVENT_NAME") + + // if event == "pull_request" { + // // this is easy, we know that this is a pr event + // } else if event == "pull_request_target" { + // // this is easy, we know that this is a pr event + // } else if event == "push" { + // // this is easy, we know that this is a push event + // } else { + // return nil, errors.New("not a PR event and not a push event") + // } + head_ref := os.Getenv("GITHUB_REF") if head_ref != "" && strings.HasPrefix(head_ref, "refs/pull/") { @@ -200,20 +214,20 @@ func main() { } } - havMergedTag := false + // havMergedTag := false for _, tag := range tags { if tag.Name == reservedTag.Name && tag.Ref == reservedTag.Ref { continue } - if prd.Merged { - if havMergedTag { - continue - } - havMergedTag = true - tag.Name = strings.Split(tag.Name, "-")[0] - } + // if prd.Merged { + // if havMergedTag { + // continue + // } + // havMergedTag = true + // tag.Name = strings.Split(tag.Name, "-")[0] + // } err := tagprov.CreateTag(ctx, tag) if err != nil { diff --git a/exec/gh.go b/exec/gh.go index 5ace92a..3748745 100644 --- a/exec/gh.go +++ b/exec/gh.go @@ -124,14 +124,29 @@ func (p *ghProvider) getRelevantPR(ctx context.Context, out []byte) (*simver.PRD return nil, false, err } + ret := func(pr *githubPR) (*simver.PRDetails, bool, error) { + dets := pr.toPRDetails() + dets.BaseCommit, err = p.getBaseCommit(ctx, dets) + if err != nil { + return nil, false, err + } + dets.RootCommit, err = p.getRootCommit(ctx) + if err != nil { + return nil, false, err + } + return dets, true, nil + } + + // first check if there is a merged PR + for _, pr := range dat { + if pr.State == "MERGED" { + return ret(pr) + } + } + for _, pr := range dat { - if pr.State == "MERGED" || pr.State == "OPEN" { - dets := pr.toPRDetails() - dets.BaseCommit, err = p.getBaseCommit(ctx, dets) - if err != nil { - return nil, false, err - } - return dets, true, nil + if pr.State == "OPEN" { + return ret(pr) } } @@ -210,6 +225,33 @@ func (p *ghProvider) getBaseCommit(ctx context.Context, dets *simver.PRDetails) return dat.Parents[0].Sha, nil } +func (p *ghProvider) getRootCommit(ctx context.Context) (string, error) { + zerolog.Ctx(ctx).Debug().Msg("Getting root commit") + + cmd := p.gh(ctx, "api", "-H", "Accept: application/vnd.github+json", fmt.Sprintf("/repos/%s/%s/git/ref/heads/main", p.Org, p.Repo)) + out, err := cmd.Output() + if err != nil { + return "", ErrExecGH.Trace(err) + } + + var dat struct { + Object struct { + Sha string `json:"sha"` + } `json:"object"` + } + + err = json.Unmarshal(out, &dat) + if err != nil { + return "", ErrExecGH.Trace(err) + } + + if dat.Object.Sha == "" { + return "", ErrExecGH.Trace("no sha found") + } + + return dat.Object.Sha, nil +} + func (p *ghProvider) PRDetailsByCommit(ctx context.Context, commitHash string) (*simver.PRDetails, bool, error) { // Implement getting PR details using exec and parsing the output of gh cli diff --git a/execution.go b/execution.go index d5ac34b..4bc14d6 100644 --- a/execution.go +++ b/execution.go @@ -13,17 +13,24 @@ import ( type Execution interface { PR() int + IsMinor() bool + IsMerged() bool HeadCommit() string BaseCommit() string - HeadBranch() string - BaseBranch() string + // HeadBranch() string + // BaseBranch() string HeadCommitTags() Tags BaseCommitTags() Tags HeadBranchTags() Tags BaseBranchTags() Tags + + RootCommit() string + // RootBranch() string // always main + RootBranchTags() Tags + RootCommitTags() Tags } const baseTag = "v0.1.0" @@ -37,7 +44,7 @@ func NewCaclulation(ctx context.Context, ex Execution) *Calculation { MyMostRecentTag: MyMostRecentTag(ex), MyMostRecentBuild: MyMostRecentBuildNumber(ex), PR: ex.PR(), - NextValidTag: GetNextValidTag(ctx, ex.BaseBranch() == "main", mrlt, mrrt), + NextValidTag: GetNextValidTag(ctx, ex.IsMinor(), mrlt, mrrt), } } @@ -181,12 +188,18 @@ type rawExecution struct { pr *PRDetails baseBranch string headBranch string + rootBranch string headCommit string baseCommit string + rootCommit string + rootBranchTags Tags + rootCommitTags Tags headCommitTags Tags baseCommitTags Tags baseBranchTags Tags headBranchTags Tags + isMerged bool + isMinor bool } func (e *rawExecution) BaseCommit() string { @@ -217,12 +230,36 @@ func (e *rawExecution) PR() int { return e.pr.Number } -func (e *rawExecution) BaseBranch() string { - return e.baseBranch +// func (e *rawExecution) BaseBranch() string { +// return e.baseBranch +// } + +// func (e *rawExecution) HeadBranch() string { +// return e.headBranch +// } + +func (e *rawExecution) IsMerged() bool { + return e.pr.Merged +} + +func (e *rawExecution) RootCommit() string { + return e.rootCommit } -func (e *rawExecution) HeadBranch() string { - return e.headBranch +func (e *rawExecution) RootBranch() string { + return e.rootBranch +} + +func (e *rawExecution) RootBranchTags() Tags { + return e.rootBranchTags +} + +func (e *rawExecution) RootCommitTags() Tags { + return e.rootCommitTags +} + +func (e *rawExecution) IsMinor() bool { + return e.baseBranch == e.rootBranch } func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Execution, *PRDetails, bool, error) { @@ -251,6 +288,16 @@ func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Exec return nil, nil, false, err } + rootCommitTags, err := tprov.TagsFromCommit(ctx, pr.RootCommit) + if err != nil { + return nil, nil, false, err + } + + rootBranchTags, err := tprov.TagsFromBranch(ctx, pr.RootBranch) + if err != nil { + return nil, nil, false, err + } + hc := pr.HeadCommit if pr.Merged { @@ -270,6 +317,8 @@ func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Exec zerolog.Ctx(ctx).Debug(). Any("baseCommitTags", baseCommitTags). Any("baseBranchTags", baseBranchTags). + Any("rootCommitTags", rootCommitTags). + Any("rootBranchTags", rootBranchTags). Any("headTags", headTags). Any("branchTags", branchTags). Any("pr", pr). @@ -285,6 +334,10 @@ func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Exec baseCommitTags: baseCommitTags, baseBranchTags: baseBranchTags, headBranchTags: branchTags, + rootBranch: pr.RootBranch, + rootCommit: pr.RootCommit, + rootBranchTags: rootBranchTags, + rootCommitTags: rootCommitTags, }, pr, true, nil } diff --git a/execution_test.go b/execution_test.go index 49022b9..7485387 100644 --- a/execution_test.go +++ b/execution_test.go @@ -114,7 +114,7 @@ func TestMrrt(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { mockExec := new(mockery.MockExecution_simver) - mockExec.EXPECT().BaseCommitTags().Return(tc.tags) + mockExec.EXPECT().BaseBranchTags().Return(tc.tags) result := simver.MostRecentReservedTag(mockExec) mockExec.AssertExpectations(t) assert.Equal(t, tc.expectedMrrt, result) @@ -191,11 +191,14 @@ func TestNewTags(t *testing.T) { baseCommitTags simver.Tags baseBranchTags simver.Tags headBranchTags simver.Tags - headBranch string - baseBranch string headCommit string baseCommit string + rootCommit string + rootBranchTags simver.Tags + rootCommitTags simver.Tags pr int + isMerged bool + isMinor bool expectedTags simver.Tags }{ { @@ -206,9 +209,12 @@ func TestNewTags(t *testing.T) { headBranchTags: simver.Tags{}, headCommit: "head123", baseCommit: "base456", - headBranch: "feature", - baseBranch: "main2", + rootCommit: "root789", + rootBranchTags: simver.Tags{}, + rootCommitTags: simver.Tags{}, pr: 0, + isMerged: false, + isMinor: false, expectedTags: simver.Tags{ simver.TagInfo{Name: "v1.2.4-pr0+1", Ref: "head123"}, simver.TagInfo{Name: "v1.2.4-reserved", Ref: "base456"}, @@ -223,9 +229,12 @@ func TestNewTags(t *testing.T) { headBranchTags: simver.Tags{}, headCommit: "head123", baseCommit: "base456", - headBranch: "feature", - baseBranch: "main", + rootCommit: "root789", + rootBranchTags: simver.Tags{}, + rootCommitTags: simver.Tags{}, pr: 99, + isMerged: false, + isMinor: true, expectedTags: simver.Tags{ simver.TagInfo{Name: "v1.3.0-pr99+1", Ref: "head123"}, simver.TagInfo{Name: "v1.3.0-reserved", Ref: "base456"}, @@ -240,9 +249,12 @@ func TestNewTags(t *testing.T) { headBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.4-pr1+1002"}}, headCommit: "head123", baseCommit: "base456", - headBranch: "feature", - baseBranch: "main2", + rootCommit: "root789", + rootBranchTags: simver.Tags{}, + rootCommitTags: simver.Tags{}, pr: 1, + isMerged: false, + isMinor: false, expectedTags: simver.Tags{simver.TagInfo{Name: "v1.2.4-pr1+1003", Ref: "head123"}}, }, { @@ -253,9 +265,12 @@ func TestNewTags(t *testing.T) { headBranchTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-pr87+1002"}}, headCommit: "head123", baseCommit: "base456", - headBranch: "main", - baseBranch: "main", + rootCommit: "root789", + rootBranchTags: simver.Tags{}, + rootCommitTags: simver.Tags{}, pr: 87, + isMerged: false, + isMinor: false, expectedTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-pr87+1003", Ref: "head123"}}, }, { @@ -266,9 +281,12 @@ func TestNewTags(t *testing.T) { headBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.99999-pr2+base"}}, headCommit: "head123", baseCommit: "base456", - headBranch: "feature", - baseBranch: "main", + rootCommit: "root789", + rootBranchTags: simver.Tags{}, + rootCommitTags: simver.Tags{}, pr: 2, + isMerged: false, + isMinor: false, expectedTags: simver.Tags{simver.TagInfo{Name: "v1.2.4-pr2+1", Ref: "head123"}}, }, { @@ -279,9 +297,12 @@ func TestNewTags(t *testing.T) { headBranchTags: simver.Tags{}, headCommit: "head123", baseCommit: "base123", - headBranch: "feature", - baseBranch: "main", + rootCommit: "root789", + rootBranchTags: simver.Tags{}, + rootCommitTags: simver.Tags{}, pr: 0, + isMerged: false, + isMinor: 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 @@ -300,9 +321,12 @@ func TestNewTags(t *testing.T) { headBranchTags: simver.Tags{}, headCommit: "head123", baseCommit: "base123", - headBranch: "main", - baseBranch: "main", + rootCommit: "root789", + rootBranchTags: simver.Tags{}, + rootCommitTags: simver.Tags{}, pr: 2, + isMerged: false, + isMinor: 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 @@ -312,6 +336,22 @@ func TestNewTags(t *testing.T) { simver.TagInfo{Name: "v0.2.0-pr2+base", Ref: "base123"}, }, }, + { + name: "merge", + headCommitTags: simver.Tags{}, + baseCommitTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-reserved"}, simver.TagInfo{Name: "v1.5.9-pr87+base"}}, + baseBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3"}}, + headBranchTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-pr87+1002"}}, + headCommit: "head123", + baseCommit: "base456", + rootCommit: "root789", + rootBranchTags: simver.Tags{}, + rootCommitTags: simver.Tags{}, + pr: 87, + isMerged: true, + isMinor: false, + expectedTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-pr87+1003", Ref: "head123"}}, + }, } ctx := context.Background() @@ -326,8 +366,12 @@ func TestNewTags(t *testing.T) { mockExec.EXPECT().HeadBranchTags().Return(tc.headBranchTags) mockExec.EXPECT().BaseBranchTags().Return(tc.baseBranchTags) mockExec.EXPECT().PR().Return(tc.pr) - mockExec.EXPECT().HeadBranch().Return(tc.headBranch) - mockExec.EXPECT().BaseBranch().Return(tc.baseBranch) + mockExec.EXPECT().IsMinor().Return(tc.isMinor) + mockExec.EXPECT().IsMerged().Return(tc.isMerged) + mockExec.EXPECT().RootCommit().Return(tc.rootCommit) + // mockExec.EXPECT().RootBranch().Return(tc.rootBranch) + mockExec.EXPECT().RootBranchTags().Return(tc.rootBranchTags) + mockExec.EXPECT().RootCommitTags().Return(tc.rootCommitTags) result := simver.NewTags(ctx, mockExec) assert.ElementsMatch(t, tc.expectedTags, result) diff --git a/gen/mockery/Execution.simver.mockery.go b/gen/mockery/Execution.simver.mockery.go index 66d5b68..bc26719 100644 --- a/gen/mockery/Execution.simver.mockery.go +++ b/gen/mockery/Execution.simver.mockery.go @@ -20,47 +20,6 @@ func (_m *MockExecution_simver) EXPECT() *MockExecution_simver_Expecter { return &MockExecution_simver_Expecter{mock: &_m.Mock} } -// BaseBranch provides a mock function with given fields: -func (_m *MockExecution_simver) BaseBranch() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// MockExecution_simver_BaseBranch_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'BaseBranch' -type MockExecution_simver_BaseBranch_Call struct { - *mock.Call -} - -// BaseBranch is a helper method to define mock.On call -func (_e *MockExecution_simver_Expecter) BaseBranch() *MockExecution_simver_BaseBranch_Call { - return &MockExecution_simver_BaseBranch_Call{Call: _e.mock.On("BaseBranch")} -} - -func (_c *MockExecution_simver_BaseBranch_Call) Run(run func()) *MockExecution_simver_BaseBranch_Call { - _c.Call.Run(func(args mock.Arguments) { - run() - }) - return _c -} - -func (_c *MockExecution_simver_BaseBranch_Call) Return(_a0 string) *MockExecution_simver_BaseBranch_Call { - _c.Call.Return(_a0) - return _c -} - -func (_c *MockExecution_simver_BaseBranch_Call) RunAndReturn(run func() string) *MockExecution_simver_BaseBranch_Call { - _c.Call.Return(run) - return _c -} - // BaseBranchTags provides a mock function with given fields: func (_m *MockExecution_simver) BaseBranchTags() simver.Tags { ret := _m.Called() @@ -188,47 +147,6 @@ func (_c *MockExecution_simver_BaseCommitTags_Call) RunAndReturn(run func() simv return _c } -// HeadBranch provides a mock function with given fields: -func (_m *MockExecution_simver) HeadBranch() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// MockExecution_simver_HeadBranch_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'HeadBranch' -type MockExecution_simver_HeadBranch_Call struct { - *mock.Call -} - -// HeadBranch is a helper method to define mock.On call -func (_e *MockExecution_simver_Expecter) HeadBranch() *MockExecution_simver_HeadBranch_Call { - return &MockExecution_simver_HeadBranch_Call{Call: _e.mock.On("HeadBranch")} -} - -func (_c *MockExecution_simver_HeadBranch_Call) Run(run func()) *MockExecution_simver_HeadBranch_Call { - _c.Call.Run(func(args mock.Arguments) { - run() - }) - return _c -} - -func (_c *MockExecution_simver_HeadBranch_Call) Return(_a0 string) *MockExecution_simver_HeadBranch_Call { - _c.Call.Return(_a0) - return _c -} - -func (_c *MockExecution_simver_HeadBranch_Call) RunAndReturn(run func() string) *MockExecution_simver_HeadBranch_Call { - _c.Call.Return(run) - return _c -} - // HeadBranchTags provides a mock function with given fields: func (_m *MockExecution_simver) HeadBranchTags() simver.Tags { ret := _m.Called() @@ -356,8 +274,8 @@ func (_c *MockExecution_simver_HeadCommitTags_Call) RunAndReturn(run func() simv return _c } -// IsMerge provides a mock function with given fields: -func (_m *MockExecution_simver) IsMerge() bool { +// IsMerged provides a mock function with given fields: +func (_m *MockExecution_simver) IsMerged() bool { ret := _m.Called() var r0 bool @@ -370,29 +288,70 @@ func (_m *MockExecution_simver) IsMerge() bool { return r0 } -// MockExecution_simver_IsMerge_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'IsMerge' -type MockExecution_simver_IsMerge_Call struct { +// MockExecution_simver_IsMerged_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'IsMerged' +type MockExecution_simver_IsMerged_Call struct { *mock.Call } -// IsMerge is a helper method to define mock.On call -func (_e *MockExecution_simver_Expecter) IsMerge() *MockExecution_simver_IsMerge_Call { - return &MockExecution_simver_IsMerge_Call{Call: _e.mock.On("IsMerge")} +// IsMerged is a helper method to define mock.On call +func (_e *MockExecution_simver_Expecter) IsMerged() *MockExecution_simver_IsMerged_Call { + return &MockExecution_simver_IsMerged_Call{Call: _e.mock.On("IsMerged")} } -func (_c *MockExecution_simver_IsMerge_Call) Run(run func()) *MockExecution_simver_IsMerge_Call { +func (_c *MockExecution_simver_IsMerged_Call) Run(run func()) *MockExecution_simver_IsMerged_Call { _c.Call.Run(func(args mock.Arguments) { run() }) return _c } -func (_c *MockExecution_simver_IsMerge_Call) Return(_a0 bool) *MockExecution_simver_IsMerge_Call { +func (_c *MockExecution_simver_IsMerged_Call) Return(_a0 bool) *MockExecution_simver_IsMerged_Call { _c.Call.Return(_a0) return _c } -func (_c *MockExecution_simver_IsMerge_Call) RunAndReturn(run func() bool) *MockExecution_simver_IsMerge_Call { +func (_c *MockExecution_simver_IsMerged_Call) RunAndReturn(run func() bool) *MockExecution_simver_IsMerged_Call { + _c.Call.Return(run) + return _c +} + +// IsMinor provides a mock function with given fields: +func (_m *MockExecution_simver) IsMinor() bool { + ret := _m.Called() + + var r0 bool + if rf, ok := ret.Get(0).(func() bool); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(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 { + *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")} +} + +func (_c *MockExecution_simver_IsMinor_Call) Run(run func()) *MockExecution_simver_IsMinor_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *MockExecution_simver_IsMinor_Call) Return(_a0 bool) *MockExecution_simver_IsMinor_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockExecution_simver_IsMinor_Call) RunAndReturn(run func() bool) *MockExecution_simver_IsMinor_Call { _c.Call.Return(run) return _c } @@ -438,6 +397,133 @@ func (_c *MockExecution_simver_PR_Call) RunAndReturn(run func() int) *MockExecut return _c } +// RootBranchTags provides a mock function with given fields: +func (_m *MockExecution_simver) RootBranchTags() simver.Tags { + ret := _m.Called() + + var r0 simver.Tags + if rf, ok := ret.Get(0).(func() simver.Tags); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(simver.Tags) + } + } + + return r0 +} + +// MockExecution_simver_RootBranchTags_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'RootBranchTags' +type MockExecution_simver_RootBranchTags_Call struct { + *mock.Call +} + +// RootBranchTags is a helper method to define mock.On call +func (_e *MockExecution_simver_Expecter) RootBranchTags() *MockExecution_simver_RootBranchTags_Call { + return &MockExecution_simver_RootBranchTags_Call{Call: _e.mock.On("RootBranchTags")} +} + +func (_c *MockExecution_simver_RootBranchTags_Call) Run(run func()) *MockExecution_simver_RootBranchTags_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *MockExecution_simver_RootBranchTags_Call) Return(_a0 simver.Tags) *MockExecution_simver_RootBranchTags_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockExecution_simver_RootBranchTags_Call) RunAndReturn(run func() simver.Tags) *MockExecution_simver_RootBranchTags_Call { + _c.Call.Return(run) + return _c +} + +// RootCommit provides a mock function with given fields: +func (_m *MockExecution_simver) RootCommit() string { + ret := _m.Called() + + var r0 string + if rf, ok := ret.Get(0).(func() string); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(string) + } + + return r0 +} + +// MockExecution_simver_RootCommit_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'RootCommit' +type MockExecution_simver_RootCommit_Call struct { + *mock.Call +} + +// RootCommit is a helper method to define mock.On call +func (_e *MockExecution_simver_Expecter) RootCommit() *MockExecution_simver_RootCommit_Call { + return &MockExecution_simver_RootCommit_Call{Call: _e.mock.On("RootCommit")} +} + +func (_c *MockExecution_simver_RootCommit_Call) Run(run func()) *MockExecution_simver_RootCommit_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *MockExecution_simver_RootCommit_Call) Return(_a0 string) *MockExecution_simver_RootCommit_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockExecution_simver_RootCommit_Call) RunAndReturn(run func() string) *MockExecution_simver_RootCommit_Call { + _c.Call.Return(run) + return _c +} + +// RootCommitTags provides a mock function with given fields: +func (_m *MockExecution_simver) RootCommitTags() simver.Tags { + ret := _m.Called() + + var r0 simver.Tags + if rf, ok := ret.Get(0).(func() simver.Tags); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(simver.Tags) + } + } + + return r0 +} + +// MockExecution_simver_RootCommitTags_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'RootCommitTags' +type MockExecution_simver_RootCommitTags_Call struct { + *mock.Call +} + +// RootCommitTags is a helper method to define mock.On call +func (_e *MockExecution_simver_Expecter) RootCommitTags() *MockExecution_simver_RootCommitTags_Call { + return &MockExecution_simver_RootCommitTags_Call{Call: _e.mock.On("RootCommitTags")} +} + +func (_c *MockExecution_simver_RootCommitTags_Call) Run(run func()) *MockExecution_simver_RootCommitTags_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *MockExecution_simver_RootCommitTags_Call) Return(_a0 simver.Tags) *MockExecution_simver_RootCommitTags_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockExecution_simver_RootCommitTags_Call) RunAndReturn(run func() simver.Tags) *MockExecution_simver_RootCommitTags_Call { + _c.Call.Return(run) + return _c +} + // NewMockExecution_simver creates a new instance of MockExecution_simver. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. // The first argument is typically a *testing.T value. func NewMockExecution_simver(t interface { diff --git a/pr.go b/pr.go index 481fea2..3b6d286 100644 --- a/pr.go +++ b/pr.go @@ -7,15 +7,14 @@ type PRDetails struct { Number int HeadBranch string BaseBranch string + RootBranch string // always main Merged bool MergeCommit string HeadCommit string - BaseCommit string PotentialMergeCommit string -} -func (d *PRDetails) IsReal() bool { - return d.Number != 0 + BaseCommit string + RootCommit string } type PRProvider interface { From 9f8bc51f156f201f9acf2fba6390bb332d17fd36 Mon Sep 17 00:00:00 2001 From: walter Date: Tue, 14 Nov 2023 06:51:31 -0600 Subject: [PATCH 13/38] some more refactoring --- calculate.go | 7 +- calculate_test.go | 26 +++++-- execution.go | 182 +++++----------------------------------------- execution_test.go | 122 +++++++++++++++++-------------- simver.go | 166 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 276 insertions(+), 227 deletions(-) diff --git a/calculate.go b/calculate.go index ea8bc63..496793c 100644 --- a/calculate.go +++ b/calculate.go @@ -20,9 +20,10 @@ var ( ErrValidatingCalculation = terrors.New("ErrValidatingCalculation") ) -func (me *Calculation) CalculateNewTagsRaw() ([]string, []string) { +func (me *Calculation) CalculateNewTagsRaw() ([]string, []string, []string) { baseTags := make([]string, 0) headTags := make([]string, 0) + rootTags := make([]string, 0) nvt := string(me.NextValidTag) @@ -45,12 +46,12 @@ func (me *Calculation) CalculateNewTagsRaw() ([]string, []string) { // if mmrt is invalid, then we need to reserve a new mmrt (which is the same as nvt) if !validMmrt { mmrt = nvt - baseTags = append(baseTags, nvt+"-reserved") + rootTags = append(rootTags, nvt+"-reserved") baseTags = append(baseTags, nvt+fmt.Sprintf("-pr%d+base", me.PR)) } // then finally we tag mmrt headTags = append(headTags, mmrt+fmt.Sprintf("-pr%d+%d", me.PR, int(me.MyMostRecentBuild)+1)) - return baseTags, headTags + return baseTags, headTags, rootTags } diff --git a/calculate_test.go b/calculate_test.go index 188202a..d023d03 100644 --- a/calculate_test.go +++ b/calculate_test.go @@ -13,6 +13,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { calculation *simver.Calculation expectedBaseTags []string expectedHeadTags []string + expectedRootTags []string }{ { name: "expired mmrt", @@ -25,12 +26,14 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { NextValidTag: "v99.99.99", }, expectedBaseTags: []string{ - "v99.99.99-reserved", "v99.99.99-pr85+base", }, expectedHeadTags: []string{ "v99.99.99-pr85+34", }, + expectedRootTags: []string{ + "v99.99.99-reserved", + }, }, { name: "missing all", @@ -43,12 +46,14 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { NextValidTag: "v3.3.3", }, expectedBaseTags: []string{ - "v3.3.3-reserved", "v3.3.3-pr1+base", }, expectedHeadTags: []string{ "v3.3.3-pr1+2", }, + expectedRootTags: []string{ + "v3.3.3-reserved", + }, }, { name: "valid mmrt", @@ -62,6 +67,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { }, expectedBaseTags: []string{}, expectedHeadTags: []string{"v1.2.4-pr1+34"}, + expectedRootTags: []string{}, }, { @@ -75,27 +81,35 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { NextValidTag: "v1.2.6", }, expectedBaseTags: []string{ - "v1.2.6-reserved", "v1.2.6-pr1+base", }, expectedHeadTags: []string{ "v1.2.6-pr1+34", }, + expectedRootTags: []string{ + "v1.2.6-reserved", + }, }, + // Add more test cases here... } for _, tc := range testCases { - for _, i := range []string{"base", "head"} { + for _, i := range []string{"base", "head", "root"} { t.Run(tc.name+"_"+i, func(t *testing.T) { - baseTags, headTags := tc.calculation.CalculateNewTagsRaw() + baseTags, headTags, rootTags := tc.calculation.CalculateNewTagsRaw() if i == "base" { require.NotContains(t, baseTags, "", "Base tags contain empty string") require.ElementsMatch(t, tc.expectedBaseTags, baseTags, "Base tags do not match") - } else { + } else if i == "head" { require.NotContains(t, headTags, "", "Head tags contain empty string") require.ElementsMatch(t, tc.expectedHeadTags, headTags, "Head tags do not match") + } else if i == "root" { + require.NotContains(t, rootTags, "", "Root tags contain empty string") + require.ElementsMatch(t, tc.expectedRootTags, rootTags, "Root tags do not match") + } else { + require.Fail(t, "invalid test case") } }) } diff --git a/execution.go b/execution.go index 4bc14d6..34d292f 100644 --- a/execution.go +++ b/execution.go @@ -51,7 +51,7 @@ func NewCaclulation(ctx context.Context, ex Execution) *Calculation { func NewTags(ctx context.Context, ex Execution) Tags { calc := NewCaclulation(ctx, ex) - baseTags, headTags := calc.CalculateNewTagsRaw() + baseTags, headTags, rootTags := calc.CalculateNewTagsRaw() tags := make(Tags, 0) for _, tag := range baseTags { @@ -60,6 +60,9 @@ func NewTags(ctx context.Context, ex Execution) Tags { for _, tag := range headTags { tags = append(tags, TagInfo{Name: tag, Ref: ex.HeadCommit()}) } + for _, tag := range rootTags { + tags = append(tags, TagInfo{Name: tag, Ref: ex.RootCommit()}) + } return tags } @@ -69,6 +72,17 @@ type MRRT string // most recent reserved tag type NVT string // next valid tag type MMRT string // my most recent tag type MMRBN int // my most recent build number +type MRT string // my reserved tag + +func MyMostRecentReservedTag(e Execution) MRT { + reg := regexp.MustCompile(fmt.Sprintf(`^v\d+\.\d+\.\d+-pr%d+\+base$`, e.PR())) + highest, err := e.RootBranchTags().HighestSemverMatching(reg) + if err != nil { + return "" + } + + return MRT(strings.Split(semver.Canonical(highest), "-")[0]) +} func MostRecentLiveTag(e Execution) MRLT { reg := regexp.MustCompile(`^v\d+\.\d+\.\d+(|-\S+\+\d+)$`) @@ -81,8 +95,8 @@ func MostRecentLiveTag(e Execution) MRLT { } func MyMostRecentTag(e Execution) MMRT { - reg := regexp.MustCompile(fmt.Sprintf(`^v\d+\.\d+\.\d+-pr%d+\+base$`, e.PR())) - highest, err := e.BaseCommitTags().HighestSemverMatching(reg) + reg := regexp.MustCompile(`^v\d+\.\d+\.\d+.*$`) + highest, err := e.HeadBranchTags().HighestSemverMatching(reg) if err != nil { return "" } @@ -92,7 +106,7 @@ func MyMostRecentTag(e Execution) MMRT { func MostRecentReservedTag(e Execution) MRRT { reg := regexp.MustCompile(`^v\d+\.\d+\.\d+-reserved$`) - highest, err := e.BaseBranchTags().HighestSemverMatching(reg) + highest, err := e.RootBranchTags().HighestSemverMatching(reg) if err != nil { return "" } @@ -181,163 +195,3 @@ func GetNextValidTag(ctx context.Context, minor bool, mrlt MRLT, mrrt MRRT) NVT return NVT(fmt.Sprintf("%s.%d.%d", semver.Major(max), minornum, patchnum)) } - -var _ Execution = &rawExecution{} - -type rawExecution struct { - pr *PRDetails - baseBranch string - headBranch string - rootBranch string - headCommit string - baseCommit string - rootCommit string - rootBranchTags Tags - rootCommitTags Tags - headCommitTags Tags - baseCommitTags Tags - baseBranchTags Tags - headBranchTags Tags - isMerged bool - isMinor bool -} - -func (e *rawExecution) BaseCommit() string { - return e.baseCommit -} - -func (e *rawExecution) HeadCommit() string { - return e.headCommit -} - -func (e *rawExecution) BaseCommitTags() Tags { - return e.baseCommitTags -} - -func (e *rawExecution) HeadCommitTags() Tags { - return e.headCommitTags -} - -func (e *rawExecution) BaseBranchTags() Tags { - return e.baseBranchTags -} - -func (e *rawExecution) HeadBranchTags() Tags { - return e.headBranchTags -} - -func (e *rawExecution) PR() int { - return e.pr.Number -} - -// func (e *rawExecution) BaseBranch() string { -// return e.baseBranch -// } - -// func (e *rawExecution) HeadBranch() string { -// return e.headBranch -// } - -func (e *rawExecution) IsMerged() bool { - return e.pr.Merged -} - -func (e *rawExecution) RootCommit() string { - return e.rootCommit -} - -func (e *rawExecution) RootBranch() string { - return e.rootBranch -} - -func (e *rawExecution) RootBranchTags() Tags { - return e.rootBranchTags -} - -func (e *rawExecution) RootCommitTags() Tags { - return e.rootCommitTags -} - -func (e *rawExecution) IsMinor() bool { - return e.baseBranch == e.rootBranch -} - -func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Execution, *PRDetails, bool, error) { - - pr, err := prr.CurrentPR(ctx) - if err != nil { - return nil, nil, false, err - } - - if pr.Number == 0 && pr.HeadBranch != "main" { - return nil, nil, false, nil - } - - _, err = tprov.FetchTags(ctx) - if err != nil { - return nil, nil, false, err - } - - baseCommitTags, err := tprov.TagsFromCommit(ctx, pr.BaseCommit) - if err != nil { - return nil, nil, false, err - } - - baseBranchTags, err := tprov.TagsFromBranch(ctx, pr.BaseBranch) - if err != nil { - return nil, nil, false, err - } - - rootCommitTags, err := tprov.TagsFromCommit(ctx, pr.RootCommit) - if err != nil { - return nil, nil, false, err - } - - rootBranchTags, err := tprov.TagsFromBranch(ctx, pr.RootBranch) - if err != nil { - return nil, nil, false, err - } - - hc := pr.HeadCommit - - if pr.Merged { - hc = pr.MergeCommit - } - - headTags, err := tprov.TagsFromCommit(ctx, hc) - if err != nil { - return nil, nil, false, err - } - - branchTags, err := tprov.TagsFromBranch(ctx, pr.HeadBranch) - if err != nil { - return nil, nil, false, err - } - - zerolog.Ctx(ctx).Debug(). - Any("baseCommitTags", baseCommitTags). - Any("baseBranchTags", baseBranchTags). - Any("rootCommitTags", rootCommitTags). - Any("rootBranchTags", rootBranchTags). - Any("headTags", headTags). - Any("branchTags", branchTags). - Any("pr", pr). - Msg("loaded tags") - - return &rawExecution{ - pr: pr, - baseBranch: pr.BaseBranch, - headBranch: pr.BaseBranch, - headCommit: pr.HeadCommit, - baseCommit: pr.BaseCommit, - headCommitTags: headTags, - baseCommitTags: baseCommitTags, - baseBranchTags: baseBranchTags, - headBranchTags: branchTags, - rootBranch: pr.RootBranch, - rootCommit: pr.RootCommit, - rootBranchTags: rootBranchTags, - rootCommitTags: rootCommitTags, - }, pr, true, nil - -} diff --git a/execution_test.go b/execution_test.go index 7485387..4e7c881 100644 --- a/execution_test.go +++ b/execution_test.go @@ -60,7 +60,7 @@ func TestMmrt(t *testing.T) { name: "Invalid MMRT", prNum: 3, tags: simver.Tags{simver.TagInfo{Name: "v1.2.3-pr3+0"}}, - expectedMmrt: "", + expectedMmrt: "v1.2.3", }, { name: "No MMRT", @@ -72,15 +72,15 @@ func TestMmrt(t *testing.T) { name: "Non-Matching PR Number", prNum: 3, tags: simver.Tags{simver.TagInfo{Name: "v1.2.3-pr1+base"}}, - expectedMmrt: "", + expectedMmrt: "v1.2.3", }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { mockExec := new(mockery.MockExecution_simver) - mockExec.EXPECT().PR().Return(tc.prNum) - mockExec.EXPECT().BaseCommitTags().Return(tc.tags) + // mockExec.EXPECT().PR().Return(tc.prNum) + mockExec.EXPECT().HeadBranchTags().Return(tc.tags) result := simver.MyMostRecentTag(mockExec) mockExec.AssertExpectations(t) assert.Equal(t, tc.expectedMmrt, result) @@ -114,7 +114,7 @@ func TestMrrt(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { mockExec := new(mockery.MockExecution_simver) - mockExec.EXPECT().BaseBranchTags().Return(tc.tags) + mockExec.EXPECT().RootBranchTags().Return(tc.tags) result := simver.MostRecentReservedTag(mockExec) mockExec.AssertExpectations(t) assert.Equal(t, tc.expectedMrrt, result) @@ -184,6 +184,12 @@ func TestNvt(t *testing.T) { } } +const ( + root_ref = "root_ref" + base_ref = "base_ref" + head_ref = "head_ref" +) + func TestNewTags(t *testing.T) { testCases := []struct { name string @@ -207,18 +213,18 @@ func TestNewTags(t *testing.T) { baseCommitTags: simver.Tags{}, baseBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3"}}, headBranchTags: simver.Tags{}, - headCommit: "head123", - baseCommit: "base456", - rootCommit: "root789", + headCommit: head_ref, + baseCommit: base_ref, + rootCommit: root_ref, rootBranchTags: simver.Tags{}, rootCommitTags: simver.Tags{}, pr: 0, isMerged: false, isMinor: false, expectedTags: simver.Tags{ - simver.TagInfo{Name: "v1.2.4-pr0+1", Ref: "head123"}, - simver.TagInfo{Name: "v1.2.4-reserved", Ref: "base456"}, - simver.TagInfo{Name: "v1.2.4-pr0+base", Ref: "base456"}, + simver.TagInfo{Name: "v1.2.4-pr0+1", Ref: head_ref}, + simver.TagInfo{Name: "v1.2.4-reserved", Ref: root_ref}, + simver.TagInfo{Name: "v1.2.4-pr0+base", Ref: base_ref}, }, }, { @@ -227,67 +233,73 @@ func TestNewTags(t *testing.T) { baseCommitTags: simver.Tags{}, baseBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3"}}, headBranchTags: simver.Tags{}, - headCommit: "head123", - baseCommit: "base456", - rootCommit: "root789", + headCommit: head_ref, + baseCommit: base_ref, + rootCommit: root_ref, rootBranchTags: simver.Tags{}, rootCommitTags: simver.Tags{}, pr: 99, isMerged: false, isMinor: true, expectedTags: simver.Tags{ - simver.TagInfo{Name: "v1.3.0-pr99+1", Ref: "head123"}, - simver.TagInfo{Name: "v1.3.0-reserved", Ref: "base456"}, - simver.TagInfo{Name: "v1.3.0-pr99+base", Ref: "base456"}, + simver.TagInfo{Name: "v1.3.0-pr99+1", Ref: head_ref}, + simver.TagInfo{Name: "v1.3.0-reserved", Ref: root_ref}, + simver.TagInfo{Name: "v1.3.0-pr99+base", Ref: base_ref}, }, }, { name: "PR Merge with Valid MMRT", headCommitTags: simver.Tags{}, - baseCommitTags: simver.Tags{simver.TagInfo{Name: "v1.2.4-reserved"}, simver.TagInfo{Name: "v1.2.4-pr1+base"}}, + baseCommitTags: simver.Tags{simver.TagInfo{Name: "v1.2.4-pr1+base"}}, baseBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3"}}, headBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.4-pr1+1002"}}, - headCommit: "head123", - baseCommit: "base456", - rootCommit: "root789", - rootBranchTags: simver.Tags{}, + headCommit: head_ref, + baseCommit: base_ref, + rootCommit: root_ref, + rootBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.4-reserved"}}, rootCommitTags: simver.Tags{}, pr: 1, isMerged: false, isMinor: false, - expectedTags: simver.Tags{simver.TagInfo{Name: "v1.2.4-pr1+1003", Ref: "head123"}}, + expectedTags: simver.Tags{ + simver.TagInfo{Name: "v1.2.4-pr1+1003", Ref: head_ref}, + }, }, { name: "PR Merge with No MMRT", headCommitTags: simver.Tags{}, - baseCommitTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-reserved"}, simver.TagInfo{Name: "v1.5.9-pr87+base"}}, + baseCommitTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-pr87+base"}}, baseBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3"}}, headBranchTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-pr87+1002"}}, - headCommit: "head123", - baseCommit: "base456", - rootCommit: "root789", - rootBranchTags: simver.Tags{}, + headCommit: head_ref, + baseCommit: base_ref, + rootCommit: root_ref, + rootBranchTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-reserved"}}, rootCommitTags: simver.Tags{}, pr: 87, isMerged: false, isMinor: false, - expectedTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-pr87+1003", Ref: "head123"}}, + expectedTags: simver.Tags{ + simver.TagInfo{Name: "v1.5.9-pr87+1003", Ref: head_ref}, + }, }, { name: "PR Merge with Invalid MMRT", - headCommitTags: simver.Tags{simver.TagInfo{Name: "v1.2.99999-pr2+base"}}, - baseCommitTags: simver.Tags{simver.TagInfo{Name: "v1.2.3-reserved"}, simver.TagInfo{Name: "v1.2.4-pr2+base"}}, + headCommitTags: simver.Tags{simver.TagInfo{Name: "v1.2.999999-pr2+5"}}, + baseCommitTags: simver.Tags{simver.TagInfo{Name: "v1.2.4-pr2+base"}}, baseBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3"}}, - headBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.99999-pr2+base"}}, - headCommit: "head123", - baseCommit: "base456", - rootCommit: "root789", - rootBranchTags: simver.Tags{}, + headBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.4-pr2+5"}}, + headCommit: head_ref, + baseCommit: base_ref, + rootCommit: root_ref, + rootBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3-reserved"}}, rootCommitTags: simver.Tags{}, pr: 2, isMerged: false, isMinor: false, - expectedTags: simver.Tags{simver.TagInfo{Name: "v1.2.4-pr2+1", Ref: "head123"}}, + expectedTags: simver.Tags{ + simver.TagInfo{Name: "v1.2.4-pr2+6", Ref: head_ref}, + }, }, { name: "No Tags Available for PR Commit", @@ -295,9 +307,9 @@ func TestNewTags(t *testing.T) { baseCommitTags: simver.Tags{}, baseBranchTags: simver.Tags{}, headBranchTags: simver.Tags{}, - headCommit: "head123", - baseCommit: "base123", - rootCommit: "root789", + headCommit: head_ref, + baseCommit: base_ref, + rootCommit: root_ref, rootBranchTags: simver.Tags{}, rootCommitTags: simver.Tags{}, pr: 0, @@ -306,11 +318,11 @@ func TestNewTags(t *testing.T) { expectedTags: simver.Tags{ // we also need to reserve the next version tag // which should be v0.2.0 since the base branch is main - simver.TagInfo{Name: "v0.2.0-reserved", Ref: "base123"}, - simver.TagInfo{Name: "v0.2.0-pr0+base", Ref: "base123"}, + simver.TagInfo{Name: "v0.2.0-reserved", Ref: root_ref}, + simver.TagInfo{Name: "v0.2.0-pr0+base", Ref: base_ref}, // and finally, we need to tag the commit with the PR number // since the base branch is main, we set it to v0.2.0-pr0 - simver.TagInfo{Name: "v0.2.0-pr0+1", Ref: "head123"}, + simver.TagInfo{Name: "v0.2.0-pr0+1", Ref: head_ref}, }, }, { @@ -319,9 +331,9 @@ func TestNewTags(t *testing.T) { baseCommitTags: simver.Tags{}, baseBranchTags: simver.Tags{}, headBranchTags: simver.Tags{}, - headCommit: "head123", - baseCommit: "base123", - rootCommit: "root789", + headCommit: head_ref, + baseCommit: base_ref, + rootCommit: root_ref, rootBranchTags: simver.Tags{}, rootCommitTags: simver.Tags{}, pr: 2, @@ -330,27 +342,29 @@ func TestNewTags(t *testing.T) { 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 - simver.TagInfo{Name: "v0.2.0-pr2+1", Ref: "head123"}, + simver.TagInfo{Name: "v0.2.0-pr2+1", Ref: head_ref}, // we need to make sure we have a reserved tag for the base branch - simver.TagInfo{Name: "v0.2.0-reserved", Ref: "base123"}, - simver.TagInfo{Name: "v0.2.0-pr2+base", Ref: "base123"}, + simver.TagInfo{Name: "v0.2.0-reserved", Ref: root_ref}, + simver.TagInfo{Name: "v0.2.0-pr2+base", Ref: base_ref}, }, }, { name: "merge", headCommitTags: simver.Tags{}, baseCommitTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-reserved"}, simver.TagInfo{Name: "v1.5.9-pr87+base"}}, + rootCommitTags: simver.Tags{}, baseBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3"}}, headBranchTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-pr87+1002"}}, - headCommit: "head123", - baseCommit: "base456", - rootCommit: "root789", rootBranchTags: simver.Tags{}, - rootCommitTags: simver.Tags{}, + headCommit: head_ref, + baseCommit: base_ref, + rootCommit: root_ref, pr: 87, isMerged: true, isMinor: false, - expectedTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-pr87+1003", Ref: "head123"}}, + expectedTags: simver.Tags{ + simver.TagInfo{Name: "v1.5.9-pr87+1003", Ref: head_ref}, + }, }, } diff --git a/simver.go b/simver.go index c60de98..6037314 100644 --- a/simver.go +++ b/simver.go @@ -1 +1,167 @@ package simver + +import ( + "context" + + "github.com/rs/zerolog" +) + +var _ Execution = &rawExecution{} + +type rawExecution struct { + pr *PRDetails + baseBranch string + headBranch string + rootBranch string + headCommit string + baseCommit string + rootCommit string + rootBranchTags Tags + rootCommitTags Tags + headCommitTags Tags + baseCommitTags Tags + baseBranchTags Tags + headBranchTags Tags + isMerged bool + isMinor bool +} + +func (e *rawExecution) BaseCommit() string { + return e.baseCommit +} + +func (e *rawExecution) HeadCommit() string { + return e.headCommit +} + +func (e *rawExecution) BaseCommitTags() Tags { + return e.baseCommitTags +} + +func (e *rawExecution) HeadCommitTags() Tags { + return e.headCommitTags +} + +func (e *rawExecution) BaseBranchTags() Tags { + return e.baseBranchTags +} + +func (e *rawExecution) HeadBranchTags() Tags { + return e.headBranchTags +} + +func (e *rawExecution) PR() int { + return e.pr.Number +} + +// func (e *rawExecution) BaseBranch() string { +// return e.baseBranch +// } + +// func (e *rawExecution) HeadBranch() string { +// return e.headBranch +// } + +func (e *rawExecution) IsMerged() bool { + return e.pr.Merged +} + +func (e *rawExecution) RootCommit() string { + return e.rootCommit +} + +func (e *rawExecution) RootBranch() string { + return e.rootBranch +} + +func (e *rawExecution) RootBranchTags() Tags { + return e.rootBranchTags +} + +func (e *rawExecution) RootCommitTags() Tags { + return e.rootCommitTags +} + +func (e *rawExecution) IsMinor() bool { + return e.baseBranch == e.rootBranch +} + +func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Execution, *PRDetails, bool, error) { + + pr, err := prr.CurrentPR(ctx) + if err != nil { + return nil, nil, false, err + } + + if pr.Number == 0 && pr.HeadBranch != "main" { + return nil, nil, false, nil + } + + _, err = tprov.FetchTags(ctx) + if err != nil { + return nil, nil, false, err + } + + baseCommitTags, err := tprov.TagsFromCommit(ctx, pr.BaseCommit) + if err != nil { + return nil, nil, false, err + } + + baseBranchTags, err := tprov.TagsFromBranch(ctx, pr.BaseBranch) + if err != nil { + return nil, nil, false, err + } + + rootCommitTags, err := tprov.TagsFromCommit(ctx, pr.RootCommit) + if err != nil { + return nil, nil, false, err + } + + rootBranchTags, err := tprov.TagsFromBranch(ctx, pr.RootBranch) + if err != nil { + return nil, nil, false, err + } + + hc := pr.HeadCommit + + if pr.Merged { + hc = pr.MergeCommit + } + + headTags, err := tprov.TagsFromCommit(ctx, hc) + if err != nil { + return nil, nil, false, err + } + + branchTags, err := tprov.TagsFromBranch(ctx, pr.HeadBranch) + if err != nil { + return nil, nil, false, err + } + + zerolog.Ctx(ctx).Debug(). + Any("baseCommitTags", baseCommitTags). + Any("baseBranchTags", baseBranchTags). + Any("rootCommitTags", rootCommitTags). + Any("rootBranchTags", rootBranchTags). + Any("headTags", headTags). + Any("branchTags", branchTags). + Any("pr", pr). + Msg("loaded tags") + + return &rawExecution{ + pr: pr, + baseBranch: pr.BaseBranch, + headBranch: pr.BaseBranch, + headCommit: pr.HeadCommit, + baseCommit: pr.BaseCommit, + headCommitTags: headTags, + baseCommitTags: baseCommitTags, + baseBranchTags: baseBranchTags, + headBranchTags: branchTags, + rootBranch: pr.RootBranch, + rootCommit: pr.RootCommit, + rootBranchTags: rootBranchTags, + rootCommitTags: rootCommitTags, + }, pr, true, nil + +} From a989d42938b5241a3cbeb50627b7cc0d2e09fec3 Mon Sep 17 00:00:00 2001 From: walter Date: Tue, 14 Nov 2023 07:07:52 -0600 Subject: [PATCH 14/38] refactor for simpler testing --- calculate.go | 44 ++++- calculate_test.go | 14 +- cmd/simver_github_actions/main.go | 4 +- execution.go | 42 ++--- execution_test.go | 21 ++- gen/mockery/Execution.simver.mockery.go | 238 ++---------------------- simver.go | 44 +++-- 7 files changed, 121 insertions(+), 286 deletions(-) diff --git a/calculate.go b/calculate.go index 496793c..06b667f 100644 --- a/calculate.go +++ b/calculate.go @@ -20,10 +20,38 @@ var ( ErrValidatingCalculation = terrors.New("ErrValidatingCalculation") ) -func (me *Calculation) CalculateNewTagsRaw() ([]string, []string, []string) { - baseTags := make([]string, 0) - headTags := make([]string, 0) - rootTags := make([]string, 0) +type CalculationOutput struct { + BaseTags []string + HeadTags []string + RootTags []string +} + +type ApplyRefsOpts struct { + HeadRef string + BaseRef string + RootRef string +} + +func (out *CalculationOutput) ApplyRefs(opts *ApplyRefsOpts) Tags { + tags := make(Tags, 0) + for _, tag := range out.BaseTags { + tags = append(tags, TagInfo{Name: tag, Ref: opts.BaseRef}) + } + for _, tag := range out.HeadTags { + tags = append(tags, TagInfo{Name: tag, Ref: opts.HeadRef}) + } + for _, tag := range out.RootTags { + tags = append(tags, TagInfo{Name: tag, Ref: opts.RootRef}) + } + return tags +} + +func (me *Calculation) CalculateNewTagsRaw() *CalculationOutput { + out := &CalculationOutput{ + BaseTags: []string{}, + HeadTags: []string{}, + RootTags: []string{}, + } nvt := string(me.NextValidTag) @@ -46,12 +74,12 @@ func (me *Calculation) CalculateNewTagsRaw() ([]string, []string, []string) { // if mmrt is invalid, then we need to reserve a new mmrt (which is the same as nvt) if !validMmrt { mmrt = nvt - rootTags = append(rootTags, nvt+"-reserved") - baseTags = append(baseTags, nvt+fmt.Sprintf("-pr%d+base", me.PR)) + out.RootTags = append(out.RootTags, nvt+"-reserved") + out.BaseTags = append(out.BaseTags, nvt+fmt.Sprintf("-pr%d+base", me.PR)) } // then finally we tag mmrt - headTags = append(headTags, mmrt+fmt.Sprintf("-pr%d+%d", me.PR, int(me.MyMostRecentBuild)+1)) + out.HeadTags = append(out.HeadTags, mmrt+fmt.Sprintf("-pr%d+%d", me.PR, int(me.MyMostRecentBuild)+1)) - return baseTags, headTags, rootTags + return out } diff --git a/calculate_test.go b/calculate_test.go index d023d03..31c49db 100644 --- a/calculate_test.go +++ b/calculate_test.go @@ -97,17 +97,17 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { for _, tc := range testCases { for _, i := range []string{"base", "head", "root"} { t.Run(tc.name+"_"+i, func(t *testing.T) { - baseTags, headTags, rootTags := tc.calculation.CalculateNewTagsRaw() + out := tc.calculation.CalculateNewTagsRaw() if i == "base" { - require.NotContains(t, baseTags, "", "Base tags contain empty string") - require.ElementsMatch(t, tc.expectedBaseTags, baseTags, "Base tags do not match") + 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, headTags, "", "Head tags contain empty string") - require.ElementsMatch(t, tc.expectedHeadTags, headTags, "Head tags do not match") + 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, rootTags, "", "Root tags contain empty string") - require.ElementsMatch(t, tc.expectedRootTags, rootTags, "Root tags do not match") + require.NotContains(t, out.RootTags, "", "Root tags contain empty string") + require.ElementsMatch(t, tc.expectedRootTags, out.RootTags, "Root 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 60f4299..debf298 100644 --- a/cmd/simver_github_actions/main.go +++ b/cmd/simver_github_actions/main.go @@ -178,7 +178,7 @@ func main() { os.Exit(0) } - tags := simver.NewTags(ctx, ee) + tags := ee.BuildTags(simver.NewTags(ctx, ee)) reservedTag, reserved := tags.GetReserved() @@ -207,7 +207,7 @@ func main() { } ee = eez prd = prz - tags := simver.NewTags(ctx, ee) + tags := ee.BuildTags(simver.NewTags(ctx, ee)) reservedTag, reserved = tags.GetReserved() } else { reserved = false diff --git a/execution.go b/execution.go index 34d292f..110372f 100644 --- a/execution.go +++ b/execution.go @@ -15,22 +15,24 @@ type Execution interface { PR() int IsMinor() bool IsMerged() bool - HeadCommit() string - BaseCommit() string + // HeadCommit() string + // BaseCommit() string // HeadBranch() string // BaseBranch() string - HeadCommitTags() Tags - BaseCommitTags() Tags + // HeadCommitTags() Tags + // BaseCommitTags() Tags HeadBranchTags() Tags BaseBranchTags() Tags - RootCommit() string + // RootCommit() string // RootBranch() string // always main RootBranchTags() Tags - RootCommitTags() Tags + // RootCommitTags() Tags + + BuildTags(tags *CalculationOutput) Tags } const baseTag = "v0.1.0" @@ -48,23 +50,21 @@ func NewCaclulation(ctx context.Context, ex Execution) *Calculation { } } -func NewTags(ctx context.Context, ex Execution) Tags { +func NewTags(ctx context.Context, ex Execution) *CalculationOutput { calc := NewCaclulation(ctx, ex) - baseTags, headTags, rootTags := calc.CalculateNewTagsRaw() - - tags := make(Tags, 0) - for _, tag := range baseTags { - tags = append(tags, TagInfo{Name: tag, Ref: ex.BaseCommit()}) - } - for _, tag := range headTags { - tags = append(tags, TagInfo{Name: tag, Ref: ex.HeadCommit()}) - } - for _, tag := range rootTags { - tags = append(tags, TagInfo{Name: tag, Ref: ex.RootCommit()}) - } - - return tags + // tags := make(Tags, 0) + // for _, tag := range baseTags { + // tags = append(tags, TagInfo{Name: tag, Ref: ex.BaseCommit()}) + // } + // for _, tag := range headTags { + // tags = append(tags, TagInfo{Name: tag, Ref: ex.HeadCommit()}) + // } + // for _, tag := range rootTags { + // tags = append(tags, TagInfo{Name: tag, Ref: ex.RootCommit()}) + // } + + return calc.CalculateNewTagsRaw() } type MRLT string // most recent live tag diff --git a/execution_test.go b/execution_test.go index 4e7c881..1530ac7 100644 --- a/execution_test.go +++ b/execution_test.go @@ -373,22 +373,29 @@ func TestNewTags(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { mockExec := new(mockery.MockExecution_simver) - mockExec.EXPECT().HeadCommitTags().Return(tc.headCommitTags) - mockExec.EXPECT().BaseCommitTags().Return(tc.baseCommitTags) - mockExec.EXPECT().HeadCommit().Return(tc.headCommit) - mockExec.EXPECT().BaseCommit().Return(tc.baseCommit) + // mockExec.EXPECT().HeadCommitTags().Return(tc.headCommitTags) + // mockExec.EXPECT().BaseCommitTags().Return(tc.baseCommitTags) + // mockExec.EXPECT().HeadCommit().Return(tc.headCommit) + // mockExec.EXPECT().BaseCommit().Return(tc.baseCommit) mockExec.EXPECT().HeadBranchTags().Return(tc.headBranchTags) mockExec.EXPECT().BaseBranchTags().Return(tc.baseBranchTags) mockExec.EXPECT().PR().Return(tc.pr) mockExec.EXPECT().IsMinor().Return(tc.isMinor) mockExec.EXPECT().IsMerged().Return(tc.isMerged) - mockExec.EXPECT().RootCommit().Return(tc.rootCommit) + // mockExec.EXPECT().RootCommit().Return(tc.rootCommit) // mockExec.EXPECT().RootBranch().Return(tc.rootBranch) mockExec.EXPECT().RootBranchTags().Return(tc.rootBranchTags) - mockExec.EXPECT().RootCommitTags().Return(tc.rootCommitTags) + // mockExec.EXPECT().RootCommitTags().Return(tc.rootCommitTags) result := simver.NewTags(ctx, mockExec) - assert.ElementsMatch(t, tc.expectedTags, result) + + got := result.ApplyRefs(&simver.ApplyRefsOpts{ + HeadRef: head_ref, + BaseRef: base_ref, + RootRef: root_ref, + }) + + assert.ElementsMatch(t, tc.expectedTags, got) }) } } diff --git a/gen/mockery/Execution.simver.mockery.go b/gen/mockery/Execution.simver.mockery.go index bc26719..4ae75dd 100644 --- a/gen/mockery/Execution.simver.mockery.go +++ b/gen/mockery/Execution.simver.mockery.go @@ -63,54 +63,13 @@ func (_c *MockExecution_simver_BaseBranchTags_Call) RunAndReturn(run func() simv return _c } -// BaseCommit provides a mock function with given fields: -func (_m *MockExecution_simver) BaseCommit() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// MockExecution_simver_BaseCommit_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'BaseCommit' -type MockExecution_simver_BaseCommit_Call struct { - *mock.Call -} - -// BaseCommit is a helper method to define mock.On call -func (_e *MockExecution_simver_Expecter) BaseCommit() *MockExecution_simver_BaseCommit_Call { - return &MockExecution_simver_BaseCommit_Call{Call: _e.mock.On("BaseCommit")} -} - -func (_c *MockExecution_simver_BaseCommit_Call) Run(run func()) *MockExecution_simver_BaseCommit_Call { - _c.Call.Run(func(args mock.Arguments) { - run() - }) - return _c -} - -func (_c *MockExecution_simver_BaseCommit_Call) Return(_a0 string) *MockExecution_simver_BaseCommit_Call { - _c.Call.Return(_a0) - return _c -} - -func (_c *MockExecution_simver_BaseCommit_Call) RunAndReturn(run func() string) *MockExecution_simver_BaseCommit_Call { - _c.Call.Return(run) - return _c -} - -// BaseCommitTags provides a mock function with given fields: -func (_m *MockExecution_simver) BaseCommitTags() simver.Tags { - ret := _m.Called() +// BuildTags provides a mock function with given fields: tags +func (_m *MockExecution_simver) BuildTags(tags *simver.CalculationOutput) simver.Tags { + ret := _m.Called(tags) var r0 simver.Tags - if rf, ok := ret.Get(0).(func() simver.Tags); ok { - r0 = rf() + if rf, ok := ret.Get(0).(func(*simver.CalculationOutput) simver.Tags); ok { + r0 = rf(tags) } else { if ret.Get(0) != nil { r0 = ret.Get(0).(simver.Tags) @@ -120,29 +79,30 @@ func (_m *MockExecution_simver) BaseCommitTags() simver.Tags { return r0 } -// MockExecution_simver_BaseCommitTags_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'BaseCommitTags' -type MockExecution_simver_BaseCommitTags_Call struct { +// MockExecution_simver_BuildTags_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'BuildTags' +type MockExecution_simver_BuildTags_Call struct { *mock.Call } -// BaseCommitTags is a helper method to define mock.On call -func (_e *MockExecution_simver_Expecter) BaseCommitTags() *MockExecution_simver_BaseCommitTags_Call { - return &MockExecution_simver_BaseCommitTags_Call{Call: _e.mock.On("BaseCommitTags")} +// BuildTags is a helper method to define mock.On call +// - tags *simver.CalculationOutput +func (_e *MockExecution_simver_Expecter) BuildTags(tags interface{}) *MockExecution_simver_BuildTags_Call { + return &MockExecution_simver_BuildTags_Call{Call: _e.mock.On("BuildTags", tags)} } -func (_c *MockExecution_simver_BaseCommitTags_Call) Run(run func()) *MockExecution_simver_BaseCommitTags_Call { +func (_c *MockExecution_simver_BuildTags_Call) Run(run func(tags *simver.CalculationOutput)) *MockExecution_simver_BuildTags_Call { _c.Call.Run(func(args mock.Arguments) { - run() + run(args[0].(*simver.CalculationOutput)) }) return _c } -func (_c *MockExecution_simver_BaseCommitTags_Call) Return(_a0 simver.Tags) *MockExecution_simver_BaseCommitTags_Call { +func (_c *MockExecution_simver_BuildTags_Call) Return(_a0 simver.Tags) *MockExecution_simver_BuildTags_Call { _c.Call.Return(_a0) return _c } -func (_c *MockExecution_simver_BaseCommitTags_Call) RunAndReturn(run func() simver.Tags) *MockExecution_simver_BaseCommitTags_Call { +func (_c *MockExecution_simver_BuildTags_Call) RunAndReturn(run func(*simver.CalculationOutput) simver.Tags) *MockExecution_simver_BuildTags_Call { _c.Call.Return(run) return _c } @@ -190,90 +150,6 @@ func (_c *MockExecution_simver_HeadBranchTags_Call) RunAndReturn(run func() simv return _c } -// HeadCommit provides a mock function with given fields: -func (_m *MockExecution_simver) HeadCommit() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// MockExecution_simver_HeadCommit_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'HeadCommit' -type MockExecution_simver_HeadCommit_Call struct { - *mock.Call -} - -// HeadCommit is a helper method to define mock.On call -func (_e *MockExecution_simver_Expecter) HeadCommit() *MockExecution_simver_HeadCommit_Call { - return &MockExecution_simver_HeadCommit_Call{Call: _e.mock.On("HeadCommit")} -} - -func (_c *MockExecution_simver_HeadCommit_Call) Run(run func()) *MockExecution_simver_HeadCommit_Call { - _c.Call.Run(func(args mock.Arguments) { - run() - }) - return _c -} - -func (_c *MockExecution_simver_HeadCommit_Call) Return(_a0 string) *MockExecution_simver_HeadCommit_Call { - _c.Call.Return(_a0) - return _c -} - -func (_c *MockExecution_simver_HeadCommit_Call) RunAndReturn(run func() string) *MockExecution_simver_HeadCommit_Call { - _c.Call.Return(run) - return _c -} - -// HeadCommitTags provides a mock function with given fields: -func (_m *MockExecution_simver) HeadCommitTags() simver.Tags { - ret := _m.Called() - - var r0 simver.Tags - if rf, ok := ret.Get(0).(func() simver.Tags); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(simver.Tags) - } - } - - return r0 -} - -// MockExecution_simver_HeadCommitTags_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'HeadCommitTags' -type MockExecution_simver_HeadCommitTags_Call struct { - *mock.Call -} - -// HeadCommitTags is a helper method to define mock.On call -func (_e *MockExecution_simver_Expecter) HeadCommitTags() *MockExecution_simver_HeadCommitTags_Call { - return &MockExecution_simver_HeadCommitTags_Call{Call: _e.mock.On("HeadCommitTags")} -} - -func (_c *MockExecution_simver_HeadCommitTags_Call) Run(run func()) *MockExecution_simver_HeadCommitTags_Call { - _c.Call.Run(func(args mock.Arguments) { - run() - }) - return _c -} - -func (_c *MockExecution_simver_HeadCommitTags_Call) Return(_a0 simver.Tags) *MockExecution_simver_HeadCommitTags_Call { - _c.Call.Return(_a0) - return _c -} - -func (_c *MockExecution_simver_HeadCommitTags_Call) RunAndReturn(run func() simver.Tags) *MockExecution_simver_HeadCommitTags_Call { - _c.Call.Return(run) - return _c -} - // IsMerged provides a mock function with given fields: func (_m *MockExecution_simver) IsMerged() bool { ret := _m.Called() @@ -440,90 +316,6 @@ func (_c *MockExecution_simver_RootBranchTags_Call) RunAndReturn(run func() simv return _c } -// RootCommit provides a mock function with given fields: -func (_m *MockExecution_simver) RootCommit() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// MockExecution_simver_RootCommit_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'RootCommit' -type MockExecution_simver_RootCommit_Call struct { - *mock.Call -} - -// RootCommit is a helper method to define mock.On call -func (_e *MockExecution_simver_Expecter) RootCommit() *MockExecution_simver_RootCommit_Call { - return &MockExecution_simver_RootCommit_Call{Call: _e.mock.On("RootCommit")} -} - -func (_c *MockExecution_simver_RootCommit_Call) Run(run func()) *MockExecution_simver_RootCommit_Call { - _c.Call.Run(func(args mock.Arguments) { - run() - }) - return _c -} - -func (_c *MockExecution_simver_RootCommit_Call) Return(_a0 string) *MockExecution_simver_RootCommit_Call { - _c.Call.Return(_a0) - return _c -} - -func (_c *MockExecution_simver_RootCommit_Call) RunAndReturn(run func() string) *MockExecution_simver_RootCommit_Call { - _c.Call.Return(run) - return _c -} - -// RootCommitTags provides a mock function with given fields: -func (_m *MockExecution_simver) RootCommitTags() simver.Tags { - ret := _m.Called() - - var r0 simver.Tags - if rf, ok := ret.Get(0).(func() simver.Tags); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(simver.Tags) - } - } - - return r0 -} - -// MockExecution_simver_RootCommitTags_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'RootCommitTags' -type MockExecution_simver_RootCommitTags_Call struct { - *mock.Call -} - -// RootCommitTags is a helper method to define mock.On call -func (_e *MockExecution_simver_Expecter) RootCommitTags() *MockExecution_simver_RootCommitTags_Call { - return &MockExecution_simver_RootCommitTags_Call{Call: _e.mock.On("RootCommitTags")} -} - -func (_c *MockExecution_simver_RootCommitTags_Call) Run(run func()) *MockExecution_simver_RootCommitTags_Call { - _c.Call.Run(func(args mock.Arguments) { - run() - }) - return _c -} - -func (_c *MockExecution_simver_RootCommitTags_Call) Return(_a0 simver.Tags) *MockExecution_simver_RootCommitTags_Call { - _c.Call.Return(_a0) - return _c -} - -func (_c *MockExecution_simver_RootCommitTags_Call) RunAndReturn(run func() simver.Tags) *MockExecution_simver_RootCommitTags_Call { - _c.Call.Return(run) - return _c -} - // NewMockExecution_simver creates a new instance of MockExecution_simver. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. // The first argument is typically a *testing.T value. func NewMockExecution_simver(t interface { diff --git a/simver.go b/simver.go index 6037314..c314c87 100644 --- a/simver.go +++ b/simver.go @@ -26,21 +26,21 @@ type rawExecution struct { isMinor bool } -func (e *rawExecution) BaseCommit() string { - return e.baseCommit -} +// func (e *rawExecution) BaseCommit() string { +// return e.baseCommit +// } -func (e *rawExecution) HeadCommit() string { - return e.headCommit -} +// func (e *rawExecution) HeadCommit() string { +// return e.headCommit +// } -func (e *rawExecution) BaseCommitTags() Tags { - return e.baseCommitTags -} +// func (e *rawExecution) BaseCommitTags() Tags { +// return e.baseCommitTags +// } -func (e *rawExecution) HeadCommitTags() Tags { - return e.headCommitTags -} +// func (e *rawExecution) HeadCommitTags() Tags { +// return e.headCommitTags +// } func (e *rawExecution) BaseBranchTags() Tags { return e.baseBranchTags @@ -66,9 +66,9 @@ func (e *rawExecution) IsMerged() bool { return e.pr.Merged } -func (e *rawExecution) RootCommit() string { - return e.rootCommit -} +// func (e *rawExecution) RootCommit() string { +// return e.rootCommit +// } func (e *rawExecution) RootBranch() string { return e.rootBranch @@ -78,14 +78,22 @@ func (e *rawExecution) RootBranchTags() Tags { return e.rootBranchTags } -func (e *rawExecution) RootCommitTags() Tags { - return e.rootCommitTags -} +// func (e *rawExecution) RootCommitTags() Tags { +// return e.rootCommitTags +// } func (e *rawExecution) IsMinor() bool { return e.baseBranch == e.rootBranch } +func (e *rawExecution) BuildTags(tags *CalculationOutput) Tags { + return tags.ApplyRefs(&ApplyRefsOpts{ + HeadRef: e.headCommit, + BaseRef: e.baseCommit, + RootRef: e.rootCommit, + }) +} + func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Execution, *PRDetails, bool, error) { pr, err := prr.CurrentPR(ctx) From c5845b1a17497fd627c958ebf7c63f4549444ff2 Mon Sep 17 00:00:00 2001 From: walter Date: Tue, 14 Nov 2023 07:33:37 -0600 Subject: [PATCH 15/38] refactor for better regex --- execution.go | 50 +++++++++++++++++++++++++++++++++++--------------- tags.go | 46 ++++++++++++++++++++++++++-------------------- 2 files changed, 61 insertions(+), 35 deletions(-) diff --git a/execution.go b/execution.go index 110372f..cd4a409 100644 --- a/execution.go +++ b/execution.go @@ -76,53 +76,73 @@ type MRT string // my reserved tag func MyMostRecentReservedTag(e Execution) MRT { reg := regexp.MustCompile(fmt.Sprintf(`^v\d+\.\d+\.\d+-pr%d+\+base$`, e.PR())) - highest, err := e.RootBranchTags().HighestSemverMatching(reg) - if err != nil { + highest := e.RootBranchTags().SemversMatching(func(s string) bool { + return reg.MatchString(s) + }) + + if len(highest) == 0 { return "" } - return MRT(strings.Split(semver.Canonical(highest), "-")[0]) + return MRT(strings.Split(semver.Canonical(highest[len(highest)-1]), "-")[0]) } func MostRecentLiveTag(e Execution) MRLT { reg := regexp.MustCompile(`^v\d+\.\d+\.\d+(|-\S+\+\d+)$`) - highest, err := e.BaseBranchTags().HighestSemverMatching(reg) - if err != nil { + highest := e.BaseBranchTags().SemversMatching(func(s string) bool { + return reg.MatchString(s) + }) + + if len(highest) == 0 { return "" } - return MRLT(strings.Split(semver.Canonical(highest), "-")[0]) + return MRLT(strings.Split(semver.Canonical(highest[len(highest)-1]), "-")[0]) } func MyMostRecentTag(e Execution) MMRT { reg := regexp.MustCompile(`^v\d+\.\d+\.\d+.*$`) - highest, err := e.HeadBranchTags().HighestSemverMatching(reg) - if err != nil { + highest := e.HeadBranchTags().SemversMatching(func(s string) bool { + if strings.Contains(s, "-reserved") { + return false + } + return reg.MatchString(s) + }) + + if len(highest) == 0 { return "" } - return MMRT(strings.Split(semver.Canonical(highest), "-")[0]) + return MMRT(strings.Split(semver.Canonical(highest[len(highest)-1]), "-")[0]) } func MostRecentReservedTag(e Execution) MRRT { reg := regexp.MustCompile(`^v\d+\.\d+\.\d+-reserved$`) - highest, err := e.RootBranchTags().HighestSemverMatching(reg) - if err != nil { + highest := e.RootBranchTags().SemversMatching(func(s string) bool { + return reg.MatchString(s) + }) + + if len(highest) == 0 { return "" } - return MRRT(strings.Split(semver.Canonical(highest), "-")[0]) + return MRRT(strings.Split(semver.Canonical(highest[len(highest)-1]), "-")[0]) } func MyMostRecentBuildNumber(e Execution) MMRBN { reg := regexp.MustCompile(fmt.Sprintf(`^.*-pr%d+\+\d+$`, e.PR())) - highest, err := e.HeadBranchTags().HighestSemverMatching(reg) - if err != nil { + highest := e.HeadBranchTags().SemversMatching(func(s string) bool { + return reg.MatchString(s) + }) + + if len(highest) == 0 { return 0 } + high := highest[0] + // get the build number - split := strings.Split(highest, "+") + split := strings.Split(high, "+") if len(split) != 2 { return 0 } diff --git a/tags.go b/tags.go index fbb0459..286956b 100644 --- a/tags.go +++ b/tags.go @@ -1,9 +1,6 @@ package simver import ( - "errors" - "regexp" - "sort" "strings" "golang.org/x/mod/semver" @@ -28,27 +25,36 @@ func (t Tags) GetReserved() (TagInfo, bool) { } // HighestSemverContainingString finds the highest semantic version tag that contains the specified string. -func (t Tags) HighestSemverMatching(matcher ...*regexp.Regexp) (string, error) { +func (t Tags) HighestSemverMatching(matches []string) (string, error) { + + // matches := t.SemversMatching(func(tag string) bool { + // for _, m := range matcher { + // if m.MatchString(tag) { + // return true + // } + // } + // return false + // }) + + semver.Sort(matches) + + return matches[len(matches)-1], nil +} + +func (t Tags) SemversMatching(matcher func(string) bool) []string { var versions []string - for _, m := range matcher { - for _, tag := range t { - if m.MatchString(tag.Name) { - // Attempt to parse the semantic version from the tag - v := semver.Canonical(tag.Name) - if v != "" && semver.IsValid(v) { - versions = append(versions, tag.Name) - } + for _, tag := range t { + if matcher(tag.Name) { + // Attempt to parse the semantic version from the tag + v := semver.Canonical(tag.Name) + if v != "" && semver.IsValid(v) { + versions = append(versions, tag.Name) } } - if len(versions) == 0 { - return "", errors.New("no matching semantic versions found") - } } - // Use sort to find the highest version - sort.Slice(versions, func(i, j int) bool { - return semver.Compare(versions[i], versions[j]) < 0 - }) - return versions[len(versions)-1], nil + semver.Sort(versions) + + return versions } From 782515f607bae9dd4ec63c153fa06f22d59450a4 Mon Sep 17 00:00:00 2001 From: walter Date: Tue, 14 Nov 2023 20:56:49 -0600 Subject: [PATCH 16/38] merge logic working --- calculate.go | 33 ++++--- calculate_test.go | 35 ++++++-- execution.go | 17 +--- execution_test.go | 112 ++++++------------------ gen/mockery/Execution.simver.mockery.go | 22 ++--- simver.go | 3 +- 6 files changed, 95 insertions(+), 127 deletions(-) diff --git a/calculate.go b/calculate.go index 06b667f..90f8cd9 100644 --- a/calculate.go +++ b/calculate.go @@ -14,6 +14,7 @@ type Calculation struct { MostRecentReservedTag MRRT PR int NextValidTag NVT + IsMerge bool } var ( @@ -21,15 +22,17 @@ var ( ) type CalculationOutput struct { - BaseTags []string - HeadTags []string - RootTags []string + BaseTags []string + HeadTags []string + RootTags []string + MergeTags []string } type ApplyRefsOpts struct { - HeadRef string - BaseRef string - RootRef string + HeadRef string + BaseRef string + RootRef string + MergeRef string } func (out *CalculationOutput) ApplyRefs(opts *ApplyRefsOpts) Tags { @@ -43,14 +46,18 @@ func (out *CalculationOutput) ApplyRefs(opts *ApplyRefsOpts) Tags { for _, tag := range out.RootTags { tags = append(tags, TagInfo{Name: tag, Ref: opts.RootRef}) } + for _, tag := range out.MergeTags { + tags = append(tags, TagInfo{Name: tag, Ref: opts.MergeRef}) + } return tags } func (me *Calculation) CalculateNewTagsRaw() *CalculationOutput { out := &CalculationOutput{ - BaseTags: []string{}, - HeadTags: []string{}, - RootTags: []string{}, + BaseTags: []string{}, + HeadTags: []string{}, + RootTags: []string{}, + MergeTags: []string{}, } nvt := string(me.NextValidTag) @@ -78,8 +85,12 @@ func (me *Calculation) CalculateNewTagsRaw() *CalculationOutput { out.BaseTags = append(out.BaseTags, nvt+fmt.Sprintf("-pr%d+base", me.PR)) } - // then finally we tag mmrt - out.HeadTags = append(out.HeadTags, mmrt+fmt.Sprintf("-pr%d+%d", me.PR, int(me.MyMostRecentBuild)+1)) + if me.IsMerge { + out.MergeTags = append(out.MergeTags, mmrt) + } else { + next := mmrt + fmt.Sprintf("-pr%d+%d", me.PR, int(me.MyMostRecentBuild)+1) + out.HeadTags = append(out.HeadTags, next) + } return out } diff --git a/calculate_test.go b/calculate_test.go index 31c49db..1070fdb 100644 --- a/calculate_test.go +++ b/calculate_test.go @@ -9,11 +9,12 @@ import ( func TestNewCalculationAndCalculateNewTags(t *testing.T) { testCases := []struct { - name string - calculation *simver.Calculation - expectedBaseTags []string - expectedHeadTags []string - expectedRootTags []string + name string + calculation *simver.Calculation + expectedBaseTags []string + expectedHeadTags []string + expectedRootTags []string + expectedMergeTags []string }{ { name: "expired mmrt", @@ -24,6 +25,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { MyMostRecentBuild: 33, PR: 85, NextValidTag: "v99.99.99", + IsMerge: false, }, expectedBaseTags: []string{ "v99.99.99-pr85+base", @@ -34,6 +36,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { expectedRootTags: []string{ "v99.99.99-reserved", }, + expectedMergeTags: []string{}, }, { name: "missing all", @@ -44,6 +47,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { MyMostRecentBuild: 1, PR: 1, NextValidTag: "v3.3.3", + IsMerge: false, }, expectedBaseTags: []string{ "v3.3.3-pr1+base", @@ -54,6 +58,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { expectedRootTags: []string{ "v3.3.3-reserved", }, + expectedMergeTags: []string{}, }, { name: "valid mmrt", @@ -64,6 +69,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { MyMostRecentBuild: 33, PR: 1, NextValidTag: "v1.2.6", + IsMerge: false, }, expectedBaseTags: []string{}, expectedHeadTags: []string{"v1.2.4-pr1+34"}, @@ -79,6 +85,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { MyMostRecentBuild: 33, PR: 1, NextValidTag: "v1.2.6", + IsMerge: false, }, expectedBaseTags: []string{ "v1.2.6-pr1+base", @@ -89,8 +96,24 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { expectedRootTags: []string{ "v1.2.6-reserved", }, + expectedMergeTags: []string{}, + }, + { + name: "valid mmrt with merge", + calculation: &simver.Calculation{ + MostRecentLiveTag: "v1.2.3", + MostRecentReservedTag: "v1.2.5-reserved", + MyMostRecentTag: "v1.2.4", + MyMostRecentBuild: 33, + PR: 1, + NextValidTag: "v1.2.6", + IsMerge: true, + }, + expectedBaseTags: []string{}, + expectedHeadTags: []string{}, + expectedRootTags: []string{}, + expectedMergeTags: []string{"v1.2.4-merge"}, }, - // Add more test cases here... } diff --git a/execution.go b/execution.go index cd4a409..1a1b866 100644 --- a/execution.go +++ b/execution.go @@ -14,24 +14,10 @@ import ( type Execution interface { PR() int IsMinor() bool - IsMerged() bool - // HeadCommit() string - // BaseCommit() string - - // HeadBranch() string - // BaseBranch() string - - // HeadCommitTags() Tags - // BaseCommitTags() Tags - + IsMerge() bool HeadBranchTags() Tags BaseBranchTags() Tags - - // RootCommit() string - // RootBranch() string // always main RootBranchTags() Tags - // RootCommitTags() Tags - BuildTags(tags *CalculationOutput) Tags } @@ -41,6 +27,7 @@ func NewCaclulation(ctx context.Context, ex Execution) *Calculation { mrlt := MostRecentLiveTag(ex) mrrt := MostRecentReservedTag(ex) return &Calculation{ + IsMerge: ex.IsMerge(), MostRecentLiveTag: mrlt, MostRecentReservedTag: mrrt, MyMostRecentTag: MyMostRecentTag(ex), diff --git a/execution_test.go b/execution_test.go index 1530ac7..639ce8c 100644 --- a/execution_test.go +++ b/execution_test.go @@ -185,41 +185,30 @@ func TestNvt(t *testing.T) { } const ( - root_ref = "root_ref" - base_ref = "base_ref" - head_ref = "head_ref" + root_ref = "root_ref" + base_ref = "base_ref" + head_ref = "head_ref" + merge_ref = "merge_ref" ) func TestNewTags(t *testing.T) { testCases := []struct { name string - headCommitTags simver.Tags - baseCommitTags simver.Tags baseBranchTags simver.Tags headBranchTags simver.Tags - headCommit string - baseCommit string - rootCommit string rootBranchTags simver.Tags - rootCommitTags simver.Tags pr int - isMerged bool + isMerge bool isMinor bool expectedTags simver.Tags }{ { name: "Normal Commit on Non-Main Base Branch", - headCommitTags: simver.Tags{}, - baseCommitTags: simver.Tags{}, baseBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3"}}, headBranchTags: simver.Tags{}, - headCommit: head_ref, - baseCommit: base_ref, - rootCommit: root_ref, rootBranchTags: simver.Tags{}, - rootCommitTags: simver.Tags{}, pr: 0, - isMerged: false, + isMerge: false, isMinor: false, expectedTags: simver.Tags{ simver.TagInfo{Name: "v1.2.4-pr0+1", Ref: head_ref}, @@ -229,17 +218,11 @@ func TestNewTags(t *testing.T) { }, { name: "Normal Commit on Main Branch", - headCommitTags: simver.Tags{}, - baseCommitTags: simver.Tags{}, baseBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3"}}, headBranchTags: simver.Tags{}, - headCommit: head_ref, - baseCommit: base_ref, - rootCommit: root_ref, rootBranchTags: simver.Tags{}, - rootCommitTags: simver.Tags{}, pr: 99, - isMerged: false, + isMerge: false, isMinor: true, expectedTags: simver.Tags{ simver.TagInfo{Name: "v1.3.0-pr99+1", Ref: head_ref}, @@ -249,17 +232,11 @@ func TestNewTags(t *testing.T) { }, { name: "PR Merge with Valid MMRT", - headCommitTags: simver.Tags{}, - baseCommitTags: simver.Tags{simver.TagInfo{Name: "v1.2.4-pr1+base"}}, baseBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3"}}, headBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.4-pr1+1002"}}, - headCommit: head_ref, - baseCommit: base_ref, - rootCommit: root_ref, rootBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.4-reserved"}}, - rootCommitTags: simver.Tags{}, pr: 1, - isMerged: false, + isMerge: false, isMinor: false, expectedTags: simver.Tags{ simver.TagInfo{Name: "v1.2.4-pr1+1003", Ref: head_ref}, @@ -267,17 +244,11 @@ func TestNewTags(t *testing.T) { }, { name: "PR Merge with No MMRT", - headCommitTags: simver.Tags{}, - baseCommitTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-pr87+base"}}, baseBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3"}}, headBranchTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-pr87+1002"}}, - headCommit: head_ref, - baseCommit: base_ref, - rootCommit: root_ref, rootBranchTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-reserved"}}, - rootCommitTags: simver.Tags{}, pr: 87, - isMerged: false, + isMerge: false, isMinor: false, expectedTags: simver.Tags{ simver.TagInfo{Name: "v1.5.9-pr87+1003", Ref: head_ref}, @@ -285,17 +256,11 @@ func TestNewTags(t *testing.T) { }, { name: "PR Merge with Invalid MMRT", - headCommitTags: simver.Tags{simver.TagInfo{Name: "v1.2.999999-pr2+5"}}, - baseCommitTags: simver.Tags{simver.TagInfo{Name: "v1.2.4-pr2+base"}}, baseBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3"}}, headBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.4-pr2+5"}}, - headCommit: head_ref, - baseCommit: base_ref, - rootCommit: root_ref, rootBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3-reserved"}}, - rootCommitTags: simver.Tags{}, pr: 2, - isMerged: false, + isMerge: false, isMinor: false, expectedTags: simver.Tags{ simver.TagInfo{Name: "v1.2.4-pr2+6", Ref: head_ref}, @@ -303,17 +268,11 @@ func TestNewTags(t *testing.T) { }, { name: "No Tags Available for PR Commit", - headCommitTags: simver.Tags{}, - baseCommitTags: simver.Tags{}, baseBranchTags: simver.Tags{}, headBranchTags: simver.Tags{}, - headCommit: head_ref, - baseCommit: base_ref, - rootCommit: root_ref, rootBranchTags: simver.Tags{}, - rootCommitTags: simver.Tags{}, pr: 0, - isMerged: false, + isMerge: false, isMinor: true, expectedTags: simver.Tags{ // we also need to reserve the next version tag @@ -327,17 +286,11 @@ func TestNewTags(t *testing.T) { }, { name: "No Tags Available for PR Merge Commit", - headCommitTags: simver.Tags{}, - baseCommitTags: simver.Tags{}, baseBranchTags: simver.Tags{}, headBranchTags: simver.Tags{}, - headCommit: head_ref, - baseCommit: base_ref, - rootCommit: root_ref, rootBranchTags: simver.Tags{}, - rootCommitTags: simver.Tags{}, pr: 2, - isMerged: false, + isMerge: false, isMinor: true, expectedTags: simver.Tags{ // since this is a merge we do not need to reserve anything @@ -350,20 +303,19 @@ func TestNewTags(t *testing.T) { }, { name: "merge", - headCommitTags: simver.Tags{}, - baseCommitTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-reserved"}, simver.TagInfo{Name: "v1.5.9-pr87+base"}}, - rootCommitTags: simver.Tags{}, - baseBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3"}}, - headBranchTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-pr87+1002"}}, - rootBranchTags: simver.Tags{}, - headCommit: head_ref, - baseCommit: base_ref, - rootCommit: root_ref, - pr: 87, - isMerged: true, - isMinor: false, + baseBranchTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-pr84+12"}}, + headBranchTags: simver.Tags{simver.TagInfo{Name: "v1.5.10-pr87+1002"}}, + rootBranchTags: simver.Tags{ + simver.TagInfo{Name: "v1.5.9-reserved"}, + simver.TagInfo{Name: "v1.5.10-reserved"}, + simver.TagInfo{Name: "v1.5.0"}, + simver.TagInfo{Name: "v1.5.9-pr84+base"}, + }, + pr: 87, + isMerge: true, + isMinor: false, expectedTags: simver.Tags{ - simver.TagInfo{Name: "v1.5.9-pr87+1003", Ref: head_ref}, + simver.TagInfo{Name: "v1.5.10", Ref: merge_ref}, }, }, } @@ -373,26 +325,20 @@ func TestNewTags(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { mockExec := new(mockery.MockExecution_simver) - // mockExec.EXPECT().HeadCommitTags().Return(tc.headCommitTags) - // mockExec.EXPECT().BaseCommitTags().Return(tc.baseCommitTags) - // mockExec.EXPECT().HeadCommit().Return(tc.headCommit) - // mockExec.EXPECT().BaseCommit().Return(tc.baseCommit) mockExec.EXPECT().HeadBranchTags().Return(tc.headBranchTags) mockExec.EXPECT().BaseBranchTags().Return(tc.baseBranchTags) mockExec.EXPECT().PR().Return(tc.pr) mockExec.EXPECT().IsMinor().Return(tc.isMinor) - mockExec.EXPECT().IsMerged().Return(tc.isMerged) - // mockExec.EXPECT().RootCommit().Return(tc.rootCommit) - // mockExec.EXPECT().RootBranch().Return(tc.rootBranch) + mockExec.EXPECT().IsMerge().Return(tc.isMerge) mockExec.EXPECT().RootBranchTags().Return(tc.rootBranchTags) - // mockExec.EXPECT().RootCommitTags().Return(tc.rootCommitTags) result := simver.NewTags(ctx, mockExec) got := result.ApplyRefs(&simver.ApplyRefsOpts{ - HeadRef: head_ref, - BaseRef: base_ref, - RootRef: root_ref, + HeadRef: head_ref, + BaseRef: base_ref, + RootRef: root_ref, + MergeRef: merge_ref, }) assert.ElementsMatch(t, tc.expectedTags, got) diff --git a/gen/mockery/Execution.simver.mockery.go b/gen/mockery/Execution.simver.mockery.go index 4ae75dd..9d9dd13 100644 --- a/gen/mockery/Execution.simver.mockery.go +++ b/gen/mockery/Execution.simver.mockery.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.36.1. DO NOT EDIT. +// Code generated by mockery v2.37.0. DO NOT EDIT. package mockery @@ -150,8 +150,8 @@ func (_c *MockExecution_simver_HeadBranchTags_Call) RunAndReturn(run func() simv return _c } -// IsMerged provides a mock function with given fields: -func (_m *MockExecution_simver) IsMerged() bool { +// IsMerge provides a mock function with given fields: +func (_m *MockExecution_simver) IsMerge() bool { ret := _m.Called() var r0 bool @@ -164,29 +164,29 @@ func (_m *MockExecution_simver) IsMerged() bool { return r0 } -// MockExecution_simver_IsMerged_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'IsMerged' -type MockExecution_simver_IsMerged_Call struct { +// MockExecution_simver_IsMerge_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'IsMerge' +type MockExecution_simver_IsMerge_Call struct { *mock.Call } -// IsMerged is a helper method to define mock.On call -func (_e *MockExecution_simver_Expecter) IsMerged() *MockExecution_simver_IsMerged_Call { - return &MockExecution_simver_IsMerged_Call{Call: _e.mock.On("IsMerged")} +// IsMerge is a helper method to define mock.On call +func (_e *MockExecution_simver_Expecter) IsMerge() *MockExecution_simver_IsMerge_Call { + return &MockExecution_simver_IsMerge_Call{Call: _e.mock.On("IsMerge")} } -func (_c *MockExecution_simver_IsMerged_Call) Run(run func()) *MockExecution_simver_IsMerged_Call { +func (_c *MockExecution_simver_IsMerge_Call) Run(run func()) *MockExecution_simver_IsMerge_Call { _c.Call.Run(func(args mock.Arguments) { run() }) return _c } -func (_c *MockExecution_simver_IsMerged_Call) Return(_a0 bool) *MockExecution_simver_IsMerged_Call { +func (_c *MockExecution_simver_IsMerge_Call) Return(_a0 bool) *MockExecution_simver_IsMerge_Call { _c.Call.Return(_a0) return _c } -func (_c *MockExecution_simver_IsMerged_Call) RunAndReturn(run func() bool) *MockExecution_simver_IsMerged_Call { +func (_c *MockExecution_simver_IsMerge_Call) RunAndReturn(run func() bool) *MockExecution_simver_IsMerge_Call { _c.Call.Return(run) return _c } diff --git a/simver.go b/simver.go index c314c87..05c00f9 100644 --- a/simver.go +++ b/simver.go @@ -16,6 +16,7 @@ type rawExecution struct { headCommit string baseCommit string rootCommit string + mergeCommit string rootBranchTags Tags rootCommitTags Tags headCommitTags Tags @@ -62,7 +63,7 @@ func (e *rawExecution) PR() int { // return e.headBranch // } -func (e *rawExecution) IsMerged() bool { +func (e *rawExecution) IsMerge() bool { return e.pr.Merged } From 01105a42812b3cd28d257af6b00d5c678e39dc21 Mon Sep 17 00:00:00 2001 From: walter Date: Tue, 14 Nov 2023 21:11:18 -0600 Subject: [PATCH 17/38] make sure main is set --- cmd/simver_github_actions/main.go | 3 --- exec/gh.go | 1 + 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/cmd/simver_github_actions/main.go b/cmd/simver_github_actions/main.go index debf298..ede7b3e 100644 --- a/cmd/simver_github_actions/main.go +++ b/cmd/simver_github_actions/main.go @@ -190,7 +190,6 @@ func main() { if tries > 5 { zerolog.Ctx(ctx).Error().Err(err).Msgf("error creating tag: %v", err) fmt.Println(terrors.FormatErrorCaller(err)) - os.Exit(1) } @@ -214,8 +213,6 @@ func main() { } } - // havMergedTag := false - for _, tag := range tags { if tag.Name == reservedTag.Name && tag.Ref == reservedTag.Ref { continue diff --git a/exec/gh.go b/exec/gh.go index 3748745..428fbfc 100644 --- a/exec/gh.go +++ b/exec/gh.go @@ -102,6 +102,7 @@ type githubPR struct { func (me *githubPR) toPRDetails() *simver.PRDetails { return &simver.PRDetails{ Number: me.Number, + RootBranch: "main", HeadBranch: me.HeadRefName, BaseBranch: me.BaseRefName, Merged: me.State == "MERGED", From aec592a16f4a7703a59da5d4e0bcafe30f90b5d6 Mon Sep 17 00:00:00 2001 From: walter Date: Wed, 15 Nov 2023 04:00:15 -0600 Subject: [PATCH 18/38] merge commit fix --- simver.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/simver.go b/simver.go index 05c00f9..1e1db21 100644 --- a/simver.go +++ b/simver.go @@ -89,9 +89,10 @@ func (e *rawExecution) IsMinor() bool { func (e *rawExecution) BuildTags(tags *CalculationOutput) Tags { return tags.ApplyRefs(&ApplyRefsOpts{ - HeadRef: e.headCommit, - BaseRef: e.baseCommit, - RootRef: e.rootCommit, + HeadRef: e.headCommit, + BaseRef: e.baseCommit, + RootRef: e.rootCommit, + MergeRef: e.mergeCommit, }) } @@ -171,6 +172,7 @@ func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Exec rootCommit: pr.RootCommit, rootBranchTags: rootBranchTags, rootCommitTags: rootCommitTags, + mergeCommit: pr.MergeCommit, }, pr, true, nil } From b0c2e46cdb4fc3ead3decd7857b850f17f6fc1c4 Mon Sep 17 00:00:00 2001 From: walter Date: Wed, 15 Nov 2023 05:44:56 -0600 Subject: [PATCH 19/38] renaming for clarity --- calculate.go | 8 +-- calculate_test.go | 7 ++- cmd/simver_github_actions/main.go | 7 ++- exec/tag.go | 20 ++++---- execution.go | 39 +++++---------- execution_test.go | 82 +++++++++++++++---------------- git.go | 8 +-- simver.go | 32 ------------ tags.go | 25 ++-------- 9 files changed, 84 insertions(+), 144 deletions(-) diff --git a/calculate.go b/calculate.go index 90f8cd9..fa506e5 100644 --- a/calculate.go +++ b/calculate.go @@ -38,16 +38,16 @@ type ApplyRefsOpts struct { func (out *CalculationOutput) ApplyRefs(opts *ApplyRefsOpts) Tags { tags := make(Tags, 0) for _, tag := range out.BaseTags { - tags = append(tags, TagInfo{Name: tag, Ref: opts.BaseRef}) + tags = append(tags, Tag{Name: tag, Ref: opts.BaseRef}) } for _, tag := range out.HeadTags { - tags = append(tags, TagInfo{Name: tag, Ref: opts.HeadRef}) + tags = append(tags, Tag{Name: tag, Ref: opts.HeadRef}) } for _, tag := range out.RootTags { - tags = append(tags, TagInfo{Name: tag, Ref: opts.RootRef}) + tags = append(tags, Tag{Name: tag, Ref: opts.RootRef}) } for _, tag := range out.MergeTags { - tags = append(tags, TagInfo{Name: tag, Ref: opts.MergeRef}) + tags = append(tags, Tag{Name: tag, Ref: opts.MergeRef}) } return tags } diff --git a/calculate_test.go b/calculate_test.go index 1070fdb..b43c271 100644 --- a/calculate_test.go +++ b/calculate_test.go @@ -112,13 +112,13 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { expectedBaseTags: []string{}, expectedHeadTags: []string{}, expectedRootTags: []string{}, - expectedMergeTags: []string{"v1.2.4-merge"}, + expectedMergeTags: []string{"v1.2.4"}, }, // Add more test cases here... } for _, tc := range testCases { - for _, i := range []string{"base", "head", "root"} { + for _, i := range []string{"base", "head", "root", "merge"} { t.Run(tc.name+"_"+i, func(t *testing.T) { out := tc.calculation.CalculateNewTagsRaw() @@ -131,6 +131,9 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { } 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 ede7b3e..4766906 100644 --- a/cmd/simver_github_actions/main.go +++ b/cmd/simver_github_actions/main.go @@ -178,7 +178,9 @@ func main() { os.Exit(0) } - tags := ee.BuildTags(simver.NewTags(ctx, ee)) + tt := simver.Calculate(ctx, ee).CalculateNewTagsRaw() + + tags := ee.BuildTags(tt) reservedTag, reserved := tags.GetReserved() @@ -206,7 +208,8 @@ func main() { } ee = eez prd = prz - tags := ee.BuildTags(simver.NewTags(ctx, ee)) + tt := simver.Calculate(ctx, ee).CalculateNewTagsRaw() + tags = ee.BuildTags(tt) reservedTag, reserved = tags.GetReserved() } else { reserved = false diff --git a/exec/tag.go b/exec/tag.go index cb8ead3..e130ac2 100644 --- a/exec/tag.go +++ b/exec/tag.go @@ -15,7 +15,7 @@ var ( _ simver.TagProvider = (*gitProvider)(nil) ) -func (p *gitProvider) TagsFromCommit(ctx context.Context, commitHash string) ([]simver.TagInfo, error) { +func (p *gitProvider) TagsFromCommit(ctx context.Context, commitHash string) ([]simver.Tag, error) { ctx = zerolog.Ctx(ctx).With().Str("commit", commitHash).Logger().WithContext(ctx) @@ -28,13 +28,13 @@ func (p *gitProvider) TagsFromCommit(ctx context.Context, commitHash string) ([] } lines := strings.Split(string(out), "\n") - var tags []simver.TagInfo + var tags []simver.Tag for _, line := range lines { line = strings.TrimSpace(line) if line == "" { continue } - tags = append(tags, simver.TagInfo{Name: line, Ref: commitHash}) + tags = append(tags, simver.Tag{Name: line, Ref: commitHash}) } zerolog.Ctx(ctx).Debug().Int("tags_len", len(tags)).Any("tags", tags).Msg("got tags from commit") @@ -42,7 +42,7 @@ func (p *gitProvider) TagsFromCommit(ctx context.Context, commitHash string) ([] return tags, nil } -func (p *gitProvider) TagsFromBranch(ctx context.Context, branch string) ([]simver.TagInfo, error) { +func (p *gitProvider) TagsFromBranch(ctx context.Context, branch string) ([]simver.Tag, error) { start := time.Now() @@ -56,7 +56,7 @@ func (p *gitProvider) TagsFromBranch(ctx context.Context, branch string) ([]simv lines := strings.Split(string(out), "\n") - var tags []simver.TagInfo + var tags []simver.Tag for _, line := range lines { if line == "" { @@ -92,7 +92,7 @@ func (p *gitProvider) TagsFromBranch(ctx context.Context, branch string) ([]simv continue } - tags = append(tags, simver.TagInfo{Name: name, Ref: dat.Ref}) + tags = append(tags, simver.Tag{Name: name, Ref: dat.Ref}) } zerolog.Ctx(ctx).Debug().Int("tags_len", len(tags)).Any("tags", tags).Dur("dur", time.Since(start)).Msg("got tags from branch") @@ -101,7 +101,7 @@ func (p *gitProvider) TagsFromBranch(ctx context.Context, branch string) ([]simv } -func (p *gitProvider) FetchTags(ctx context.Context) ([]simver.TagInfo, error) { +func (p *gitProvider) FetchTags(ctx context.Context) ([]simver.Tag, error) { start := time.Now() @@ -125,7 +125,7 @@ func (p *gitProvider) FetchTags(ctx context.Context) ([]simver.TagInfo, error) { } lines := strings.Split(string(out), "\n") - var tagInfos []simver.TagInfo + var tagInfos []simver.Tag for _, line := range lines { parts := strings.Split(line, " ") if len(parts) != 2 { @@ -137,7 +137,7 @@ func (p *gitProvider) FetchTags(ctx context.Context) ([]simver.TagInfo, error) { if name == "" || ref == "" { continue // Skip empty or invalid entries } - tagInfos = append(tagInfos, simver.TagInfo{Name: name, Ref: ref}) + tagInfos = append(tagInfos, simver.Tag{Name: name, Ref: ref}) } zerolog.Ctx(ctx).Debug().Int("tags_len", len(tagInfos)).Dur("duration", time.Since(start)).Any("tags", tagInfos).Msg("tags fetched") @@ -145,7 +145,7 @@ func (p *gitProvider) FetchTags(ctx context.Context) ([]simver.TagInfo, error) { return tagInfos, nil } -func (p *gitProvider) CreateTag(ctx context.Context, tag simver.TagInfo) error { +func (p *gitProvider) CreateTag(ctx context.Context, tag simver.Tag) error { ctx = zerolog.Ctx(ctx).With().Str("name", tag.Name).Str("ref", tag.Ref).Logger().WithContext(ctx) diff --git a/execution.go b/execution.go index 1a1b866..ff69a78 100644 --- a/execution.go +++ b/execution.go @@ -23,7 +23,7 @@ type Execution interface { const baseTag = "v0.1.0" -func NewCaclulation(ctx context.Context, ex Execution) *Calculation { +func Calculate(ctx context.Context, ex Execution) *Calculation { mrlt := MostRecentLiveTag(ex) mrrt := MostRecentReservedTag(ex) return &Calculation{ @@ -37,23 +37,6 @@ func NewCaclulation(ctx context.Context, ex Execution) *Calculation { } } -func NewTags(ctx context.Context, ex Execution) *CalculationOutput { - calc := NewCaclulation(ctx, ex) - - // tags := make(Tags, 0) - // for _, tag := range baseTags { - // tags = append(tags, TagInfo{Name: tag, Ref: ex.BaseCommit()}) - // } - // for _, tag := range headTags { - // tags = append(tags, TagInfo{Name: tag, Ref: ex.HeadCommit()}) - // } - // for _, tag := range rootTags { - // tags = append(tags, TagInfo{Name: tag, Ref: ex.RootCommit()}) - // } - - return calc.CalculateNewTagsRaw() -} - type MRLT string // most recent live tag type MRRT string // most recent reserved tag type NVT string // next valid tag @@ -61,18 +44,18 @@ type MMRT string // my most recent tag type MMRBN int // my most recent build number type MRT string // my reserved tag -func MyMostRecentReservedTag(e Execution) MRT { - reg := regexp.MustCompile(fmt.Sprintf(`^v\d+\.\d+\.\d+-pr%d+\+base$`, e.PR())) - highest := e.RootBranchTags().SemversMatching(func(s string) bool { - return reg.MatchString(s) - }) +// func MyMostRecentReservedTag(e Execution) MRT { +// reg := regexp.MustCompile(fmt.Sprintf(`^v\d+\.\d+\.\d+-pr%d+\+base$`, e.PR())) +// highest := e.RootBranchTags().SemversMatching(func(s string) bool { +// return reg.MatchString(s) +// }) - if len(highest) == 0 { - return "" - } +// if len(highest) == 0 { +// return "" +// } - return MRT(strings.Split(semver.Canonical(highest[len(highest)-1]), "-")[0]) -} +// return MRT(strings.Split(semver.Canonical(highest[len(highest)-1]), "-")[0]) +// } func MostRecentLiveTag(e Execution) MRLT { reg := regexp.MustCompile(`^v\d+\.\d+\.\d+(|-\S+\+\d+)$`) diff --git a/execution_test.go b/execution_test.go index 639ce8c..9d82dfe 100644 --- a/execution_test.go +++ b/execution_test.go @@ -17,7 +17,7 @@ func TestMrlt(t *testing.T) { }{ { name: "Valid MRLT", - tags: simver.Tags{simver.TagInfo{Name: "v1.2.3"}, simver.TagInfo{Name: "v1.2.4"}}, + tags: simver.Tags{simver.Tag{Name: "v1.2.3"}, simver.Tag{Name: "v1.2.4"}}, expectedMrlt: "v1.2.4", }, { @@ -27,7 +27,7 @@ func TestMrlt(t *testing.T) { }, { name: "Invalid Semver Format", - tags: simver.Tags{simver.TagInfo{Name: "v1.2"}, simver.TagInfo{Name: "v1.2.x"}}, + tags: simver.Tags{simver.Tag{Name: "v1.2"}, simver.Tag{Name: "v1.2.x"}}, expectedMrlt: "", }, } @@ -53,13 +53,13 @@ func TestMmrt(t *testing.T) { { name: "Valid MMRT", prNum: 1, - tags: simver.Tags{simver.TagInfo{Name: "v1.2.3-pr1+base"}}, + tags: simver.Tags{simver.Tag{Name: "v1.2.3-pr1+base"}}, expectedMmrt: "v1.2.3", }, { name: "Invalid MMRT", prNum: 3, - tags: simver.Tags{simver.TagInfo{Name: "v1.2.3-pr3+0"}}, + tags: simver.Tags{simver.Tag{Name: "v1.2.3-pr3+0"}}, expectedMmrt: "v1.2.3", }, { @@ -71,7 +71,7 @@ func TestMmrt(t *testing.T) { { name: "Non-Matching PR Number", prNum: 3, - tags: simver.Tags{simver.TagInfo{Name: "v1.2.3-pr1+base"}}, + tags: simver.Tags{simver.Tag{Name: "v1.2.3-pr1+base"}}, expectedMmrt: "v1.2.3", }, } @@ -96,7 +96,7 @@ func TestMrrt(t *testing.T) { }{ { name: "Valid MRRT", - tags: simver.Tags{simver.TagInfo{Name: "v1.2.3-reserved"}}, + tags: simver.Tags{simver.Tag{Name: "v1.2.3-reserved"}}, expectedMrrt: "v1.2.3", }, { @@ -106,7 +106,7 @@ func TestMrrt(t *testing.T) { }, { name: "Invalid Reserved Tag Format", - tags: simver.Tags{simver.TagInfo{Name: "v1.2-reserved"}}, + tags: simver.Tags{simver.Tag{Name: "v1.2-reserved"}}, expectedMrrt: "", }, } @@ -204,66 +204,66 @@ func TestNewTags(t *testing.T) { }{ { name: "Normal Commit on Non-Main Base Branch", - baseBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3"}}, + baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3"}}, headBranchTags: simver.Tags{}, rootBranchTags: simver.Tags{}, pr: 0, isMerge: false, isMinor: false, expectedTags: simver.Tags{ - simver.TagInfo{Name: "v1.2.4-pr0+1", Ref: head_ref}, - simver.TagInfo{Name: "v1.2.4-reserved", Ref: root_ref}, - simver.TagInfo{Name: "v1.2.4-pr0+base", Ref: base_ref}, + simver.Tag{Name: "v1.2.4-pr0+1", Ref: head_ref}, + simver.Tag{Name: "v1.2.4-reserved", Ref: root_ref}, + simver.Tag{Name: "v1.2.4-pr0+base", Ref: base_ref}, }, }, { name: "Normal Commit on Main Branch", - baseBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3"}}, + baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3"}}, headBranchTags: simver.Tags{}, rootBranchTags: simver.Tags{}, pr: 99, isMerge: false, isMinor: true, expectedTags: simver.Tags{ - simver.TagInfo{Name: "v1.3.0-pr99+1", Ref: head_ref}, - simver.TagInfo{Name: "v1.3.0-reserved", Ref: root_ref}, - simver.TagInfo{Name: "v1.3.0-pr99+base", Ref: base_ref}, + simver.Tag{Name: "v1.3.0-pr99+1", Ref: head_ref}, + simver.Tag{Name: "v1.3.0-reserved", Ref: root_ref}, + simver.Tag{Name: "v1.3.0-pr99+base", Ref: base_ref}, }, }, { name: "PR Merge with Valid MMRT", - baseBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3"}}, - headBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.4-pr1+1002"}}, - rootBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.4-reserved"}}, + baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3"}}, + headBranchTags: simver.Tags{simver.Tag{Name: "v1.2.4-pr1+1002"}}, + rootBranchTags: simver.Tags{simver.Tag{Name: "v1.2.4-reserved"}}, pr: 1, isMerge: false, isMinor: false, expectedTags: simver.Tags{ - simver.TagInfo{Name: "v1.2.4-pr1+1003", Ref: head_ref}, + simver.Tag{Name: "v1.2.4-pr1+1003", Ref: head_ref}, }, }, { name: "PR Merge with No MMRT", - baseBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3"}}, - headBranchTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-pr87+1002"}}, - rootBranchTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-reserved"}}, + baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3"}}, + headBranchTags: simver.Tags{simver.Tag{Name: "v1.5.9-pr87+1002"}}, + rootBranchTags: simver.Tags{simver.Tag{Name: "v1.5.9-reserved"}}, pr: 87, isMerge: false, isMinor: false, expectedTags: simver.Tags{ - simver.TagInfo{Name: "v1.5.9-pr87+1003", Ref: head_ref}, + simver.Tag{Name: "v1.5.9-pr87+1003", Ref: head_ref}, }, }, { name: "PR Merge with Invalid MMRT", - baseBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3"}}, - headBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.4-pr2+5"}}, - rootBranchTags: simver.Tags{simver.TagInfo{Name: "v1.2.3-reserved"}}, + baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3"}}, + headBranchTags: simver.Tags{simver.Tag{Name: "v1.2.4-pr2+5"}}, + rootBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3-reserved"}}, pr: 2, isMerge: false, isMinor: false, expectedTags: simver.Tags{ - simver.TagInfo{Name: "v1.2.4-pr2+6", Ref: head_ref}, + simver.Tag{Name: "v1.2.4-pr2+6", Ref: head_ref}, }, }, { @@ -277,11 +277,11 @@ func TestNewTags(t *testing.T) { expectedTags: simver.Tags{ // we also need to reserve the next version tag // which should be v0.2.0 since the base branch is main - simver.TagInfo{Name: "v0.2.0-reserved", Ref: root_ref}, - simver.TagInfo{Name: "v0.2.0-pr0+base", Ref: base_ref}, + simver.Tag{Name: "v0.2.0-reserved", Ref: root_ref}, + simver.Tag{Name: "v0.2.0-pr0+base", Ref: base_ref}, // and finally, we need to tag the commit with the PR number // since the base branch is main, we set it to v0.2.0-pr0 - simver.TagInfo{Name: "v0.2.0-pr0+1", Ref: head_ref}, + simver.Tag{Name: "v0.2.0-pr0+1", Ref: head_ref}, }, }, { @@ -295,27 +295,27 @@ func TestNewTags(t *testing.T) { 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 - simver.TagInfo{Name: "v0.2.0-pr2+1", Ref: head_ref}, + simver.Tag{Name: "v0.2.0-pr2+1", Ref: head_ref}, // we need to make sure we have a reserved tag for the base branch - simver.TagInfo{Name: "v0.2.0-reserved", Ref: root_ref}, - simver.TagInfo{Name: "v0.2.0-pr2+base", Ref: base_ref}, + simver.Tag{Name: "v0.2.0-reserved", Ref: root_ref}, + simver.Tag{Name: "v0.2.0-pr2+base", Ref: base_ref}, }, }, { name: "merge", - baseBranchTags: simver.Tags{simver.TagInfo{Name: "v1.5.9-pr84+12"}}, - headBranchTags: simver.Tags{simver.TagInfo{Name: "v1.5.10-pr87+1002"}}, + baseBranchTags: simver.Tags{simver.Tag{Name: "v1.5.9-pr84+12"}}, + headBranchTags: simver.Tags{simver.Tag{Name: "v1.5.10-pr87+1002"}}, rootBranchTags: simver.Tags{ - simver.TagInfo{Name: "v1.5.9-reserved"}, - simver.TagInfo{Name: "v1.5.10-reserved"}, - simver.TagInfo{Name: "v1.5.0"}, - simver.TagInfo{Name: "v1.5.9-pr84+base"}, + simver.Tag{Name: "v1.5.9-reserved"}, + simver.Tag{Name: "v1.5.10-reserved"}, + simver.Tag{Name: "v1.5.0"}, + simver.Tag{Name: "v1.5.9-pr84+base"}, }, pr: 87, isMerge: true, isMinor: false, expectedTags: simver.Tags{ - simver.TagInfo{Name: "v1.5.10", Ref: merge_ref}, + simver.Tag{Name: "v1.5.10", Ref: merge_ref}, }, }, } @@ -332,7 +332,7 @@ func TestNewTags(t *testing.T) { mockExec.EXPECT().IsMerge().Return(tc.isMerge) mockExec.EXPECT().RootBranchTags().Return(tc.rootBranchTags) - result := simver.NewTags(ctx, mockExec) + result := simver.Calculate(ctx, mockExec).CalculateNewTagsRaw() got := result.ApplyRefs(&simver.ApplyRefsOpts{ HeadRef: head_ref, diff --git a/git.go b/git.go index 0c66d89..bcfa880 100644 --- a/git.go +++ b/git.go @@ -9,8 +9,8 @@ type GitProvider interface { } type TagProvider interface { - FetchTags(ctx context.Context) ([]TagInfo, error) - CreateTag(ctx context.Context, tag TagInfo) error - TagsFromCommit(ctx context.Context, commitHash string) ([]TagInfo, error) - TagsFromBranch(ctx context.Context, branch string) ([]TagInfo, error) + FetchTags(ctx context.Context) ([]Tag, error) + CreateTag(ctx context.Context, tag Tag) error + TagsFromCommit(ctx context.Context, commitHash string) ([]Tag, error) + TagsFromBranch(ctx context.Context, branch string) ([]Tag, error) } diff --git a/simver.go b/simver.go index 1e1db21..55ac884 100644 --- a/simver.go +++ b/simver.go @@ -27,22 +27,6 @@ type rawExecution struct { isMinor bool } -// func (e *rawExecution) BaseCommit() string { -// return e.baseCommit -// } - -// func (e *rawExecution) HeadCommit() string { -// return e.headCommit -// } - -// func (e *rawExecution) BaseCommitTags() Tags { -// return e.baseCommitTags -// } - -// func (e *rawExecution) HeadCommitTags() Tags { -// return e.headCommitTags -// } - func (e *rawExecution) BaseBranchTags() Tags { return e.baseBranchTags } @@ -55,22 +39,10 @@ func (e *rawExecution) PR() int { return e.pr.Number } -// func (e *rawExecution) BaseBranch() string { -// return e.baseBranch -// } - -// func (e *rawExecution) HeadBranch() string { -// return e.headBranch -// } - func (e *rawExecution) IsMerge() bool { return e.pr.Merged } -// func (e *rawExecution) RootCommit() string { -// return e.rootCommit -// } - func (e *rawExecution) RootBranch() string { return e.rootBranch } @@ -79,10 +51,6 @@ func (e *rawExecution) RootBranchTags() Tags { return e.rootBranchTags } -// func (e *rawExecution) RootCommitTags() Tags { -// return e.rootCommitTags -// } - func (e *rawExecution) IsMinor() bool { return e.baseBranch == e.rootBranch } diff --git a/tags.go b/tags.go index 286956b..4a1368e 100644 --- a/tags.go +++ b/tags.go @@ -6,14 +6,14 @@ import ( "golang.org/x/mod/semver" ) -type TagInfo struct { +type Tag struct { Name string Ref string } -type Tags []TagInfo +type Tags []Tag -func (t Tags) GetReserved() (TagInfo, bool) { +func (t Tags) GetReserved() (Tag, bool) { for _, tag := range t { if strings.Contains(tag.Name, "-reserved") { @@ -21,24 +21,7 @@ func (t Tags) GetReserved() (TagInfo, bool) { } } - return TagInfo{}, false -} - -// HighestSemverContainingString finds the highest semantic version tag that contains the specified string. -func (t Tags) HighestSemverMatching(matches []string) (string, error) { - - // matches := t.SemversMatching(func(tag string) bool { - // for _, m := range matcher { - // if m.MatchString(tag) { - // return true - // } - // } - // return false - // }) - - semver.Sort(matches) - - return matches[len(matches)-1], nil + return Tag{}, false } func (t Tags) SemversMatching(matcher func(string) bool) []string { From faf4bafa7343df4f46a739494e27e1296f000bfa Mon Sep 17 00:00:00 2001 From: walter Date: Wed, 15 Nov 2023 06:44:24 -0600 Subject: [PATCH 20/38] force patch --- calculate.go | 7 +++ calculate_test.go | 39 ++++++++++++++ execution.go | 72 ++++++++++++++++++++----- execution_test.go | 69 ++++++++++++++++++++++++ gen/mockery/Execution.simver.mockery.go | 43 +++++++++++++++ simver.go | 4 ++ 6 files changed, 222 insertions(+), 12 deletions(-) diff --git a/calculate.go b/calculate.go index fa506e5..72bba57 100644 --- a/calculate.go +++ b/calculate.go @@ -15,6 +15,7 @@ type Calculation struct { PR int NextValidTag NVT IsMerge bool + ForcePatch bool } var ( @@ -78,6 +79,12 @@ func (me *Calculation) CalculateNewTagsRaw() *CalculationOutput { validMmrt = true } + // force patch is ignored if this is a merge + if me.ForcePatch && !me.IsMerge { + mmrt = BumpPatch(mmrt) + validMmrt = false + } + // if mmrt is invalid, then we need to reserve a new mmrt (which is the same as nvt) if !validMmrt { mmrt = nvt diff --git a/calculate_test.go b/calculate_test.go index b43c271..413690e 100644 --- a/calculate_test.go +++ b/calculate_test.go @@ -26,6 +26,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { PR: 85, NextValidTag: "v99.99.99", IsMerge: false, + ForcePatch: false, }, expectedBaseTags: []string{ "v99.99.99-pr85+base", @@ -48,6 +49,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { PR: 1, NextValidTag: "v3.3.3", IsMerge: false, + ForcePatch: false, }, expectedBaseTags: []string{ "v3.3.3-pr1+base", @@ -70,6 +72,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { PR: 1, NextValidTag: "v1.2.6", IsMerge: false, + ForcePatch: false, }, expectedBaseTags: []string{}, expectedHeadTags: []string{"v1.2.4-pr1+34"}, @@ -86,6 +89,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { PR: 1, NextValidTag: "v1.2.6", IsMerge: false, + ForcePatch: false, }, expectedBaseTags: []string{ "v1.2.6-pr1+base", @@ -108,6 +112,41 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { PR: 1, NextValidTag: "v1.2.6", IsMerge: true, + ForcePatch: false, + }, + expectedBaseTags: []string{}, + expectedHeadTags: []string{}, + expectedRootTags: []string{}, + expectedMergeTags: []string{"v1.2.4"}, + }, + { + name: "valid mmrt with force patch", + calculation: &simver.Calculation{ + MostRecentLiveTag: "v1.2.3", + MostRecentReservedTag: "v1.2.5-reserved", + MyMostRecentTag: "v1.2.4", + MyMostRecentBuild: 33, + PR: 1, + NextValidTag: "v1.2.6", + IsMerge: false, + ForcePatch: true, + }, + expectedBaseTags: []string{"v1.2.6-pr1+base"}, + expectedHeadTags: []string{"v1.2.6-pr1+34"}, + expectedRootTags: []string{"v1.2.6-reserved"}, + expectedMergeTags: []string{}, + }, + { + name: "valid mmrt with force patch (merge override)", + calculation: &simver.Calculation{ + MostRecentLiveTag: "v1.2.3", + MostRecentReservedTag: "v1.2.5-reserved", + MyMostRecentTag: "v1.2.4", + MyMostRecentBuild: 33, + PR: 1, + NextValidTag: "v1.2.6", + IsMerge: true, + ForcePatch: true, }, expectedBaseTags: []string{}, expectedHeadTags: []string{}, diff --git a/execution.go b/execution.go index ff69a78..b889023 100644 --- a/execution.go +++ b/execution.go @@ -15,6 +15,7 @@ type Execution interface { PR() int IsMinor() bool IsMerge() bool + HeadCommitTags() Tags HeadBranchTags() Tags BaseBranchTags() Tags RootBranchTags() Tags @@ -25,18 +26,31 @@ const baseTag = "v0.1.0" func Calculate(ctx context.Context, ex Execution) *Calculation { mrlt := MostRecentLiveTag(ex) + mrrt := MostRecentReservedTag(ex) + + mmrt := MyMostRecentTag(ex) + + // // if mmrt already exists on head + + // if ForcePatch(ctx, ex, mmrt) { + // mmrt = BumpPatch(mmrt) + // } + return &Calculation{ IsMerge: ex.IsMerge(), MostRecentLiveTag: mrlt, MostRecentReservedTag: mrrt, - MyMostRecentTag: MyMostRecentTag(ex), + ForcePatch: ForcePatch(ctx, ex, mmrt), + MyMostRecentTag: mmrt, MyMostRecentBuild: MyMostRecentBuildNumber(ex), PR: ex.PR(), NextValidTag: GetNextValidTag(ctx, ex.IsMinor(), mrlt, mrrt), } } +// type TagString string + type MRLT string // most recent live tag type MRRT string // most recent reserved tag type NVT string // next valid tag @@ -44,21 +58,55 @@ type MMRT string // my most recent tag type MMRBN int // my most recent build number type MRT string // my reserved tag -// func MyMostRecentReservedTag(e Execution) MRT { -// reg := regexp.MustCompile(fmt.Sprintf(`^v\d+\.\d+\.\d+-pr%d+\+base$`, e.PR())) -// highest := e.RootBranchTags().SemversMatching(func(s string) bool { -// return reg.MatchString(s) -// }) +func BumpPatch[S ~string](arg S) S { + + maj := semver.MajorMinor(string(arg)) + patch := strings.Split(strings.TrimPrefix(string(arg), maj), "-")[0] + + patch = strings.TrimPrefix(patch, ".") + + if patch == "" { + patch = "0" + } + + patchnum, err := strconv.Atoi(patch) + if err != nil { + panic("patchnum is not a number somehow: " + patch) + } + + patchnum++ + + return S(fmt.Sprintf("%s.%d", maj, patchnum)) + +} + +func ForcePatch(ctx context.Context, ee Execution, mmrt MMRT) bool { + // if our head branch has a + reg := regexp.MustCompile(fmt.Sprintf(`^%s$`, mmrt)) -// if len(highest) == 0 { -// return "" -// } + // head commit tags matching mmrt + hct := ee.HeadCommitTags().SemversMatching(func(s string) bool { + return reg.MatchString(s) + }) -// return MRT(strings.Split(semver.Canonical(highest[len(highest)-1]), "-")[0]) -// } + if len(hct) > 0 { + return false + } + + // head branch tags matching mmrt + hbt := ee.HeadBranchTags().SemversMatching(func(s string) bool { + return reg.MatchString(s) + }) + + if len(hbt) > 0 { + return true + } + + return false +} func MostRecentLiveTag(e Execution) MRLT { - reg := regexp.MustCompile(`^v\d+\.\d+\.\d+(|-\S+\+\d+)$`) + reg := regexp.MustCompile(`^v\d+\.\d+\.\d+$`) highest := e.BaseBranchTags().SemversMatching(func(s string) bool { return reg.MatchString(s) }) diff --git a/execution_test.go b/execution_test.go index 9d82dfe..2387701 100644 --- a/execution_test.go +++ b/execution_test.go @@ -197,6 +197,7 @@ func TestNewTags(t *testing.T) { baseBranchTags simver.Tags headBranchTags simver.Tags rootBranchTags simver.Tags + headCommitTags simver.Tags pr int isMerge bool isMinor bool @@ -206,6 +207,7 @@ func TestNewTags(t *testing.T) { 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, @@ -220,6 +222,7 @@ 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: 99, isMerge: false, @@ -234,6 +237,7 @@ 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, @@ -246,6 +250,7 @@ func TestNewTags(t *testing.T) { 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, @@ -258,6 +263,7 @@ func TestNewTags(t *testing.T) { 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, @@ -270,6 +276,7 @@ func TestNewTags(t *testing.T) { name: "No Tags Available for PR Commit", baseBranchTags: simver.Tags{}, headBranchTags: simver.Tags{}, + headCommitTags: simver.Tags{}, rootBranchTags: simver.Tags{}, pr: 0, isMerge: false, @@ -288,6 +295,7 @@ 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, @@ -305,6 +313,7 @@ func TestNewTags(t *testing.T) { name: "merge", baseBranchTags: simver.Tags{simver.Tag{Name: "v1.5.9-pr84+12"}}, headBranchTags: simver.Tags{simver.Tag{Name: "v1.5.10-pr87+1002"}}, + headCommitTags: simver.Tags{}, rootBranchTags: simver.Tags{ simver.Tag{Name: "v1.5.9-reserved"}, simver.Tag{Name: "v1.5.10-reserved"}, @@ -318,6 +327,26 @@ func TestNewTags(t *testing.T) { simver.Tag{Name: "v1.5.10", Ref: merge_ref}, }, }, + { + name: "after merge", + baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.2"}}, + headBranchTags: simver.Tags{simver.Tag{Name: "v1.5.10-pr84+1002"}, simver.Tag{Name: "v1.5.10"}}, + headCommitTags: simver.Tags{}, + rootBranchTags: simver.Tags{ + simver.Tag{Name: "v1.5.9-reserved"}, + simver.Tag{Name: "v1.5.10-reserved"}, + simver.Tag{Name: "v1.5.0"}, + simver.Tag{Name: "v1.5.9-pr84+base"}, + }, + pr: 84, + isMerge: false, + isMinor: 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}, + simver.Tag{Name: "v1.5.11-pr84+base", Ref: base_ref}, + }, + }, } ctx := context.Background() @@ -326,6 +355,7 @@ func TestNewTags(t *testing.T) { t.Run(tc.name, func(t *testing.T) { mockExec := new(mockery.MockExecution_simver) mockExec.EXPECT().HeadBranchTags().Return(tc.headBranchTags) + mockExec.EXPECT().HeadCommitTags().Return(tc.headCommitTags) mockExec.EXPECT().BaseBranchTags().Return(tc.baseBranchTags) mockExec.EXPECT().PR().Return(tc.pr) mockExec.EXPECT().IsMinor().Return(tc.isMinor) @@ -345,3 +375,42 @@ func TestNewTags(t *testing.T) { }) } } +func TestTagString_BumpPatch(t *testing.T) { + testCases := []struct { + name string + input string + expected string + panic bool + }{ + { + name: "BumpPatch with patch version", + input: "v1.2.3", + expected: "v1.2.4", + panic: false, + }, + { + name: "BumpPatch with no patch version", + input: "v1.2", + expected: "v1.2.1", + panic: false, + }, + { + name: "BumpPatch with invalid patch version", + input: "v1.2.x", + panic: true, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + if tc.panic { + assert.Panics(t, func() { + simver.BumpPatch(tc.input) + }) + return + } + result := simver.BumpPatch(tc.input) + assert.Equal(t, tc.expected, result) + }) + } +} diff --git a/gen/mockery/Execution.simver.mockery.go b/gen/mockery/Execution.simver.mockery.go index 9d9dd13..7a0c8f3 100644 --- a/gen/mockery/Execution.simver.mockery.go +++ b/gen/mockery/Execution.simver.mockery.go @@ -150,6 +150,49 @@ func (_c *MockExecution_simver_HeadBranchTags_Call) RunAndReturn(run func() simv return _c } +// HeadCommitTags provides a mock function with given fields: +func (_m *MockExecution_simver) HeadCommitTags() simver.Tags { + ret := _m.Called() + + var r0 simver.Tags + if rf, ok := ret.Get(0).(func() simver.Tags); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(simver.Tags) + } + } + + return r0 +} + +// MockExecution_simver_HeadCommitTags_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'HeadCommitTags' +type MockExecution_simver_HeadCommitTags_Call struct { + *mock.Call +} + +// HeadCommitTags is a helper method to define mock.On call +func (_e *MockExecution_simver_Expecter) HeadCommitTags() *MockExecution_simver_HeadCommitTags_Call { + return &MockExecution_simver_HeadCommitTags_Call{Call: _e.mock.On("HeadCommitTags")} +} + +func (_c *MockExecution_simver_HeadCommitTags_Call) Run(run func()) *MockExecution_simver_HeadCommitTags_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *MockExecution_simver_HeadCommitTags_Call) Return(_a0 simver.Tags) *MockExecution_simver_HeadCommitTags_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockExecution_simver_HeadCommitTags_Call) RunAndReturn(run func() simver.Tags) *MockExecution_simver_HeadCommitTags_Call { + _c.Call.Return(run) + return _c +} + // IsMerge provides a mock function with given fields: func (_m *MockExecution_simver) IsMerge() bool { ret := _m.Called() diff --git a/simver.go b/simver.go index 55ac884..4d68078 100644 --- a/simver.go +++ b/simver.go @@ -55,6 +55,10 @@ func (e *rawExecution) IsMinor() bool { return e.baseBranch == e.rootBranch } +func (e *rawExecution) HeadCommitTags() Tags { + return e.headCommitTags +} + func (e *rawExecution) BuildTags(tags *CalculationOutput) Tags { return tags.ApplyRefs(&ApplyRefsOpts{ HeadRef: e.headCommit, From 2c225504c35b7e83141ecb3db1481c2dea5b9456 Mon Sep 17 00:00:00 2001 From: walter Date: Wed, 15 Nov 2023 06:56:48 -0600 Subject: [PATCH 21/38] fix build number bug --- execution.go | 12 +++++++++++- execution_test.go | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/execution.go b/execution.go index b889023..cfc480a 100644 --- a/execution.go +++ b/execution.go @@ -4,9 +4,11 @@ import ( "context" "fmt" "regexp" + "slices" "strconv" "strings" + // exp sort "github.com/rs/zerolog" "golang.org/x/mod/semver" ) @@ -148,7 +150,7 @@ func MostRecentReservedTag(e Execution) MRRT { } func MyMostRecentBuildNumber(e Execution) MMRBN { - reg := regexp.MustCompile(fmt.Sprintf(`^.*-pr%d+\+\d+$`, e.PR())) + reg := regexp.MustCompile(fmt.Sprintf(`^.*-pr%d\+\d+$`, e.PR())) highest := e.HeadBranchTags().SemversMatching(func(s string) bool { return reg.MatchString(s) }) @@ -157,6 +159,14 @@ func MyMostRecentBuildNumber(e Execution) MMRBN { return 0 } + slices.SortFunc(highest, func(a, b string) int { + // because we know the regex matches, we know the split will be len 2 + // and the second element will be a valid number + ai, _ := strconv.Atoi(strings.Split(a, "+")[1]) + bi, _ := strconv.Atoi(strings.Split(b, "+")[1]) + return bi - ai + }) + high := highest[0] // get the build number diff --git a/execution_test.go b/execution_test.go index 2387701..003e0bc 100644 --- a/execution_test.go +++ b/execution_test.go @@ -347,6 +347,22 @@ func TestNewTags(t *testing.T) { simver.Tag{Name: "v1.5.11-pr84+base", Ref: base_ref}, }, }, + { + name: "extra build tags", + baseBranchTags: simver.Tags{simver.Tag{Name: "v1.2.3"}}, + headBranchTags: simver.Tags{ + 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, + expectedTags: simver.Tags{ + simver.Tag{Name: "v1.2.4-pr2+6", Ref: head_ref}, + }, + }, } ctx := context.Background() From d274cdf11613923cc91054b0951eb176b1276115 Mon Sep 17 00:00:00 2001 From: walter Date: Wed, 15 Nov 2023 07:30:05 -0600 Subject: [PATCH 22/38] try assuming deletion --- simver.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/simver.go b/simver.go index 4d68078..d64f24e 100644 --- a/simver.go +++ b/simver.go @@ -104,18 +104,22 @@ func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Exec return nil, nil, false, err } - hc := pr.HeadCommit + var headBranchTags Tags + var headCommit string if pr.Merged { - hc = pr.MergeCommit + headCommit = pr.MergeCommit + headBranchTags = baseBranchTags + } else { + headCommit = pr.HeadCommit + branchTags, err := tprov.TagsFromBranch(ctx, pr.HeadBranch) + if err != nil { + return nil, nil, false, err + } + headBranchTags = branchTags } - headTags, err := tprov.TagsFromCommit(ctx, hc) - if err != nil { - return nil, nil, false, err - } - - branchTags, err := tprov.TagsFromBranch(ctx, pr.HeadBranch) + headTags, err := tprov.TagsFromCommit(ctx, headCommit) if err != nil { return nil, nil, false, err } @@ -126,7 +130,7 @@ func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Exec Any("rootCommitTags", rootCommitTags). Any("rootBranchTags", rootBranchTags). Any("headTags", headTags). - Any("branchTags", branchTags). + Any("headBranchTags", headBranchTags). Any("pr", pr). Msg("loaded tags") @@ -139,7 +143,7 @@ func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Exec headCommitTags: headTags, baseCommitTags: baseCommitTags, baseBranchTags: baseBranchTags, - headBranchTags: branchTags, + headBranchTags: headBranchTags, rootBranch: pr.RootBranch, rootCommit: pr.RootCommit, rootBranchTags: rootBranchTags, From f69c03d5de5af6c8c33e89b57bbeafcc65e0e089 Mon Sep 17 00:00:00 2001 From: walter Date: Wed, 15 Nov 2023 07:40:53 -0600 Subject: [PATCH 23/38] fix base problem --- execution.go | 2 +- execution_test.go | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/execution.go b/execution.go index cfc480a..bde3c93 100644 --- a/execution.go +++ b/execution.go @@ -123,7 +123,7 @@ func MostRecentLiveTag(e Execution) MRLT { func MyMostRecentTag(e Execution) MMRT { reg := regexp.MustCompile(`^v\d+\.\d+\.\d+.*$`) highest := e.HeadBranchTags().SemversMatching(func(s string) bool { - if strings.Contains(s, "-reserved") { + if strings.Contains(s, "-reserved") || strings.Contains(s, "-base") { return false } return reg.MatchString(s) diff --git a/execution_test.go b/execution_test.go index 003e0bc..17a7126 100644 --- a/execution_test.go +++ b/execution_test.go @@ -363,6 +363,19 @@ func TestNewTags(t *testing.T) { 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, + expectedTags: simver.Tags{ + simver.Tag{Name: "v1.2.4-pr2+6", Ref: head_ref}, + }, + }, } ctx := context.Background() From b346e2b183d0e112e9eb02332f7a12bb2a0975ff Mon Sep 17 00:00:00 2001 From: walter Date: Wed, 15 Nov 2023 07:53:56 -0600 Subject: [PATCH 24/38] fix accidental minor bump --- calculate.go | 2 +- calculate_test.go | 29 ++++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/calculate.go b/calculate.go index 72bba57..22ce733 100644 --- a/calculate.go +++ b/calculate.go @@ -81,7 +81,7 @@ func (me *Calculation) CalculateNewTagsRaw() *CalculationOutput { // force patch is ignored if this is a merge if me.ForcePatch && !me.IsMerge { - mmrt = BumpPatch(mmrt) + nvt = BumpPatch(mmrt) validMmrt = false } diff --git a/calculate_test.go b/calculate_test.go index 413690e..a608a89 100644 --- a/calculate_test.go +++ b/calculate_test.go @@ -131,9 +131,9 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { IsMerge: false, ForcePatch: true, }, - expectedBaseTags: []string{"v1.2.6-pr1+base"}, - expectedHeadTags: []string{"v1.2.6-pr1+34"}, - expectedRootTags: []string{"v1.2.6-reserved"}, + expectedBaseTags: []string{"v1.2.5-pr1+base"}, + expectedHeadTags: []string{"v1.2.5-pr1+34"}, + expectedRootTags: []string{"v1.2.5-reserved"}, expectedMergeTags: []string{}, }, { @@ -153,6 +153,29 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { expectedRootTags: []string{}, expectedMergeTags: []string{"v1.2.4"}, }, + { + name: "expired mmrt", + calculation: &simver.Calculation{ + MostRecentLiveTag: "v1.10.3", + MostRecentReservedTag: "v1.18.3-reserved", + MyMostRecentTag: "v1.9.9", + MyMostRecentBuild: 33, + PR: 85, + NextValidTag: "v99.99.99", + IsMerge: false, + ForcePatch: true, + }, + expectedBaseTags: []string{ + "v1.9.10-pr85+base", + }, + expectedHeadTags: []string{ + "v1.9.10-pr85+34", + }, + expectedRootTags: []string{ + "v1.9.10-reserved", + }, + expectedMergeTags: []string{}, + }, // Add more test cases here... } From fc9096d87704e484f0f4ff91bb3eba86bd527b36 Mon Sep 17 00:00:00 2001 From: walter Date: Wed, 15 Nov 2023 21:50:59 -0600 Subject: [PATCH 25/38] add context to calculation for logging --- README.md | 11 ++++++----- calculate.go | 15 ++++++++++++++- calculate_test.go | 5 ++++- cmd/simver_github_actions/main.go | 4 ++-- execution_test.go | 2 +- 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 8b48089..d29bf1c 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,11 @@ two bugs: - need to make sure merges do not have build numbers - need to make sure that build nums are picked up +# probs to test +1. make sure that a new pr to main does a minor bump +2. make sure that a new pr not to main does a patch bump +3. make sure that a new commit to a pr who has been tagged with a version and was last used for it does a patch bump + # process @@ -91,14 +96,10 @@ two bugs: 1. find the mrrt and mrlt, calculate the nvt 2. create a new tag (based on nvt) on the head commit with no build number or prerelease - - - - - when you merge a pr: - find the mmrt or the pr branch, and we need to start using that for this branch - the base branch should inherit the mmrt from the pr branch - so we need to create: 1. a new "base" tag for the base branch with the mmrt of the pr branch 2. create a new build tag using the mmrt + diff --git a/calculate.go b/calculate.go index 22ce733..923e3b9 100644 --- a/calculate.go +++ b/calculate.go @@ -1,8 +1,10 @@ package simver import ( + "context" "fmt" + "github.com/rs/zerolog" "github.com/walteh/terrors" "golang.org/x/mod/semver" ) @@ -53,7 +55,7 @@ func (out *CalculationOutput) ApplyRefs(opts *ApplyRefsOpts) Tags { return tags } -func (me *Calculation) CalculateNewTagsRaw() *CalculationOutput { +func (me *Calculation) CalculateNewTagsRaw(ctx context.Context) *CalculationOutput { out := &CalculationOutput{ BaseTags: []string{}, HeadTags: []string{}, @@ -99,5 +101,16 @@ func (me *Calculation) CalculateNewTagsRaw() *CalculationOutput { out.HeadTags = append(out.HeadTags, next) } + zerolog.Ctx(ctx).Debug(). + Any("calculation", me). + Any("output", out). + Str("mmrt", mmrt). + Str("mrlt", mrlt). + Str("nvt", nvt). + Str("pr", fmt.Sprintf("%d", me.PR)). + Bool("isMerge", me.IsMerge). + Bool("forcePatch", me.ForcePatch). + Msg("CalculateNewTagsRaw") + return out } diff --git a/calculate_test.go b/calculate_test.go index a608a89..667b7d0 100644 --- a/calculate_test.go +++ b/calculate_test.go @@ -1,6 +1,7 @@ package simver_test import ( + "context" "testing" "github.com/stretchr/testify/require" @@ -179,10 +180,12 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { // 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() + out := tc.calculation.CalculateNewTagsRaw(ctx) if i == "base" { require.NotContains(t, out.BaseTags, "", "Base tags contain empty string") diff --git a/cmd/simver_github_actions/main.go b/cmd/simver_github_actions/main.go index 4766906..4d96c5e 100644 --- a/cmd/simver_github_actions/main.go +++ b/cmd/simver_github_actions/main.go @@ -178,7 +178,7 @@ func main() { os.Exit(0) } - tt := simver.Calculate(ctx, ee).CalculateNewTagsRaw() + tt := simver.Calculate(ctx, ee).CalculateNewTagsRaw(ctx) tags := ee.BuildTags(tt) @@ -208,7 +208,7 @@ func main() { } ee = eez prd = prz - tt := simver.Calculate(ctx, ee).CalculateNewTagsRaw() + tt := simver.Calculate(ctx, ee).CalculateNewTagsRaw(ctx) tags = ee.BuildTags(tt) reservedTag, reserved = tags.GetReserved() } else { diff --git a/execution_test.go b/execution_test.go index 17a7126..5c47131 100644 --- a/execution_test.go +++ b/execution_test.go @@ -391,7 +391,7 @@ func TestNewTags(t *testing.T) { mockExec.EXPECT().IsMerge().Return(tc.isMerge) mockExec.EXPECT().RootBranchTags().Return(tc.rootBranchTags) - result := simver.Calculate(ctx, mockExec).CalculateNewTagsRaw() + result := simver.Calculate(ctx, mockExec).CalculateNewTagsRaw(ctx) got := result.ApplyRefs(&simver.ApplyRefsOpts{ HeadRef: head_ref, From c1218abd32e179998befcffb67846ea9dc606770 Mon Sep 17 00:00:00 2001 From: walter Date: Thu, 16 Nov 2023 06:25:52 -0600 Subject: [PATCH 26/38] refactor for logical fixes --- README.md | 1 + calculate.go | 33 +++---- calculate_test.go | 120 +++++++++++------------- cmd/simver_github_actions/main.go | 77 ++++++++------- exec/exec.go | 1 + exec/tag.go | 16 ++-- execution.go | 25 ++--- execution_test.go | 37 ++++++-- gen/mockery/Execution.simver.mockery.go | 89 +++++++++--------- git.go | 6 +- refs.go | 31 ++++++ simver.go | 30 ++++-- tags.go | 32 +++++++ 13 files changed, 287 insertions(+), 211 deletions(-) create mode 100644 exec/exec.go create mode 100644 refs.go diff --git a/README.md b/README.md index d29bf1c..e211748 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ two bugs: 1. make sure that a new pr to main does a minor bump 2. make sure that a new pr not to main does a patch bump 3. make sure that a new commit to a pr who has been tagged with a version and was last used for it does a patch bump +1. make sure if reserved is set, but others are not that it does not loop infinitely # process diff --git a/calculate.go b/calculate.go index 923e3b9..c505d65 100644 --- a/calculate.go +++ b/calculate.go @@ -10,14 +10,13 @@ import ( ) type Calculation struct { - MyMostRecentTag MMRT - MostRecentLiveTag MRLT - MyMostRecentBuild MMRBN - MostRecentReservedTag MRRT - PR int - NextValidTag NVT - IsMerge bool - ForcePatch bool + MyMostRecentTag MMRT + MostRecentLiveTag MRLT + MyMostRecentBuild MMRBN + PR int + NextValidTag NVT + IsMerge bool + ForcePatch bool } var ( @@ -31,26 +30,20 @@ type CalculationOutput struct { MergeTags []string } -type ApplyRefsOpts struct { - HeadRef string - BaseRef string - RootRef string - MergeRef string -} - -func (out *CalculationOutput) ApplyRefs(opts *ApplyRefsOpts) Tags { +func (out *CalculationOutput) ApplyRefs(opts RefProvider) Tags { tags := make(Tags, 0) for _, tag := range out.BaseTags { - tags = append(tags, Tag{Name: tag, Ref: opts.BaseRef}) + tags = append(tags, Tag{Name: tag, Ref: opts.Base()}) } + for _, tag := range out.HeadTags { - tags = append(tags, Tag{Name: tag, Ref: opts.HeadRef}) + tags = append(tags, Tag{Name: tag, Ref: opts.Head()}) } for _, tag := range out.RootTags { - tags = append(tags, Tag{Name: tag, Ref: opts.RootRef}) + tags = append(tags, Tag{Name: tag, Ref: opts.Root()}) } for _, tag := range out.MergeTags { - tags = append(tags, Tag{Name: tag, Ref: opts.MergeRef}) + tags = append(tags, Tag{Name: tag, Ref: opts.Merge()}) } return tags } diff --git a/calculate_test.go b/calculate_test.go index 667b7d0..aec6cff 100644 --- a/calculate_test.go +++ b/calculate_test.go @@ -20,14 +20,13 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { { name: "expired mmrt", calculation: &simver.Calculation{ - MostRecentLiveTag: "v1.10.3", - MostRecentReservedTag: "v1.18.3-reserved", - MyMostRecentTag: "v1.9.9", - MyMostRecentBuild: 33, - PR: 85, - NextValidTag: "v99.99.99", - IsMerge: false, - ForcePatch: false, + MostRecentLiveTag: "v1.10.3", + MyMostRecentTag: "v1.9.9", + MyMostRecentBuild: 33, + PR: 85, + NextValidTag: "v99.99.99", + IsMerge: false, + ForcePatch: false, }, expectedBaseTags: []string{ "v99.99.99-pr85+base", @@ -43,14 +42,13 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { { name: "missing all", calculation: &simver.Calculation{ - MostRecentLiveTag: "", - MostRecentReservedTag: "", - MyMostRecentTag: "", - MyMostRecentBuild: 1, - PR: 1, - NextValidTag: "v3.3.3", - IsMerge: false, - ForcePatch: false, + MostRecentLiveTag: "", + MyMostRecentTag: "", + MyMostRecentBuild: 1, + PR: 1, + NextValidTag: "v3.3.3", + IsMerge: false, + ForcePatch: false, }, expectedBaseTags: []string{ "v3.3.3-pr1+base", @@ -66,14 +64,13 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { { name: "valid mmrt", calculation: &simver.Calculation{ - MostRecentLiveTag: "v1.2.3", - MostRecentReservedTag: "v1.2.5-reserved", - MyMostRecentTag: "v1.2.4", - MyMostRecentBuild: 33, - PR: 1, - NextValidTag: "v1.2.6", - IsMerge: false, - ForcePatch: false, + MostRecentLiveTag: "v1.2.3", + MyMostRecentTag: "v1.2.4", + MyMostRecentBuild: 33, + PR: 1, + NextValidTag: "v1.2.6", + IsMerge: false, + ForcePatch: false, }, expectedBaseTags: []string{}, expectedHeadTags: []string{"v1.2.4-pr1+34"}, @@ -83,14 +80,13 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { { name: "i have a tag reserved but do not have my own tag", calculation: &simver.Calculation{ - MostRecentLiveTag: "v1.2.3", - MostRecentReservedTag: "v1.2.5-reserved", - MyMostRecentTag: "", - MyMostRecentBuild: 33, - PR: 1, - NextValidTag: "v1.2.6", - IsMerge: false, - ForcePatch: false, + MostRecentLiveTag: "v1.2.3", + MyMostRecentTag: "", + MyMostRecentBuild: 33, + PR: 1, + NextValidTag: "v1.2.6", + IsMerge: false, + ForcePatch: false, }, expectedBaseTags: []string{ "v1.2.6-pr1+base", @@ -106,14 +102,13 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { { name: "valid mmrt with merge", calculation: &simver.Calculation{ - MostRecentLiveTag: "v1.2.3", - MostRecentReservedTag: "v1.2.5-reserved", - MyMostRecentTag: "v1.2.4", - MyMostRecentBuild: 33, - PR: 1, - NextValidTag: "v1.2.6", - IsMerge: true, - ForcePatch: false, + MostRecentLiveTag: "v1.2.3", + MyMostRecentTag: "v1.2.4", + MyMostRecentBuild: 33, + PR: 1, + NextValidTag: "v1.2.6", + IsMerge: true, + ForcePatch: false, }, expectedBaseTags: []string{}, expectedHeadTags: []string{}, @@ -123,14 +118,13 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { { name: "valid mmrt with force patch", calculation: &simver.Calculation{ - MostRecentLiveTag: "v1.2.3", - MostRecentReservedTag: "v1.2.5-reserved", - MyMostRecentTag: "v1.2.4", - MyMostRecentBuild: 33, - PR: 1, - NextValidTag: "v1.2.6", - IsMerge: false, - ForcePatch: true, + MostRecentLiveTag: "v1.2.3", + MyMostRecentTag: "v1.2.4", + MyMostRecentBuild: 33, + PR: 1, + NextValidTag: "v1.2.6", + IsMerge: false, + ForcePatch: true, }, expectedBaseTags: []string{"v1.2.5-pr1+base"}, expectedHeadTags: []string{"v1.2.5-pr1+34"}, @@ -140,14 +134,13 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { { name: "valid mmrt with force patch (merge override)", calculation: &simver.Calculation{ - MostRecentLiveTag: "v1.2.3", - MostRecentReservedTag: "v1.2.5-reserved", - MyMostRecentTag: "v1.2.4", - MyMostRecentBuild: 33, - PR: 1, - NextValidTag: "v1.2.6", - IsMerge: true, - ForcePatch: true, + MostRecentLiveTag: "v1.2.3", + MyMostRecentTag: "v1.2.4", + MyMostRecentBuild: 33, + PR: 1, + NextValidTag: "v1.2.6", + IsMerge: true, + ForcePatch: true, }, expectedBaseTags: []string{}, expectedHeadTags: []string{}, @@ -157,14 +150,13 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { { name: "expired mmrt", calculation: &simver.Calculation{ - MostRecentLiveTag: "v1.10.3", - MostRecentReservedTag: "v1.18.3-reserved", - MyMostRecentTag: "v1.9.9", - MyMostRecentBuild: 33, - PR: 85, - NextValidTag: "v99.99.99", - IsMerge: false, - ForcePatch: true, + MostRecentLiveTag: "v1.10.3", + MyMostRecentTag: "v1.9.9", + MyMostRecentBuild: 33, + PR: 85, + NextValidTag: "v99.99.99", + IsMerge: false, + ForcePatch: true, }, expectedBaseTags: []string{ "v1.9.10-pr85+base", diff --git a/cmd/simver_github_actions/main.go b/cmd/simver_github_actions/main.go index 4d96c5e..27e1d34 100644 --- a/cmd/simver_github_actions/main.go +++ b/cmd/simver_github_actions/main.go @@ -7,7 +7,6 @@ import ( "os" "strconv" "strings" - "time" "github.com/rs/zerolog" "github.com/walteh/simver" @@ -180,46 +179,46 @@ func main() { tt := simver.Calculate(ctx, ee).CalculateNewTagsRaw(ctx) - tags := ee.BuildTags(tt) - - reservedTag, reserved := tags.GetReserved() - - tries := 0 - - for reserved { - err := tagprov.CreateTag(ctx, reservedTag) - if err != nil { - if tries > 5 { - zerolog.Ctx(ctx).Error().Err(err).Msgf("error creating tag: %v", err) - fmt.Println(terrors.FormatErrorCaller(err)) - os.Exit(1) - } - - time.Sleep(1 * time.Second) - eez, prz, keepgoing, err := simver.LoadExecution(ctx, tagprov, prr) - if err != nil { - zerolog.Ctx(ctx).Error().Err(err).Msgf("error loading execution: %v", err) - fmt.Println(terrors.FormatErrorCaller(err)) - os.Exit(1) - } - if !keepgoing { - zerolog.Ctx(ctx).Debug().Msg("execution is complete, exiting") - os.Exit(0) - } - ee = eez - prd = prz - tt := simver.Calculate(ctx, ee).CalculateNewTagsRaw(ctx) - tags = ee.BuildTags(tt) - reservedTag, reserved = tags.GetReserved() - } else { - reserved = false - } - } + tags := tt.ApplyRefs(ee.ProvideRefs()) + + // reservedTag, reserved := tags.GetReserved() + + // tries := 0 + + // for reserved { + // err := tagprov.CreateTag(ctx, reservedTag) + // if err != nil { + // tries++ + // if tries > 5 { + // zerolog.Ctx(ctx).Error().Err(err).Msgf("error creating tag: %v", err) + // fmt.Println(terrors.FormatErrorCaller(err)) + // os.Exit(1) + // } + + // time.Sleep(1 * time.Second) + // eez, prz, keepgoing, err := simver.LoadExecution(ctx, tagprov, prr) + // if err != nil { + // zerolog.Ctx(ctx).Error().Err(err).Msgf("error loading execution: %v", err) + // fmt.Println(terrors.FormatErrorCaller(err)) + // os.Exit(1) + // } + // if !keepgoing { + // zerolog.Ctx(ctx).Debug().Msg("execution is complete, exiting") + // os.Exit(0) + // } + // ee = eez + // prd = prz + // tags := simver.Calculate(ctx, ee).CalculateNewTagsRaw(ctx).ApplyRefs(ee.ProvideRefs()) + // reservedTag, reserved = tags.GetReserved() + // } else { + // reserved = false + // } + // } for _, tag := range tags { - if tag.Name == reservedTag.Name && tag.Ref == reservedTag.Ref { - continue - } + // if tag.Name == reservedTag.Name && tag.Ref == reservedTag.Ref { + // continue + // } // if prd.Merged { // if havMergedTag { diff --git a/exec/exec.go b/exec/exec.go new file mode 100644 index 0000000..81c33c4 --- /dev/null +++ b/exec/exec.go @@ -0,0 +1 @@ +package exec diff --git a/exec/tag.go b/exec/tag.go index e130ac2..ee0ef46 100644 --- a/exec/tag.go +++ b/exec/tag.go @@ -15,7 +15,7 @@ var ( _ simver.TagProvider = (*gitProvider)(nil) ) -func (p *gitProvider) TagsFromCommit(ctx context.Context, commitHash string) ([]simver.Tag, error) { +func (p *gitProvider) TagsFromCommit(ctx context.Context, commitHash string) (simver.Tags, error) { ctx = zerolog.Ctx(ctx).With().Str("commit", commitHash).Logger().WithContext(ctx) @@ -28,7 +28,7 @@ func (p *gitProvider) TagsFromCommit(ctx context.Context, commitHash string) ([] } lines := strings.Split(string(out), "\n") - var tags []simver.Tag + var tags simver.Tags for _, line := range lines { line = strings.TrimSpace(line) if line == "" { @@ -42,7 +42,7 @@ func (p *gitProvider) TagsFromCommit(ctx context.Context, commitHash string) ([] return tags, nil } -func (p *gitProvider) TagsFromBranch(ctx context.Context, branch string) ([]simver.Tag, error) { +func (p *gitProvider) TagsFromBranch(ctx context.Context, branch string) (simver.Tags, error) { start := time.Now() @@ -56,7 +56,7 @@ func (p *gitProvider) TagsFromBranch(ctx context.Context, branch string) ([]simv lines := strings.Split(string(out), "\n") - var tags []simver.Tag + var tags simver.Tags for _, line := range lines { if line == "" { @@ -95,13 +95,15 @@ func (p *gitProvider) TagsFromBranch(ctx context.Context, branch string) ([]simv tags = append(tags, simver.Tag{Name: name, Ref: dat.Ref}) } + tags = tags.ExtractCommitRefs() + zerolog.Ctx(ctx).Debug().Int("tags_len", len(tags)).Any("tags", tags).Dur("dur", time.Since(start)).Msg("got tags from branch") return tags, nil } -func (p *gitProvider) FetchTags(ctx context.Context) ([]simver.Tag, error) { +func (p *gitProvider) FetchTags(ctx context.Context) (simver.Tags, error) { start := time.Now() @@ -125,7 +127,7 @@ func (p *gitProvider) FetchTags(ctx context.Context) ([]simver.Tag, error) { } lines := strings.Split(string(out), "\n") - var tagInfos []simver.Tag + var tagInfos simver.Tags for _, line := range lines { parts := strings.Split(line, " ") if len(parts) != 2 { @@ -140,6 +142,8 @@ func (p *gitProvider) FetchTags(ctx context.Context) ([]simver.Tag, error) { tagInfos = append(tagInfos, simver.Tag{Name: name, Ref: ref}) } + tagInfos = tagInfos.ExtractCommitRefs() + zerolog.Ctx(ctx).Debug().Int("tags_len", len(tagInfos)).Dur("duration", time.Since(start)).Any("tags", tagInfos).Msg("tags fetched") return tagInfos, nil diff --git a/execution.go b/execution.go index bde3c93..0255ea3 100644 --- a/execution.go +++ b/execution.go @@ -21,7 +21,7 @@ type Execution interface { HeadBranchTags() Tags BaseBranchTags() Tags RootBranchTags() Tags - BuildTags(tags *CalculationOutput) Tags + ProvideRefs() RefProvider } const baseTag = "v0.1.0" @@ -33,26 +33,17 @@ func Calculate(ctx context.Context, ex Execution) *Calculation { mmrt := MyMostRecentTag(ex) - // // if mmrt already exists on head - - // if ForcePatch(ctx, ex, mmrt) { - // mmrt = BumpPatch(mmrt) - // } - return &Calculation{ - IsMerge: ex.IsMerge(), - MostRecentLiveTag: mrlt, - MostRecentReservedTag: mrrt, - ForcePatch: ForcePatch(ctx, ex, mmrt), - MyMostRecentTag: mmrt, - MyMostRecentBuild: MyMostRecentBuildNumber(ex), - PR: ex.PR(), - NextValidTag: GetNextValidTag(ctx, ex.IsMinor(), mrlt, mrrt), + IsMerge: ex.IsMerge(), + MostRecentLiveTag: mrlt, + ForcePatch: ForcePatch(ctx, ex, mmrt), + MyMostRecentTag: mmrt, + MyMostRecentBuild: MyMostRecentBuildNumber(ex), + PR: ex.PR(), + NextValidTag: GetNextValidTag(ctx, ex.IsMinor(), mrlt, mrrt), } } -// type TagString string - type MRLT string // most recent live tag type MRRT string // most recent reserved tag type NVT string // next valid tag diff --git a/execution_test.go b/execution_test.go index 5c47131..b7f34c4 100644 --- a/execution_test.go +++ b/execution_test.go @@ -376,12 +376,33 @@ func TestNewTags(t *testing.T) { 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: 0, + isMerge: false, + isMinor: 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) + // we expect to use the next valid tag, which is v1.2.5 + simver.Tag{Name: "v1.2.5-pr0+1", Ref: head_ref}, + simver.Tag{Name: "v1.2.5-reserved", Ref: root_ref}, + simver.Tag{Name: "v1.2.5-pr0+base", Ref: base_ref}, + }, + }, } ctx := context.Background() for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { + + // ctx = zerolog.New(zerolog.NewTestWriter(t)).With().Logger().WithContext(ctx) + mockExec := new(mockery.MockExecution_simver) mockExec.EXPECT().HeadBranchTags().Return(tc.headBranchTags) mockExec.EXPECT().HeadCommitTags().Return(tc.headCommitTags) @@ -391,14 +412,14 @@ func TestNewTags(t *testing.T) { mockExec.EXPECT().IsMerge().Return(tc.isMerge) mockExec.EXPECT().RootBranchTags().Return(tc.rootBranchTags) - result := simver.Calculate(ctx, mockExec).CalculateNewTagsRaw(ctx) - - got := result.ApplyRefs(&simver.ApplyRefsOpts{ - HeadRef: head_ref, - BaseRef: base_ref, - RootRef: root_ref, - MergeRef: merge_ref, - }) + got := simver.Calculate(ctx, mockExec). + CalculateNewTagsRaw(ctx). + ApplyRefs(&simver.BasicRefProvider{ + HeadRef: head_ref, + BaseRef: base_ref, + RootRef: root_ref, + MergeRef: merge_ref, + }) assert.ElementsMatch(t, tc.expectedTags, got) }) diff --git a/gen/mockery/Execution.simver.mockery.go b/gen/mockery/Execution.simver.mockery.go index 7a0c8f3..ee4f54d 100644 --- a/gen/mockery/Execution.simver.mockery.go +++ b/gen/mockery/Execution.simver.mockery.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.37.0. DO NOT EDIT. +// Code generated by mockery v2.37.1. DO NOT EDIT. package mockery @@ -63,50 +63,6 @@ func (_c *MockExecution_simver_BaseBranchTags_Call) RunAndReturn(run func() simv return _c } -// BuildTags provides a mock function with given fields: tags -func (_m *MockExecution_simver) BuildTags(tags *simver.CalculationOutput) simver.Tags { - ret := _m.Called(tags) - - var r0 simver.Tags - if rf, ok := ret.Get(0).(func(*simver.CalculationOutput) simver.Tags); ok { - r0 = rf(tags) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(simver.Tags) - } - } - - return r0 -} - -// MockExecution_simver_BuildTags_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'BuildTags' -type MockExecution_simver_BuildTags_Call struct { - *mock.Call -} - -// BuildTags is a helper method to define mock.On call -// - tags *simver.CalculationOutput -func (_e *MockExecution_simver_Expecter) BuildTags(tags interface{}) *MockExecution_simver_BuildTags_Call { - return &MockExecution_simver_BuildTags_Call{Call: _e.mock.On("BuildTags", tags)} -} - -func (_c *MockExecution_simver_BuildTags_Call) Run(run func(tags *simver.CalculationOutput)) *MockExecution_simver_BuildTags_Call { - _c.Call.Run(func(args mock.Arguments) { - run(args[0].(*simver.CalculationOutput)) - }) - return _c -} - -func (_c *MockExecution_simver_BuildTags_Call) Return(_a0 simver.Tags) *MockExecution_simver_BuildTags_Call { - _c.Call.Return(_a0) - return _c -} - -func (_c *MockExecution_simver_BuildTags_Call) RunAndReturn(run func(*simver.CalculationOutput) simver.Tags) *MockExecution_simver_BuildTags_Call { - _c.Call.Return(run) - return _c -} - // HeadBranchTags provides a mock function with given fields: func (_m *MockExecution_simver) HeadBranchTags() simver.Tags { ret := _m.Called() @@ -316,6 +272,49 @@ func (_c *MockExecution_simver_PR_Call) RunAndReturn(run func() int) *MockExecut return _c } +// ProvideRefs provides a mock function with given fields: +func (_m *MockExecution_simver) ProvideRefs() simver.RefProvider { + ret := _m.Called() + + var r0 simver.RefProvider + if rf, ok := ret.Get(0).(func() simver.RefProvider); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(simver.RefProvider) + } + } + + return r0 +} + +// MockExecution_simver_ProvideRefs_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ProvideRefs' +type MockExecution_simver_ProvideRefs_Call struct { + *mock.Call +} + +// ProvideRefs is a helper method to define mock.On call +func (_e *MockExecution_simver_Expecter) ProvideRefs() *MockExecution_simver_ProvideRefs_Call { + return &MockExecution_simver_ProvideRefs_Call{Call: _e.mock.On("ProvideRefs")} +} + +func (_c *MockExecution_simver_ProvideRefs_Call) Run(run func()) *MockExecution_simver_ProvideRefs_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *MockExecution_simver_ProvideRefs_Call) Return(_a0 simver.RefProvider) *MockExecution_simver_ProvideRefs_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockExecution_simver_ProvideRefs_Call) RunAndReturn(run func() simver.RefProvider) *MockExecution_simver_ProvideRefs_Call { + _c.Call.Return(run) + return _c +} + // RootBranchTags provides a mock function with given fields: func (_m *MockExecution_simver) RootBranchTags() simver.Tags { ret := _m.Called() diff --git a/git.go b/git.go index bcfa880..a46d236 100644 --- a/git.go +++ b/git.go @@ -9,8 +9,8 @@ type GitProvider interface { } type TagProvider interface { - FetchTags(ctx context.Context) ([]Tag, error) + FetchTags(ctx context.Context) (Tags, error) CreateTag(ctx context.Context, tag Tag) error - TagsFromCommit(ctx context.Context, commitHash string) ([]Tag, error) - TagsFromBranch(ctx context.Context, branch string) ([]Tag, error) + TagsFromCommit(ctx context.Context, commitHash string) (Tags, error) + TagsFromBranch(ctx context.Context, branch string) (Tags, error) } diff --git a/refs.go b/refs.go new file mode 100644 index 0000000..f39ce8a --- /dev/null +++ b/refs.go @@ -0,0 +1,31 @@ +package simver + +type RefProvider interface { + Head() string + Base() string + Root() string + Merge() string +} + +type BasicRefProvider struct { + HeadRef string + BaseRef string + RootRef string + MergeRef string +} + +func (e *BasicRefProvider) Head() string { + return e.HeadRef +} + +func (e *BasicRefProvider) Base() string { + return e.BaseRef +} + +func (e *BasicRefProvider) Root() string { + return e.RootRef +} + +func (e *BasicRefProvider) Merge() string { + return e.MergeRef +} diff --git a/simver.go b/simver.go index d64f24e..76f1e4c 100644 --- a/simver.go +++ b/simver.go @@ -7,6 +7,7 @@ import ( ) var _ Execution = &rawExecution{} +var _ RefProvider = &rawExecution{} type rawExecution struct { pr *PRDetails @@ -27,6 +28,26 @@ type rawExecution struct { isMinor bool } +func (e *rawExecution) Head() string { + return e.headCommit +} + +func (e *rawExecution) Base() string { + return e.baseCommit +} + +func (e *rawExecution) Root() string { + return e.rootCommit +} + +func (e *rawExecution) Merge() string { + return e.mergeCommit +} + +func (e *rawExecution) ProvideRefs() RefProvider { + return e +} + func (e *rawExecution) BaseBranchTags() Tags { return e.baseBranchTags } @@ -59,15 +80,6 @@ func (e *rawExecution) HeadCommitTags() Tags { return e.headCommitTags } -func (e *rawExecution) BuildTags(tags *CalculationOutput) Tags { - return tags.ApplyRefs(&ApplyRefsOpts{ - HeadRef: e.headCommit, - BaseRef: e.baseCommit, - RootRef: e.rootCommit, - MergeRef: e.mergeCommit, - }) -} - func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Execution, *PRDetails, bool, error) { pr, err := prr.CurrentPR(ctx) diff --git a/tags.go b/tags.go index 4a1368e..d29a5c8 100644 --- a/tags.go +++ b/tags.go @@ -24,6 +24,16 @@ func (t Tags) GetReserved() (Tag, bool) { return Tag{}, false } +func (t Tags) Names() []string { + var names []string + + for _, tag := range t { + names = append(names, tag.Name) + } + + return names +} + func (t Tags) SemversMatching(matcher func(string) bool) []string { var versions []string @@ -41,3 +51,25 @@ func (t Tags) SemversMatching(matcher func(string) bool) []string { return versions } + +func (t Tags) ExtractCommitRefs() Tags { + var tags Tags + + for _, tag := range t { + if len(tag.Ref) == 40 { + tags = append(tags, tag) + } + } + + return tags +} + +func (t Tags) MappedByName() map[string]string { + m := make(map[string]string) + + for _, tag := range t { + m[tag.Name] = tag.Ref + } + + return m +} From f6554b5a9000393eb256aae390e5ecaa67104539 Mon Sep 17 00:00:00 2001 From: walter Date: Thu, 16 Nov 2023 06:36:52 -0600 Subject: [PATCH 27/38] test 123 --- exec/tag.go | 2 ++ tags_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 tags_test.go diff --git a/exec/tag.go b/exec/tag.go index ee0ef46..a17c1eb 100644 --- a/exec/tag.go +++ b/exec/tag.go @@ -95,6 +95,8 @@ func (p *gitProvider) TagsFromBranch(ctx context.Context, branch string) (simver tags = append(tags, simver.Tag{Name: name, Ref: dat.Ref}) } + zerolog.Ctx(ctx).Debug().Int("tags_len", len(tags)).Any("tags", tags).Dur("dur", time.Since(start)).Msg("got tags from branch") + tags = tags.ExtractCommitRefs() zerolog.Ctx(ctx).Debug().Int("tags_len", len(tags)).Any("tags", tags).Dur("dur", time.Since(start)).Msg("got tags from branch") diff --git a/tags_test.go b/tags_test.go new file mode 100644 index 0000000..30909ba --- /dev/null +++ b/tags_test.go @@ -0,0 +1,57 @@ +package simver_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/walteh/simver" +) + +func TestExtractCommitRefs(t *testing.T) { + testCases := []struct { + name string + tags simver.Tags + expected simver.Tags + }{ + { + name: "No Commit Refs", + tags: simver.Tags{ + simver.Tag{Name: "v1.2.3"}, + simver.Tag{Name: "v1.2.4-pr1+base"}, + simver.Tag{Name: "v1.2.4-reserved"}, + }, + expected: simver.Tags{}, + }, + { + name: "One Commit Ref", + tags: simver.Tags{ + simver.Tag{Name: "v1.2.3"}, + simver.Tag{Name: "v1.2.4-pr1+base", Ref: "1234567890123456789012345678901234567890"}, + simver.Tag{Name: "v1.2.4-reserved"}, + }, + expected: simver.Tags{ + simver.Tag{Name: "v1.2.4-pr1+base", Ref: "1234567890123456789012345678901234567890"}, + }, + }, + { + name: "Multiple Commit Refs", + tags: simver.Tags{ + simver.Tag{Name: "v1.2.3"}, + simver.Tag{Name: "v1.2.4-pr1+base", Ref: "1234567890123456789012345678901234567890"}, + simver.Tag{Name: "v1.2.4-reserved"}, + simver.Tag{Name: "v1.2.5-pr2+base", Ref: "0987654321098765432109876543210987654321"}, + }, + expected: simver.Tags{ + simver.Tag{Name: "v1.2.4-pr1+base", Ref: "1234567890123456789012345678901234567890"}, + simver.Tag{Name: "v1.2.5-pr2+base", Ref: "0987654321098765432109876543210987654321"}, + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result := tc.tags.ExtractCommitRefs() + assert.ElementsMatch(t, tc.expected, result) + }) + } +} From b26d2e3782f27c636025d120e8017708be25b1c2 Mon Sep 17 00:00:00 2001 From: walter Date: Thu, 16 Nov 2023 06:40:35 -0600 Subject: [PATCH 28/38] fixed now --- exec/tag.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/exec/tag.go b/exec/tag.go index a17c1eb..3bde69f 100644 --- a/exec/tag.go +++ b/exec/tag.go @@ -92,12 +92,12 @@ func (p *gitProvider) TagsFromBranch(ctx context.Context, branch string) (simver continue } - tags = append(tags, simver.Tag{Name: name, Ref: dat.Ref}) + tags = append(tags, simver.Tag{Name: name, Ref: dat.Sha}) } zerolog.Ctx(ctx).Debug().Int("tags_len", len(tags)).Any("tags", tags).Dur("dur", time.Since(start)).Msg("got tags from branch") - tags = tags.ExtractCommitRefs() + // tags = tags.ExtractCommitRefs() zerolog.Ctx(ctx).Debug().Int("tags_len", len(tags)).Any("tags", tags).Dur("dur", time.Since(start)).Msg("got tags from branch") @@ -144,7 +144,7 @@ func (p *gitProvider) FetchTags(ctx context.Context) (simver.Tags, error) { tagInfos = append(tagInfos, simver.Tag{Name: name, Ref: ref}) } - tagInfos = tagInfos.ExtractCommitRefs() + // tagInfos = tagInfos.ExtractCommitRefs() zerolog.Ctx(ctx).Debug().Int("tags_len", len(tagInfos)).Dur("duration", time.Since(start)).Any("tags", tagInfos).Msg("tags fetched") From e3205de57366ac8f51fd27dd51a12e5cc2b7d6f8 Mon Sep 17 00:00:00 2001 From: walter Date: Thu, 16 Nov 2023 14:29:04 -0600 Subject: [PATCH 29/38] might work now --- calculate_test.go | 25 ++++++++++- execution.go | 78 ++++++++++++++++++++++++++------- execution_test.go | 107 +++++++++++++++++++++++++++++++++++++++------- 3 files changed, 178 insertions(+), 32 deletions(-) diff --git a/calculate_test.go b/calculate_test.go index aec6cff..b9b5b9d 100644 --- a/calculate_test.go +++ b/calculate_test.go @@ -148,7 +148,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { expectedMergeTags: []string{"v1.2.4"}, }, { - name: "expired mmrt", + name: "expired mmrt with force patch", calculation: &simver.Calculation{ MostRecentLiveTag: "v1.10.3", MyMostRecentTag: "v1.9.9", @@ -169,6 +169,29 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) { }, expectedMergeTags: []string{}, }, + + { + name: "expired mmrt", + calculation: &simver.Calculation{ + ForcePatch: false, + IsMerge: false, + MostRecentLiveTag: "v0.17.2", + MyMostRecentBuild: 1.000000, + MyMostRecentTag: "v0.17.3", + NextValidTag: "v0.18.0", + PR: 13.000000, + }, + expectedBaseTags: []string{ + // "v0.18.0-pr13+base", + }, + expectedHeadTags: []string{ + "v0.17.3-pr13+2", + }, + expectedRootTags: []string{ + // "v0.18.0-reserved", + }, + expectedMergeTags: []string{}, + }, // Add more test cases here... } diff --git a/execution.go b/execution.go index 0255ea3..404e79a 100644 --- a/execution.go +++ b/execution.go @@ -33,14 +33,18 @@ func Calculate(ctx context.Context, ex Execution) *Calculation { mmrt := MyMostRecentTag(ex) + maxlr := MaxLiveOrReservedTag(mrlt, mrrt) + + maxmr := MaxMyOrReservedTag(mrrt, mmrt) + return &Calculation{ IsMerge: ex.IsMerge(), MostRecentLiveTag: mrlt, - ForcePatch: ForcePatch(ctx, ex, mmrt), + ForcePatch: ForcePatch(ctx, ex, maxmr), MyMostRecentTag: mmrt, MyMostRecentBuild: MyMostRecentBuildNumber(ex), PR: ex.PR(), - NextValidTag: GetNextValidTag(ctx, ex.IsMinor(), mrlt, mrrt), + NextValidTag: GetNextValidTag(ctx, ex.IsMinor(), maxlr), } } @@ -51,6 +55,18 @@ type MMRT string // my most recent tag type MMRBN int // my most recent build number type MRT string // my reserved tag +type MAXLR string // max live or reserved tag + +type MAXMR string // max my reserved tag + +func MaxLiveOrReservedTag(mrlt MRLT, mrrt MRRT) MAXLR { + return MAXLR(Max(mrlt, mrrt)) +} + +func MaxMyOrReservedTag(mrrt MRRT, mmrt MMRT) MAXMR { + return MAXMR(Max(mrrt, mmrt)) +} + func BumpPatch[S ~string](arg S) S { maj := semver.MajorMinor(string(arg)) @@ -73,7 +89,7 @@ func BumpPatch[S ~string](arg S) S { } -func ForcePatch(ctx context.Context, ee Execution, mmrt MMRT) bool { +func ForcePatch(ctx context.Context, ee Execution, mmrt MAXMR) bool { // if our head branch has a reg := regexp.MustCompile(fmt.Sprintf(`^%s$`, mmrt)) @@ -173,27 +189,61 @@ func MyMostRecentBuildNumber(e Execution) MMRBN { return MMRBN(n) } -func GetNextValidTag(ctx context.Context, minor bool, mrlt MRLT, mrrt MRRT) NVT { - +// func MaxLiveOrReservedTag(mrlt MRLT, mrrt MRRT) MAXLR { +// var max string + +// if mrlt == "" || mrrt == "" { +// if mrlt != "" { +// max = string(mrlt) +// } else if mrrt != "" { +// max = string(mrrt) +// } else { +// max = baseTag +// } +// } else { +// // only compare if both exist +// if semver.Compare(string(mrrt), string(mrlt)) > 0 { +// max = string(mrrt) +// } else { +// max = string(mrlt) +// } +// } + +// return MAXLR(max) +// } + +func Max[A ~string, B ~string](a A, b B) string { var max string - if mrlt == "" || mrrt == "" { - if mrlt != "" { - max = string(mrlt) - } else if mrrt != "" { - max = string(mrrt) + if a == "" || b == "" { + if a != "" { + max = string(a) + } else if b != "" { + max = string(b) } else { max = baseTag } } else { // only compare if both exist - if semver.Compare(string(mrrt), string(mrlt)) > 0 { - max = string(mrrt) + if semver.Compare(string(b), string(a)) > 0 { + max = string(b) } else { - max = string(mrlt) + max = string(a) } } + return max +} + +func GetNextValidTag(ctx context.Context, minor bool, maxt MAXLR) NVT { + + var max string + if maxt == "" { + max = baseTag + } else { + max = string(maxt) + } + maj := semver.Major(max) + "." majmin := semver.MajorMinor(max) @@ -225,8 +275,6 @@ func GetNextValidTag(ctx context.Context, minor bool, mrlt MRLT, mrrt MRRT) NVT Str("majmin", majmin). Str("patch", patch). Str("min", min). - Str("mrlt", string(mrlt)). - Str("mrrt", string(mrrt)). Int("minornum", minornum). Int("patchnum", patchnum). Msg("calculated next valid tag") diff --git a/execution_test.go b/execution_test.go index b7f34c4..3fd68fd 100644 --- a/execution_test.go +++ b/execution_test.go @@ -122,53 +122,103 @@ func TestMrrt(t *testing.T) { } } +func TestMax(t *testing.T) { + testCases := []struct { + name string + a string + b string + expected string + }{ + { + name: "normal", + a: "v1.2.3", + b: "v1.2.4-reserved", + expected: "v1.2.4-reserved", + }, + + { + name: "no a", + a: "", + b: "v1.2.4-reserved", + expected: "v1.2.4-reserved", + }, + { + name: "no b", + a: "v1.2.3", + b: "", + expected: "v1.2.3", + }, + { + name: "no a or b", + a: "", + b: "", + expected: "v0.1.0", // base tag is v0.1.0 + }, + { + name: "invalid a", + a: "x", + b: "v1.2.4-reserved", + expected: "v1.2.4-reserved", + }, + { + name: "invalid b", + a: "v9.8.7-pr33+4444", + b: "x", + expected: "v9.8.7-pr33+4444", + }, + } + + // ctx := context.Background() + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + res := simver.Max(tc.a, tc.b) + assert.Equal(t, tc.expected, res) + }) + } +} + func TestNvt(t *testing.T) { testCases := []struct { name string - mrlt simver.MRLT - mrrt simver.MRRT + max simver.MAXLR minor bool expectedNvt simver.NVT }{ { name: "normal", - mrlt: "v1.2.3", - mrrt: "v1.2.4-reserved", + max: "v1.2.4-reserved", minor: false, expectedNvt: "v1.2.5", }, { name: "minor", - mrlt: "v1.2.3", - mrrt: "v1.2.4-reserved", + max: "v1.2.4-reserved", minor: true, expectedNvt: "v1.3.0", }, { name: "no mrlt", - mrlt: "", - mrrt: "v1.2.4-reserved", + max: "v1.2.4-reserved", minor: false, expectedNvt: "v1.2.5", }, { name: "no mrrt", - mrlt: "v1.2.3", - mrrt: "", + max: "v1.2.3", minor: false, expectedNvt: "v1.2.4", }, { - name: "no mrlt or mrrt", - mrlt: "", - mrrt: "", + name: "no mrlt or mrrt", + max: "", + minor: false, expectedNvt: "v0.1.1", // base tag is v0.1.0 }, { name: "invalid mrlt", - mrlt: "x", - mrrt: "v1.2.4-reserved", + max: "v1.2.4-reserved", minor: false, expectedNvt: "v1.2.5", }, @@ -178,7 +228,8 @@ func TestNvt(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - result := simver.GetNextValidTag(ctx, tc.minor, tc.mrlt, tc.mrrt) + + result := simver.GetNextValidTag(ctx, tc.minor, tc.max) assert.Equal(t, tc.expectedNvt, result) }) } @@ -394,6 +445,30 @@ func TestNewTags(t *testing.T) { simver.Tag{Name: "v1.2.5-pr0+base", Ref: base_ref}, }, }, + { + name: "reserved tag already exists", + baseBranchTags: simver.Tags{ + simver.Tag{Name: "v0.17.3-reserved"}, + simver.Tag{Name: "v0.17.3-pr85+base"}, + simver.Tag{Name: "v0.17.3-pr85+1"}, + }, + headBranchTags: simver.Tags{}, + headCommitTags: simver.Tags{}, + rootBranchTags: simver.Tags{ + simver.Tag{Name: "v0.17.3-reserved"}, + }, + pr: 0, + isMerge: false, + isMinor: 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) + // we expect to use the next valid tag, which is v1.2.5 + simver.Tag{Name: "v1.2.5-pr0+1", Ref: head_ref}, + simver.Tag{Name: "v1.2.5-reserved", Ref: root_ref}, + simver.Tag{Name: "v1.2.5-pr0+base", Ref: base_ref}, + }, + }, } ctx := context.Background() From 87a54394cf721592f907a1e697c89bed10f09968 Mon Sep 17 00:00:00 2001 From: walter Date: Thu, 16 Nov 2023 14:48:56 -0600 Subject: [PATCH 30/38] better logging --- cmd/simver_github_actions/main.go | 69 ++++--------------------------- 1 file changed, 7 insertions(+), 62 deletions(-) diff --git a/cmd/simver_github_actions/main.go b/cmd/simver_github_actions/main.go index 27e1d34..2f94f7f 100644 --- a/cmd/simver_github_actions/main.go +++ b/cmd/simver_github_actions/main.go @@ -25,19 +25,6 @@ type PullRequestResolver struct { } func (p *PullRequestResolver) CurrentPR(ctx context.Context) (*simver.PRDetails, error) { - // is the an open, closed, or synchronized PR event? - - // event := os.Getenv("GITHUB_EVENT_NAME") - - // if event == "pull_request" { - // // this is easy, we know that this is a pr event - // } else if event == "pull_request_target" { - // // this is easy, we know that this is a pr event - // } else if event == "push" { - // // this is easy, we know that this is a push event - // } else { - // return nil, errors.New("not a PR event and not a push event") - // } head_ref := os.Getenv("GITHUB_REF") @@ -168,12 +155,15 @@ func main() { os.Exit(1) } - isPush := os.Getenv("GITHUB_EVENT_NAME") == "push" + if !keepgoing { + zerolog.Ctx(ctx).Debug().Msg("execution is complete, exiting because keepgoing is false") + os.Exit(0) + } - isMain := prd.HeadBranch == "main" + isPush := os.Getenv("GITHUB_EVENT_NAME") == "push" - if !keepgoing || (isPush && !isMain) { - zerolog.Ctx(ctx).Debug().Msg("execution is complete, exiting") + if isPush && prd.HeadBranch != "main" { + zerolog.Ctx(ctx).Debug().Msg("execution is complete, exiting because this is not a direct push to main") os.Exit(0) } @@ -181,52 +171,7 @@ func main() { tags := tt.ApplyRefs(ee.ProvideRefs()) - // reservedTag, reserved := tags.GetReserved() - - // tries := 0 - - // for reserved { - // err := tagprov.CreateTag(ctx, reservedTag) - // if err != nil { - // tries++ - // if tries > 5 { - // zerolog.Ctx(ctx).Error().Err(err).Msgf("error creating tag: %v", err) - // fmt.Println(terrors.FormatErrorCaller(err)) - // os.Exit(1) - // } - - // time.Sleep(1 * time.Second) - // eez, prz, keepgoing, err := simver.LoadExecution(ctx, tagprov, prr) - // if err != nil { - // zerolog.Ctx(ctx).Error().Err(err).Msgf("error loading execution: %v", err) - // fmt.Println(terrors.FormatErrorCaller(err)) - // os.Exit(1) - // } - // if !keepgoing { - // zerolog.Ctx(ctx).Debug().Msg("execution is complete, exiting") - // os.Exit(0) - // } - // ee = eez - // prd = prz - // tags := simver.Calculate(ctx, ee).CalculateNewTagsRaw(ctx).ApplyRefs(ee.ProvideRefs()) - // reservedTag, reserved = tags.GetReserved() - // } else { - // reserved = false - // } - // } - for _, tag := range tags { - // if tag.Name == reservedTag.Name && tag.Ref == reservedTag.Ref { - // continue - // } - - // if prd.Merged { - // if havMergedTag { - // continue - // } - // havMergedTag = true - // tag.Name = strings.Split(tag.Name, "-")[0] - // } err := tagprov.CreateTag(ctx, tag) if err != nil { From 95ab20525b84f23c3f47eefcfb8ebf26147e929d Mon Sep 17 00:00:00 2001 From: walter Date: Thu, 16 Nov 2023 14:59:37 -0600 Subject: [PATCH 31/38] even better logging --- simver.go | 12 ++++++------ tags.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/simver.go b/simver.go index 76f1e4c..08fd68a 100644 --- a/simver.go +++ b/simver.go @@ -137,12 +137,12 @@ func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Exec } zerolog.Ctx(ctx).Debug(). - Any("baseCommitTags", baseCommitTags). - Any("baseBranchTags", baseBranchTags). - Any("rootCommitTags", rootCommitTags). - Any("rootBranchTags", rootBranchTags). - Any("headTags", headTags). - Any("headBranchTags", headBranchTags). + Array("baseCommitTags", baseCommitTags). + Array("baseBranchTags", baseBranchTags). + Array("rootCommitTags", rootCommitTags). + Array("rootBranchTags", rootBranchTags). + Array("headTags", headTags). + Array("headBranchTags", headBranchTags). Any("pr", pr). Msg("loaded tags") diff --git a/tags.go b/tags.go index d29a5c8..ff3caef 100644 --- a/tags.go +++ b/tags.go @@ -1,8 +1,10 @@ package simver import ( + "slices" "strings" + "github.com/rs/zerolog" "golang.org/x/mod/semver" ) @@ -11,8 +13,39 @@ type Tag struct { Ref string } +var _ zerolog.LogArrayMarshaler = (*Tags)(nil) + type Tags []Tag +func shortRef(ref string) string { + if len(ref) <= 11 { + return ref + } + + return ref[:4] + "..." + ref[len(ref)-4:] +} + +func (t Tags) Sort() Tags { + tags := make(Tags, len(t)) + copy(tags, t) + + slices.SortFunc(tags, func(a, b Tag) int { + return semver.Compare(a.Name, b.Name) + }) + + return tags +} + +// MarshalZerologArray implements zerolog.LogArrayMarshaler. +func (t Tags) MarshalZerologArray(a *zerolog.Array) { + + tr := t.Sort() + + for _, tag := range tr { + a.Str(shortRef(tag.Ref) + " => " + tag.Name) + } +} + func (t Tags) GetReserved() (Tag, bool) { for _, tag := range t { From 5daa437da45900c69da3656814a5d3b9c34beb98 Mon Sep 17 00:00:00 2001 From: walter Date: Thu, 16 Nov 2023 15:10:24 -0600 Subject: [PATCH 32/38] bug fix on the commit to main --- cmd/simver_github_actions/main.go | 31 ++++++++++++++----------------- pr.go | 19 +++++++++++++++++++ simver.go | 2 +- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/cmd/simver_github_actions/main.go b/cmd/simver_github_actions/main.go index 2f94f7f..d94fd23 100644 --- a/cmd/simver_github_actions/main.go +++ b/cmd/simver_github_actions/main.go @@ -68,22 +68,19 @@ func (p *PullRequestResolver) CurrentPR(ctx context.Context) (*simver.PRDetails, return pr, nil } + isPush := os.Getenv("GITHUB_EVENT_NAME") == "push" + + if !isPush { + return nil, errors.New("not a PR event and not a push event") + } + // get the parent commit parent, err := p.git.CommitFromRef(ctx, "HEAD^") if err != nil { return nil, Err.Trace(err, "error getting parent commit") } - return &simver.PRDetails{ - Number: 0, - HeadBranch: branch, - BaseBranch: branch, - Merged: true, - MergeCommit: sha, - HeadCommit: sha, - BaseCommit: parent, - PotentialMergeCommit: "", - }, nil + return simver.NewPushSimulatedPRDetails(parent, sha, branch), nil } @@ -148,7 +145,7 @@ func main() { os.Exit(1) } - ee, prd, keepgoing, err := simver.LoadExecution(ctx, tagprov, prr) + ee, _, keepgoing, err := simver.LoadExecution(ctx, tagprov, prr) if err != nil { zerolog.Ctx(ctx).Error().Err(err).Msgf("error loading execution") fmt.Println(terrors.FormatErrorCaller(err)) @@ -156,16 +153,16 @@ func main() { } if !keepgoing { - zerolog.Ctx(ctx).Debug().Msg("execution is complete, exiting because keepgoing is false") + 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" + // isPush := os.Getenv("GITHUB_EVENT_NAME") == "push" - if isPush && prd.HeadBranch != "main" { - zerolog.Ctx(ctx).Debug().Msg("execution is complete, exiting because this is not a direct push to main") - os.Exit(0) - } + // if isPush && prd.HeadBranch != "main" { + // zerolog.Ctx(ctx).Debug().Msg("execution is complete, exiting because this is not a direct push to main") + // os.Exit(0) + // } tt := simver.Calculate(ctx, ee).CalculateNewTagsRaw(ctx) diff --git a/pr.go b/pr.go index 3b6d286..c4cdf0f 100644 --- a/pr.go +++ b/pr.go @@ -17,6 +17,25 @@ type PRDetails struct { RootCommit string } +func NewPushSimulatedPRDetails(parentCommit, headCommit, branch string) *PRDetails { + return &PRDetails{ + Number: 0, + HeadBranch: branch, + BaseBranch: branch, + RootBranch: branch, + Merged: true, + MergeCommit: headCommit, + HeadCommit: headCommit, + RootCommit: parentCommit, + BaseCommit: parentCommit, + PotentialMergeCommit: "", + } +} + +func (dets *PRDetails) IsSimulatedPush() bool { + return dets.Number == 0 +} + type PRProvider interface { PRDetailsByPRNumber(ctx context.Context, prNumber int) (*PRDetails, bool, error) PRDetailsByCommit(ctx context.Context, commit string) (*PRDetails, bool, error) diff --git a/simver.go b/simver.go index 08fd68a..f8aaf21 100644 --- a/simver.go +++ b/simver.go @@ -87,7 +87,7 @@ func LoadExecution(ctx context.Context, tprov TagProvider, prr PRResolver) (Exec return nil, nil, false, err } - if pr.Number == 0 && pr.HeadBranch != "main" { + if pr.IsSimulatedPush() && pr.HeadBranch != "main" { return nil, nil, false, nil } From 63ca52632ea1364845368f878427dacc8c8ae60b Mon Sep 17 00:00:00 2001 From: walter Date: Thu, 16 Nov 2023 17:28:01 -0600 Subject: [PATCH 33/38] idk if this is it --- calculate.go | 14 ++++++++++---- execution.go | 23 ----------------------- execution_test.go | 38 +++++++++++++++++++------------------- 3 files changed, 29 insertions(+), 46 deletions(-) diff --git a/calculate.go b/calculate.go index c505d65..5998aab 100644 --- a/calculate.go +++ b/calculate.go @@ -83,15 +83,21 @@ func (me *Calculation) CalculateNewTagsRaw(ctx context.Context) *CalculationOutp // if mmrt is invalid, then we need to reserve a new mmrt (which is the same as nvt) if !validMmrt { mmrt = nvt - out.RootTags = append(out.RootTags, nvt+"-reserved") - out.BaseTags = append(out.BaseTags, nvt+fmt.Sprintf("-pr%d+base", me.PR)) + // pr will be 0 if this is not a and is a push to the root branch + if me.PR != 0 { + 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) } else { - next := mmrt + fmt.Sprintf("-pr%d+%d", me.PR, int(me.MyMostRecentBuild)+1) - out.HeadTags = append(out.HeadTags, next) + if me.PR == 0 { + out.HeadTags = append(out.HeadTags, mmrt) + } else { + out.HeadTags = append(out.HeadTags, mmrt+fmt.Sprintf("-pr%d+%d", me.PR, int(me.MyMostRecentBuild)+1)) + } } zerolog.Ctx(ctx).Debug(). diff --git a/execution.go b/execution.go index 404e79a..2bc0412 100644 --- a/execution.go +++ b/execution.go @@ -189,29 +189,6 @@ func MyMostRecentBuildNumber(e Execution) MMRBN { return MMRBN(n) } -// func MaxLiveOrReservedTag(mrlt MRLT, mrrt MRRT) MAXLR { -// var max string - -// if mrlt == "" || mrrt == "" { -// if mrlt != "" { -// max = string(mrlt) -// } else if mrrt != "" { -// max = string(mrrt) -// } else { -// max = baseTag -// } -// } else { -// // only compare if both exist -// if semver.Compare(string(mrrt), string(mrlt)) > 0 { -// max = string(mrrt) -// } else { -// max = string(mrlt) -// } -// } - -// return MAXLR(max) -// } - func Max[A ~string, B ~string](a A, b B) string { var max string diff --git a/execution_test.go b/execution_test.go index 3fd68fd..a5bebf3 100644 --- a/execution_test.go +++ b/execution_test.go @@ -264,9 +264,9 @@ func TestNewTags(t *testing.T) { isMerge: false, isMinor: false, expectedTags: simver.Tags{ - simver.Tag{Name: "v1.2.4-pr0+1", Ref: head_ref}, - simver.Tag{Name: "v1.2.4-reserved", Ref: root_ref}, - simver.Tag{Name: "v1.2.4-pr0+base", Ref: base_ref}, + simver.Tag{Name: "v1.2.4", Ref: head_ref}, + // simver.Tag{Name: "v1.2.4-reserved", Ref: root_ref}, + // simver.Tag{Name: "v1.2.4-pr0+base", Ref: base_ref}, }, }, { @@ -275,13 +275,13 @@ func TestNewTags(t *testing.T) { headBranchTags: simver.Tags{}, headCommitTags: simver.Tags{}, rootBranchTags: simver.Tags{}, - pr: 99, - isMerge: false, + pr: 0, + isMerge: true, isMinor: true, expectedTags: simver.Tags{ - simver.Tag{Name: "v1.3.0-pr99+1", Ref: head_ref}, - simver.Tag{Name: "v1.3.0-reserved", Ref: root_ref}, - simver.Tag{Name: "v1.3.0-pr99+base", Ref: base_ref}, + simver.Tag{Name: "v1.3.0", Ref: merge_ref}, + // simver.Tag{Name: "v1.3.0-reserved", Ref: root_ref}, + // simver.Tag{Name: "v1.3.0-pr99+base", Ref: base_ref}, }, }, { @@ -329,17 +329,17 @@ func TestNewTags(t *testing.T) { headBranchTags: simver.Tags{}, headCommitTags: simver.Tags{}, rootBranchTags: simver.Tags{}, - pr: 0, + pr: 1, isMerge: false, isMinor: 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 simver.Tag{Name: "v0.2.0-reserved", Ref: root_ref}, - simver.Tag{Name: "v0.2.0-pr0+base", Ref: base_ref}, + simver.Tag{Name: "v0.2.0-pr1+base", Ref: base_ref}, // and finally, we need to tag the commit with the PR number // since the base branch is main, we set it to v0.2.0-pr0 - simver.Tag{Name: "v0.2.0-pr0+1", Ref: head_ref}, + simver.Tag{Name: "v0.2.0-pr1+1", Ref: head_ref}, }, }, { @@ -433,16 +433,16 @@ func TestNewTags(t *testing.T) { headBranchTags: simver.Tags{}, headCommitTags: simver.Tags{}, rootBranchTags: simver.Tags{simver.Tag{Name: "v1.2.4-reserved"}}, - pr: 0, + pr: 3, isMerge: false, isMinor: 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) // we expect to use the next valid tag, which is v1.2.5 - simver.Tag{Name: "v1.2.5-pr0+1", Ref: head_ref}, + simver.Tag{Name: "v1.2.5-pr3+1", Ref: head_ref}, simver.Tag{Name: "v1.2.5-reserved", Ref: root_ref}, - simver.Tag{Name: "v1.2.5-pr0+base", Ref: base_ref}, + simver.Tag{Name: "v1.2.5-pr3+base", Ref: base_ref}, }, }, { @@ -457,16 +457,16 @@ func TestNewTags(t *testing.T) { rootBranchTags: simver.Tags{ simver.Tag{Name: "v0.17.3-reserved"}, }, - pr: 0, + pr: 99, isMerge: false, isMinor: 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) + // but since it exists, and pr99 does not know it owns it (via its own v1.2.4-pr99+base tag) // we expect to use the next valid tag, which is v1.2.5 - simver.Tag{Name: "v1.2.5-pr0+1", Ref: head_ref}, - simver.Tag{Name: "v1.2.5-reserved", Ref: root_ref}, - simver.Tag{Name: "v1.2.5-pr0+base", Ref: base_ref}, + simver.Tag{Name: "v0.17.4-pr99+1", Ref: head_ref}, + simver.Tag{Name: "v0.17.4-reserved", Ref: root_ref}, + simver.Tag{Name: "v0.17.4-pr99+base", Ref: base_ref}, }, }, } From 909ef3a955d2308cd8bf12f8066c0ea3caa33500 Mon Sep 17 00:00:00 2001 From: walter Date: Thu, 16 Nov 2023 17:38:29 -0600 Subject: [PATCH 34/38] bulk create tags --- cmd/simver_github_actions/main.go | 13 +++++-------- exec/tag.go | 28 ++++++++++++---------------- git.go | 7 ------- tags.go | 8 ++++++++ 4 files changed, 25 insertions(+), 31 deletions(-) diff --git a/cmd/simver_github_actions/main.go b/cmd/simver_github_actions/main.go index d94fd23..6c4d440 100644 --- a/cmd/simver_github_actions/main.go +++ b/cmd/simver_github_actions/main.go @@ -168,15 +168,12 @@ func main() { tags := tt.ApplyRefs(ee.ProvideRefs()) - for _, tag := range tags { - - err := tagprov.CreateTag(ctx, tag) - if err != nil { - zerolog.Ctx(ctx).Error().Err(err).Msgf("error creating tag: %v", err) - fmt.Println(terrors.FormatErrorCaller(err)) + err = tagprov.CreateTags(ctx, tags...) + if err != nil { + zerolog.Ctx(ctx).Error().Err(err).Msgf("error creating tag: %v", err) + fmt.Println(terrors.FormatErrorCaller(err)) - os.Exit(1) - } + os.Exit(1) } } diff --git a/exec/tag.go b/exec/tag.go index 3bde69f..154cfbc 100644 --- a/exec/tag.go +++ b/exec/tag.go @@ -151,33 +151,29 @@ func (p *gitProvider) FetchTags(ctx context.Context) (simver.Tags, error) { return tagInfos, nil } -func (p *gitProvider) CreateTag(ctx context.Context, tag simver.Tag) error { - - ctx = zerolog.Ctx(ctx).With().Str("name", tag.Name).Str("ref", tag.Ref).Logger().WithContext(ctx) +func (p *gitProvider) CreateTags(ctx context.Context, tag ...simver.Tag) error { if p.ReadOnly { zerolog.Ctx(ctx).Debug().Msg("read only mode, skipping tag creation") return nil } - zerolog.Ctx(ctx).Debug().Msg("creating tag") - - cmd := p.git(ctx, "tag", tag.Name, tag.Ref) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err := cmd.Run() - if err != nil { - return ErrExecGit.Trace(err, "name="+tag.Name, "ref="+tag.Ref) + for _, t := range tag { + cmd := p.git(ctx, "tag", t.Name, t.Ref) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Run() + if err != nil { + return ErrExecGit.Trace(err, "name="+t.Name, "ref="+t.Ref) + } } - zerolog.Ctx(ctx).Debug().Msg("pushing tag") - - cmd = p.git(ctx, "push", "origin", tag.Name) + cmd := p.git(ctx, "push", "origin", "--tags") cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - err = cmd.Run() + err := cmd.Run() if err != nil { - return ErrExecGit.Trace(err, "name="+tag.Name, "ref="+tag.Ref) + return ErrExecGit.Trace(err) } zerolog.Ctx(ctx).Debug().Msg("tag created") diff --git a/git.go b/git.go index a46d236..65b07b9 100644 --- a/git.go +++ b/git.go @@ -7,10 +7,3 @@ type GitProvider interface { CommitFromRef(ctx context.Context, ref string) (string, error) Branch(ctx context.Context) (string, error) } - -type TagProvider interface { - FetchTags(ctx context.Context) (Tags, error) - CreateTag(ctx context.Context, tag Tag) error - TagsFromCommit(ctx context.Context, commitHash string) (Tags, error) - TagsFromBranch(ctx context.Context, branch string) (Tags, error) -} diff --git a/tags.go b/tags.go index ff3caef..7685e32 100644 --- a/tags.go +++ b/tags.go @@ -1,6 +1,7 @@ package simver import ( + "context" "slices" "strings" @@ -8,6 +9,13 @@ import ( "golang.org/x/mod/semver" ) +type TagProvider interface { + FetchTags(ctx context.Context) (Tags, error) + CreateTags(ctx context.Context, tag ...Tag) error + TagsFromCommit(ctx context.Context, commitHash string) (Tags, error) + TagsFromBranch(ctx context.Context, branch string) (Tags, error) +} + type Tag struct { Name string Ref string From 0a024b3f3c9c12127610558bb8e86e06738886fb Mon Sep 17 00:00:00 2001 From: walter Date: Thu, 16 Nov 2023 17:43:06 -0600 Subject: [PATCH 35/38] fix the mmrt max bug --- execution.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/execution.go b/execution.go index 2bc0412..8d6961d 100644 --- a/execution.go +++ b/execution.go @@ -35,12 +35,12 @@ func Calculate(ctx context.Context, ex Execution) *Calculation { maxlr := MaxLiveOrReservedTag(mrlt, mrrt) - maxmr := MaxMyOrReservedTag(mrrt, mmrt) + // maxmr := MaxMyOrReservedTag(mrrt, mmrt) return &Calculation{ IsMerge: ex.IsMerge(), MostRecentLiveTag: mrlt, - ForcePatch: ForcePatch(ctx, ex, maxmr), + ForcePatch: ForcePatch(ctx, ex, mmrt), MyMostRecentTag: mmrt, MyMostRecentBuild: MyMostRecentBuildNumber(ex), PR: ex.PR(), @@ -89,7 +89,7 @@ func BumpPatch[S ~string](arg S) S { } -func ForcePatch(ctx context.Context, ee Execution, mmrt MAXMR) bool { +func ForcePatch(ctx context.Context, ee Execution, mmrt MMRT) bool { // if our head branch has a reg := regexp.MustCompile(fmt.Sprintf(`^%s$`, mmrt)) From 678b6f26092d8998021e99c4f387e2d09860cb2d Mon Sep 17 00:00:00 2001 From: walter Date: Fri, 17 Nov 2023 21:02:31 -0600 Subject: [PATCH 36/38] bre build download --- .github/workflows/simver.yaml | 45 +++++++++++++++++++++++++++++++++++ .vscode/simver.code-workspace | 23 +++++++++--------- README.md | 3 +++ 3 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/simver.yaml diff --git a/.github/workflows/simver.yaml b/.github/workflows/simver.yaml new file mode 100644 index 0000000..8bb9f94 --- /dev/null +++ b/.github/workflows/simver.yaml @@ -0,0 +1,45 @@ +{ + name: simver, + concurrency: { group: simver, cancel-in-progress: false }, + permissions: { id-token: write, contents: write, pull-requests: read }, + on: + { + workflow_dispatch: null, + workflow_call: null, + push: { branches: [main] }, + pull_request: { types: [opened, synchronize, reopened, closed] }, + }, + env: { GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" }, + defaults: { run: { shell: bash } }, + jobs: + { + simver: + { + runs-on: ubuntu-latest, + + steps: + [ + { + name: checkout code, + uses: actions/checkout@v4, + with: { fetch-depth: 0 }, + }, + { + name: setup golang, + uses: actions/setup-go@v4, + with: { go-version: 1.21.4 }, + }, + { + name: install simver, + run: go install github.com/walteh/simver/cmd/simver_github_actions@main, + }, + { + name: run simver, + id: simver, + env: { SIMVER_READ_ONLY: false }, + run: simver_github_actions, + }, + ], + }, + }, +} diff --git a/.vscode/simver.code-workspace b/.vscode/simver.code-workspace index bcac362..15d6484 100644 --- a/.vscode/simver.code-workspace +++ b/.vscode/simver.code-workspace @@ -10,6 +10,7 @@ VSCODE ////////////////////////////////////////////// */ "workbench.tree.indent": 16, + "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.formatOnSave": true, "search.useIgnoreFiles": true, "debug.console.wordWrap": false, @@ -25,17 +26,17 @@ /* ////////////////////////////////////////////// YAML ////////////////////////////////////////////// */ - "yaml.format.enable": true, - "[yaml]": { - "editor.defaultFormatter": "redhat.vscode-yaml", - "editor.bracketPairColorization.enabled": true, - "editor.formatOnSave": true, - }, - "yaml.style.flowSequence": "allow", - "yaml.style.flowMapping": "allow", - "yaml.format.proseWrap": "preserve", - "yaml.format.singleQuote": false, - "yaml.extension.recommendations": true, + // "yaml.format.enable": true, + // "[yaml]": { + // "editor.defaultFormatter": "esbenp.prettier-vscode", + // "editor.bracketPairColorization.enabled": true, + // "editor.formatOnSave": true, + // }, + // "yaml.style.flowSequence": "allow", + // "yaml.style.flowMapping": "allow", + // "yaml.format.proseWrap": "preserve", + // "yaml.format.singleQuote": false, + // "yaml.extension.recommendations": true, /* ////////////////////////////////////////////// DOCKER ////////////////////////////////////////////// */ diff --git a/README.md b/README.md index e211748..56368fc 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,9 @@ simple, pr based, semver git tagging logic # definitions + # how can we make sure that a version is reserved - and if it is not reserved we need to bump it + + ## when is mmrt valid? 1. it exists 2. it is higher than the mrlt From bd1bb74302db834fe642b18fabbd630f90603c34 Mon Sep 17 00:00:00 2001 From: walter Date: Sat, 18 Nov 2023 06:48:04 -0600 Subject: [PATCH 37/38] adjust workflow --- .github/workflows/simver.yaml | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/.github/workflows/simver.yaml b/.github/workflows/simver.yaml index 8bb9f94..e59d5dd 100644 --- a/.github/workflows/simver.yaml +++ b/.github/workflows/simver.yaml @@ -9,7 +9,6 @@ push: { branches: [main] }, pull_request: { types: [opened, synchronize, reopened, closed] }, }, - env: { GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" }, defaults: { run: { shell: bash } }, jobs: { @@ -21,22 +20,26 @@ [ { name: checkout code, - uses: actions/checkout@v4, + uses: "actions/checkout@v4", with: { fetch-depth: 0 }, }, { name: setup golang, - uses: actions/setup-go@v4, + uses: "actions/setup-go@v4", with: { go-version: 1.21.4 }, }, { name: install simver, - run: go install github.com/walteh/simver/cmd/simver_github_actions@main, + run: "go install github.com/walteh/simver/cmd/simver_github_actions@main", }, { name: run simver, id: simver, - env: { SIMVER_READ_ONLY: false }, + env: + { + SIMVER_READ_ONLY: false, + GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}", + }, run: simver_github_actions, }, ], From e5cfc34bd37eb73cb78bef0870e1956fb1111584 Mon Sep 17 00:00:00 2001 From: walter Date: Sat, 18 Nov 2023 06:51:31 -0600 Subject: [PATCH 38/38] set v0.2.0 as default --- .github/workflows/simver.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/simver.yaml b/.github/workflows/simver.yaml index e59d5dd..13d8d87 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@main", + run: "go install github.com/walteh/simver/cmd/simver_github_actions@v0.2.0", }, { name: run simver,