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 Dec 26, 2024
1 parent d77d8c8 commit 176e497
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 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
5 changes: 5 additions & 0 deletions artifactory/emptymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions artifactory/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
40 changes: 37 additions & 3 deletions artifactory/services/releasebundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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}
}
Expand Down Expand Up @@ -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 = "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
}
15 changes: 15 additions & 0 deletions tests/releasebundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 176e497

Please sign in to comment.