Skip to content

Commit

Permalink
Make checkoutinfo a primer property and use it to set commit IDs, and…
Browse files Browse the repository at this point in the history
… initialize and update build scripts.
  • Loading branch information
mitchell-as committed Sep 16, 2024
1 parent a54541d commit b3810dc
Show file tree
Hide file tree
Showing 56 changed files with 347 additions and 423 deletions.
1 change: 1 addition & 0 deletions internal/events/cmdcall/cmdcall.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type primeable interface {
primer.Configurer
primer.Analyticer
primer.SvcModeler
primer.CheckoutInfoer
}

// CmdCall manages dependencies for the handling of events triggered by command
Expand Down
7 changes: 4 additions & 3 deletions internal/migrator/migrator.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package migrator

import (
"path/filepath"
"github.com/go-openapi/strfmt"

"github.com/ActiveState/cli/internal/config"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/runbits/buildscript"
"github.com/ActiveState/cli/pkg/checkoutinfo"
"github.com/ActiveState/cli/pkg/platform/authentication"
"github.com/ActiveState/cli/pkg/projectfile"
)
Expand All @@ -26,7 +26,8 @@ func NewMigrator(auth *authentication.Auth, cfg *config.Instance) projectfile.Mi
// have completed. Ensure you roll back any partial updates in the case of an error as they will need to be attempted again.
case 0:
logging.Debug("Attempting to create buildscript")
if err := buildscript_runbit.Initialize(filepath.Dir(project.Path()), project.Owner(), project.Name(), project.BranchName(), project.LegacyCommitID(), auth, cfg); err != nil {
info := checkoutinfo.New(auth, cfg, project)
if err := info.InitializeBuildScript(strfmt.UUID(project.LegacyCommitID())); err != nil {
return v, errs.Wrap(err, "Failed to initialize buildscript")
}
}
Expand Down
34 changes: 23 additions & 11 deletions internal/primer/primer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,26 @@ import (
"github.com/ActiveState/cli/internal/prompt"
"github.com/ActiveState/cli/internal/subshell"
"github.com/ActiveState/cli/internal/svcctl"
"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"
"github.com/ActiveState/cli/pkg/projectfile"
)

type Values struct {
project *project.Project
projectfile *projectfile.Project
output output.Outputer
auth *authentication.Auth
prompt prompt.Prompter
subshell subshell.SubShell
conditional *constraints.Conditional
config *config.Instance
ipComm svcctl.IPCommunicator
svcModel *model.SvcModel
analytics analytics.Dispatcher
project *project.Project
projectfile *projectfile.Project
output output.Outputer
auth *authentication.Auth
prompt prompt.Prompter
subshell subshell.SubShell
conditional *constraints.Conditional
config *config.Instance
ipComm svcctl.IPCommunicator
svcModel *model.SvcModel
analytics analytics.Dispatcher
checkoutinfo *checkoutinfo.CheckoutInfo
}

func New(values ...any) *Values {
Expand Down Expand Up @@ -65,12 +67,14 @@ func New(values ...any) *Values {
}
}
}
result.checkoutinfo = checkoutinfo.New(result.auth, result.config, result.project)
return result
}

func (v *Values) SetProject(p *project.Project) {
v.project = p
v.projectfile = p.Source()
v.checkoutinfo = checkoutinfo.New(v.auth, v.config, p)
}

