Skip to content

Commit

Permalink
add import
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalDelarea committed Mar 11, 2024
1 parent f35f6dc commit 1b2bfe2
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
5 changes: 5 additions & 0 deletions artifactory/emptymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type ArtifactoryServicesManager interface {
FileList(relativePath string, optionalParams utils.FileListParams) (*utils.FileListResponse, error)
GetStorageInfo() (*utils.StorageInfo, error)
CalculateStorageInfo() error
ReleaseBundleImport(string) error
}

// By using this struct, you have the option of overriding only some of the ArtifactoryServicesManager
Expand Down Expand Up @@ -465,6 +466,10 @@ func (esm *EmptyArtifactoryServicesManager) CalculateStorageInfo() error {
panic("Failed: Method is not implemented")
}

func (esm *EmptyArtifactoryServicesManager) ReleaseBundleImport(string) 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 @@ -605,3 +605,8 @@ func (sm *ArtifactoryServicesManagerImp) CalculateStorageInfo() error {
storageService := services.NewStorageService(sm.config.GetServiceDetails(), sm.client)
return storageService.StorageInfoRefresh()
}

func (sm *ArtifactoryServicesManagerImp) ReleaseBundleImport(filePath string) error {
releaseService := services.NewReleaseService(sm.config.GetServiceDetails(), sm.client)
return releaseService.ImportReleaseBundle(filePath)
}
84 changes: 84 additions & 0 deletions artifactory/services/release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package services

import (
"encoding/json"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
ioutils "github.com/jfrog/jfrog-client-go/utils/io"
"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"
importRestApiEndpoint = "api/release/import"
// TODO change this when the bug is fixed
// https://jfrog-int.atlassian.net/browse/JR-8542
tempImportRestApiEndpoint = "ui/api/v1/ui/release/import"
octetStream = "application/octet-stream"
)

type ReleaseService struct {
client *jfroghttpclient.JfrogHttpClient
ArtDetails auth.ServiceDetails
Progress ioutils.ProgressMgr
}

type ErrorResponseWithMessage struct {
Errors []ErrorDetail `json:"errors"`
}

type ErrorDetail struct {
Status int `json:"status"`
Message string `json:"message"`
}

func NewReleaseService(artDetails auth.ServiceDetails, client *jfroghttpclient.JfrogHttpClient) *ReleaseService {
return &ReleaseService{client: client, ArtDetails: artDetails}
}

func (rs *ReleaseService) GetJfrogHttpClient() *jfroghttpclient.JfrogHttpClient {
return rs.client
}

func (rs *ReleaseService) ImportReleaseBundle(filePath string) (err error) {
// Load desired file
content, err := fileutils.ReadFile(filePath)
if err != nil {
return
}
// Upload file
httpClientsDetails := rs.ArtDetails.CreateHttpClientDetails()

// TODO replace URL when artifactory bug is fixed
//url := rs.ArtDetails.GetUrl() + importRestApiEndpoint

Check failure on line 58 in artifactory/services/release.go

View workflow job for this annotation

GitHub Actions / Static-Check

commentFormatting: put a space between `//` and comment text (gocritic)
tempUrl := strings.TrimSuffix(rs.ArtDetails.GetUrl(), "/artifactory/") + "/" + tempImportRestApiEndpoint

utils.SetContentType(octetStream, &httpClientsDetails.Headers)
var resp *http.Response
var body []byte
log.Info("Uploading archive...")
if resp, body, err = rs.client.SendPost(tempUrl, content, &httpClientsDetails); err != nil {
return
}
// When a release bundle already exists, don't return an error message of failure.
if resp.StatusCode == http.StatusBadRequest {
response := ErrorResponseWithMessage{}
if err = json.Unmarshal(body, &response); err != nil {
return
}
if response.Errors[0].Message == conflictErrorMessage {
log.Warn("Bundle already exists, did not upload a new bundle")
return
}
}
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusAccepted); err != nil {
return
}
log.Info("Upload Successful")
return
}

0 comments on commit 1b2bfe2

Please sign in to comment.