Skip to content

Commit

Permalink
Implemented a new API endpoint to check if a release bundle exists in…
Browse files Browse the repository at this point in the history
… RBV2.
  • Loading branch information
oshratZairi committed Jan 2, 2025
1 parent d77d8c8 commit f7f636c
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
- [Getting a listing of files and folders within a folder in Artifactory](#getting-a-listing-of-files-and-folders-within-a-folder-in-artifactory)
- [Getting Storage Summary Info of Artifactory](#getting-storage-summary-info-of-artifactory)
- [Getting package artifact Lead File](#getting-package-artifact-lead-file)
- [Check if Release Bundle is V2](#check-if-rb-is-v2)
- [Triggering Storage Info Recalculation in Artifactory](#triggering-storage-info-recalculation-in-artifactory)
- [Access APIs](#access-apis)
- [Creating Access Service Manager](#creating-access-service-manager)
Expand Down Expand Up @@ -1446,6 +1447,13 @@ storageInfo, err := serviceManager.GetStorageInfo()
leadArtifact, err := serviceManager.GetPackageLeadFile()
```

#### check-if-rb-is-v2

```go

isV2, err := serviceManager.IsRbV2()
```

#### Triggering Storage Info Recalculation in Artifactory

```go
Expand Down
30 changes: 30 additions & 0 deletions lifecycle/lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,33 @@ func TestGetReleaseBundleVersionPromotions(t *testing.T) {
assert.Equal(t, "2024-03-14T15:26:46.637Z", promotion.Created)
assert.Equal(t, "1710430006637", promotion.CreatedMillis.String())
}

func TestIsReleaseBundleExist(t *testing.T) {
mockServer, rbService := createMockServer(t, func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/"+lifecycle.GetIsExistReleaseBundleApi("rbName/reVersion") {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(
`{"exists":true}`))
assert.NoError(t, err)
}
})
defer mockServer.Close()
exist, err := rbService.IsExist("", "rbName/reVersion")
assert.NoError(t, err)
assert.True(t, exist)
}

func TestIsReleaseBundleExistWithProject(t *testing.T) {
mockServer, rbService := createMockServer(t, func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/"+lifecycle.GetIsExistReleaseBundleApi("rbName/reVersion?project=projectKey") {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(
`{"exists":false}`))
assert.NoError(t, err)
}
})
defer mockServer.Close()
exist, err := rbService.IsExist("projectKey", "rbName/reVersion")
assert.NoError(t, err)
assert.False(t, exist)
}
5 changes: 5 additions & 0 deletions lifecycle/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,8 @@ func (lcs *LifecycleServicesManager) ExportReleaseBundle(rbDetails lifecycle.Rel
rbService := lifecycle.NewReleaseBundlesService(lcs.config.GetServiceDetails(), lcs.client)
return rbService.ExportReleaseBundle(rbDetails, modifications, queryParams)
}

func (lcs *LifecycleServicesManager) IsReleaseBundleExist(projectKey, releaseBundleNameAndVersion string) (bool, error) {
rbService := lifecycle.NewReleaseBundlesService(lcs.config.GetServiceDetails(), lcs.client)
return rbService.IsExist(projectKey, releaseBundleNameAndVersion)
}
53 changes: 53 additions & 0 deletions lifecycle/services/isExist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package services

import (
"encoding/json"
"github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/distribution"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/log"
"net/http"
"path"
)

const (
isExistInRbV2Endpoint = "api/v2/release_bundle/existence"
)

func (rbs *ReleaseBundlesService) IsExist(projectName, releaseBundleNameAndVersion string) (bool, error) {
queryParams := distribution.GetProjectQueryParam(projectName)
restApi := path.Join(isExistInRbV2Endpoint, releaseBundleNameAndVersion)
requestFullUrl, err := utils.BuildUrl(rbs.GetLifecycleDetails().GetUrl(), restApi, queryParams)

if err != nil {
return false, err
}

httpClientDetails := rbs.GetLifecycleDetails().CreateHttpClientDetails()
httpClientDetails.SetContentTypeApplicationJson()

resp, body, _, err := rbs.client.SendGet(requestFullUrl, true, &httpClientDetails)
if err != nil {
return false, err
}
log.Debug("Artifactory response:", resp.Status)

if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusAccepted, http.StatusOK); err != nil {
return false, err
}

response := &isReleaseBundleExistResponse{}
if err := json.Unmarshal(body, response); err != nil {
return false, err
}

return response.Exists, nil
}

func GetIsExistReleaseBundleApi(releaseBundleNameAndVersion string) string {
return path.Join(isExistInRbV2Endpoint, releaseBundleNameAndVersion)
}

type isReleaseBundleExistResponse struct {
Exists bool `json:"exists"`
}

0 comments on commit f7f636c

Please sign in to comment.