diff --git a/internal/runners/install/install.go b/internal/runners/install/install.go index f0b4ce3b86..e11ca22eed 100644 --- a/internal/runners/install/install.go +++ b/internal/runners/install/install.go @@ -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) @@ -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()) @@ -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") } @@ -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, diff --git a/internal/runners/platforms/add.go b/internal/runners/platforms/add.go index b4d8d5c253..5b4d7b4300 100644 --- a/internal/runners/platforms/add.go +++ b/internal/runners/platforms/add.go @@ -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 diff --git a/internal/runners/upgrade/upgrade.go b/internal/runners/upgrade/upgrade.go index 953b20098c..ec1320542d 100644 --- a/internal/runners/upgrade/upgrade.go +++ b/internal/runners/upgrade/upgrade.go @@ -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. diff --git a/pkg/buildscript/buildscript.go b/pkg/buildscript/buildscript.go index 30e9b3b264..79ad6b7474 100644 --- a/pkg/buildscript/buildscript.go +++ b/pkg/buildscript/buildscript.go @@ -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() { @@ -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 } diff --git a/pkg/buildscript/buildscript_test.go b/pkg/buildscript/buildscript_test.go index 7d2ccbe0fe..4bc18a2d06 100644 --- a/pkg/buildscript/buildscript_test.go +++ b/pkg/buildscript/buildscript_test.go @@ -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)) @@ -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() diff --git a/pkg/buildscript/merge.go b/pkg/buildscript/merge.go index 889cafffb1..32db4da729 100644 --- a/pkg/buildscript/merge.go +++ b/pkg/buildscript/merge.go @@ -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 diff --git a/pkg/buildscript/unmarshal.go b/pkg/buildscript/unmarshal.go index 4719b63460..9602aef76c 100644 --- a/pkg/buildscript/unmarshal.go +++ b/pkg/buildscript/unmarshal.go @@ -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 } diff --git a/pkg/buildscript/unmarshal_buildexpression.go b/pkg/buildscript/unmarshal_buildexpression.go index 959d6303b8..fe7eee2eeb 100644 --- a/pkg/buildscript/unmarshal_buildexpression.go +++ b/pkg/buildscript/unmarshal_buildexpression.go @@ -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") } diff --git a/pkg/platform/model/buildplanner/build.go b/pkg/platform/model/buildplanner/build.go index 0b11fc1712..feef9b1a28 100644 --- a/pkg/platform/model/buildplanner/build.go +++ b/pkg/platform/model/buildplanner/build.go @@ -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 } diff --git a/pkg/platform/model/buildplanner/buildscript.go b/pkg/platform/model/buildplanner/buildscript.go index 28bd980ffc..c6fcc6a2ee 100644 --- a/pkg/platform/model/buildplanner/buildscript.go +++ b/pkg/platform/model/buildplanner/buildscript.go @@ -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") } diff --git a/pkg/platform/model/buildplanner/commit.go b/pkg/platform/model/buildplanner/commit.go index 387296b1ff..3086d32930 100644 --- a/pkg/platform/model/buildplanner/commit.go +++ b/pkg/platform/model/buildplanner/commit.go @@ -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") } diff --git a/scripts/to-buildscript/main.go b/scripts/to-buildscript/main.go index 60c7cde520..84a2e4599f 100644 --- a/scripts/to-buildscript/main.go +++ b/scripts/to-buildscript/main.go @@ -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 {