Skip to content

Commit

Permalink
wait on simver
Browse files Browse the repository at this point in the history
  • Loading branch information
walteh committed Nov 24, 2023
1 parent a3a0b94 commit 385539f
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 132 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/wait-on-simver.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
name: wait-on-simver,
concurrency:
{ group: "wait-on-simver-${{github.ref}}", cancel-in-progress: false },
permissions: { id-token: write, contents: read },
on: { workflow_dispatch: null, workflow_call: {}, push: { branches: "*" } },
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/[email protected]",
},
{
name: run simver,
id: simver,
env:
{
SIMVER_WAIT: "2m",
SIMVER_INTERVAL: "10s",
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}",
},
run: wait_on_simver_github_actions,
},
],
},
},
}
20 changes: 7 additions & 13 deletions calculate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ type Calculation struct {
MyMostRecentTag MMRT
MostRecentLiveTag MRLT
MyMostRecentBuild MMRBN
LastSymbolicTag LST
PR int
NextValidTag NVT
IsMerge bool
IsMerged bool
ForcePatch bool
Skip bool
IsDirty bool
}

type CalculationOutput struct {
Expand Down Expand Up @@ -65,9 +63,7 @@ func (me *Calculation) CalculateNewTagsRaw(ctx context.Context) *CalculationOutp
}

if me.Skip {
zerolog.Ctx(ctx).Debug().
Any("calculation", me).
Msg("Skipping calculation")
zerolog.Ctx(ctx).Debug().Any("calculation", me).Msg("Skipping calculation")
return out
}

Expand Down Expand Up @@ -95,9 +91,7 @@ func (me *Calculation) CalculateNewTagsRaw(ctx context.Context) *CalculationOutp
if mmrt != "" && semver.Compare(mmrt, mrlt) == 0 && me.MyMostRecentBuild != 0 {
validMmrt = false
nvt = BumpPatch(mmrt)
}

