Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Artifactory Release Lifecycle Management - Support release bundle creation by aql and artifacts #1142

Merged
merged 7 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 8 additions & 20 deletions artifactory/commands/generic/search.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generic

import (
"errors"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
clientartutils "github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
Expand All @@ -26,7 +27,7 @@ func (sc *SearchCommand) Run() error {
return err
}

func (sc *SearchCommand) Search() (contentReader *content.ContentReader, err error) {
func (sc *SearchCommand) Search() (*content.ContentReader, error) {
// Service Manager
serverDetails, err := sc.ServerDetails()
if errorutils.CheckError(err) != nil {
Expand All @@ -38,28 +39,15 @@ func (sc *SearchCommand) Search() (contentReader *content.ContentReader, err err
}
// Search Loop
log.Info("Searching artifacts...")
var searchResults []*content.ContentReader

searchResults, callbackFunc, err := utils.SearchFiles(servicesManager, sc.Spec())
defer func() {
for _, reader := range searchResults {
e := reader.Close()
if err == nil {
err = e
}
}
err = errors.Join(err, callbackFunc())
}()
for i := 0; i < len(sc.Spec().Files); i++ {
searchParams, err := utils.GetSearchParams(sc.Spec().Get(i))
if err != nil {
log.Error(err)
return nil, err
}
reader, err := servicesManager.SearchFiles(searchParams)
if err != nil {
log.Error(err)
return nil, err
}
searchResults = append(searchResults, reader)
if err != nil {
return nil, err
}

reader, err := utils.AqlResultToSearchResult(searchResults)
if err != nil {
return nil, err
Expand Down
27 changes: 27 additions & 0 deletions artifactory/utils/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package utils

import (
"encoding/json"
"errors"
"github.com/jfrog/jfrog-client-go/artifactory"

"github.com/jfrog/jfrog-cli-core/v2/common/spec"
"github.com/jfrog/jfrog-client-go/artifactory/services"
Expand Down Expand Up @@ -170,3 +172,28 @@ func SearchResultNoDate(reader *content.ContentReader) (contentReader *content.C
contentReader = content.NewContentReader(writer.GetFilePath(), writer.GetArrayKey())
return
}

func SearchFiles(servicesManager artifactory.ArtifactoryServicesManager, spec *spec.SpecFiles) (searchResults []*content.ContentReader, callbackFunc func() error, err error) {
callbackFunc = func() error {
var errs error
for _, reader := range searchResults {
errs = errors.Join(errs, reader.Close())
}
return errs
}

var curSearchParams services.SearchParams
var curReader *content.ContentReader
for i := 0; i < len(spec.Files); i++ {
curSearchParams, err = GetSearchParams(spec.Get(i))
if err != nil {
return
}
curReader, err = servicesManager.SearchFiles(curSearchParams)
if err != nil {
return
}
searchResults = append(searchResults, curReader)
}
return
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ require (
gopkg.in/warnings.v0 v0.1.2 // indirect
)

replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20240318065424-90669dbbcc54
replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20240318111807-75c3311549ab

replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go v1.8.9-0.20240225113943-096bf22ca54c

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ github.com/jfrog/build-info-go v1.8.9-0.20240225113943-096bf22ca54c h1:M1QiuCYGC
github.com/jfrog/build-info-go v1.8.9-0.20240225113943-096bf22ca54c/go.mod h1:QHcKuesY4MrBVBuEwwBz4uIsX6mwYuMEDV09ng4AvAU=
github.com/jfrog/gofrog v1.6.3 h1:F7He0+75HcgCe6SGTSHLFCBDxiE2Ja0tekvvcktW6wc=
github.com/jfrog/gofrog v1.6.3/go.mod h1:SZ1EPJUruxrVGndOzHd+LTiwWYKMlHqhKD+eu+v5Hqg=
github.com/jfrog/jfrog-client-go v1.28.1-0.20240318065424-90669dbbcc54 h1:FTrss/ffJPjTHOOhQ8P+8DrkGYkxcaHlxp12nOfeZJQ=
github.com/jfrog/jfrog-client-go v1.28.1-0.20240318065424-90669dbbcc54/go.mod h1:NB8tYFgkWtn+wHsKC+aYC75aLnS6yW81d8JAFTBxsi0=
github.com/jfrog/jfrog-client-go v1.28.1-0.20240318111807-75c3311549ab h1:A5VTYC5x9swhypyJt2j47WBRPiJjn+0mc7DMgcmdiSE=
github.com/jfrog/jfrog-client-go v1.28.1-0.20240318111807-75c3311549ab/go.mod h1:NB8tYFgkWtn+wHsKC+aYC75aLnS6yW81d8JAFTBxsi0=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
Expand Down
179 changes: 176 additions & 3 deletions lifecycle/createcommon.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
package lifecycle

import (
"github.com/jfrog/jfrog-cli-core/v2/common/spec"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-client-go/lifecycle/services"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
)

const (
missingCreationSourcesErrMsg = "unexpected err while validating spec - could not detect any creation sources"
multipleCreationSourcesErrMsg = "multiple creation sources were detected in separate spec files. Only a single creation source should be provided. Detected:"
singleAqlErrMsg = "only a single aql query can be provided"
)

type ReleaseBundleCreateCommand struct {
releaseBundleCmd
signingKeyName string
signingKeyName string
spec *spec.SpecFiles
// Backward compatibility:
buildsSpecPath string
releaseBundlesSpecPath string
}
Expand Down Expand Up @@ -45,11 +57,18 @@ func (rbc *ReleaseBundleCreateCommand) SetReleaseBundleProject(rbProjectKey stri
return rbc
}

func (rbc *ReleaseBundleCreateCommand) SetSpec(spec *spec.SpecFiles) *ReleaseBundleCreateCommand {
rbc.spec = spec
return rbc
}

// Deprecated
func (rbc *ReleaseBundleCreateCommand) SetBuildsSpecPath(buildsSpecPath string) *ReleaseBundleCreateCommand {
rbc.buildsSpecPath = buildsSpecPath
return rbc
}

// Deprecated
func (rbc *ReleaseBundleCreateCommand) SetReleaseBundlesSpecPath(releaseBundlesSpecPath string) *ReleaseBundleCreateCommand {
rbc.releaseBundlesSpecPath = releaseBundlesSpecPath
return rbc
Expand All @@ -73,8 +92,162 @@ func (rbc *ReleaseBundleCreateCommand) Run() error {
return err
}

if rbc.buildsSpecPath != "" {
sourceType, err := rbc.identifySourceType()
if err != nil {
return err
}

switch sourceType {
case services.Aql:
return rbc.createFromAql(servicesManager, rbDetails, queryParams)
case services.Artifacts:
return rbc.createFromArtifacts(servicesManager, rbDetails, queryParams)
case services.Builds:
return rbc.createFromBuilds(servicesManager, rbDetails, queryParams)
case services.ReleaseBundles:
return rbc.createFromReleaseBundles(servicesManager, rbDetails, queryParams)
default:
return errorutils.CheckErrorf("unknown source for release bundle creation was provided")
}
}

func (rbc *ReleaseBundleCreateCommand) identifySourceType() (services.SourceType, error) {
switch {
case rbc.buildsSpecPath != "":
return services.Builds, nil
case rbc.releaseBundlesSpecPath != "":
return services.ReleaseBundles, nil
case rbc.spec != nil:
return validateAndIdentifyRbCreationSpec(rbc.spec.Files)
default:
return "", errorutils.CheckErrorf("a spec file input is mandatory")
}
}

func validateAndIdentifyRbCreationSpec(files []spec.File) (services.SourceType, error) {
if len(files) == 0 {
return "", errorutils.CheckErrorf("spec must include at least one file group")
}

var detectedCreationSources []services.SourceType
for _, file := range files {
sourceType, err := validateFile(file)
if err != nil {
return "", err
}
detectedCreationSources = append(detectedCreationSources, sourceType)
}

if err := validateCreationSources(detectedCreationSources); err != nil {
return "", err
}
return detectedCreationSources[0], nil
}

func validateCreationSources(detectedCreationSources []services.SourceType) error {
RobiNino marked this conversation as resolved.
Show resolved Hide resolved
if len(detectedCreationSources) == 0 {
return errorutils.CheckErrorf(missingCreationSourcesErrMsg)
}

// Assert single creation source.
for i := 1; i < len(detectedCreationSources); i++ {
if detectedCreationSources[i] != detectedCreationSources[0] {
return generateSingleCreationSourceErr(detectedCreationSources)
}
}

// If aql, assert single file.
if detectedCreationSources[0] == services.Aql && len(detectedCreationSources) > 1 {
return errorutils.CheckErrorf(singleAqlErrMsg)
}
return nil
}

func generateSingleCreationSourceErr(detectedCreationSources []services.SourceType) error {
var detectedStr []string
for _, source := range detectedCreationSources {
detectedStr = append(detectedStr, string(source))
}
return errorutils.CheckErrorf(
"%s '%s'", multipleCreationSourcesErrMsg, coreutils.ListToText(detectedStr))
}

func validateFile(file spec.File) (services.SourceType, error) {
RobiNino marked this conversation as resolved.
Show resolved Hide resolved
// Aql creation source:
isAql := len(file.Aql.ItemsFind) > 0

// Build creation source:
isBuild := len(file.Build) > 0
isIncludeDeps, _ := file.IsIncludeDeps(false)

// Bundle creation source:
isBundle := len(file.Bundle) > 0

// Build & bundle:
isProject := len(file.Project) > 0

// Artifacts creation source:
isPattern := len(file.Pattern) > 0
isExclusions := len(file.Exclusions) > 0 && len(file.Exclusions[0]) > 0
isProps := len(file.Props) > 0
isExcludeProps := len(file.ExcludeProps) > 0
isRecursive, err := file.IsRecursive(true)
if err != nil {
return "", errorutils.CheckErrorf("invalid value provided to the 'recursive' field. error: %s", err.Error())
}

// Unsupported:
isPathMapping := len(file.PathMapping.Input) > 0 || len(file.PathMapping.Output) > 0
isTarget := len(file.Target) > 0
isSortOrder := len(file.SortOrder) > 0
isSortBy := len(file.SortBy) > 0
isExcludeArtifacts, _ := file.IsExcludeArtifacts(false)
isGPGKey := len(file.PublicGpgKey) > 0
isOffset := file.Offset > 0
isLimit := file.Limit > 0
isArchive := len(file.Archive) > 0
isSymlinks, _ := file.IsSymlinks(false)
isRegexp := file.Regexp == "true"
isAnt := file.Ant == "true"
isExplode, _ := file.IsExplode(false)
isBypassArchiveInspection, _ := file.IsBypassArchiveInspection(false)
isTransitive, _ := file.IsTransitive(false)

if isPathMapping || isTarget || isSortOrder || isSortBy || isExcludeArtifacts || isGPGKey || isOffset || isLimit ||
isSymlinks || isArchive || isAnt || isRegexp || isExplode || isBypassArchiveInspection || isTransitive {
return "", errorutils.CheckErrorf("unsupported fields were provided in file spec. " +
"release bundle creation file spec only supports the following fields: " +
"'aql', 'build', 'includeDeps', 'bundle', 'project', 'pattern', 'exclusions', 'props', 'excludeProps' and 'recursive'")
}
if coreutils.SumTrueValues([]bool{isAql, isBuild, isBundle, isPattern}) != 1 {
return "", errorutils.CheckErrorf("exactly one creation source is supported (aql, builds, release bundles or pattern (artifacts))")
}

switch {
case isAql:
return services.Aql,
validateCreationSource([]bool{isIncludeDeps, isProject, isExclusions, isProps, isExcludeProps, !isRecursive},
"aql creation source supports no other fields")
case isBuild:
return services.Builds,
validateCreationSource([]bool{isExclusions, isProps, isExcludeProps, !isRecursive},
"builds creation source only supports the 'includeDeps' and 'project' fields")
case isBundle:
return services.ReleaseBundles,
validateCreationSource([]bool{isIncludeDeps, isExclusions, isProps, isExcludeProps, !isRecursive},
"release bundles creation source only supports the 'project' field")
case isPattern:
return services.Artifacts,
validateCreationSource([]bool{isIncludeDeps, isProject},
"release bundles creation source only supports the 'exclusions', 'props', 'excludeProps' and 'recursive' fields")
default:
return "", errorutils.CheckErrorf("unexpected err in spec validation")
}
}

func validateCreationSource(unsupportedFields []bool, errMsg string) error {
if coreutils.SumTrueValues(unsupportedFields) > 0 {
return errorutils.CheckErrorf(errMsg)
}
return rbc.createFromReleaseBundles(servicesManager, rbDetails, queryParams)
return nil
}
76 changes: 76 additions & 0 deletions lifecycle/createcommon_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package lifecycle

import (
"github.com/jfrog/jfrog-cli-core/v2/common/spec"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/lifecycle/services"
"github.com/stretchr/testify/assert"
"testing"
)

func TestValidateCreationSources(t *testing.T) {
testCases := []struct {
testName string
detectedCreationSources []services.SourceType
errExpected bool
errMsg string
}{
{"missing creation sources", []services.SourceType{}, true, missingCreationSourcesErrMsg},
{"single creation source", []services.SourceType{services.Aql, services.Artifacts, services.Builds},
true, multipleCreationSourcesErrMsg + " 'aql, artifacts and builds'"},
{"single aql err", []services.SourceType{services.Aql, services.Aql}, true, singleAqlErrMsg},
{"valid aql", []services.SourceType{services.Aql}, false, ""},
{"valid artifacts", []services.SourceType{services.Artifacts, services.Artifacts}, false, ""},
{"valid builds", []services.SourceType{services.Builds, services.Builds}, false, ""},
{"valid release bundles", []services.SourceType{services.ReleaseBundles, services.ReleaseBundles}, false, ""},
}
for _, testCase := range testCases {
t.Run(testCase.testName, func(t *testing.T) {
err := validateCreationSources(testCase.detectedCreationSources)
if testCase.errExpected {
assert.EqualError(t, err, testCase.errMsg)
} else {
assert.NoError(t, err)
}
})
}
}

func TestValidateFile(t *testing.T) {
testCases := []struct {
testName string
file spec.File
errExpected bool
expectedSourceType services.SourceType
}{
{"valid aql", spec.File{Aql: utils.Aql{ItemsFind: "abc"}}, false, services.Aql},
{"valid build", spec.File{Build: "name/number", IncludeDeps: "true", Project: "project"}, false, services.Builds},
{"valid bundle", spec.File{Bundle: "name/number", Project: "project"}, false, services.ReleaseBundles},
{"valid artifacts",
spec.File{
Pattern: "repo/path/file",
Exclusions: []string{"exclude"},
Props: "prop",
ExcludeProps: "exclude prop",
Recursive: "false"}, false, services.Artifacts},
{"invalid fields", spec.File{PathMapping: utils.PathMapping{Input: "input"}, Target: "target"}, true, ""},
{"multiple creation sources",
spec.File{Aql: utils.Aql{ItemsFind: "abc"}, Build: "name/number", Bundle: "name/number", Pattern: "repo/path/file"},
true, ""},
{"invalid aql", spec.File{Aql: utils.Aql{ItemsFind: "abc"}, Props: "prop"}, true, ""},
{"invalid builds", spec.File{Build: "name/number", Recursive: "false"}, true, ""},
{"invalid bundles", spec.File{Bundle: "name/number", IncludeDeps: "true"}, true, ""},
{"invalid artifacts", spec.File{Pattern: "repo/path/file", Project: "proj"}, true, ""},
}
for _, testCase := range testCases {
t.Run(testCase.testName, func(t *testing.T) {
sourceType, err := validateFile(testCase.file)
if testCase.errExpected {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, testCase.expectedSourceType, sourceType)
}
})
}
}
Loading
Loading