Skip to content

Commit

Permalink
artifactory : add support for exclude-props in aql build (#75)
Browse files Browse the repository at this point in the history
* artifactory : add support for exclude-props in aql build

* CR fixes
  • Loading branch information
liron-shalom authored and eyalbe4 committed Aug 14, 2019
1 parent 8ae8b68 commit bc1cc34
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
40 changes: 40 additions & 0 deletions artifactory/services/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/log"
"net/http"
"strings"
)

type DeleteService struct {
Expand Down Expand Up @@ -54,6 +55,11 @@ func (ds *DeleteService) GetPathsToDelete(deleteParams DeleteParams) (resultItem
err = e
return
}
tempResultItems, e = removeNotToBeDeletedDirs(*deleteParams.GetFile(), ds, tempResultItems)
if e != nil {
err = e
return
}
paths := utils.ReduceDirResult(tempResultItems, utils.FilterTopChainResults)
resultItems = append(resultItems, paths...)
}
Expand Down Expand Up @@ -122,3 +128,37 @@ func (ds *DeleteParams) SetIncludeDirs(includeDirs bool) {
func NewDeleteParams() DeleteParams {
return DeleteParams{ArtifactoryCommonParams: &utils.ArtifactoryCommonParams{}}
}

// This function receives as an argument the list of files and folders to be deleted from Artifactory.
// In case the search params used to create this list included excludeProps, we might need to remove some directories from this list.
// These directories must be removed, because they include files, which should not be deleted, because of the excludeProps params.
// hese directories must not be deleted from Artifactory.
func removeNotToBeDeletedDirs(specFile utils.ArtifactoryCommonParams, ds *DeleteService, deleteCandidates []utils.ResultItem) ([]utils.ResultItem, error) {
if specFile.ExcludeProps == "" {
return deleteCandidates, nil
}
specFile.Props = specFile.ExcludeProps
specFile.ExcludeProps = ""
remainArtifacts, err := utils.SearchBySpecWithPattern(&specFile, ds, utils.NONE)
if err != nil {
return nil, err
}
var result []utils.ResultItem
for _, candidate := range deleteCandidates {
deleteCandidate := true
if candidate.Type == "folder" {
candidatePath := candidate.GetItemRelativePath()
for _, artifact := range remainArtifacts {
artifactPath := artifact.GetItemRelativePath()
if strings.HasPrefix(artifactPath, candidatePath) {
deleteCandidate = false
break
}
}
}
if deleteCandidate {
result = append(result, candidate)
}
}
return result, nil
}
2 changes: 1 addition & 1 deletion artifactory/services/gitlfsclean.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func getRefsRegex(refs string) string {
}

func (glc *GitLfsCleanService) searchLfsFilesInArtifactory(repo string) ([]utils.ResultItem, error) {
spec := &utils.ArtifactoryCommonParams{Pattern: repo, Target: "", Props: "", Build: "", Recursive: true, Regexp: false, IncludeDirs: false}
spec := &utils.ArtifactoryCommonParams{Pattern: repo, Target: "", Props: "", ExcludeProps: "", Build: "", Recursive: true, Regexp: false, IncludeDirs: false}
return utils.SearchBySpecWithPattern(spec, glc, utils.NONE)
}

Expand Down
34 changes: 26 additions & 8 deletions artifactory/services/utils/aqlquerybuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func createAqlBodyForSpecWithPattern(params *ArtifactoryCommonParams) (string, e
includeRoot := strings.Count(searchPattern, "/") < 2
pathPairsSize := len(pathFilePairs)

propsQueryPart, err := buildPropsQueryPart(params.Props)
propsQueryPart, err := buildPropsQueryPart(params.Props, params.ExcludeProps)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -111,25 +111,43 @@ func prepareSearchPattern(pattern string, repositoryExists bool) string {
return pattern
}

func buildPropsQueryPart(props string) (string, error) {
if props == "" {
return "", nil
}
func buildPropsQueryPart(props, excludeProps string) (string, error) {
propsQuery := ""
properties, err := ParseProperties(props, JoinCommas)
if err != nil {
return "", err
}
query := ""
for _, v := range properties.Properties {
query += buildKeyValQueryPart(v.Key, v.Value) + `,`
propsQuery += buildKeyValQueryPart(v.Key, v.Value) + `,`
}

excludePropsQuery := ""
excludeProperties, err := ParseProperties(excludeProps, JoinCommas)
if err != nil {
return "", err
}
return query, nil
excludePropsLen := len(excludeProperties.Properties)
if excludePropsLen == 1 {
singleProp := &excludeProperties.Properties[0]
excludePropsQuery = buildExcludedKeyValQueryPart(singleProp.Key, singleProp.Value) + `,`
} else if excludePropsLen > 1 {
excludePropsQuery = `"$or":[`
for _, v := range excludeProperties.Properties {
excludePropsQuery += `{` + buildExcludedKeyValQueryPart(v.Key, v.Value) + `},`
}
excludePropsQuery = strings.TrimSuffix(excludePropsQuery, ",") + `],`
}
return propsQuery + excludePropsQuery, nil
}

func buildKeyValQueryPart(key string, value string) string {
return fmt.Sprintf(`"@%s":%s`, key, getAqlValue(value))
}

func buildExcludedKeyValQueryPart(key string, value string) string {
return fmt.Sprintf(`"@%s":{"$ne":%s}`, key, getAqlValue(value))
}

func buildItemTypeQueryPart(params *ArtifactoryCommonParams) string {
if params.IncludeDirs {
return `"type":"any",`
Expand Down
9 changes: 9 additions & 0 deletions artifactory/services/utils/specutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ArtifactoryCommonParams struct {
ExcludePatterns []string
Target string
Props string
ExcludeProps string
SortOrder string
SortBy []string
Offset int
Expand Down Expand Up @@ -82,6 +83,10 @@ func (params *ArtifactoryCommonParams) GetProps() string {
return params.Props
}

func (params *ArtifactoryCommonParams) GetExcludeProps() string {
return params.ExcludeProps
}

func (params *ArtifactoryCommonParams) IsExplode() bool {
return params.Recursive
}
Expand Down Expand Up @@ -110,6 +115,10 @@ func (params *ArtifactoryCommonParams) SetProps(props string) {
params.Props = props
}

func (params *ArtifactoryCommonParams) SetExcludeProps(excludeProps string) {
params.ExcludeProps = excludeProps
}

func (params *ArtifactoryCommonParams) GetSortBy() []string {
return params.SortBy
}
Expand Down

0 comments on commit bc1cc34

Please sign in to comment.