Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalDelarea committed Nov 13, 2024
2 parents 7a8064d + b60e50e commit 045c2cf
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 103 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
- [Getting Info of a File in Artifactory](#getting-info-of-a-file-in-artifactory)
- [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)
- [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 @@ -1439,6 +1440,12 @@ serviceManager.FileList("repo/path/", optionalParams)
storageInfo, err := serviceManager.GetStorageInfo()
```

#### Getting Package Artifact Lead File

```go
leadArtifact, err := serviceManager.GetPackageLeadFile()
```

#### 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 @@ -106,6 +106,7 @@ type ArtifactoryServicesManager interface {
GetStorageInfo() (*utils.StorageInfo, error)
CalculateStorageInfo() error
ImportReleaseBundle(string) error
GetPackageLeadFile(leadFileParams services.LeadFileParams) ([]byte, error)
}

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

func (esm *EmptyArtifactoryServicesManager) GetPackageLeadFile(services.LeadFileParams) ([]byte, 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
6 changes: 6 additions & 0 deletions artifactory/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ func (sm *ArtifactoryServicesManagerImp) GetRepository(repoKey string, repoDetai
return repositoriesService.Get(repoKey, repoDetails)
}

func (sm *ArtifactoryServicesManagerImp) GetPackageLeadFile(leadFileParams services.LeadFileParams) ([]byte, error) {
packageService := services.NewPackageService(sm.client)
packageService.ArtDetails = sm.config.GetServiceDetails()
return packageService.GetPackageLeadFile(leadFileParams)
}

func (sm *ArtifactoryServicesManagerImp) GetAllRepositories() (*[]services.RepositoryDetails, error) {
repositoriesService := services.NewRepositoriesService(sm.client)
repositoriesService.ArtDetails = sm.config.GetServiceDetails()
Expand Down
53 changes: 53 additions & 0 deletions artifactory/services/packages.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/auth"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
clientUtils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"net/http"
)

const apiLeadFile = "api/packagesSearch/leadFile"

type PackageService struct {
Client *jfroghttpclient.JfrogHttpClient
ArtDetails auth.ServiceDetails
}

func NewPackageService(client *jfroghttpclient.JfrogHttpClient) *PackageService {
return &PackageService{Client: client}
}

func (ps *PackageService) GetJfrogHttpClient() *jfroghttpclient.JfrogHttpClient {
return ps.Client
}

func (ps *PackageService) GetPackageLeadFile(leadFileRequest LeadFileParams) ([]byte, error) {
requestUrl, err := clientUtils.BuildUrl(ps.ArtDetails.GetUrl(), apiLeadFile, nil)
if err != nil {
return nil, err
}

requestContent, err := json.Marshal(leadFileRequest)
if err != nil {
return nil, errorutils.CheckError(err)
}

httpClientsDetails := ps.ArtDetails.CreateHttpClientDetails()
httpClientsDetails.SetContentTypeApplicationJson()

resp, body, err := ps.Client.SendPost(requestUrl, requestContent, &httpClientsDetails)
if err != nil {
return nil, err
}
return body, errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK)
}

type LeadFileParams struct {
PackageVersion string `json:"package_version"`
PackageName string `json:"package_name"`
PackageRepoName string `json:"package_repo_name"`
PackageType string `json:"package_type"`
}
54 changes: 23 additions & 31 deletions artifactory/services/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@ func CollectFilesForUpload(uploadParams UploadParams, progressMgr ioutils.Progre
buildProps += vcsProps
}
uploadData := UploadData{Artifact: artifact, TargetProps: props, BuildProps: buildProps}
incGeneralProgressTotal(progressMgr, uploadParams)
if progressMgr != nil {
progressMgr.IncGeneralProgressTotalBy(1)
}
dataHandlerFunc(uploadData)
return nil
}
Expand Down Expand Up @@ -355,9 +357,10 @@ func scanFilesByPattern(uploadParams UploadParams, rootPath string, progressMgr
continue
}
uploadedDirs = append(uploadedDirs, path)
} else if progressMgr != nil {
// Increment the progress counter for each file (no increment for directories is needed)
progressMgr.IncGeneralProgressTotalBy(1)
}
// Update progress
incGeneralProgressTotal(progressMgr, uploadParams)
// Create upload task
err = createUploadTask(taskData, dataHandlerFunc, uploadParams.Regexp)
if err != nil {
Expand Down Expand Up @@ -391,16 +394,6 @@ func skipDirUpload(targetFiles, sourceDirs []string, targetDir, sourceDir string
return false
}

func incGeneralProgressTotal(progressMgr ioutils.ProgressMgr, uploadParams UploadParams) {
if progressMgr != nil {
if uploadParams.Archive != "" {
progressMgr.IncGeneralProgressTotalBy(2)
} else {
progressMgr.IncGeneralProgressTotalBy(1)
}
}
}

type uploadTaskData struct {
target string
path string
Expand Down Expand Up @@ -479,24 +472,24 @@ func getUploadTarget(rootPath, target string, isFlat, placeholdersUsed bool) str

// Uploads the file in the specified local path to the specified target path.
// Returns true if the file was successfully uploaded.
func (us *UploadService) uploadFile(artifact UploadData, uploadParams UploadParams, logMsgPrefix string) (*fileutils.FileDetails, bool, error) {
func (us *UploadService) uploadFile(uploadData UploadData, uploadParams UploadParams, logMsgPrefix string) (*fileutils.FileDetails, bool, error) {
var checksumDeployed = false
var resp *http.Response
var details *fileutils.FileDetails
var body []byte
targetPathWithProps, err := buildUploadUrls(us.ArtDetails.GetUrl(), artifact.Artifact.TargetPath, artifact.BuildProps, uploadParams.GetDebian(), artifact.TargetProps)
targetPathWithProps, err := buildUploadUrls(us.ArtDetails.GetUrl(), uploadData.Artifact.TargetPath, uploadData.BuildProps, uploadParams.GetDebian(), uploadData.TargetProps)
if err != nil {
return nil, false, err
}
fileInfo, err := os.Lstat(artifact.Artifact.LocalPath)
fileInfo, err := os.Lstat(uploadData.Artifact.LocalPath)
if errorutils.CheckError(err) != nil {
return nil, false, err
}
httpClientsDetails := us.ArtDetails.CreateHttpClientDetails()
if uploadParams.IsSymlink() && fileutils.IsFileSymlink(fileInfo) {
resp, details, body, err = us.uploadSymlink(targetPathWithProps, logMsgPrefix, httpClientsDetails, uploadParams)
} else {
resp, details, body, checksumDeployed, err = us.doUpload(artifact, targetPathWithProps, logMsgPrefix, httpClientsDetails, fileInfo, uploadParams)
resp, details, body, checksumDeployed, err = us.doUpload(uploadData.Artifact, targetPathWithProps, logMsgPrefix, httpClientsDetails, uploadParams)
}
if err != nil {
return nil, false, err
Expand Down Expand Up @@ -595,10 +588,10 @@ func (us *UploadService) uploadSymlink(targetPath, logMsgPrefix string, httpClie
return
}

func (us *UploadService) doUpload(artifact UploadData, targetUrlWithProps, logMsgPrefix string, httpClientsDetails httputils.HttpClientDetails, fileInfo os.FileInfo, uploadParams UploadParams) (
func (us *UploadService) doUpload(artifact clientutils.Artifact, targetUrlWithProps, logMsgPrefix string, httpClientsDetails httputils.HttpClientDetails, uploadParams UploadParams) (
resp *http.Response, details *fileutils.FileDetails, body []byte, checksumDeployed bool, err error) {
// Get local file details
details, err = fileutils.GetFileDetails(artifact.Artifact.LocalPath, uploadParams.ChecksumsCalcEnabled)
details, err = fileutils.GetFileDetails(artifact.LocalPath, uploadParams.ChecksumsCalcEnabled)
if err != nil {
return
}
Expand All @@ -609,7 +602,7 @@ func (us *UploadService) doUpload(artifact UploadData, targetUrlWithProps, logMs
}

// Try checksum deploy
if us.shouldTryChecksumDeploy(fileInfo.Size(), uploadParams) {
if us.shouldTryChecksumDeploy(details.Size, uploadParams) {
resp, body, err = us.doChecksumDeploy(details, targetUrlWithProps, httpClientsDetails, us.client)
if err != nil {
return resp, details, body, checksumDeployed, err
Expand All @@ -625,13 +618,13 @@ func (us *UploadService) doUpload(artifact UploadData, targetUrlWithProps, logMs

// Try multipart upload
var shouldTryMultipart bool
if shouldTryMultipart, err = us.shouldDoMultipartUpload(fileInfo.Size(), uploadParams); err != nil {
if shouldTryMultipart, err = us.shouldDoMultipartUpload(details.Size, uploadParams); err != nil {
return
}
if shouldTryMultipart {
var checksumToken string
if checksumToken, err = us.MultipartUpload.UploadFileConcurrently(artifact.Artifact.LocalPath, artifact.Artifact.TargetPath,
fileInfo.Size(), details.Checksum.Sha1, us.Progress, uploadParams.SplitCount, uploadParams.ChunkSize); err != nil {
if checksumToken, err = us.MultipartUpload.UploadFileConcurrently(artifact.LocalPath, artifact.TargetPath,
details.Size, details.Checksum.Sha1, us.Progress, uploadParams.SplitCount, uploadParams.ChunkSize); err != nil {
return
}
// Once the file is uploaded to the storage, we finalize the multipart upload by performing a checksum deployment to save the file in Artifactory.
Expand All @@ -642,7 +635,7 @@ func (us *UploadService) doUpload(artifact UploadData, targetUrlWithProps, logMs

// Do regular upload
addExplodeHeader(&httpClientsDetails, uploadParams.IsExplodeArchive())
resp, body, err = utils.UploadFile(artifact.Artifact.LocalPath, targetUrlWithProps, logMsgPrefix, &us.ArtDetails, details,
resp, body, err = utils.UploadFile(artifact.LocalPath, targetUrlWithProps, logMsgPrefix, &us.ArtDetails, details,
httpClientsDetails, us.client, uploadParams.ChecksumsCalcEnabled, us.Progress)
return
}
Expand Down Expand Up @@ -792,9 +785,6 @@ func (us *UploadService) createArtifactHandlerFunc(uploadResult *utils.Result, u
if err != nil {
return
}
if us.Progress != nil {
us.Progress.IncrementGeneralProgress()
}
uploaded = true
} else {
// Upload file
Expand Down Expand Up @@ -1017,12 +1007,14 @@ func buildUploadUrls(artifactoryUrl, targetPath, buildProps, debianConfig string
return
}

func addPropsToTargetPath(targetPath, buildProps, debConfig string, props *utils.Properties) (string, error) {
func addPropsToTargetPath(targetPath, buildProps, debConfig string, targetProps *utils.Properties) (string, error) {
pathParts := []string{targetPath}

encodedTargetProps := props.ToEncodedString(false)
if len(encodedTargetProps) > 0 {
pathParts = append(pathParts, encodedTargetProps)
if targetProps != nil {
encodedTargetProps := targetProps.ToEncodedString(false)
if len(encodedTargetProps) > 0 {
pathParts = append(pathParts, encodedTargetProps)
}
}

debianProps, err := utils.ParseProperties(getDebianProps(debConfig))
Expand Down
22 changes: 11 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
module github.com/jfrog/jfrog-client-go

go 1.22.7
go 1.22.9

require (
github.com/ProtonMail/go-crypto v1.0.0
github.com/ProtonMail/go-crypto v1.1.2
github.com/buger/jsonparser v1.1.1
github.com/forPelevin/gomoji v1.2.0
github.com/go-git/go-git/v5 v5.12.0
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/golang-jwt/jwt/v4 v4.5.1
github.com/gookit/color v1.5.4
github.com/jfrog/archiver/v3 v3.6.1
github.com/jfrog/build-info-go v1.10.5
github.com/jfrog/gofrog v1.7.6
github.com/minio/sha256-simd v1.0.1
github.com/stretchr/testify v1.9.0
github.com/xanzy/ssh-agent v0.3.3
golang.org/x/crypto v0.27.0
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
golang.org/x/term v0.24.0
golang.org/x/crypto v0.29.0
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
golang.org/x/term v0.26.0
)

require (
Expand Down Expand Up @@ -49,11 +49,11 @@ require (
github.com/ulikunitz/xz v0.5.12 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/tools v0.25.0 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/tools v0.27.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand Down
Loading

0 comments on commit 045c2cf

Please sign in to comment.