Skip to content

Commit aff64e3

Browse files
authored
Revert "Enhancements to Release Bundle Create Command: Optional Flags and Fallback Support (#2772)" (#2788)
1 parent a124f63 commit aff64e3

File tree

4 files changed

+45
-205
lines changed

4 files changed

+45
-205
lines changed

lifecycle/cli.go

+9-71
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ package lifecycle
22

33
import (
44
"errors"
5-
"fmt"
65
commonCliUtils "github.com/jfrog/jfrog-cli-core/v2/common/cliutils"
76
"github.com/jfrog/jfrog-cli-core/v2/common/commands"
87
"github.com/jfrog/jfrog-cli-core/v2/common/spec"
9-
speccore "github.com/jfrog/jfrog-cli-core/v2/common/spec"
108
coreCommon "github.com/jfrog/jfrog-cli-core/v2/docs/common"
119
"github.com/jfrog/jfrog-cli-core/v2/lifecycle"
1210
coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config"
@@ -26,7 +24,6 @@ import (
2624
"github.com/jfrog/jfrog-client-go/utils"
2725
"github.com/jfrog/jfrog-client-go/utils/errorutils"
2826
"github.com/urfave/cli"
29-
"os"
3027
"strings"
3128
)
3229

@@ -134,56 +131,21 @@ func validateCreateReleaseBundleContext(c *cli.Context) error {
134131
}
135132

136133
func assertValidCreationMethod(c *cli.Context) error {
137-
// Determine the methods provided
138134
methods := []bool{
139-
c.IsSet("spec"),
140-
c.IsSet(cliutils.Builds),
141-
c.IsSet(cliutils.ReleaseBundles),
142-
}
143-
methodCount := coreutils.SumTrueValues(methods)
144-
145-
// Validate that only one creation method is provided
146-
if err := validateSingleCreationMethod(methodCount); err != nil {
147-
return err
148-
}
149-
150-
if err := validateCreationValuesPresence(c, methodCount); err != nil {
151-
return err
152-
}
153-
return nil
154-
}
155-
156-
func validateSingleCreationMethod(methodCount int) error {
157-
if methodCount > 1 {
158-
return errorutils.CheckErrorf(
159-
"exactly one creation source must be supplied: --%s, --%s, or --%s.\n"+
160-
"Opt to use the --%s option as the --%s and --%s are deprecated",
161-
"spec", cliutils.Builds, cliutils.ReleaseBundles,
135+
c.IsSet("spec"), c.IsSet(cliutils.Builds), c.IsSet(cliutils.ReleaseBundles)}
136+
if coreutils.SumTrueValues(methods) > 1 {
137+
return errorutils.CheckErrorf("exactly one creation source must be supplied: --%s, --%s or --%s.\n"+
138+
"Opt to use the --%s option as the --%s and --%s are deprecated",
162139
"spec", cliutils.Builds, cliutils.ReleaseBundles,
163-
)
140+
"spec", cliutils.Builds, cliutils.ReleaseBundles)
164141
}
165-
return nil
166-
}
167-
168-
func validateCreationValuesPresence(c *cli.Context, methodCount int) error {
169-
if methodCount == 0 {
170-
if !areBuildFlagsSet(c) && !areBuildEnvVarsSet() {
171-
return errorutils.CheckErrorf("Either --build-name or JFROG_CLI_BUILD_NAME, and --build-number or JFROG_CLI_BUILD_NUMBER must be defined")
172-
}
142+
// If the user did not provide a source, we suggest only the recommended spec approach.
143+
if coreutils.SumTrueValues(methods) == 0 {
144+
return errorutils.CheckErrorf("the --spec option is mandatory")
173145
}
174146
return nil
175147
}
176148

177-
// areBuildFlagsSet checks if build-name or build-number flags are set.
178-
func areBuildFlagsSet(c *cli.Context) bool {
179-
return c.IsSet(cliutils.BuildName) || c.IsSet(cliutils.BuildNumber)
180-
}
181-
182-
// areBuildEnvVarsSet checks if build environment variables are set.
183-
func areBuildEnvVarsSet() bool {
184-
return os.Getenv("JFROG_CLI_BUILD_NUMBER") != "" && os.Getenv("JFROG_CLI_BUILD_NAME") != ""
185-
}
186-
187149
func create(c *cli.Context) (err error) {
188150
if err = validateCreateReleaseBundleContext(c); err != nil {
189151
return err
@@ -207,34 +169,10 @@ func create(c *cli.Context) (err error) {
207169
}
208170

209171
func getReleaseBundleCreationSpec(c *cli.Context) (*spec.SpecFiles, error) {
210-
// לֹhecking if the "builds" or "release-bundles" flags are set - if so, the spec flag should be ignored
211-
if c.IsSet(cliutils.Builds) || c.IsSet(cliutils.ReleaseBundles) {
212-
return nil, nil
213-
}
214-
215-
// Check if the "spec" flag is set - if so, return the spec
216172
if c.IsSet("spec") {
217173
return cliutils.GetSpec(c, true, false)
218174
}
219-
220-
// Else - create a spec from the buildName and buildnumber flags or env vars
221-
buildName := getStringFlagOrEnv(c, cliutils.BuildName, coreutils.BuildName)
222-
buildNumber := getStringFlagOrEnv(c, cliutils.BuildNumber, coreutils.BuildNumber)
223-
224-
if buildName != "" && buildNumber != "" {
225-
return speccore.CreateSpecFromBuildNameAndNumber(buildName, buildNumber)
226-
}
227-
228-
return nil, fmt.Errorf("either the --spec flag must be provided, " +
229-
"or both --build-name and --build-number flags (or their corresponding environment variables " +
230-
"JFROG_CLI_BUILD_NAME and JFROG_CLI_BUILD_NUMBER) must be set")
231-
}
232-
233-
func getStringFlagOrEnv(c *cli.Context, flag string, envVar string) string {
234-
if c.IsSet(flag) {
235-
return c.String(flag)
236-
}
237-
return os.Getenv(envVar)
175+
return nil, nil
238176
}
239177

240178
func promote(c *cli.Context) error {

lifecycle/cli_test.go

-71
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"github.com/jfrog/jfrog-cli/utils/cliutils"
55
"github.com/jfrog/jfrog-cli/utils/tests"
66
"github.com/stretchr/testify/assert"
7-
"os"
87
"path/filepath"
98
"testing"
109
)
@@ -57,73 +56,3 @@ func TestCreateReleaseBundleSpecWithProject(t *testing.T) {
5756
creationSpec.Get(0).Project = ""
5857
assert.Equal(t, projectKey, cliutils.GetProject(context))
5958
}
60-
61-
func TestGetReleaseBundleCreationSpec(t *testing.T) {
62-
63-
t.Run("Spec Flag Set", func(t *testing.T) {
64-
specFile := filepath.Join("testdata", "specfile.json")
65-
ctx, _ := tests.CreateContext(t, []string{"spec=" + specFile}, []string{})
66-
67-
spec, err := getReleaseBundleCreationSpec(ctx)
68-
69-
assert.NoError(t, err)
70-
assert.NotNil(t, spec)
71-
})
72-
73-
t.Run("Build Name and Number Set via Flags", func(t *testing.T) {
74-
ctx, _ := tests.CreateContext(t, []string{"build-name=Common-builds", "build-number=1.0.0"}, []string{})
75-
76-
spec, err := getReleaseBundleCreationSpec(ctx)
77-
78-
assert.NoError(t, err)
79-
assert.NotNil(t, spec)
80-
assert.Equal(t, "Common-builds/1.0.0", spec.Files[0].Build)
81-
})
82-
83-
t.Run("Build Name and Number Set via Env Variables", func(t *testing.T) {
84-
t.Setenv("JFROG_CLI_BUILD_NAME", "Common-builds")
85-
t.Setenv("JFROG_CLI_BUILD_NUMBER", "2.0.0")
86-
87-
ctx, _ := tests.CreateContext(t, []string{}, []string{})
88-
89-
spec, err := getReleaseBundleCreationSpec(ctx)
90-
91-
assert.NoError(t, err)
92-
assert.NotNil(t, spec)
93-
assert.Equal(t, "Common-builds/2.0.0", spec.Files[0].Build)
94-
os.Unsetenv("JFROG_CLI_BUILD_NAME")
95-
os.Unsetenv("JFROG_CLI_BUILD_NUMBER")
96-
})
97-
98-
t.Run("Missing Build Name and Number", func(t *testing.T) {
99-
ctx, _ := tests.CreateContext(t, []string{}, []string{})
100-
101-
spec, err := getReleaseBundleCreationSpec(ctx)
102-
103-
assert.Error(t, err)
104-
assert.Nil(t, spec)
105-
assert.EqualError(t, err, "either the --spec flag must be provided, or both --build-name and --build-number flags (or their corresponding environment variables JFROG_CLI_BUILD_NAME and JFROG_CLI_BUILD_NUMBER) must be set")
106-
})
107-
108-
t.Run("Only One Build Variable Set", func(t *testing.T) {
109-
ctx, _ := tests.CreateContext(t, []string{"build-name=Common-builds"}, []string{})
110-
111-
spec, err := getReleaseBundleCreationSpec(ctx)
112-
113-
assert.Error(t, err)
114-
assert.Nil(t, spec)
115-
assert.EqualError(t, err, "either the --spec flag must be provided, or both --build-name and --build-number flags (or their corresponding environment variables JFROG_CLI_BUILD_NAME and JFROG_CLI_BUILD_NUMBER) must be set")
116-
})
117-
118-
t.Run("One Env Variable One Flag", func(t *testing.T) {
119-
ctx, _ := tests.CreateContext(t, []string{"build-name=Common-builds"}, []string{})
120-
t.Setenv("JFROG_CLI_BUILD_NUMBER", "2.0.0")
121-
122-
spec, err := getReleaseBundleCreationSpec(ctx)
123-
124-
assert.NoError(t, err)
125-
assert.NotNil(t, spec)
126-
assert.Equal(t, "Common-builds/2.0.0", spec.Files[0].Build)
127-
os.Unsetenv("JFROG_CLI_BUILD_NUMBER")
128-
})
129-
}

lifecycle_test.go

+6-33
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ func TestLifecycleFullFlow(t *testing.T) {
167167
// Verify the artifacts were distributed correctly by the provided path mappings.
168168
assertExpectedArtifacts(t, tests.SearchAllDevRepo, tests.GetExpectedLifecycleDistributedArtifacts())
169169
*/
170+
170171
}
171172

172173
// Import bundles only work on onPerm platforms
@@ -205,58 +206,30 @@ func uploadBuilds(t *testing.T) func() {
205206
func createRbBackwardCompatible(t *testing.T, specName, sourceOption, rbName, rbVersion string, sync bool) {
206207
specFile, err := getSpecFile(specName)
207208
assert.NoError(t, err)
208-
createRbWithFlags(t, specFile, sourceOption, "", "", rbName, rbVersion, sync, false)
209+
createRb(t, specFile, sourceOption, rbName, rbVersion, sync, false)
209210
}
210211

211212
func createRbFromSpec(t *testing.T, specName, rbName, rbVersion string, sync bool, withoutSigningKey bool) {
212213
specFile, err := tests.CreateSpec(specName)
213214
assert.NoError(t, err)
214-
createRbWithFlags(t, specFile, "spec", "", "", rbName, rbVersion, sync, withoutSigningKey)
215+
createRb(t, specFile, "spec", rbName, rbVersion, sync, withoutSigningKey)
215216
}
216217

217-
func TestCreateBundleWithoutSpec(t *testing.T) {
218-
cleanCallback := initLifecycleTest(t, signingKeyOptionalArtifactoryMinVersion)
219-
defer cleanCallback()
220-
221-
lcManager := getLcServiceManager(t)
222-
223-
deleteBuilds := uploadBuilds(t)
224-
defer deleteBuilds()
225-
226-
createRbWithFlags(t, "", "", tests.LcBuildName1, number1, tests.LcRbName1, number1, false, false)
227-
assertStatusCompleted(t, lcManager, tests.LcRbName1, number1, "")
228-
defer deleteReleaseBundle(t, lcManager, tests.LcRbName1, number1)
229-
230-
createRbWithFlags(t, "", "", tests.LcBuildName2, number2, tests.LcRbName2, number2, false, true)
231-
assertStatusCompleted(t, lcManager, tests.LcRbName2, number2, "")
232-
defer deleteReleaseBundle(t, lcManager, tests.LcRbName2, number2)
233-
}
234-
235-
func createRbWithFlags(t *testing.T, specFilePath, sourceOption, buildName, buildNumber, rbName, rbVersion string,
236-
sync, withoutSigningKey bool) {
218+
func createRb(t *testing.T, specFilePath, sourceOption, rbName, rbVersion string, sync bool, withoutSigningKey bool) {
237219
argsAndOptions := []string{
238220
"rbc",
239221
rbName,
240222
rbVersion,
241-
}
242-
243-
if specFilePath != "" {
244-
argsAndOptions = append(argsAndOptions, getOption(sourceOption, specFilePath))
245-
}
246-
247-
if buildName != "" && buildNumber != "" {
248-
argsAndOptions = append(argsAndOptions, getOption(cliutils.BuildName, buildName))
249-
argsAndOptions = append(argsAndOptions, getOption(cliutils.BuildNumber, buildNumber))
223+
getOption(sourceOption, specFilePath),
250224
}
251225

252226
if !withoutSigningKey {
253227
argsAndOptions = append(argsAndOptions, getOption(cliutils.SigningKey, gpgKeyPairName))
254228
}
255-
229+
// Add the --sync option only if requested, to test the default value.
256230
if sync {
257231
argsAndOptions = append(argsAndOptions, getOption(cliutils.Sync, "true"))
258232
}
259-
260233
assert.NoError(t, lcCli.Exec(argsAndOptions...))
261234
}
262235

0 commit comments

Comments
 (0)