Skip to content

Commit 66522b1

Browse files
committed
APP-1675 - Add version-update-sources command for draft version source updates
1 parent 1efd928 commit 66522b1

11 files changed

Lines changed: 586 additions & 11 deletions

apptrust/commands/flags.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const (
1414
VersionRollback = "version-rollback"
1515
VersionDelete = "version-delete"
1616
VersionRelease = "version-release"
17-
VersionUpdate = "version-update"
17+
VersionUpdate = "version-update"
18+
VersionUpdateSources = "version-update-sources"
1819
PackageBind = "package-bind"
1920
PackageUnbind = "package-unbind"
2021
AppCreate = "app-create"
@@ -44,6 +45,7 @@ const (
4445
SyncFlag = "sync"
4546
PromotionTypeFlag = "promotion-type"
4647
DryRunFlag = "dry-run"
48+
FailFastFlag = "fail-fast"
4749
ExcludeReposFlag = "exclude-repos"
4850
IncludeReposFlag = "include-repos"
4951
PropsFlag = "props"
@@ -85,6 +87,7 @@ var flagsMap = map[string]components.Flag{
8587
SyncFlag: components.NewBoolFlag(SyncFlag, "Whether to synchronize the operation.", components.WithBoolDefaultValueTrue()),
8688
PromotionTypeFlag: components.NewStringFlag(PromotionTypeFlag, "The promotion type. The following values are supported: "+coreutils.ListToText(model.PromotionTypeValues), func(f *components.StringFlag) { f.Mandatory = false; f.DefaultValue = model.PromotionTypeCopy }),
8789
DryRunFlag: components.NewBoolFlag(DryRunFlag, "Perform a simulation of the operation.", components.WithBoolDefaultValueFalse()),
90+
FailFastFlag: components.NewBoolFlag(FailFastFlag, "Stop the operation on the first error. Only relevant when sources are provided.", components.WithBoolDefaultValueTrue()),
8891
ExcludeReposFlag: components.NewStringFlag(ExcludeReposFlag, "Semicolon-separated list of repositories to exclude.", func(f *components.StringFlag) { f.Mandatory = false }),
8992
IncludeReposFlag: components.NewStringFlag(IncludeReposFlag, "Semicolon-separated list of repositories to include.", func(f *components.StringFlag) { f.Mandatory = false }),
9093
PropsFlag: components.NewStringFlag(PropsFlag, "Semicolon-separated list of properties in the form of 'key1=value1;key2=value2;...' to be added to each artifact.", func(f *components.StringFlag) { f.Mandatory = false }),
@@ -168,6 +171,24 @@ var commandFlags = map[string][]string{
168171
PropertiesFlag,
169172
DeletePropertiesFlag,
170173
},
174+
VersionUpdateSources: {
175+
url,
176+
user,
177+
accessToken,
178+
serverId,
179+
SyncFlag,
180+
DryRunFlag,
181+
FailFastFlag,
182+
SourceTypeBuildsFlag,
183+
SourceTypeReleaseBundlesFlag,
184+
SourceTypeApplicationVersionsFlag,
185+
SourceTypePackagesFlag,
186+
SourceTypeArtifactsFlag,
187+
SpecFlag,
188+
SpecVarsFlag,
189+
IncludeFilterFlag,
190+
ExcludeFilterFlag,
191+
},
171192

172193
PackageBind: {
173194
url,
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package version
2+
3+
import (
4+
"github.com/jfrog/jfrog-cli-application/apptrust/app"
5+
"github.com/jfrog/jfrog-cli-application/apptrust/commands"
6+
"github.com/jfrog/jfrog-cli-application/apptrust/commands/utils"
7+
"github.com/jfrog/jfrog-cli-application/apptrust/common"
8+
"github.com/jfrog/jfrog-cli-application/apptrust/model"
9+
"github.com/jfrog/jfrog-cli-application/apptrust/service"
10+
"github.com/jfrog/jfrog-cli-application/apptrust/service/versions"
11+
commonCLiCommands "github.com/jfrog/jfrog-cli-core/v2/common/commands"
12+
pluginsCommon "github.com/jfrog/jfrog-cli-core/v2/plugins/common"
13+
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
14+
coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config"
15+
"github.com/jfrog/jfrog-client-go/utils/errorutils"
16+
"github.com/jfrog/jfrog-client-go/utils/log"
17+
)
18+
19+
type updateAppVersionSourcesCommand struct {
20+
versionService versions.VersionService
21+
serverDetails *coreConfig.ServerDetails
22+
applicationKey string
23+
version string
24+
requestPayload *model.UpdateVersionSourcesRequest
25+
sync bool
26+
dryRun bool
27+
failFast bool
28+
}
29+
30+
func (cmd *updateAppVersionSourcesCommand) Run() error {
31+
ctx, err := service.NewContext(*cmd.serverDetails)
32+
if err != nil {
33+
log.Error("Failed to create service context:", err)
34+
return err
35+
}
36+
37+
err = cmd.versionService.UpdateAppVersionSources(ctx, cmd.applicationKey, cmd.version, cmd.requestPayload, cmd.sync, cmd.dryRun, cmd.failFast)
38+
if err != nil {
39+
log.Error("Failed to update application version sources:", err)
40+
return err
41+
}
42+
43+
return nil
44+
}
45+
46+
func (cmd *updateAppVersionSourcesCommand) ServerDetails() (*coreConfig.ServerDetails, error) {
47+
return cmd.serverDetails, nil
48+
}
49+
50+
func (cmd *updateAppVersionSourcesCommand) CommandName() string {
51+
return commands.VersionUpdateSources
52+
}
53+
54+
func (cmd *updateAppVersionSourcesCommand) prepareAndRunCommand(ctx *components.Context) error {
55+
if err := validateUpdateSourcesContext(ctx); err != nil {
56+
return err
57+
}
58+
59+
if err := cmd.parseFlagsAndSetFields(ctx); err != nil {
60+
return err
61+
}
62+
63+
var err error
64+
cmd.requestPayload, err = cmd.buildRequestPayload(ctx)
65+
if errorutils.CheckError(err) != nil {
66+
return err
67+
}
68+
69+
return commonCLiCommands.Exec(cmd)
70+
}
71+
72+
func validateUpdateSourcesContext(ctx *components.Context) error {
73+
if len(ctx.Arguments) != 2 {
74+
return pluginsCommon.WrongNumberOfArgumentsHandler(ctx)
75+
}
76+
77+
if err := validateNoSpecAndFlagsTogether(ctx); err != nil {
78+
return err
79+
}
80+
81+
if !hasSourceFlags(ctx) {
82+
return errorutils.CheckErrorf(
83+
"At least one source flag is required. Please provide --%s or at least one of the following: --%s, --%s, --%s, --%s, --%s.",
84+
commands.SpecFlag, commands.SourceTypeBuildsFlag, commands.SourceTypeReleaseBundlesFlag, commands.SourceTypeApplicationVersionsFlag, commands.SourceTypePackagesFlag, commands.SourceTypeArtifactsFlag)
85+
}
86+
87+
return nil
88+
}
89+
90+
// parseFlagsAndSetFields parses CLI flags and sets struct fields accordingly.
91+
func (cmd *updateAppVersionSourcesCommand) parseFlagsAndSetFields(ctx *components.Context) error {
92+
cmd.applicationKey = ctx.Arguments[0]
93+
cmd.version = ctx.Arguments[1]
94+
95+
serverDetails, err := utils.ServerDetailsByFlags(ctx)
96+
if err != nil {
97+
return err
98+
}
99+
cmd.serverDetails = serverDetails
100+
101+
cmd.sync = ctx.GetBoolTFlagValue(commands.SyncFlag)
102+
cmd.dryRun = ctx.GetBoolFlagValue(commands.DryRunFlag)
103+
cmd.failFast = ctx.GetBoolTFlagValue(commands.FailFastFlag)
104+
105+
return nil
106+
}
107+
108+
func (cmd *updateAppVersionSourcesCommand) buildRequestPayload(ctx *components.Context) (*model.UpdateVersionSourcesRequest, error) {
109+
sources, filters, err := buildSourcesAndFiltersFromContext(ctx)
110+
if err != nil {
111+
return nil, err
112+
}
113+
114+
return &model.UpdateVersionSourcesRequest{
115+
Sources: sources,
116+
Filters: filters,
117+
}, nil
118+
}
119+
120+
func GetUpdateAppVersionSourcesCommand(appContext app.Context) components.Command {
121+
cmd := &updateAppVersionSourcesCommand{versionService: appContext.GetVersionService()}
122+
return components.Command{
123+
Name: commands.VersionUpdateSources,
124+
Description: "Updates the sources for a draft application version.",
125+
Category: common.CategoryVersion,
126+
Aliases: []string{"vus"},
127+
Arguments: []components.Argument{
128+
{
129+
Name: "app-key",
130+
Description: "The application key of the application for which the version sources are being updated.",
131+
Optional: false,
132+
},
133+
{
134+
Name: "version",
135+
Description: "The version number (in SemVer format) for the application version to update sources.",
136+
Optional: false,
137+
},
138+
},
139+
Flags: commands.GetCommandFlags(commands.VersionUpdateSources),
140+
Action: cmd.prepareAndRunCommand,
141+
}
142+
}

0 commit comments

Comments
 (0)