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 aab504a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@
- [Export Release Bundle Archive](#export-release-bundle-archive)
- [Import Release Bundle Archive](#import-release-bundle-archive)
- [Remote Delete Release Bundle](#remote-delete-release-bundle)
- [Check if Release Bundle exists in V2](#check-if-rb-exists-in-v2)
- [Lifecycle APIs](#lifecycle-apis)
- [Creating Lifecycle Service Manager](#creating-lifeCycle-service-manager)
- [Creating Lifecycle Details](#creating-lifeCycle-details)
Expand Down Expand Up @@ -2949,6 +2950,14 @@ dryRun := true

resp, err := serviceManager.RemoteDeleteReleaseBundle(params, dryRun)
```
#### check-if-rb-exists-in-v2
```go

isExist, err := serviceManager.IsExist()
```
## Evidence APIs
### Creating Evidence Service Manager
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.IsExists("", "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.IsExists("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.IsExists(projectKey, releaseBundleNameAndVersion)
}
53 changes: 53 additions & 0 deletions lifecycle/services/is_exists.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) IsExists(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 aab504a

Please sign in to comment.