Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add separate checkout info block to buildscripts. #3483

Open
wants to merge 18 commits into
base: version/0-47-0-RC1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
2081db6
Participle should unquote strings.
mitchell-as Sep 6, 2024
5033503
Initial support for commit info in buildscript.
mitchell-as Sep 6, 2024
72cb762
Fetching buildscripts should also populate the Project commit info.
mitchell-as Sep 9, 2024
ec39083
localcommit package should update buildscript's Project commit info.
mitchell-as Sep 9, 2024
5b403b6
Rename "commit info" to "checkout info" in recent changes.
mitchell-as Sep 9, 2024
41ee7a1
Rename localcommit package to checkoutinfo.
mitchell-as Sep 9, 2024
e504147
Fixed failing unit tests.
mitchell-as Sep 9, 2024
a2e043d
Add warning about using an outdated buildscript.
mitchell-as Sep 9, 2024
e036299
Sort artifact changesets by name.
mitchell-as Sep 10, 2024
1bbf6c2
Revert "Sort artifact changesets by name."
mitchell-as Sep 10, 2024
008161f
Pass project details to the buildscript runbit, not project objects.
mitchell-as Sep 10, 2024
4a91dda
Include branch in buildscript commit info.
mitchell-as Sep 11, 2024
ccd9554
Use YAML parser when unmarshaling buildscript checkout info.
mitchell-as Sep 11, 2024
b1bb954
Update activestate.yaml with any buildscript checkout info changes.
mitchell-as Sep 11, 2024
9bcce03
Make build scripts first-class citizens.
mitchell-as Sep 12, 2024
a54541d
Fix some failing tests.
mitchell-as Sep 13, 2024
6b174f8
Make checkoutinfo a primer property and use it to set commit IDs, and…
mitchell-as Sep 14, 2024
4a150c0
Moved `buildscript_runbit.ScriptFromProject()` into `checkoutinfo.Bui…
mitchell-as Sep 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/captain/rationalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/runbits/rationalize"
"github.com/ActiveState/cli/pkg/localcommit"
"github.com/ActiveState/cli/pkg/checkoutinfo"
)

func rationalizeError(err *error) {
var errInvalidCommitID *localcommit.ErrInvalidCommitID
var errInvalidCommitID *checkoutinfo.ErrInvalidCommitID

switch {
case err == nil:
Expand Down
2 changes: 1 addition & 1 deletion internal/constraints/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func NewPrimeConditional(auth *authentication.Auth, pj projectable, subshellName
pjName = pj.Name()
pjNamespace = pj.NamespaceString()
pjURL = pj.URL()
pjCommit = pj.LegacyCommitID() // Not using localcommit due to import cycle. See anti-pattern comment in localcommit pkg.
pjCommit = pj.LegacyCommitID() // Not using checkoutinfo due to import cycle. See anti-pattern comment in checkoutinfo pkg.
pjBranch = pj.BranchName()
pjDir = pj.Dir()
}
Expand Down
4 changes: 1 addition & 3 deletions internal/migrator/migrator.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package migrator

import (
"path/filepath"

"github.com/ActiveState/cli/internal/config"
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
Expand All @@ -28,7 +26,7 @@ func NewMigrator(auth *authentication.Auth, cfg *config.Instance) projectfile.Mi
case 0:
if cfg.GetBool(constants.OptinBuildscriptsConfig) {
logging.Debug("Creating buildscript")
if err := buildscript_runbit.Initialize(filepath.Dir(project.Path()), auth); err != nil {
if err := buildscript_runbit.Initialize(project, auth); err != nil {
return v, errs.Wrap(err, "Failed to initialize buildscript")
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/runbits/buildplanner/buildplanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/ActiveState/cli/internal/rtutils/ptr"
"github.com/ActiveState/cli/internal/runbits/rationalize"
"github.com/ActiveState/cli/pkg/buildplan"
"github.com/ActiveState/cli/pkg/localcommit"
"github.com/ActiveState/cli/pkg/checkoutinfo"
"github.com/ActiveState/cli/pkg/platform/api/buildplanner/request"
"github.com/ActiveState/cli/pkg/platform/authentication"
"github.com/ActiveState/cli/pkg/platform/model"
Expand Down Expand Up @@ -76,7 +76,7 @@ func GetCommit(
switch {
// Return the buildplan from this runtime.
case !namespaceProvided && !commitIdProvided:
localCommitID, err := localcommit.Get(pj.Path())
localCommitID, err := checkoutinfo.GetCommitID(pj.Path())
if err != nil {
return nil, errs.Wrap(err, "Could not get local commit")
}
Expand Down Expand Up @@ -143,7 +143,7 @@ func GetCommit(
owner = pj.Owner()
name = pj.Name()
nsString = pj.NamespaceString()
commitID, err := localcommit.Get(pj.Path())
commitID, err := checkoutinfo.GetCommitID(pj.Path())
if err != nil {
return nil, errs.Wrap(err, "Could not get local commit")
}
Expand Down
29 changes: 22 additions & 7 deletions internal/runbits/buildscript/buildscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@ import (
"github.com/stretchr/testify/require"
)

const testProject = "https://platform.activestate.com/org/project?branch=main&commitID=00000000-0000-0000-0000-000000000000"
const testTime = "2000-01-01T00:00:00.000Z"

func checkoutInfo(project, time string) string {
return "```\n" +
"Project: " + project + "\n" +
"Time: " + time + "\n" +
"```\n"
}

