github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/pkg/platform/model/buildplanner/project.go (about)

     1  package buildplanner
     2  
     3  import (
     4  	"github.com/ActiveState/cli/internal/errs"
     5  	"github.com/ActiveState/cli/internal/logging"
     6  	"github.com/ActiveState/cli/pkg/platform/api/buildplanner/request"
     7  	"github.com/ActiveState/cli/pkg/platform/api/buildplanner/response"
     8  	"github.com/ActiveState/cli/pkg/platform/api/buildplanner/types"
     9  	"github.com/ActiveState/cli/pkg/platform/runtime/buildexpression"
    10  	"github.com/go-openapi/strfmt"
    11  )
    12  
    13  // CreateProjectParams contains information for the project to create.
    14  // When creating a project from scratch, the PlatformID, Language, Version, and Timestamp fields
    15  // are used to create a buildexpression to use.
    16  // When creating a project based off of another one, the Expr field is used (PlatformID, Language,
    17  // Version, and Timestamp are ignored).
    18  type CreateProjectParams struct {
    19  	Owner       string
    20  	Project     string
    21  	PlatformID  strfmt.UUID
    22  	Language    string
    23  	Version     string
    24  	Private     bool
    25  	Description string
    26  	Expr        *buildexpression.BuildExpression
    27  }
    28  
    29  func (b *BuildPlanner) CreateProject(params *CreateProjectParams) (strfmt.UUID, error) {
    30  	logging.Debug("CreateProject, owner: %s, project: %s, language: %s, version: %s", params.Owner, params.Project, params.Language, params.Version)
    31  
    32  	expr := params.Expr
    33  	if expr == nil {
    34  		// Construct an initial buildexpression for the new project.
    35  		var err error
    36  		expr, err = buildexpression.NewEmpty()
    37  		if err != nil {
    38  			return "", errs.Wrap(err, "Unable to create initial buildexpression")
    39  		}
    40  
    41  		// Add the platform.
    42  		if err := expr.UpdatePlatform(types.OperationAdded, params.PlatformID); err != nil {
    43  			return "", errs.Wrap(err, "Unable to add platform")
    44  		}
    45  
    46  		// Create a requirement for the given language and version.
    47  		versionRequirements, err := VersionStringToRequirements(params.Version)
    48  		if err != nil {
    49  			return "", errs.Wrap(err, "Unable to read version")
    50  		}
    51  		if err := expr.UpdateRequirement(types.OperationAdded, types.Requirement{
    52  			Name:               params.Language,
    53  			Namespace:          "language", // TODO: make this a constant DX-1738
    54  			VersionRequirement: versionRequirements,
    55  		}); err != nil {
    56  			return "", errs.Wrap(err, "Unable to add language requirement")
    57  		}
    58  	}
    59  
    60  	// Create the project.
    61  	request := request.CreateProject(params.Owner, params.Project, params.Private, expr, params.Description)
    62  	resp := &response.CreateProjectResult{}
    63  	err := b.client.Run(request, resp)
    64  	if err != nil {
    65  		return "", processBuildPlannerError(err, "Failed to create project")
    66  	}
    67  
    68  	if resp.ProjectCreated == nil {
    69  		return "", errs.New("ProjectCreated is nil")
    70  	}
    71  
    72  	if response.IsErrorResponse(resp.ProjectCreated.Type) {
    73  		return "", response.ProcessProjectCreatedError(resp.ProjectCreated, "Could not create project")
    74  	}
    75  
    76  	if resp.ProjectCreated.Commit == nil {
    77  		return "", errs.New("ProjectCreated.Commit is nil")
    78  	}
    79  
    80  	return resp.ProjectCreated.Commit.CommitID, nil
    81  }