Skip to content

Commit d59cca9

Browse files
Feature/add properties for repository via j frog cli (#1148)
Added feature for setting properties for repo-only --------- Co-authored-by: Eyal Delarea <eyalde@jfrog.com>
1 parent 5cfe81b commit d59cca9

File tree

2 files changed

+101
-4
lines changed

2 files changed

+101
-4
lines changed

artifactory/services/props.go

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ func (ps *PropsService) GetThreads() int {
4545

4646
func (ps *PropsService) SetProps(propsParams PropsParams) (int, error) {
4747
log.Info("Setting properties...")
48-
totalSuccess, err := ps.performRequest(propsParams, false)
48+
var err error
49+
var totalSuccess int
50+
if propsParams.RepoOnly {
51+
totalSuccess, err = ps.performRequestForRepoOnly(propsParams, false)
52+
} else {
53+
totalSuccess, err = ps.performRequest(propsParams, false)
54+
}
4955
if err == nil {
5056
log.Info("Done setting properties.")
5157
}
@@ -54,16 +60,23 @@ func (ps *PropsService) SetProps(propsParams PropsParams) (int, error) {
5460

5561
func (ps *PropsService) DeleteProps(propsParams PropsParams) (int, error) {
5662
log.Info("Deleting properties...")
57-
totalSuccess, err := ps.performRequest(propsParams, true)
63+
var err error
64+
var totalSuccess int
65+
if propsParams.RepoOnly {
66+
totalSuccess, err = ps.performRequestForRepoOnly(propsParams, true)
67+
} else {
68+
totalSuccess, err = ps.performRequest(propsParams, true)
69+
}
5870
if err == nil {
5971
log.Info("Done deleting properties.")
6072
}
6173
return totalSuccess, err
6274
}
6375

6476
type PropsParams struct {
65-
Reader *content.ContentReader
66-
Props string
77+
Reader *content.ContentReader
78+
Props string
79+
RepoOnly bool
6780
}
6881

6982
func (sp *PropsParams) GetReader() *content.ContentReader {
@@ -74,6 +87,88 @@ func (sp *PropsParams) GetProps() string {
7487
return sp.Props
7588
}
7689

90+
func (ps *PropsService) getEncodedParam(propsParams PropsParams, isDelete bool) (string, error) {
91+
var encodedParam string
92+
if !isDelete {
93+
props, err := utils.ParseProperties(propsParams.GetProps())
94+
if err != nil {
95+
return "", err
96+
}
97+
encodedParam = props.ToEncodedString(true)
98+
} else {
99+
propList := strings.Split(propsParams.GetProps(), ",")
100+
for _, prop := range propList {
101+
encodedParam += url.QueryEscape(prop) + ","
102+
}
103+
// Remove trailing comma
104+
encodedParam = strings.TrimSuffix(encodedParam, ",")
105+
}
106+
return encodedParam, nil
107+
}
108+
109+
func (ps *PropsService) addOrDeletePropertiesForRepo(propsParams PropsParams, isDelete bool, encodedParam string) (int, error) {
110+
var action func(string, string, string) (*http.Response, []byte, error)
111+
if isDelete {
112+
action = ps.sendDeleteRequest
113+
} else {
114+
action = ps.sendPutRequest
115+
}
116+
successCounters := make([]int, ps.GetThreads())
117+
producerConsumer := parallel.NewBounedRunner(ps.GetThreads(), false)
118+
errorsQueue := clientutils.NewErrorsQueue(1)
119+
reader := propsParams.GetReader()
120+
resultItem := new(utils.ResultItem)
121+
if reader.NextRecord(resultItem) == nil {
122+
repoName := resultItem.Repo
123+
setPropsTask := func(threadId int) error {
124+
var err error
125+
logMsgPrefix := clientutils.GetLogMsgPrefix(threadId, ps.IsDryRun())
126+
127+
restAPI := path.Join("api", "storage", repoName)
128+
setPropertiesURL, err := clientutils.BuildUrl(ps.GetArtifactoryDetails().GetUrl(), restAPI, make(map[string]string))
129+
if err != nil {
130+
return err
131+
}
132+
// Because we do set/delete props on search results that took into account the
133+
// recursive flag, we do not want the action itself to be recursive.
134+
setPropertiesURL += "?properties=" + encodedParam + "&recursive=0"
135+
resp, body, err := action(logMsgPrefix, repoName, setPropertiesURL)
136+
137+
if err != nil {
138+
return err
139+
}
140+
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusNoContent); err != nil {
141+
return err
142+
}
143+
successCounters[threadId]++
144+
return nil
145+
}
146+
147+
_, _ = producerConsumer.AddTaskWithError(setPropsTask, errorsQueue.AddError)
148+
}
149+
150+
defer producerConsumer.Done()
151+
if err := reader.GetError(); err != nil {
152+
errorsQueue.AddError(err)
153+
}
154+
reader.Reset()
155+
producerConsumer.Run()
156+
totalSuccess := 0
157+
for _, v := range successCounters {
158+
totalSuccess += v
159+
}
160+
return totalSuccess, errorsQueue.GetError()
161+
}
162+
163+
func (ps *PropsService) performRequestForRepoOnly(propsParams PropsParams, isDelete bool) (int, error) {
164+
encodedParam, err := ps.getEncodedParam(propsParams, isDelete)
165+
if err != nil {
166+
return 0, err
167+
}
168+
169+
return ps.addOrDeletePropertiesForRepo(propsParams, isDelete, encodedParam)
170+
}
171+
77172
func (ps *PropsService) performRequest(propsParams PropsParams, isDelete bool) (int, error) {
78173
var encodedParam string
79174
if !isDelete {

lifecycle/services/delete.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func (rbs *ReleaseBundlesService) deleteReleaseBundle(params CommonOptionalQuery
4646
return errorutils.CheckResponseStatusWithBody(resp, body, http.StatusNoContent)
4747
}
4848

49+
4950
func (rbs *ReleaseBundlesService) RemoteDeleteReleaseBundle(rbDetails ReleaseBundleDetails, params ReleaseBundleRemoteDeleteParams, isNewReleaseBundleApiSupported bool) error {
5051
dryRunStr := ""
5152
if params.DryRun {
@@ -59,6 +60,7 @@ func (rbs *ReleaseBundlesService) RemoteDeleteReleaseBundle(rbDetails ReleaseBun
5960
return errorutils.CheckError(err)
6061
}
6162

63+
6264
restApi := GetRemoteDeleteReleaseBundleApi(rbDetails, isNewReleaseBundleApiSupported)
6365
requestFullUrl, err := utils.BuildUrl(rbs.GetLifecycleDetails().GetUrl(), restApi, nil)
6466
if err != nil {

0 commit comments

Comments
 (0)