diff --git a/artifactory/emptymanager.go b/artifactory/emptymanager.go index 6764ca3df..2121207fa 100644 --- a/artifactory/emptymanager.go +++ b/artifactory/emptymanager.go @@ -107,6 +107,7 @@ type ArtifactoryServicesManager interface { CalculateStorageInfo() error ImportReleaseBundle(string) error GetPackageLeadFile(leadFileParams services.LeadFileParams) ([]byte, error) + IsRbv2(string, string) (bool, error) } // By using this struct, you have the option of overriding only some of the ArtifactoryServicesManager @@ -475,6 +476,10 @@ func (esm *EmptyArtifactoryServicesManager) GetPackageLeadFile(services.LeadFile panic("Failed: Method is not implemented") } +func (esm *EmptyArtifactoryServicesManager) IsRbv2(string, string) (bool, error) { + panic("Failed: Method is not implemented") +} + // Compile time check of interface implementation. // Since EmptyArtifactoryServicesManager can be used by tests external to this project, we want this project's tests to fail, // if EmptyArtifactoryServicesManager stops implementing the ArtifactoryServicesManager interface. diff --git a/artifactory/manager.go b/artifactory/manager.go index 3125fbb55..5f996c3a4 100644 --- a/artifactory/manager.go +++ b/artifactory/manager.go @@ -156,6 +156,11 @@ func (sm *ArtifactoryServicesManagerImp) GetPackageLeadFile(leadFileParams servi return packageService.GetPackageLeadFile(leadFileParams) } +func (sm *ArtifactoryServicesManagerImp) IsRbv2(project, bundleNameAndVersion string) (bool, error) { + releaseService := services.NewReleaseService(sm.config.GetServiceDetails(), sm.client) + return releaseService.IsReleaseBundleExistInRbV2(project, bundleNameAndVersion) +} + func (sm *ArtifactoryServicesManagerImp) GetAllRepositories() (*[]services.RepositoryDetails, error) { repositoriesService := services.NewRepositoriesService(sm.client) repositoriesService.ArtDetails = sm.config.GetServiceDetails() diff --git a/artifactory/services/releasebundle.go b/artifactory/services/releasebundle.go index b9c5513da..3b6bfd79f 100644 --- a/artifactory/services/releasebundle.go +++ b/artifactory/services/releasebundle.go @@ -2,6 +2,7 @@ package services import ( "encoding/json" + "fmt" "github.com/jfrog/jfrog-client-go/artifactory/services/utils" "github.com/jfrog/jfrog-client-go/auth" "github.com/jfrog/jfrog-client-go/http/jfroghttpclient" @@ -10,12 +11,14 @@ import ( "github.com/jfrog/jfrog-client-go/utils/io/fileutils" "github.com/jfrog/jfrog-client-go/utils/log" "net/http" + "strings" ) const ( - conflictErrorMessage = "Bundle already exists" - ReleaseBundleImportRestApiEndpoint = "api/release/import/" - octetStream = "application/octet-stream" + conflictErrorMessage = "Bundle already exists" + ReleaseBundleImportRestApiEndpoint = "api/release/import/" + octetStream = "application/octet-stream" + ReleaseBundleExistInRbV2RestApiEndpoint = "lifecycle/api/v2/release_bundle/existence" ) type releaseService struct { @@ -32,6 +35,10 @@ type ErrorDetail struct { Message string `json:"message"` } +type isReleaseBundleExistResponse struct { + Exists bool `json:"exists"` +} + func NewReleaseService(artDetails auth.ServiceDetails, client *jfroghttpclient.JfrogHttpClient) *releaseService { return &releaseService{client: client, ArtDetails: artDetails} } @@ -76,3 +83,30 @@ func (rs *releaseService) ImportReleaseBundle(filePath string) (err error) { log.Info("Release Bundle Imported Successfully") return } + +func (rs *releaseService) IsReleaseBundleExistInRbV2(project, bundleNameAndVersion string) (bool, error) { + httpClientsDetails := rs.ArtDetails.CreateHttpClientDetails() + if project != "" { + project = fmt.Sprintf("project=%s&", project) + } else { + project = fmt.Sprintf("project=default") + } + + rtUrl := strings.Replace(rs.ArtDetails.GetUrl(), "/artifactory", "", 1) + url := fmt.Sprintf("%s%s/%s/?%s", rtUrl, ReleaseBundleExistInRbV2RestApiEndpoint, bundleNameAndVersion, project) + resp, body, _, err := rs.client.SendGet(url, true, &httpClientsDetails) + if err != nil { + return false, err + } + + if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK); err != nil { + return false, err + } + + response := &isReleaseBundleExistResponse{} + if err := json.Unmarshal(body, response); err != nil { + return false, err + } + + return response.Exists, nil +} diff --git a/tests/releasebundle_test.go b/tests/releasebundle_test.go index 4e2543503..f506dcdef 100644 --- a/tests/releasebundle_test.go +++ b/tests/releasebundle_test.go @@ -31,6 +31,21 @@ func TestImportReleaseBundle(t *testing.T) { assert.NoError(t, err) } +func TestIsReleaseBundleExist(t *testing.T) { + mockServer, rbService := createMockServer(t, func(w http.ResponseWriter, r *http.Request) { + if r.RequestURI == "/lifecycle/api/v2/release_bundle/existence/rbName/reVersion/?project=default" { + w.WriteHeader(http.StatusOK) + _, err := w.Write([]byte( + `{"exists":true}`)) + assert.NoError(t, err) + } + }) + defer mockServer.Close() + exist, err := rbService.IsRbv2("", "rbName/reVersion") + assert.NoError(t, err) + assert.True(t, exist) +} + func createMockServer(t *testing.T, testHandler http.HandlerFunc) (*httptest.Server, artifactory.ArtifactoryServicesManager) { testServer := httptest.NewServer(testHandler)