Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/dev' into transfer-checksum-de…
Browse files Browse the repository at this point in the history
…ploy
  • Loading branch information
yahavi committed Mar 18, 2024
2 parents 0f0a2b0 + d806156 commit 9cdb5b0
Show file tree
Hide file tree
Showing 52 changed files with 1,434 additions and 419 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- uses: actions/checkout@v4

- name: Setup Python3
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: "3.x"

Expand Down
2 changes: 1 addition & 1 deletion artifactory/commands/dotnet/nuget.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func DependencyTreeCmd() error {
return errorutils.CheckError(err)
}

sol, err := solution.Load(workspace, "", log.Logger)
sol, err := solution.Load(workspace, "", "", log.Logger)
if err != nil {
return err
}
Expand Down
28 changes: 8 additions & 20 deletions artifactory/commands/generic/search.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generic

import (
"errors"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
clientartutils "github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
Expand All @@ -26,7 +27,7 @@ func (sc *SearchCommand) Run() error {
return err
}

func (sc *SearchCommand) Search() (contentReader *content.ContentReader, err error) {
func (sc *SearchCommand) Search() (*content.ContentReader, error) {
// Service Manager
serverDetails, err := sc.ServerDetails()
if errorutils.CheckError(err) != nil {
Expand All @@ -38,28 +39,15 @@ func (sc *SearchCommand) Search() (contentReader *content.ContentReader, err err
}
// Search Loop
log.Info("Searching artifacts...")
var searchResults []*content.ContentReader

searchResults, callbackFunc, err := utils.SearchFiles(servicesManager, sc.Spec())
defer func() {
for _, reader := range searchResults {
e := reader.Close()
if err == nil {
err = e
}
}
err = errors.Join(err, callbackFunc())
}()
for i := 0; i < len(sc.Spec().Files); i++ {
searchParams, err := utils.GetSearchParams(sc.Spec().Get(i))
if err != nil {
log.Error(err)
return nil, err
}
reader, err := servicesManager.SearchFiles(searchParams)
if err != nil {
log.Error(err)
return nil, err
}
searchResults = append(searchResults, reader)
if err != nil {
return nil, err
}

reader, err := utils.AqlResultToSearchResult(searchResults)
if err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions artifactory/commands/generic/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ func getUploadParams(f *spec.File, configuration *utils.UploadConfiguration, bui
}
uploadParams.Deb = configuration.Deb
uploadParams.MinChecksumDeploy = configuration.MinChecksumDeploySize
uploadParams.MinSplitSize = configuration.MinSplitSizeMB * rtServicesUtils.SizeMiB
uploadParams.SplitCount = configuration.SplitCount
uploadParams.AddVcsProps = addVcsProps
uploadParams.BuildProps = buildProps
uploadParams.Archive = f.Archive
Expand Down
18 changes: 18 additions & 0 deletions artifactory/commands/golang/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/jfrog/jfrog-client-go/utils/log"
"net/http"
"os"
"path"
"path/filepath"
"strings"
Expand Down Expand Up @@ -328,3 +329,20 @@ func buildPackageVersionRequest(name, branchName string) string {
// No version was given to "go get" command, so the latest version should be requested
return path.Join(packageVersionRequest, "latest.info")
}

func SetArtifactoryAsResolutionServer(serverDetails *config.ServerDetails, depsRepo string) (err error) {
err = setGoProxy(serverDetails, depsRepo)
if err != nil {
err = fmt.Errorf("failed while setting Artifactory as a dependencies resolution registry: %s", err.Error())
}
return
}

func setGoProxy(server *config.ServerDetails, remoteGoRepo string) error {
repoUrl, err := goutils.GetArtifactoryRemoteRepoUrl(server, remoteGoRepo)
if err != nil {
return err
}
repoUrl += "|direct"
return os.Setenv("GOPROXY", repoUrl)
}
24 changes: 24 additions & 0 deletions artifactory/commands/golang/go_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package golang

import (
"fmt"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
goutils "github.com/jfrog/jfrog-cli-core/v2/utils/golang"
testsutils "github.com/jfrog/jfrog-client-go/utils/tests"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"strings"
"testing"
)

Expand Down Expand Up @@ -42,3 +45,24 @@ func TestGetPackageFilesPath(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, expectedPackagePath, actualPackagePath)
}

func TestSetArtifactoryAsResolutionServer(t *testing.T) {
server := &config.ServerDetails{
Url: "http://localhost:8080/",
ArtifactoryUrl: "http://localhost:8080/artifactory/",
User: "myUser",
Password: "myPassword",
ServerId: "myServer",
}
repo := "myRepo"

// Setting the GOPROXY value to "" to ensure that the new value set in SetArtifactoryAsResolutionServer is correctly validated.
cleanup := testsutils.SetEnvWithCallbackAndAssert(t, "GOPROXY", "")
defer cleanup()

assert.NoError(t, SetArtifactoryAsResolutionServer(server, repo))

serverUrlWithoutHttp := strings.TrimPrefix(server.ArtifactoryUrl, "http://")
expectedGoProxy := fmt.Sprintf("http://%s:%s@%sapi/go/%s|direct", server.User, server.Password, serverUrlWithoutHttp, repo)
assert.Equal(t, expectedGoProxy, os.Getenv("GOPROXY"))
}
117 changes: 69 additions & 48 deletions artifactory/commands/npm/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package npm
import (
"archive/tar"
"compress/gzip"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -37,7 +38,7 @@ type NpmPublishCommandArgs struct {
executablePath string
workingDirectory string
collectBuildInfo bool
packedFilePath string
packedFilePaths []string
packageInfo *biutils.PackageInfo
publishPath string
tarballProvided bool
Expand Down Expand Up @@ -172,11 +173,11 @@ func (npc *NpmPublishCommand) Run() (err error) {
return err
}
// We should delete the tarball we created
return deleteCreatedTarballAndError(npc.packedFilePath, err)
return errors.Join(err, deleteCreatedTarball(npc.packedFilePaths))
}

if !npc.tarballProvided {
if err := deleteCreatedTarball(npc.packedFilePath); err != nil {
if err := deleteCreatedTarball(npc.packedFilePaths); err != nil {
return err
}
}
Expand Down Expand Up @@ -217,6 +218,7 @@ func (npc *NpmPublishCommand) CommandName() string {
}

func (npc *NpmPublishCommand) preparePrerequisites() error {
npc.packedFilePaths = make([]string, 0)
currentDir, err := os.Getwd()
if err != nil {
return errorutils.CheckError(err)
Expand Down Expand Up @@ -251,7 +253,7 @@ func (npc *NpmPublishCommand) preparePrerequisites() error {

func (npc *NpmPublishCommand) pack() error {
log.Debug("Creating npm package.")
packageFileName, err := npm.Pack(npc.npmArgs, npc.executablePath)
packedFileNames, err := npm.Pack(npc.npmArgs, npc.executablePath)
if err != nil {
return err
}
Expand All @@ -261,8 +263,10 @@ func (npc *NpmPublishCommand) pack() error {
return err
}

npc.packedFilePath = filepath.Join(tarballDir, packageFileName)
log.Debug("Created npm package at", npc.packedFilePath)
for _, packageFileName := range packedFileNames {
npc.packedFilePaths = append(npc.packedFilePaths, filepath.Join(tarballDir, packageFileName))
}

return nil
}

Expand All @@ -279,34 +283,36 @@ func (npc *NpmPublishCommand) getTarballDir() (string, error) {
return dest, nil
}

func (npc *NpmPublishCommand) publish() error {
log.Debug("Deploying npm package.")
if err := npc.readPackageInfoFromTarball(); err != nil {
return err
}
target := fmt.Sprintf("%s/%s", npc.repo, npc.packageInfo.GetDeployPath())

// If requested, perform a Xray binary scan before deployment. If a FailBuildError is returned, skip the deployment.
if npc.xrayScan {
fileSpec := spec.NewBuilder().
Pattern(npc.packedFilePath).
Target(npc.repo + "/").
BuildSpec()
err := commandsutils.ConditionalUploadScanFunc(npc.serverDetails, fileSpec, 1, npc.scanOutputFormat)
if err != nil {
return err
func (npc *NpmPublishCommand) publish() (err error) {
for _, packedFilePath := range npc.packedFilePaths {
log.Debug("Deploying npm package.")
if err = npc.readPackageInfoFromTarball(packedFilePath); err != nil {
return
}
target := fmt.Sprintf("%s/%s", npc.repo, npc.packageInfo.GetDeployPath())

// If requested, perform a Xray binary scan before deployment. If a FailBuildError is returned, skip the deployment.
if npc.xrayScan {
fileSpec := spec.NewBuilder().
Pattern(packedFilePath).
Target(npc.repo + "/").
BuildSpec()
if err = commandsutils.ConditionalUploadScanFunc(npc.serverDetails, fileSpec, 1, npc.scanOutputFormat); err != nil {
return
}
}
err = errors.Join(err, npc.doDeploy(target, npc.serverDetails, packedFilePath))
}
return npc.doDeploy(target, npc.serverDetails)
return
}

func (npc *NpmPublishCommand) doDeploy(target string, artDetails *config.ServerDetails) error {
func (npc *NpmPublishCommand) doDeploy(target string, artDetails *config.ServerDetails, packedFilePath string) error {
servicesManager, err := utils.CreateServiceManager(artDetails, -1, 0, false)
if err != nil {
return err
}
up := services.NewUploadParams()
up.CommonParams = &specutils.CommonParams{Pattern: npc.packedFilePath, Target: target}
up.CommonParams = &specutils.CommonParams{Pattern: packedFilePath, Target: target}
var totalFailed int
if npc.collectBuildInfo || npc.detailedSummary {
if npc.collectBuildInfo {
Expand Down Expand Up @@ -341,12 +347,11 @@ func (npc *NpmPublishCommand) doDeploy(target string, artDetails *config.ServerD
}
}
if npc.detailedSummary {
npc.result.SetReader(summary.TransferDetailsReader)
npc.result.SetFailCount(totalFailed)
npc.result.SetSuccessCount(summary.TotalSucceeded)
if err = npc.setDetailedSummary(summary); err != nil {
return err
}
} else {
err = summary.TransferDetailsReader.Close()
if err != nil {
if err = summary.TransferDetailsReader.Close(); err != nil {
return err
}
}
Expand All @@ -364,6 +369,29 @@ func (npc *NpmPublishCommand) doDeploy(target string, artDetails *config.ServerD
return nil
}

func (npc *NpmPublishCommand) setDetailedSummary(summary *specutils.OperationSummary) (err error) {
npc.result.SetFailCount(npc.result.FailCount() + summary.TotalFailed)
npc.result.SetSuccessCount(npc.result.SuccessCount() + summary.TotalSucceeded)
if npc.result.Reader() == nil {
npc.result.SetReader(summary.TransferDetailsReader)
} else {
if err = npc.appendReader(summary); err != nil {
return
}
}
return
}

func (npc *NpmPublishCommand) appendReader(summary *specutils.OperationSummary) error {
readersSlice := []*content.ContentReader{npc.result.Reader(), summary.TransferDetailsReader}
reader, err := content.MergeReaders(readersSlice, content.DefaultKey)
if err != nil {
return err
}
npc.result.SetReader(reader)
return nil
}

func (npc *NpmPublishCommand) setPublishPath() error {
log.Debug("Reading Package Json.")

Expand Down Expand Up @@ -394,13 +422,12 @@ func (npc *NpmPublishCommand) setPackageInfo() error {
}
log.Debug("The provided path is not a directory, we assume this is a compressed npm package")
npc.tarballProvided = true
npc.packedFilePath = npc.publishPath
return npc.readPackageInfoFromTarball()
return npc.readPackageInfoFromTarball(npc.publishPath)
}

func (npc *NpmPublishCommand) readPackageInfoFromTarball() (err error) {
log.Debug("Extracting info from npm package:", npc.packedFilePath)
tarball, err := os.Open(npc.packedFilePath)
func (npc *NpmPublishCommand) readPackageInfoFromTarball(packedFilePath string) (err error) {
log.Debug("Extracting info from npm package:", npc.packedFilePaths)
tarball, err := os.Open(packedFilePath)
if err != nil {
return errorutils.CheckError(err)
}
Expand All @@ -420,7 +447,7 @@ func (npc *NpmPublishCommand) readPackageInfoFromTarball() (err error) {
hdr, err := tarReader.Next()
if err != nil {
if err == io.EOF {
return errorutils.CheckErrorf("Could not find 'package.json' in the compressed npm package: " + npc.packedFilePath)
return errorutils.CheckErrorf("Could not find 'package.json' in the compressed npm package: " + packedFilePath)
}
return errorutils.CheckError(err)
}
Expand All @@ -436,18 +463,12 @@ func (npc *NpmPublishCommand) readPackageInfoFromTarball() (err error) {
}
}

func deleteCreatedTarballAndError(packedFilePath string, currentError error) error {
if err := deleteCreatedTarball(packedFilePath); err != nil {
errorText := fmt.Sprintf("Two errors occurred: \n%s \n%s", currentError, err)
return errorutils.CheckErrorf(errorText)
}
return currentError
}

func deleteCreatedTarball(packedFilePath string) error {
if err := os.Remove(packedFilePath); err != nil {
return errorutils.CheckError(err)
func deleteCreatedTarball(packedFilesPath []string) error {
for _, packedFilePath := range packedFilesPath {
if err := os.Remove(packedFilePath); err != nil {
return errorutils.CheckError(err)
}
log.Debug("Successfully deleted the created npm package:", packedFilePath)
}
log.Debug("Successfully deleted the created npm package:", packedFilePath)
return nil
}
13 changes: 10 additions & 3 deletions artifactory/commands/npm/publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ import (

func TestReadPackageInfoFromTarball(t *testing.T) {
npmPublish := NewNpmPublishCommand()
npmPublish.packedFilePath = filepath.Join("..", "testdata", "npm", "npm-example-0.0.3.tgz")
err := npmPublish.readPackageInfoFromTarball()
assert.NoError(t, err)
npmPublish.packedFilePaths = append(npmPublish.packedFilePaths, filepath.Join("..", "testdata", "npm", "npm-example-0.0.3.tgz"))
npmPublish.packedFilePaths = append(npmPublish.packedFilePaths, filepath.Join("..", "testdata", "npm", "npm-example-0.0.4.tgz"))

err := npmPublish.readPackageInfoFromTarball(npmPublish.packedFilePaths[0])
assert.NoError(t, err)
assert.Equal(t, "npm-example", npmPublish.packageInfo.Name)
assert.Equal(t, "0.0.3", npmPublish.packageInfo.Version)

err = npmPublish.readPackageInfoFromTarball(npmPublish.packedFilePaths[1])
assert.NoError(t, err)
assert.Equal(t, "npm-example", npmPublish.packageInfo.Name)
assert.Equal(t, "0.0.4", npmPublish.packageInfo.Version)

}
Loading

0 comments on commit 9cdb5b0

Please sign in to comment.