Skip to content

Commit

Permalink
Fix release bundle creation with project
Browse files Browse the repository at this point in the history
RobiNino committed May 1, 2024
1 parent b6c104e commit 13e417c
Showing 6 changed files with 56 additions and 19 deletions.
10 changes: 5 additions & 5 deletions artifactory/cli.go
Original file line number Diff line number Diff line change
@@ -1186,7 +1186,7 @@ func prepareDownloadCommand(c *cli.Context) (*spec.SpecFiles, error) {
var downloadSpec *spec.SpecFiles
var err error
if c.IsSet("spec") {
downloadSpec, err = cliutils.GetSpec(c, true)
downloadSpec, err = cliutils.GetSpec(c, true, true)
} else {
downloadSpec, err = createDefaultDownloadSpec(c)
}
@@ -1315,7 +1315,7 @@ func prepareCopyMoveCommand(c *cli.Context) (*spec.SpecFiles, error) {
var copyMoveSpec *spec.SpecFiles
var err error
if c.IsSet("spec") {
copyMoveSpec, err = cliutils.GetSpec(c, false)
copyMoveSpec, err = cliutils.GetSpec(c, false, true)
} else {
copyMoveSpec, err = createDefaultCopyMoveSpec(c)
}
@@ -1403,7 +1403,7 @@ func prepareDeleteCommand(c *cli.Context) (*spec.SpecFiles, error) {
var deleteSpec *spec.SpecFiles
var err error
if c.IsSet("spec") {
deleteSpec, err = cliutils.GetSpec(c, false)
deleteSpec, err = cliutils.GetSpec(c, false, true)
} else {
deleteSpec, err = createDefaultDeleteSpec(c)
}
@@ -1458,7 +1458,7 @@ func prepareSearchCommand(c *cli.Context) (*spec.SpecFiles, error) {
var searchSpec *spec.SpecFiles
var err error
if c.IsSet("spec") {
searchSpec, err = cliutils.GetSpec(c, false)
searchSpec, err = cliutils.GetSpec(c, false, true)
} else {
searchSpec, err = createDefaultSearchSpec(c)
}
@@ -1525,7 +1525,7 @@ func preparePropsCmd(c *cli.Context) (*generic.PropsCommand, error) {
var props string
if c.IsSet("spec") {
props = c.Args()[0]
propsSpec, err = cliutils.GetSpec(c, false)
propsSpec, err = cliutils.GetSpec(c, false, true)
} else {
propsSpec, err = createDefaultPropertiesSpec(c)
if c.NArg() == 1 {
4 changes: 2 additions & 2 deletions distribution/cli.go
Original file line number Diff line number Diff line change
@@ -95,7 +95,7 @@ func releaseBundleCreateCmd(c *cli.Context) error {
var releaseBundleCreateSpec *spec.SpecFiles
var err error
if c.IsSet("spec") {
releaseBundleCreateSpec, err = cliutils.GetSpec(c, true)
releaseBundleCreateSpec, err = cliutils.GetSpec(c, true, true)
} else {
releaseBundleCreateSpec = createDefaultReleaseBundleSpec(c)
}
@@ -137,7 +137,7 @@ func releaseBundleUpdateCmd(c *cli.Context) error {
var releaseBundleUpdateSpec *spec.SpecFiles
var err error
if c.IsSet("spec") {
releaseBundleUpdateSpec, err = cliutils.GetSpec(c, true)
releaseBundleUpdateSpec, err = cliutils.GetSpec(c, true, true)
} else {
releaseBundleUpdateSpec = createDefaultReleaseBundleSpec(c)
}
16 changes: 10 additions & 6 deletions lifecycle/cli.go
Original file line number Diff line number Diff line change
@@ -155,12 +155,9 @@ func create(c *cli.Context) (err error) {
return err
}

var creationSpec *spec.SpecFiles
if c.IsSet("spec") {
creationSpec, err = cliutils.GetSpec(c, true)
if err != nil {
return
}
creationSpec, err := getReleaseBundleCreationSpec(c)
if err != nil {
return
}

lcDetails, err := createLifecycleDetailsByFlags(c)
@@ -175,6 +172,13 @@ func create(c *cli.Context) (err error) {
return commands.Exec(createCmd)
}

func getReleaseBundleCreationSpec(c *cli.Context) (*spec.SpecFiles, error) {
if c.IsSet("spec") {
return cliutils.GetSpec(c, true, false)
}
return nil, nil
}

func promote(c *cli.Context) error {
if show, err := cliutils.ShowCmdHelpIfNeeded(c, c.Args()); show || err != nil {
return err
13 changes: 13 additions & 0 deletions lifecycle/cli_test.go
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import (
"github.com/jfrog/jfrog-cli/utils/cliutils"
"github.com/jfrog/jfrog-cli/utils/tests"
"github.com/stretchr/testify/assert"
"path/filepath"
"testing"
)

@@ -43,3 +44,15 @@ func TestValidateCreateReleaseBundleContext(t *testing.T) {
})
}
}

// Validates that the project option does not override the project field in the spec file.
func TestCreateReleaseBundleSpecWithProject(t *testing.T) {
projectKey := "myproj"
specFile := filepath.Join("testdata", "specfile.json")
context, _ := tests.CreateContext(t, []string{"spec=" + specFile, "project=" + projectKey}, []string{})
creationSpec, err := getReleaseBundleCreationSpec(context)
assert.NoError(t, err)
assert.Equal(t, creationSpec.Get(0).Pattern, "path/to/file")
creationSpec.Get(0).Project = ""
assert.Equal(t, projectKey, cliutils.GetProject(context))
}
7 changes: 7 additions & 0 deletions lifecycle/testdata/specfile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"files": [
{
"pattern": "path/to/file"
}
]
}
25 changes: 19 additions & 6 deletions utils/cliutils/utils.go
Original file line number Diff line number Diff line change
@@ -401,19 +401,32 @@ func handleSecretInput(c *cli.Context, stringFlag, stdinFlag string) (secret str
return commonCliUtils.HandleSecretInput(stringFlag, c.String(stringFlag), stdinFlag, c.Bool(stdinFlag))
}

func GetSpec(c *cli.Context, isDownload bool) (specFiles *speccore.SpecFiles, err error) {
func GetSpec(c *cli.Context, isDownload, overrideFieldsIfSet bool) (specFiles *speccore.SpecFiles, err error) {
specFiles, err = speccore.CreateSpecFromFile(c.String("spec"), coreutils.SpecVarsStringToMap(c.String("spec-vars")))
if err != nil {
return nil, err
}
// Override spec with CLI options
trimPatternPrefixIfDownload(specFiles, isDownload)
overrideSpecFieldsIfNeeded(c, specFiles, overrideFieldsIfSet)
return
}

func overrideSpecFieldsIfNeeded(c *cli.Context, specFiles *speccore.SpecFiles, overrideFieldsIfSet bool) {
if !overrideFieldsIfSet {
return
}
for i := 0; i < len(specFiles.Files); i++ {
if isDownload {
specFiles.Get(i).Pattern = strings.TrimPrefix(specFiles.Get(i).Pattern, "/")
}
OverrideFieldsIfSet(specFiles.Get(i), c)
}
return
}

func trimPatternPrefixIfDownload(specFiles *speccore.SpecFiles, isDownload bool) {
if !isDownload {
return
}
for i := 0; i < len(specFiles.Files); i++ {
specFiles.Get(i).Pattern = strings.TrimPrefix(specFiles.Get(i).Pattern, "/")
}
}

func GetFileSystemSpec(c *cli.Context) (fsSpec *speccore.SpecFiles, err error) {

0 comments on commit 13e417c

Please sign in to comment.