Skip to content
Merged
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
94 changes: 35 additions & 59 deletions artifactory/services/props.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,58 +106,49 @@ func (ps *PropsService) getEncodedParam(propsParams PropsParams, isDelete bool)
return encodedParam, nil
}

func (ps *PropsService) addOrDeletePropertiesForRepo(propsParams PropsParams, isDelete bool, encodedParam string) (int, error) {
var action func(string, string, string) (*http.Response, []byte, error)
func (ps *PropsService) actionTypeBasedOnIsDeleteFlag(isDelete bool, action *func(string, string, string) (*http.Response, []byte, error)) {
if isDelete {
action = ps.sendDeleteRequest
*action = ps.sendDeleteRequest
} else {
action = ps.sendPutRequest
*action = ps.sendPutRequest
}
successCounters := make([]int, ps.GetThreads())
producerConsumer := parallel.NewBounedRunner(ps.GetThreads(), false)
errorsQueue := clientutils.NewErrorsQueue(1)
}

func (ps *PropsService) addOrDeletePropertiesForRepo(propsParams PropsParams, isDelete bool, encodedParam string) (int, error) {
// Determine which action to perform (PUT or DELETE).
var action func(string, string, string) (*http.Response, []byte, error)
ps.actionTypeBasedOnIsDeleteFlag(isDelete, &action)

reader := propsParams.GetReader()
defer reader.Reset()
resultItem := new(utils.ResultItem)
if reader.NextRecord(resultItem) == nil {
repoName := resultItem.Repo
setPropsTask := func(threadId int) error {
var err error
logMsgPrefix := clientutils.GetLogMsgPrefix(threadId, ps.IsDryRun())

restAPI := path.Join("api", "storage", repoName)
setPropertiesURL, err := clientutils.BuildUrl(ps.GetArtifactoryDetails().GetUrl(), restAPI, make(map[string]string))
if err != nil {
return err
}
// Because we do set/delete props on search results that took into account the
// recursive flag, we do not want the action itself to be recursive.
setPropertiesURL += "?properties=" + encodedParam + "&recursive=0"
resp, body, err := action(logMsgPrefix, repoName, setPropertiesURL)
logMsgPrefix := clientutils.GetLogMsgPrefix(0, ps.IsDryRun())

if err != nil {
return err
}
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusNoContent); err != nil {
return err
}
successCounters[threadId]++
return nil
storageAPI := path.Join("api", "storage", repoName)
setPropertiesURL, err := clientutils.BuildUrl(ps.GetArtifactoryDetails().GetUrl(), storageAPI, make(map[string]string))
if err != nil {
return 0, err
}
setPropertiesURL += "?properties=" + encodedParam + "&recursive=0"

resp, body, err := action(logMsgPrefix, repoName, setPropertiesURL)
if err != nil {
return 0, err
}
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusNoContent); err != nil {
return 0, err
}

_, _ = producerConsumer.AddTaskWithError(setPropsTask, errorsQueue.AddError)
return 1, nil
}

defer producerConsumer.Done()
if err := reader.GetError(); err != nil {
errorsQueue.AddError(err)
}
reader.Reset()
producerConsumer.Run()
totalSuccess := 0
for _, v := range successCounters {
totalSuccess += v
return 0, err
}
return totalSuccess, errorsQueue.GetError()

return 0, nil
}

func (ps *PropsService) performRequestForRepoOnly(propsParams PropsParams, isDelete bool) (int, error) {
Expand All @@ -170,27 +161,12 @@ func (ps *PropsService) performRequestForRepoOnly(propsParams PropsParams, isDel
}

func (ps *PropsService) performRequest(propsParams PropsParams, isDelete bool) (int, error) {
var encodedParam string
if !isDelete {
props, err := utils.ParseProperties(propsParams.GetProps())
if err != nil {
return 0, err
}
encodedParam = props.ToEncodedString(true)
} else {
propList := strings.Split(propsParams.GetProps(), ",")
for _, prop := range propList {
encodedParam += url.QueryEscape(prop) + ","
}
// Remove trailing comma
encodedParam = strings.TrimSuffix(encodedParam, ",")
encodedParam, err := ps.getEncodedParam(propsParams, isDelete)
if err != nil {
return 0, err
}
var action func(string, string, string) (*http.Response, []byte, error)
if isDelete {
action = ps.sendDeleteRequest
} else {
action = ps.sendPutRequest
}
ps.actionTypeBasedOnIsDeleteFlag(isDelete, &action)
successCounters := make([]int, ps.GetThreads())
producerConsumer := parallel.NewBounedRunner(ps.GetThreads(), false)
errorsQueue := clientutils.NewErrorsQueue(1)
Expand All @@ -202,8 +178,8 @@ func (ps *PropsService) performRequest(propsParams PropsParams, isDelete bool) (
var err error
logMsgPrefix := clientutils.GetLogMsgPrefix(threadId, ps.IsDryRun())

restAPI := path.Join("api", "storage", relativePath)
setPropertiesURL, err := clientutils.BuildUrl(ps.GetArtifactoryDetails().GetUrl(), restAPI, make(map[string]string))
storageAPI := path.Join("api", "storage", relativePath)
setPropertiesURL, err := clientutils.BuildUrl(ps.GetArtifactoryDetails().GetUrl(), storageAPI, make(map[string]string))
if err != nil {
return err
}
Expand Down
Loading