Skip to content

Commit

Permalink
Use override when setting atTime
Browse files Browse the repository at this point in the history
  • Loading branch information
MDrakos committed Oct 25, 2024
1 parent a67002f commit 33bf8fb
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 29 deletions.
8 changes: 5 additions & 3 deletions internal/runners/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (i *Install) Run(params Params) (rerr error) {
var oldCommit *bpModel.Commit
var reqs requirements
var ts time.Time
var override bool
{
pg = output.StartSpinner(out, locale.T("progress_search"), constants.TerminalAnimationInterval)

Expand All @@ -133,6 +134,7 @@ func (i *Install) Run(params Params) (rerr error) {
if err != nil {
return errs.Wrap(err, "Unable to get timestamp from params")
}
override = params.Timestamp.String() != ""

// Get languages used in current project
languages, err := model.FetchLanguagesForBuildScript(oldCommit.BuildScript())
Expand All @@ -153,7 +155,7 @@ func (i *Install) Run(params Params) (rerr error) {

// Prepare updated buildscript
script := oldCommit.BuildScript()
if err := prepareBuildScript(script, reqs, ts); err != nil {
if err := prepareBuildScript(script, reqs, ts, override); err != nil {
return errs.Wrap(err, "Could not prepare build script")
}

Expand Down Expand Up @@ -340,8 +342,8 @@ func (i *Install) renderUserFacing(reqs requirements) {
i.prime.Output().Notice("")
}

func prepareBuildScript(script *buildscript.BuildScript, requirements requirements, ts time.Time) error {
script.SetAtTime(ts)
func prepareBuildScript(script *buildscript.BuildScript, requirements requirements, ts time.Time, override bool) error {
script.SetAtTime(ts, override)
for _, req := range requirements {
requirement := types.Requirement{
Namespace: req.Resolved.Namespace,
Expand Down
2 changes: 1 addition & 1 deletion internal/runners/platforms/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (a *Add) Run(params AddRunParams) (rerr error) {

// Prepare updated buildscript
script := oldCommit.BuildScript()
script.SetAtTime(ts)
script.SetAtTime(ts, false)
script.AddPlatform(*platform.PlatformID)

// Update local checkout and source runtime changes
Expand Down
2 changes: 1 addition & 1 deletion internal/runners/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (u *Upgrade) Run(params *Params) (rerr error) {
if err != nil {
return errs.Wrap(err, "Failed to fetch latest timestamp")
}
bumpedBS.SetAtTime(ts)
bumpedBS.SetAtTime(ts, params.Timestamp.String() != "")

// Since our platform is commit based we need to create a commit for the "after" buildplan, even though we may not
// end up using it it the user doesn't confirm the upgrade.
Expand Down
17 changes: 5 additions & 12 deletions pkg/buildscript/buildscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ type BuildScript struct {

project string
atTime *time.Time

// solveAtTime is the original at_time found in the solve node.
// This is used to support the legacy use case where the at_time
// is an actual time stamp and not a reference to the $at_time variable.
solveAtTime *time.Time
}

func init() {
Expand Down Expand Up @@ -58,15 +53,13 @@ func (b *BuildScript) SetProject(url string) {
}

func (b *BuildScript) AtTime() *time.Time {
// If the atTime is set, prefer that over the
// legacy solveAtTime.
if b.atTime != nil {
return b.atTime
}
return b.solveAtTime
return b.atTime
}

func (b *BuildScript) SetAtTime(t time.Time) {
func (b *BuildScript) SetAtTime(t time.Time, override bool) {
if b.atTime != nil && !override {
return
}
b.atTime = &t
}

Expand Down
22 changes: 17 additions & 5 deletions pkg/buildscript/buildscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,30 @@ func TestRoundTripFromBuildExpressionWithLegacyAtTime(t *testing.T) {
assert.Contains(t, string(data), "$at_time")
assert.NotContains(t, string(data), initialTimeStamp)

// Update the time in the build script
// Update the time in the build script but don't override the existing time
updatedTime, err := time.Parse(strfmt.RFC3339Millis, updatedTimeStamp)
require.NoError(t, err)
script.SetAtTime(updatedTime)
script.SetAtTime(updatedTime, false)

// The updated time should be reflected in the build script
require.Equal(t, updatedTime, *script.AtTime())
require.Equal(t, initialTimeStamp, script.AtTime().Format(strfmt.RFC3339Millis))

data, err = script.Marshal()
require.NoError(t, err)

// The marshalled build script should now contain the updated time
// The marshalled build script should NOT contain the updated time
// in the Time block at the top of the script.
assert.Contains(t, string(data), initialTimeStamp)
assert.NotContains(t, string(data), fmt.Sprintf("Time: %s", updatedTime))

// Now override the time in the build script
script.SetAtTime(updatedTime, true)
require.Equal(t, updatedTimeStamp, script.AtTime().Format(strfmt.RFC3339Millis))

data, err = script.Marshal()
require.NoError(t, err)

// The marshalled build script should NOW contain the updated time
// in the Time block at the top of the script.
assert.Contains(t, string(data), updatedTimeStamp)
assert.NotContains(t, string(data), fmt.Sprintf("Time: %s", initialTimeStamp))
Expand All @@ -158,7 +170,7 @@ func TestExpressionToScript(t *testing.T) {

script := New()
script.SetProject(testProject)
script.SetAtTime(ts)
script.SetAtTime(ts, false)
require.NoError(t, script.UnmarshalBuildExpression(basicBuildExpression))

data, err := script.Marshal()
Expand Down
2 changes: 1 addition & 1 deletion pkg/buildscript/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (b *BuildScript) Merge(other *BuildScript, strategies *mono_models.MergeStr
// When merging build scripts we want to use the most recent timestamp
atTime := other.AtTime()
if atTime != nil && b.AtTime() != nil && atTime.After(*b.AtTime()) {
b.SetAtTime(*atTime)
b.SetAtTime(*atTime, true)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/buildscript/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ func Unmarshal(data []byte) (*BuildScript, error) {
atTime = ptr.To(time.Time(atTimeStr))
}

return &BuildScript{raw, project, atTime, nil}, nil
return &BuildScript{raw, project, atTime}, nil
}
2 changes: 1 addition & 1 deletion pkg/buildscript/unmarshal_buildexpression.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (b *BuildScript) UnmarshalBuildExpression(data []byte) error {
atTimeNode.Str = nil
atTimeNode.Ident = ptr.To("TIME")
// Preserve the original at_time found in the solve node.
b.solveAtTime = ptr.To(time.Time(atTime))
b.atTime = ptr.To(time.Time(atTime))
} else if atTimeNode.Ident != nil && *atTimeNode.Ident == "at_time" {
atTimeNode.Ident = ptr.To("TIME")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/platform/model/buildplanner/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (b *BuildPlanner) FetchCommit(commitID strfmt.UUID, owner, project string,
if err := script.UnmarshalBuildExpression(commit.Expression); err != nil {
return nil, errs.Wrap(err, "failed to parse build expression")
}
script.SetAtTime(time.Time(commit.AtTime))
script.SetAtTime(time.Time(commit.AtTime), false)

return &Commit{commit, bp, script}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/platform/model/buildplanner/buildscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (b *BuildPlanner) GetBuildScript(commitID string) (*buildscript.BuildScript
}

script := buildscript.New()
script.SetAtTime(time.Time(resp.Commit.AtTime))
script.SetAtTime(time.Time(resp.Commit.AtTime), false)
if err := script.UnmarshalBuildExpression(resp.Commit.Expression); err != nil {
return nil, errs.Wrap(err, "failed to parse build expression")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/platform/model/buildplanner/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (b *BuildPlanner) StageCommit(params StageCommitParams) (*Commit, error) {
}

stagedScript := buildscript.New()
stagedScript.SetAtTime(time.Time(resp.Commit.AtTime))
stagedScript.SetAtTime(time.Time(resp.Commit.AtTime), false)
if err := stagedScript.UnmarshalBuildExpression(resp.Commit.Expression); err != nil {
return nil, errs.Wrap(err, "failed to parse build expression")
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/to-buildscript/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func main() {
bs := buildscript.New()
bs.SetProject(project)
if atTime != nil {
bs.SetAtTime(*atTime)
bs.SetAtTime(*atTime, true)
}
err := bs.UnmarshalBuildExpression([]byte(input))
if err != nil {
Expand Down

0 comments on commit 33bf8fb

Please sign in to comment.