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

Add Release Bundle exists API #1066

Merged
merged 3 commits into from
Jan 12, 2025
Merged
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
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](#check-rb-exists)
- [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-rb-exists

```go
// projectKey is optional. If not provided, the default project will be used.
exists, err := serviceManager.ReleaseBundleExists(rbName, rbVersion, projectKey)
```

## 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.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("rbName", "reVersion", "projectKey")
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(rbName, rbVersion, projectKey string) (bool, error) {
rbService := lifecycle.NewReleaseBundlesService(lcs.config.GetServiceDetails(), lcs.client)
return rbService.ReleaseBundleExists(rbName, rbVersion, projectKey)
}
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) ReleaseBundleExists(rbName, rbVersion, projectKey string) (bool, error) {
queryParams := distribution.GetProjectQueryParam(projectKey)
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"`
}
Loading