diff --git a/README.md b/README.md index ea2588297..06d279f3e 100644 --- a/README.md +++ b/README.md @@ -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](#check-rb-exists) - [Lifecycle APIs](#lifecycle-apis) - [Creating Lifecycle Service Manager](#creating-lifeCycle-service-manager) - [Creating Lifecycle Details](#creating-lifeCycle-details) @@ -2949,6 +2950,14 @@ dryRun := true resp, err := serviceManager.RemoteDeleteReleaseBundle(params, dryRun) ``` + +#### check-rb-exists + +```go +// projectName is optional. If not provided, the default project will be used. +exists, err := serviceManager.ReleaseBundleExists(projectName, rbName, rbVersion) +``` + ## Evidence APIs ### Creating Evidence Service Manager diff --git a/lifecycle/lifecycle_test.go b/lifecycle/lifecycle_test.go index 063d490ef..178c611bf 100644 --- a/lifecycle/lifecycle_test.go +++ b/lifecycle/lifecycle_test.go @@ -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.ReleaseBundleExists("", "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.ReleaseBundleExists("projectKey", "rbName", "reVersion") + assert.NoError(t, err) + assert.False(t, exist) +} diff --git a/lifecycle/manager.go b/lifecycle/manager.go index 72d76dd74..4866648f0 100644 --- a/lifecycle/manager.go +++ b/lifecycle/manager.go @@ -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, rbName, rbVersion string) (bool, error) { + rbService := lifecycle.NewReleaseBundlesService(lcs.config.GetServiceDetails(), lcs.client) + return rbService.ReleaseBundleExists(projectKey, rbName, rbVersion) +} diff --git a/lifecycle/services/is_exists.go b/lifecycle/services/is_exists.go new file mode 100644 index 000000000..b1dbb7680 --- /dev/null +++ b/lifecycle/services/is_exists.go @@ -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) ReleaseBundleExists(projectName, rbName, rbVersion string) (bool, error) { + queryParams := distribution.GetProjectQueryParam(projectName) + restApi := path.Join(isExistInRbV2Endpoint, rbName, rbVersion) + 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"` +}