Skip to content

Commit 7630e2b

Browse files
Add create ingredient version revision runner
1 parent 07367b2 commit 7630e2b

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package createrevision
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/ActiveState/cli/internal/output"
8+
"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"
11+
"github.com/ActiveState/cli/pkg/platform/api/inventory/inventory_models"
12+
"github.com/ActiveState/cli/pkg/platform/authentication"
13+
"github.com/ActiveState/cli/pkg/platform/model"
14+
"github.com/go-openapi/strfmt"
15+
)
16+
17+
type CreateRevisionRunner struct {
18+
auth *authentication.Auth
19+
output output.Outputer
20+
}
21+
22+
func New(p *primer.Values) *CreateRevisionRunner {
23+
return &CreateRevisionRunner{
24+
auth: p.Auth(),
25+
output: p.Output(),
26+
}
27+
}
28+
29+
type Params struct {
30+
namespace string
31+
name string
32+
version string
33+
dependencies string
34+
comment string
35+
}
36+
37+
func NewParams(namespace string, name string, version string, dependencies string, comment string) *Params {
38+
return &Params{
39+
namespace: namespace,
40+
name: name,
41+
version: version,
42+
dependencies: dependencies,
43+
comment: comment,
44+
}
45+
}
46+
47+
func (runner *CreateRevisionRunner) Run(params *Params) error {
48+
// Unmarshal JSON with the new dependency info
49+
var dependencies []inventory_models.Dependency
50+
err := json.Unmarshal([]byte(params.dependencies), &dependencies)
51+
if err != nil {
52+
return fmt.Errorf("error unmarshaling dependencies, dependency JSON is in wrong format: %w", err)
53+
}
54+
55+
// Retrieve ingredient version to access ingredient and version IDs
56+
ingredient, err := model.GetIngredientByNameAndVersion(params.namespace, params.name, params.version, nil, runner.auth)
57+
if err != nil {
58+
return fmt.Errorf("error fetching ingredient: %w", err)
59+
}
60+
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())
146+
if err != nil {
147+
return fmt.Errorf("error creating revision: %w", err)
148+
}
149+
150+
marshalledRevision, err := json.Marshal(newRevision)
151+
if err != nil {
152+
return fmt.Errorf("error marshalling revision response: %w", err)
153+
}
154+
runner.output.Print(string(marshalledRevision))
155+
156+
return nil
157+
}

0 commit comments

Comments
 (0)