if !me.IsMerge {
} else if !me.IsMerged {
if me.MyMostRecentBuild == 0 {
validMmrt = false
} else if me.ForcePatch {
Expand All @@ -109,14 +103,14 @@ 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
// pr will be 0 if this is not a and is a push to the root branch
if me.PR != 0 && !me.IsMerge {
// pr will be 0 if this is not merged and is a push to the root branch
if me.PR != 0 && !me.IsMerged {
out.RootTags = append(out.RootTags, mmrt+"-reserved")
out.BaseTags = append(out.BaseTags, mmrt+fmt.Sprintf("-pr%d+base", me.PR))
}
}

if me.IsMerge {
if me.IsMerged {
// if !matching {
out.MergeTags = append(out.MergeTags, mmrt)
// }
Expand All @@ -135,7 +129,7 @@ func (me *Calculation) CalculateNewTagsRaw(ctx context.Context) *CalculationOutp
Str("mrlt", mrlt).
Str("nvt", nvt).
Str("pr", fmt.Sprintf("%d", me.PR)).
Bool("isMerge", me.IsMerge).
Bool("isMerge", me.IsMerged).
Bool("forcePatch", me.ForcePatch).
Msg("CalculateNewTagsRaw")

Expand Down
30 changes: 15 additions & 15 deletions calculate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
MyMostRecentBuild: 33,
PR: 85,
NextValidTag: "v99.99.99",
IsMerge: false,
IsMerged: false,
ForcePatch: false,
},
output: &simver.CalculationOutput{
Expand All @@ -46,7 +46,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
MyMostRecentBuild: 1,
PR: 1,
NextValidTag: "v3.3.3",
IsMerge: false,
IsMerged: false,
ForcePatch: false,
},

Expand All @@ -71,7 +71,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
MyMostRecentBuild: 33,
PR: 1,
NextValidTag: "v1.2.6",
IsMerge: false,
IsMerged: false,
ForcePatch: false,
},

Expand All @@ -91,7 +91,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
MyMostRecentBuild: 33,
PR: 1,
NextValidTag: "v1.2.6",
IsMerge: false,
IsMerged: false,
ForcePatch: false,
},

Expand All @@ -110,7 +110,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
MyMostRecentBuild: 33,
PR: 1,
NextValidTag: "v1.2.6",
IsMerge: true,
IsMerged: true,
ForcePatch: false,
},
output: &simver.CalculationOutput{
Expand All @@ -128,7 +128,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
MyMostRecentBuild: 33,
PR: 1,
NextValidTag: "v1.2.6",
IsMerge: false,
IsMerged: false,
ForcePatch: true,
},
output: &simver.CalculationOutput{
Expand All @@ -146,7 +146,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
MyMostRecentBuild: 33,
PR: 1,
NextValidTag: "v1.2.6",
IsMerge: true,
IsMerged: true,
ForcePatch: true,
},
output: &simver.CalculationOutput{
Expand All @@ -164,7 +164,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
MyMostRecentBuild: 33,
PR: 85,
NextValidTag: "v99.99.99",
IsMerge: false,
IsMerged: false,
ForcePatch: true,
},
output: &simver.CalculationOutput{
Expand All @@ -179,7 +179,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
name: "expired mmrt",
calculation: &simver.Calculation{
ForcePatch: false,
IsMerge: false,
IsMerged: false,
MostRecentLiveTag: "v0.17.2",
MyMostRecentBuild: 1.000000,
MyMostRecentTag: "v0.17.3",
Expand All @@ -197,7 +197,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
name: "when merging a branch that already is tagged correctly, bump by patch",
calculation: &simver.Calculation{
ForcePatch: false,
IsMerge: true,
IsMerged: true,
MostRecentLiveTag: "v0.3.0",
MyMostRecentBuild: 1.000000,
MyMostRecentTag: "v0.3.0",
Expand All @@ -215,7 +215,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
name: "when merging a branch that already is tagged correctly, bump by patch (ignoring force patch)",
calculation: &simver.Calculation{
ForcePatch: true,
IsMerge: true,
IsMerged: true,
MostRecentLiveTag: "v0.2.0",
MyMostRecentBuild: 1.000000,
MyMostRecentTag: "v0.2.0",
Expand All @@ -233,7 +233,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
name: "when merging a branch that already is tagged correctly on first build, bump to next",
calculation: &simver.Calculation{
ForcePatch: true,
IsMerge: true,
IsMerged: true,
MostRecentLiveTag: "v0.2.0",
MyMostRecentBuild: 0,
MyMostRecentTag: "v0.2.0",
Expand All @@ -253,7 +253,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
calculation: &simver.Calculation{

ForcePatch: true,
IsMerge: false,
IsMerged: false,
MostRecentLiveTag: "v0.4.1",
MyMostRecentBuild: 0.000000,
MyMostRecentTag: "v0.4.1",
Expand All @@ -272,7 +272,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
name: "skip if flagged",
calculation: &simver.Calculation{
ForcePatch: false,
IsMerge: true,
IsMerged: true,
MostRecentLiveTag: "v0.4.2",
MyMostRecentBuild: 3.000000,
MyMostRecentTag: "v0.4.2",
Expand All @@ -291,7 +291,7 @@ func TestNewCalculationAndCalculateNewTags(t *testing.T) {
name: "if is merge, build is 0, skip is false - tag mmrt",
calculation: &simver.Calculation{
ForcePatch: false,
IsMerge: true,
IsMerged: true,
MostRecentLiveTag: "v0.18.0",
MyMostRecentBuild: 0.000000,
MyMostRecentTag: "v0.18.1",
Expand Down
123 changes: 123 additions & 0 deletions cmd/wait_on_simver_github_actions/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package main

import (
"context"
"os"
"time"

"github.com/rs/zerolog"
"github.com/walteh/simver"
"github.com/walteh/simver/gitexec"
szl "github.com/walteh/snake/zerolog"
)

func check(ctx context.Context, gp simver.GitProvider, tr simver.TagReader, tw simver.TagWriter, head string) (*simver.Tag, bool, error) {
if head == "" {
return nil, false, nil
}

_, err := tw.FetchTags(ctx)
if err != nil {
return nil, false, err
}

tags, err := tr.TagsFromCommit(ctx, head)
if err != nil {
return nil, false, err
}

if len(tags) == 0 {
return nil, false, nil

}

return &tags[0], true, nil
}

func main() {

eventName := os.Getenv("GITHUB_EVENT_NAME")
if eventName != "push" {
zerolog.Ctx(context.Background()).Error().Str("event_name", eventName).Msg("not a push event - this action is only useful for push events")
os.Exit(1)
}

waitInput := os.Getenv("SIMVER_WAIT")
if waitInput == "" {
waitInput = "2m"
}

intervalInput := os.Getenv("SIMVER_INTERVAL")
if intervalInput == "" {
intervalInput = "5s"
}

wait, err := time.ParseDuration(waitInput)
if err != nil {
panic(err)
}

start := time.Now()

end := start.Add(wait)

interval, err := time.ParseDuration(intervalInput)
if err != nil {
panic(err)
}
// get commit to wait on
ctx := context.Background()

ctx = szl.NewVerboseConsoleLogger().WithContext(ctx)

zerolog.SetGlobalLevel(zerolog.DebugLevel)

ctx, can := context.WithTimeout(ctx, wait)

defer can()

git, tr, tw, _, _, err := gitexec.BuildGitHubActionsProviders()
if err != nil {
zerolog.Ctx(ctx).Error().Err(err).Msg("error creating provider")
os.Exit(1)
}

head, err := git.GetHeadRef(ctx)
if err != nil {
zerolog.Ctx(ctx).Error().Err(err).Msg("error getting head ref")
os.Exit(1)
}

ctx = zerolog.Ctx(ctx).With().Str("head", head).Logger().WithContext(ctx)

zerolog.Ctx(ctx).Info().Msg("waiting for tag on head commit")

for {

select {
case <-ctx.Done():
{
zerolog.Ctx(ctx).Error().Err(err).Msg("timeout waiting for tag")
os.Exit(1)
}
default:
{
zerolog.Ctx(ctx).Info().Msg("checking for commit for commit")
tg, ok, err := check(ctx, git, tr, tw, head)
if err != nil {
zerolog.Ctx(ctx).Error().Err(err).Msg("error checking for tag")
panic(err)
}

if ok {
zerolog.Ctx(ctx).Info().Str("name", tg.Name).Msg("tag found")
break
}

zerolog.Ctx(ctx).Info().Dur("remaining", time.Until(end)).Dur("interval", interval).Msg("tag not found, waiting")

time.Sleep(interval)
}
}
}
}
Loading

0 comments on commit 385539f

Please sign in to comment.