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

Initial usage of buildplanner for state init. #2844

Merged
merged 5 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
27 changes: 9 additions & 18 deletions internal/runners/initialize/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,15 @@ func (r *Initialize) Run(params *RunParams) (rerr error) {
return err
}

commitID, err := model.CommitInitial(model.HostPlatform, lang.Requirement(), version)
logging.Debug("Creating Platform project")

platformID, err := model.PlatformNameToPlatformID(model.HostPlatform)
if err != nil {
return errs.Wrap(err, "Unable to determine Platform ID from %s", model.HostPlatform)
}

bp := model.NewBuildPlannerModel(r.auth)
commitID, err := bp.CreateProject(namespace.Owner, namespace.Project, platformID, lang.Requirement(), version, params.Private)
if err != nil {
return locale.WrapError(err, "err_init_commit", "Could not create initial commit")
}
Expand All @@ -233,23 +241,6 @@ func (r *Initialize) Run(params *RunParams) (rerr error) {
}
}

logging.Debug("Creating Platform project and pushing it")

platformProject, err := model.CreateEmptyProject(namespace.Owner, namespace.Project, params.Private)
if err != nil {
return locale.WrapInputError(err, "err_init_create_project", "Failed to create a Platform project at {{.V0}}.", namespace.String())
}

branch, err := model.DefaultBranchForProject(platformProject) // only one branch for newly created project
if err != nil {
return locale.NewInputError("err_no_default_branch")
}

err = model.UpdateProjectBranchCommitWithModel(platformProject, branch.Label, commitID)
if err != nil {
return locale.WrapError(err, "err_init_push", "Failed to push to the newly created Platform project at {{.V0}}", namespace.String())
}

err = runbits.RefreshRuntime(r.auth, r.out, r.analytics, proj, commitID, true, target.TriggerInit, r.svcModel)
if err != nil {
logging.Debug("Deleting remotely created project due to runtime setup error")
Expand Down
8 changes: 8 additions & 0 deletions pkg/platform/api/buildplanner/model/buildplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,14 @@ type StageCommitResult struct {
Commit *Commit `json:"stageCommit"`
}

type projectCreated struct {
Commit *Commit `json:"commit"`
}

type CreateProjectResult struct {
ProjectCreated *projectCreated `json:"createProject"`
}
mitchell-as marked this conversation as resolved.
Show resolved Hide resolved

// Error contains an error message.
type Error struct {
Message string `json:"message"`
Expand Down
58 changes: 58 additions & 0 deletions pkg/platform/api/buildplanner/request/createproject.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package request

import "github.com/ActiveState/cli/pkg/platform/runtime/buildexpression"

func CreateProject(owner, project string, private bool, expr *buildexpression.BuildExpression, description string) *createProject {
return &createProject{map[string]interface{}{
"organization": owner,
"project": project,
"private": private,
"expr": expr,
"description": description,
}}
}

type createProject struct {
vars map[string]interface{}
}

func (c *createProject) Query() string {
return `
mutation ($organization: String!, $project: String!, $private: Boolean!, $expr: BuildExpr!, $description: String!) {
createProject(input:{organization:$organization, project:$project, private:$private, expr:$expr, description:$description}) {
... on ProjectCreated {
__typename
commit {
__typename
commitId
}
}
... on AlreadyExists {
__typename
message
}
... on NotFound {
__typename
message
mayNeedAuthentication
}
... on ParseError {
__typename
message
path
}
... on ValidationError {
__typename
message
}
... on Forbidden {
__typename
message
}
}
}`
}

func (c *createProject) Vars() map[string]interface{} {
return c.vars
}
49 changes: 49 additions & 0 deletions pkg/platform/model/buildplanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
"errors"
"fmt"
"os"
"regexp"
"strconv"
Expand Down Expand Up @@ -349,6 +350,54 @@ func (bp *BuildPlanner) GetBuildExpression(owner, project, commitID string) (*bu
return expression, nil
}

func (bp *BuildPlanner) CreateProject(owner, project, platformID, language, version string, private bool) (strfmt.UUID, error) {
logging.Debug("CreateProject, owner: %s, project: %s, language: %s, version: %s", owner, project, language, version)

// At this time, there is no way to ask the Platform for an empty buildexpression, so build one
// manually and then add a timestamp, platform, and language requirement to it.
expr, err := buildexpression.New([]byte(fmt.Sprintf(`
{
"let": {
"runtime": {
"solve_legacy": {
"at_time": "%s",
"build_flags": [],
"camel_flags": [],
"platforms": ["%s"],
"requirements": [],
"solver_version": null
}
},
"in": "$runtime"
}
}
`, time.Now().Format(time.RFC3339), platformID)))
mitchell-as marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return "", errs.Wrap(err, "Unable to create initial buildexpression")
}
versionRequirements, err := VersionStringToRequirements(version)
if err != nil {
return "", errs.Wrap(err, "Unable to read version")
}
expr.UpdateRequirement(bpModel.OperationAdded, bpModel.Requirement{
Name: language,
Namespace: "language", // TODO: make this a constant DX-1738
VersionRequirement: versionRequirements,
})

request := request.CreateProject(owner, project, private, expr, locale.T("commit_message_add_initial"))
resp := &bpModel.CreateProjectResult{}
err = bp.client.Run(request, resp)
if err != nil {
return "", processBuildPlannerError(err, "Failed to create project")
}
if resp.ProjectCreated == nil {
return "", errs.New("ProjectCreated is nil")
mitchell-as marked this conversation as resolved.
Show resolved Hide resolved
}

return resp.ProjectCreated.Commit.CommitID, nil
}

// processBuildPlannerError will check for special error types that should be
// handled differently. If no special error type is found, the fallback message
// will be used.
Expand Down
Loading