Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented a new API endpoint to check if a release bundle exists in… #1065

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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 @@
"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 @@
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 @@
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")

Check failure on line 92 in artifactory/services/releasebundle.go

View workflow job for this annotation

GitHub Actions / Static-Check

S1039: unnecessary use of fmt.Sprintf (gosimple)

Check failure on line 92 in artifactory/services/releasebundle.go

View workflow job for this annotation

GitHub Actions / Static-Check

S1039: unnecessary use of fmt.Sprintf (gosimple)

Check failure on line 92 in artifactory/services/releasebundle.go

View workflow job for this annotation

GitHub Actions / Static-Check

S1039: unnecessary use of fmt.Sprintf (gosimple)
}

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
Loading