func TestDiff(t *testing.T) {
script, err := buildscript.Unmarshal([]byte(
`at_time = "2000-01-01T00:00:00.000Z"
checkoutInfo(testProject, testTime) + `
runtime = solve(
at_time = at_time,
platforms = [
Expand All @@ -38,7 +48,7 @@ main = runtime`))
// Generate the difference between the modified script and the original expression.
result, err := generateDiff(modifiedScript, script)
require.NoError(t, err)
assert.Equal(t, `at_time = "2000-01-01T00:00:00.000Z"
assert.Equal(t, checkoutInfo(testProject, testTime)+`
runtime = solve(
at_time = at_time,
platforms = [
Expand Down Expand Up @@ -71,11 +81,16 @@ func TestRealWorld(t *testing.T) {
require.NoError(t, err)
result, err := generateDiff(script1, script2)
require.NoError(t, err)
assert.Equal(t, `<<<<<<< local
at_time = "2023-10-16T22:20:29.000Z"
=======
at_time = "2023-08-01T16:20:11.985Z"
>>>>>>> remote
assert.Equal(t,
"```\n"+
"<<<<<<< local\n"+
"Project: https://platform.activestate.com/ActiveState-CLI/Merge?branch=main&commitID=d908a758-6a81-40d4-b0eb-87069cd7f07d\n"+
"Time: 2024-05-10T00:00:13.138Z\n"+
"=======\n"+
"Project: https://platform.activestate.com/ActiveState-CLI/Merge?branch=main&commitID=f3263ee4-ac4c-41ee-b778-2585333f49f7\n"+
"Time: 2023-08-01T16:20:11.985Z\n"+
">>>>>>> remote\n"+
"```\n"+`
runtime = state_tool_artifacts_v1(
build_flags = [
],
Expand Down
22 changes: 14 additions & 8 deletions internal/runbits/buildscript/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import (
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/pkg/buildscript"
"github.com/ActiveState/cli/pkg/localcommit"
"github.com/ActiveState/cli/pkg/checkoutinfo"
"github.com/ActiveState/cli/pkg/platform/authentication"
"github.com/ActiveState/cli/pkg/platform/model/buildplanner"
)

// projecter is a union between project.Project and setup.Targeter
type projecter interface {
ProjectDir() string
Dir() string
Owner() string
Name() string
}

var ErrBuildscriptNotExist = errors.New("Build script does not exist")

func ScriptFromProject(proj projecter) (*buildscript.BuildScript, error) {
path := filepath.Join(proj.ProjectDir(), constants.BuildScriptFileName)
path := filepath.Join(proj.Dir(), constants.BuildScriptFileName)
return ScriptFromFile(path)
}

Expand All @@ -40,8 +40,8 @@ func ScriptFromFile(path string) (*buildscript.BuildScript, error) {
return buildscript.Unmarshal(data)
}

func Initialize(path string, auth *authentication.Auth) error {
scriptPath := filepath.Join(path, constants.BuildScriptFileName)
func Initialize(proj projecter, auth *authentication.Auth) error {
mitchell-as marked this conversation as resolved.
Show resolved Hide resolved
scriptPath := filepath.Join(proj.Dir(), constants.BuildScriptFileName)
script, err := ScriptFromFile(scriptPath)
if err == nil {
return nil // nothing to do, buildscript already exists
Expand All @@ -51,13 +51,13 @@ func Initialize(path string, auth *authentication.Auth) error {
}

logging.Debug("Build script does not exist. Creating one.")
commitId, err := localcommit.Get(path)
commitId, err := checkoutinfo.GetCommitID(proj.Dir())
if err != nil {
return errs.Wrap(err, "Unable to get the local commit ID")
}

buildplanner := buildplanner.NewBuildPlannerModel(auth)
script, err = buildplanner.GetBuildScript(commitId.String())
script, err = buildplanner.GetBuildScript(proj.Owner(), proj.Name(), commitId.String())
if err != nil {
return errs.Wrap(err, "Unable to get the remote build expression and time")
}
Expand Down Expand Up @@ -96,8 +96,14 @@ func Update(proj projecter, newScript *buildscript.BuildScript) error {
}

logging.Debug("Writing build script")
if err := fileutils.WriteFile(filepath.Join(proj.ProjectDir(), constants.BuildScriptFileName), sb); err != nil {
if err := fileutils.WriteFile(filepath.Join(proj.Dir(), constants.BuildScriptFileName), sb); err != nil {
return errs.Wrap(err, "Could not write build script to file")
}
return nil
}

// Remove removes an existing buildscript.
// This is primarily for updating an outdated buildscript.
func Remove(path string) error {
return os.Remove(filepath.Join(path, constants.BuildScriptFileName))
}
8 changes: 6 additions & 2 deletions internal/runbits/buildscript/testdata/buildscript1.as
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
at_time = "2023-10-16T22:20:29.000000Z"
```
Project: https://platform.activestate.com/ActiveState-CLI/Merge?branch=main&commitID=d908a758-6a81-40d4-b0eb-87069cd7f07d
Time: 2024-05-10T00:00:13.138Z
```

runtime = state_tool_artifacts_v1(
build_flags = [
],
Expand All @@ -20,4 +24,4 @@ sources = solve(
solver_version = null
)

main = runtime
main = runtime
8 changes: 6 additions & 2 deletions internal/runbits/buildscript/testdata/buildscript2.as
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
at_time = "2023-08-01T16:20:11.985000Z"
```
Project: https://platform.activestate.com/ActiveState-CLI/Merge?branch=main&commitID=f3263ee4-ac4c-41ee-b778-2585333f49f7
Time: 2023-08-01T16:20:11.985000Z
```

runtime = state_tool_artifacts_v1(
build_flags = [
],
Expand All @@ -20,4 +24,4 @@ sources = solve(
solver_version = null
)

main = runtime
main = runtime
4 changes: 2 additions & 2 deletions internal/runbits/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/pkg/localcommit"
"github.com/ActiveState/cli/pkg/checkoutinfo"
"github.com/ActiveState/cli/pkg/platform/authentication"
"github.com/ActiveState/cli/pkg/platform/model"
"github.com/ActiveState/cli/pkg/project"
Expand Down Expand Up @@ -49,7 +49,7 @@ func CommitsBehind(p *project.Project, auth *authentication.Auth) (int, error) {
return 0, locale.NewError("err_latest_commit", "Latest commit ID is nil")
}

commitID, err := localcommit.Get(p.Dir())
commitID, err := checkoutinfo.GetCommitID(p.Dir())
if err != nil {
return 0, errs.Wrap(err, "Unable to get local commit")
}
Expand Down
10 changes: 7 additions & 3 deletions internal/runbits/checkout/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/ActiveState/cli/internal/osutils"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/internal/runbits/git"
"github.com/ActiveState/cli/pkg/localcommit"
"github.com/ActiveState/cli/pkg/checkoutinfo"
"github.com/ActiveState/cli/pkg/platform/api/mono/mono_models"
"github.com/ActiveState/cli/pkg/platform/authentication"
"github.com/ActiveState/cli/pkg/platform/model"
Expand Down Expand Up @@ -104,7 +104,11 @@ func (r *Checkout) Run(ns *project.Namespaced, branchName, cachePath, targetPath
}

if r.config.GetBool(constants.OptinBuildscriptsConfig) {
if err := buildscript_runbit.Initialize(path, r.auth); err != nil {
pj, err := project.FromPath(path)
if err != nil {
return "", errs.Wrap(err, "Could not load created project file")
}
if err := buildscript_runbit.Initialize(pj, r.auth); err != nil {
return "", errs.Wrap(err, "Unable to initialize buildscript")
}
}
Expand Down Expand Up @@ -201,7 +205,7 @@ func CreateProjectFiles(checkoutPath, cachePath, owner, name, branch, commitID,
}
}

if err := localcommit.Set(checkoutPath, commitID); err != nil {
if err := checkoutinfo.SetCommitID(checkoutPath, commitID); err != nil {
return errs.Wrap(err, "Could not create local commit file")
}

Expand Down
4 changes: 2 additions & 2 deletions internal/runbits/commits_runbit/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/ActiveState/cli/internal/captain"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/pkg/localcommit"
"github.com/ActiveState/cli/pkg/checkoutinfo"
"github.com/ActiveState/cli/pkg/platform/authentication"
"github.com/ActiveState/cli/pkg/platform/model"
"github.com/ActiveState/cli/pkg/project"
Expand Down Expand Up @@ -46,7 +46,7 @@ func ExpandTimeForProject(ts *captain.TimeValue, auth *authentication.Auth, proj
}

if proj != nil {
commitID, err := localcommit.Get(proj.Dir())
commitID, err := checkoutinfo.GetCommitID(proj.Dir())
if err != nil {
return time.Time{}, errs.Wrap(err, "Unable to get commit ID")
}
Expand Down
15 changes: 7 additions & 8 deletions internal/runbits/runtime/requirements/requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
runtime_runbit "github.com/ActiveState/cli/internal/runbits/runtime"
"github.com/ActiveState/cli/internal/runbits/runtime/trigger"
"github.com/ActiveState/cli/pkg/buildscript"
"github.com/ActiveState/cli/pkg/localcommit"
"github.com/ActiveState/cli/pkg/checkoutinfo"
"github.com/ActiveState/cli/pkg/platform/api/buildplanner/response"
"github.com/ActiveState/cli/pkg/platform/api/buildplanner/types"
medmodel "github.com/ActiveState/cli/pkg/platform/api/mediator/model"
Expand Down Expand Up @@ -162,7 +162,7 @@ func (r *RequirementOperation) ExecuteRequirementOperation(ts *time.Time, requir
return errs.Wrap(err, "Could not validate packages")
}

parentCommitID, err := localcommit.Get(r.Project.Dir())
parentCommitID, err := checkoutinfo.GetCommitID(r.Project.Dir())
if err != nil {
return errs.Wrap(err, "Unable to get local commit")
}
Expand Down Expand Up @@ -282,7 +282,7 @@ func (r *RequirementOperation) ExecuteRequirementOperation(ts *time.Time, requir
}

func (r *RequirementOperation) prepareBuildScript(bp *bpModel.BuildPlanner, parentCommit strfmt.UUID, requirements []*Requirement, ts *time.Time) (*buildscript.BuildScript, error) {
script, err := bp.GetBuildScript(string(parentCommit))
script, err := bp.GetBuildScript(r.Project.Owner(), r.Project.Name(), string(parentCommit))
if err != nil {
return nil, errs.Wrap(err, "Failed to get build expression")
}
Expand All @@ -296,8 +296,7 @@ func (r *RequirementOperation) prepareBuildScript(bp *bpModel.BuildPlanner, pare
if err != nil {
return nil, errs.Wrap(err, "Unable to fetch latest Platform timestamp")
}
atTime := script.AtTime()
if atTime == nil || latest.After(*atTime) {
if latest.After(script.AtTime()) {
script.SetAtTime(latest)
}
}
Expand Down Expand Up @@ -352,7 +351,7 @@ func (r *RequirementOperation) resolveNamespace(ts *time.Time, requirement *Requ
if requirement.NamespaceType != nil {
switch *requirement.NamespaceType {
case model.NamespacePackage, model.NamespaceBundle:
commitID, err := localcommit.Get(r.Project.Dir())
commitID, err := checkoutinfo.GetCommitID(r.Project.Dir())
if err != nil {
return errs.Wrap(err, "Unable to get local commit")
}
Expand Down Expand Up @@ -533,13 +532,13 @@ func (r *RequirementOperation) resolveRequirement(requirement *Requirement) erro
}

func (r *RequirementOperation) updateCommitID(commitID strfmt.UUID) error {
if err := localcommit.Set(r.Project.Dir(), commitID.String()); err != nil {
if err := checkoutinfo.SetCommitID(r.Project.Dir(), commitID.String()); err != nil {
return locale.WrapError(err, "err_package_update_commit_id")
}

if r.Config.GetBool(constants.OptinBuildscriptsConfig) {
bp := bpModel.NewBuildPlannerModel(r.Auth)
script, err := bp.GetBuildScript(commitID.String())
script, err := bp.GetBuildScript(r.Project.Owner(), r.Project.Name(), commitID.String())
if err != nil {
return errs.Wrap(err, "Could not get remote build expr and time")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/runbits/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/ActiveState/cli/internal/runbits/runtime/progress"
"github.com/ActiveState/cli/internal/runbits/runtime/trigger"
"github.com/ActiveState/cli/pkg/buildplan"
"github.com/ActiveState/cli/pkg/localcommit"
"github.com/ActiveState/cli/pkg/checkoutinfo"
"github.com/ActiveState/cli/pkg/platform/model"
bpModel "github.com/ActiveState/cli/pkg/platform/model/buildplanner"
"github.com/ActiveState/cli/pkg/project"
Expand Down Expand Up @@ -140,7 +140,7 @@ func Update(
commitID = opts.Commit.CommitID
}
if commitID == "" {
commitID, err = localcommit.Get(proj.Dir())
commitID, err = checkoutinfo.GetCommitID(proj.Dir())
if err != nil {
return nil, errs.Wrap(err, "Failed to get local commit")
}
Expand Down
6 changes: 3 additions & 3 deletions internal/runners/activate/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/ActiveState/cli/internal/runbits/runtime/trigger"
"github.com/ActiveState/cli/internal/subshell"
"github.com/ActiveState/cli/internal/virtualenvironment"
"github.com/ActiveState/cli/pkg/localcommit"
"github.com/ActiveState/cli/pkg/checkoutinfo"
"github.com/ActiveState/cli/pkg/platform/authentication"
"github.com/ActiveState/cli/pkg/platform/model"
"github.com/ActiveState/cli/pkg/project"
Expand Down Expand Up @@ -146,7 +146,7 @@ func (r *Activate) Run(params *ActivateParams) (rerr error) {
}

if proj != nil {
commitID, err := localcommit.Get(proj.Dir())
commitID, err := checkoutinfo.GetCommitID(proj.Dir())
if err != nil {
return errs.Wrap(err, "Unable to get local commit")
}
Expand Down Expand Up @@ -196,7 +196,7 @@ func (r *Activate) Run(params *ActivateParams) (rerr error) {
}
}

commitID, err := localcommit.Get(proj.Dir())
commitID, err := checkoutinfo.GetCommitID(proj.Dir())
if err != nil {
return errs.Wrap(err, "Unable to get local commit")
}
Expand Down
Loading
Loading