@@ -2,16 +2,13 @@ package createrevision
22
33import (
44 "encoding/json"
5- "fmt"
65
6+ "github.com/ActiveState/cli/internal/errs"
77 "github.com/ActiveState/cli/internal/output"
88 "github.com/ActiveState/cli/internal/primer"
9- "github.com/ActiveState/cli/pkg/platform/api/inventory"
10- "github.com/ActiveState/cli/pkg/platform/api/inventory/inventory_client/inventory_operations"
119 "github.com/ActiveState/cli/pkg/platform/api/inventory/inventory_models"
1210 "github.com/ActiveState/cli/pkg/platform/authentication"
1311 "github.com/ActiveState/cli/pkg/platform/model"
14- "github.com/go-openapi/strfmt"
1512)
1613
1714type CreateRevisionRunner struct {
@@ -49,107 +46,23 @@ func (runner *CreateRevisionRunner) Run(params *Params) error {
4946 var dependencies []inventory_models.Dependency
5047 err := json .Unmarshal ([]byte (params .dependencies ), & dependencies )
5148 if err != nil {
52- return fmt . Errorf ( "error unmarshaling dependencies, dependency JSON is in wrong format: %w" , err )
49+ return errs . Wrap ( err , "error unmarshaling dependencies, dependency JSON is in wrong format" )
5350 }
5451
5552 // Retrieve ingredient version to access ingredient and version IDs
5653 ingredient , err := model .GetIngredientByNameAndVersion (params .namespace , params .name , params .version , nil , runner .auth )
5754 if err != nil {
58- return fmt . Errorf ( "error fetching ingredient: %w" , err )
55+ return errs . Wrap ( err , "error fetching ingredient" )
5956 }
6057
61- // Retrieve its latest revision to copy all data - but comment and dependencies - from
62- getParams := inventory_operations .NewGetIngredientVersionRevisionsParams ()
63- getParams .SetIngredientID (* ingredient .IngredientID )
64- getParams .SetIngredientVersionID (* ingredient .IngredientVersionID )
65-
66- client := inventory .Get (runner .auth )
67- revisions , err := client .GetIngredientVersionRevisions (getParams , runner .auth .ClientAuth ())
68- if err != nil {
69- return fmt .Errorf ("error getting version revisions: %w" , err )
70- }
71- revision := revisions .Payload .IngredientVersionRevisions [len (revisions .Payload .IngredientVersionRevisions )- 1 ]
72-
73- // Prepare new ingredient version revision params to create a new revision
74- // This leaves all the attributes untouched, but dependencies and comments
75- newParams := inventory_operations .NewAddIngredientVersionRevisionParams ()
76- newParams .SetIngredientID (* ingredient .IngredientID )
77- newParams .SetIngredientVersionID (* ingredient .IngredientVersionID )
78-
79- // Extract build script IDs
80- var buildScriptIDs []strfmt.UUID
81- for _ , script := range revision .BuildScripts {
82- buildScriptIDs = append (buildScriptIDs , * script .BuildScriptID )
83- }
84-
85- // Replicate patches
86- var patches []* inventory_models.IngredientVersionRevisionCreatePatch
87- for _ , patch := range revision .Patches {
88- patches = append (patches , & inventory_models.IngredientVersionRevisionCreatePatch {
89- PatchID : patch .PatchID ,
90- SequenceNumber : patch .SequenceNumber ,
91- })
92- }
93-
94- // Retrieve and prepare default and override option sets
95- optsetParams := inventory_operations .NewGetIngredientVersionIngredientOptionSetsParams ()
96- optsetParams .SetIngredientID (* ingredient .IngredientID )
97- optsetParams .SetIngredientVersionID (* ingredient .IngredientVersionID )
98-
99- response , err := client .GetIngredientVersionIngredientOptionSets (optsetParams , runner .auth .ClientAuth ())
100- if err != nil {
101- return fmt .Errorf ("error getting optsets: %w" , err )
102- }
103-
104- var default_optsets []strfmt.UUID
105- var override_optsets []strfmt.UUID
106- for _ , optset := range response .Payload .IngredientOptionSetsWithUsageType {
107- switch * optset .UsageType {
108- case "default" :
109- default_optsets = append (default_optsets , * optset .IngredientOptionSetID )
110- case "override" :
111- override_optsets = append (override_optsets , * optset .IngredientOptionSetID )
112- }
113- }
114-
115- // Create and set the new revision object from model, setting the reason as manual change
116- manual_change := inventory_models .IngredientVersionRevisionCoreReasonManualChange
117- new_revision := inventory_models.IngredientVersionRevisionCreate {
118- IngredientVersionRevisionCore : inventory_models.IngredientVersionRevisionCore {
119- Comment : & params .comment ,
120- ProvidedFeatures : revision .ProvidedFeatures ,
121- Reason : & manual_change ,
122- ActivestateLicenseExpression : revision .ActivestateLicenseExpression ,
123- AuthorPlatformUserID : revision .AuthorPlatformUserID ,
124- CamelExtras : revision .CamelExtras ,
125- Dependencies : dependencies ,
126- IsIndemnified : revision .IsIndemnified ,
127- IsStableRelease : revision .IsStableRelease ,
128- IsStableRevision : revision .IsStableRevision ,
129- LicenseManifestURI : revision .LicenseManifestURI ,
130- PlatformSourceURI : revision .PlatformSourceURI ,
131- ScannerLicenseExpression : revision .ScannerLicenseExpression ,
132- SourceChecksum : revision .SourceChecksum ,
133- Status : revision .Status ,
134- },
135- IngredientVersionRevisionCreateAllOf0 : inventory_models.IngredientVersionRevisionCreateAllOf0 {
136- BuildScripts : buildScriptIDs ,
137- DefaultIngredientOptionSets : default_optsets ,
138- IngredientOptionSetOverrides : override_optsets ,
139- Patches : patches ,
140- },
141- }
142- newParams .SetIngredientVersionRevision (& new_revision )
143-
144- // Create the new revision and output its marshalled string
145- newRevision , err := client .AddIngredientVersionRevision (newParams , runner .auth .ClientAuth ())
58+ newRevision , err := model .CreateNewIngredientVersionRevision (ingredient , params .comment , dependencies , runner .auth )
14659 if err != nil {
147- return fmt . Errorf ( "error creating revision: %w" , err )
60+ return errs . Wrap ( err , "error creating ingredient version revision" )
14861 }
14962
15063 marshalledRevision , err := json .Marshal (newRevision )
15164 if err != nil {
152- return fmt . Errorf ( "error marshalling revision response: %w" , err )
65+ return errs . Wrap ( err , "error marshalling revision response" )
15366 }
15467 runner .output .Print (string (marshalledRevision ))
15568
0 commit comments