type Projecter interface {
Expand Down Expand Up @@ -118,6 +122,10 @@ type Conditioner interface {
Conditional() *constraints.Conditional
}

type CheckoutInfoer interface {
CheckoutInfo() *checkoutinfo.CheckoutInfo
}

func (v *Values) Project() *project.Project {
return v.project
}
Expand Down Expand Up @@ -161,3 +169,7 @@ func (v *Values) Config() *config.Instance {
func (v *Values) Analytics() analytics.Dispatcher {
return v.analytics
}

func (v *Values) CheckoutInfo() *checkoutinfo.CheckoutInfo {
return v.checkoutinfo
}
13 changes: 6 additions & 7 deletions internal/runbits/buildplanner/buildplanner.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package buildplanner

import (
"github.com/ActiveState/cli/internal/config"
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/internal/rtutils/ptr"
buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript"
"github.com/ActiveState/cli/internal/runbits/rationalize"
"github.com/ActiveState/cli/pkg/buildplan"
"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 @@ -45,7 +44,7 @@ func GetCommit(
target string,
auth *authentication.Auth,
out output.Outputer,
cfg *config.Instance) (commit *bpModel.Commit, rerr error) {
info *checkoutinfo.CheckoutInfo) (commit *bpModel.Commit, rerr error) {
if pj == nil && !namespace.IsValid() {
return nil, rationalize.ErrNoProject
}
Expand Down Expand Up @@ -78,7 +77,7 @@ func GetCommit(
switch {
// Return the buildplan from this runtime.
case !namespaceProvided && !commitIdProvided:
localCommitID, err := buildscript_runbit.CommitID(pj.Path(), cfg)
localCommitID, err := info.CommitID()
if err != nil {
return nil, errs.Wrap(err, "Could not get commit ID")
}
Expand Down Expand Up @@ -155,7 +154,7 @@ func GetCommit(
owner = pj.Owner()
name = pj.Name()
nsString = pj.NamespaceString()
commitID, err := buildscript_runbit.CommitID(pj.Path(), cfg)
commitID, err := info.CommitID()
if err != nil {
return nil, errs.Wrap(err, "Could not get commit ID")
}
Expand Down Expand Up @@ -183,8 +182,8 @@ func GetBuildPlan(
target string,
auth *authentication.Auth,
out output.Outputer,
cfg *config.Instance) (bp *buildplan.BuildPlan, rerr error) {
commit, err := GetCommit(pj, namespace, commitID, target, auth, out, cfg)
info *checkoutinfo.CheckoutInfo) (bp *buildplan.BuildPlan, rerr error) {
commit, err := GetCommit(pj, namespace, commitID, target, auth, out, info)
if err != nil {
return nil, errs.Wrap(err, "Could not get commit")
}
Expand Down
36 changes: 0 additions & 36 deletions internal/runbits/buildscript/commitid.go

This file was deleted.

105 changes: 0 additions & 105 deletions internal/runbits/buildscript/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,9 @@ import (
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/pkg/buildscript"
"github.com/ActiveState/cli/pkg/checkoutinfo"
"github.com/ActiveState/cli/pkg/platform/authentication"
"github.com/ActiveState/cli/pkg/platform/model/buildplanner"
)

// configurer is here until buildscripts are no longer walled behind an opt-in config option.
type configurer interface {
GetBool(string) bool
}

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

func ScriptFromProject(projectDir string) (*buildscript.BuildScript, error) {
Expand All @@ -38,101 +29,5 @@ func ScriptFromProject(projectDir string) (*buildscript.BuildScript, error) {
return nil, errs.Wrap(err, "Could not unmarshal build script")
}

// Synchronize any changes with activestate.yaml.
err = checkoutinfo.UpdateProject(script, path)
if err != nil {
return nil, errs.Wrap(err, "Could not update project file")
}

return script, nil
}

func Initialize(path, owner, project, branch, commitID string, auth *authentication.Auth, cfg configurer) error {
if cfg.GetBool(constants.OptinBuildscriptsConfig) {
_, err := ScriptFromProject(path)
if err == nil {
return nil // nothing to do, buildscript already exists
}
if !errors.Is(err, os.ErrNotExist) {
return errs.Wrap(err, "Could not read project build script")
}
}

buildplanner := buildplanner.NewBuildPlannerModel(auth)
script, err := buildplanner.GetBuildScript(owner, project, branch, commitID)
if err != nil {
return errs.Wrap(err, "Unable to get the remote build script")
}

if !cfg.GetBool(constants.OptinBuildscriptsConfig) {
// Just update the project file with the new commit ID.
err = checkoutinfo.UpdateProject(script, path)
if err != nil {
return errs.Wrap(err, "Unable to update project file")
}
return nil
}

scriptBytes, err := script.Marshal()
if err != nil {
return errs.Wrap(err, "Unable to marshal build script")
}

scriptPath := filepath.Join(path, constants.BuildScriptFileName)
logging.Debug("Initializing build script at %s", scriptPath)
err = fileutils.WriteFile(scriptPath, scriptBytes)
if err != nil {
return errs.Wrap(err, "Unable to write build script")
}

return nil
}

func Update(path string, newScript *buildscript.BuildScript, cfg configurer) error {
if !cfg.GetBool(constants.OptinBuildscriptsConfig) {
// Just update the activestate.yaml file (e.g. with the new commit ID).
// Eventually the buildscript will be the one source of truth.
return checkoutinfo.UpdateProject(newScript, path)
}

script, err := ScriptFromProject(path)
if err != nil {
return errs.Wrap(err, "Could not read build script")
}

equals, err := script.Equals(newScript)
if err != nil {
return errs.Wrap(err, "Could not compare build script")
}
if script != nil && equals {
return nil // no changes to write
}

sb, err := newScript.Marshal()
if err != nil {
return errs.Wrap(err, "Could not marshal build script")
}

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

// Synchronize changes with activestate.yaml.
err = checkoutinfo.UpdateProject(newScript, path)
if err != nil {
return errs.Wrap(err, "Could not update project file")
}

return nil
}

// Remove removes an existing buildscript if it exists.
// This is primarily for updating an outdated buildscript.
func Remove(path string) error {
bsPath := filepath.Join(path, constants.BuildScriptFileName)
if !fileutils.TargetExists(bsPath) {
return nil
}
return os.Remove(bsPath)
}
11 changes: 5 additions & 6 deletions internal/runbits/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ import (
"errors"
"strconv"

"github.com/ActiveState/cli/internal/config"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/output"
buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript"
"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"
)

// RunCommitsBehindNotifier checks for the commits behind count based on the
// provided project and displays the results to the user in a helpful manner.
func RunCommitsBehindNotifier(p *project.Project, out output.Outputer, auth *authentication.Auth, cfg *config.Instance) {
count, err := CommitsBehind(p, auth, cfg)
func RunCommitsBehindNotifier(p *project.Project, out output.Outputer, auth *authentication.Auth, info *checkoutinfo.CheckoutInfo) {
count, err := CommitsBehind(p, auth, info)
if err != nil {
if errors.Is(err, model.ErrCommitCountUnknowable) {
out.Notice(output.Title(locale.Tr("runtime_update_notice_unknown_count")))
Expand All @@ -36,7 +35,7 @@ func RunCommitsBehindNotifier(p *project.Project, out output.Outputer, auth *aut
}
}

func CommitsBehind(p *project.Project, auth *authentication.Auth, cfg *config.Instance) (int, error) {
func CommitsBehind(p *project.Project, auth *authentication.Auth, info *checkoutinfo.CheckoutInfo) (int, error) {
if p.IsHeadless() {
return 0, nil
}
Expand All @@ -50,7 +49,7 @@ func CommitsBehind(p *project.Project, auth *authentication.Auth, cfg *config.In
return 0, locale.NewError("err_latest_commit", "Latest commit ID is nil")
}

commitID, err := buildscript_runbit.CommitID(p.Dir(), cfg)
commitID, err := info.CommitID()
if err != nil {
return 0, errs.Wrap(err, "Unable to get commit ID")
}
Expand Down
Loading

0 comments on commit b3810dc

Please sign in to comment.