From cdc4a6e23ffd036fcbb0920b68a9f9096d6555dc Mon Sep 17 00:00:00 2001 From: delarea Date: Wed, 6 Mar 2024 12:03:38 +0200 Subject: [PATCH 01/10] working publish but need to fix info message --- artifactory/commands/npm/publish.go | 80 ++++++++++++++++------------- artifactory/utils/npm/pack.go | 11 ++-- 2 files changed, 48 insertions(+), 43 deletions(-) diff --git a/artifactory/commands/npm/publish.go b/artifactory/commands/npm/publish.go index 5313d2d74..6ff88832a 100644 --- a/artifactory/commands/npm/publish.go +++ b/artifactory/commands/npm/publish.go @@ -3,6 +3,7 @@ package npm import ( "archive/tar" "compress/gzip" + "errors" "fmt" "io" "os" @@ -37,7 +38,7 @@ type NpmPublishCommandArgs struct { executablePath string workingDirectory string collectBuildInfo bool - packedFilePath string + packedFilesPath []string packageInfo *biutils.PackageInfo publishPath string tarballProvided bool @@ -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 deleteCreatedTarballAndError(npc.packedFilesPath, err) } if !npc.tarballProvided { - if err := deleteCreatedTarball(npc.packedFilePath); err != nil { + if err := deleteCreatedTarball(npc.packedFilesPath); err != nil { return err } } @@ -217,6 +218,7 @@ func (npc *NpmPublishCommand) CommandName() string { } func (npc *NpmPublishCommand) preparePrerequisites() error { + npc.packedFilesPath = make([]string, 0) currentDir, err := os.Getwd() if err != nil { return errorutils.CheckError(err) @@ -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 } @@ -261,8 +263,11 @@ func (npc *NpmPublishCommand) pack() error { return err } - npc.packedFilePath = filepath.Join(tarballDir, packageFileName) - log.Debug("Created npm package at", npc.packedFilePath) + for index, packageFileName := range packedFileNames { + npc.packedFilesPath = append(npc.packedFilesPath, filepath.Join(tarballDir, packageFileName)) + log.Debug("Created npm package at", npc.packedFilesPath[index]) + } + return nil } @@ -279,34 +284,34 @@ 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 deployCount, packedFilePath := range npc.packedFilesPath { + 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() + err = commandsutils.ConditionalUploadScanFunc(npc.serverDetails, fileSpec, 1, npc.scanOutputFormat) } + err = errors.Join(err, npc.doDeploy(target, npc.serverDetails, packedFilePath, deployCount)) } - 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, deployCount int) 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 { @@ -394,13 +399,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.packedFilesPath) + tarball, err := os.Open(packedFilePath) if err != nil { return errorutils.CheckError(err) } @@ -420,7 +424,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) } @@ -436,18 +440,20 @@ func (npc *NpmPublishCommand) readPackageInfoFromTarball() (err error) { } } -func deleteCreatedTarballAndError(packedFilePath string, currentError error) error { - if err := deleteCreatedTarball(packedFilePath); err != nil { +func deleteCreatedTarballAndError(packedFilesPath []string, currentError error) error { + if err := deleteCreatedTarball(packedFilesPath); 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 } diff --git a/artifactory/utils/npm/pack.go b/artifactory/utils/npm/pack.go index 0cefe8cd8..9f9686709 100644 --- a/artifactory/utils/npm/pack.go +++ b/artifactory/utils/npm/pack.go @@ -8,13 +8,13 @@ import ( "github.com/jfrog/jfrog-client-go/utils/errorutils" ) -func Pack(npmFlags []string, executablePath string) (string, error) { +func Pack(npmFlags []string, executablePath string) ([]string, error) { configListCmdConfig := createPackCmdConfig(executablePath, npmFlags) output, err := gofrogcmd.RunCmdOutput(configListCmdConfig) if err != nil { - return "", errorutils.CheckError(err) + return []string{}, errorutils.CheckError(err) } - return getPackageFileNameFromOutput(output) + return getPackageFileNameFromOutput(output), nil } func createPackCmdConfig(executablePath string, splitFlags []string) *npmutils.NpmConfig { @@ -27,8 +27,7 @@ func createPackCmdConfig(executablePath string, splitFlags []string) *npmutils.N } } -func getPackageFileNameFromOutput(output string) (string, error) { +func getPackageFileNameFromOutput(output string) []string { output = strings.TrimSpace(output) - lines := strings.Split(output, "\n") - return strings.TrimSpace(lines[len(lines)-1]), nil + return strings.Split(output, "\n") } From e1ace418028c1fcf9fe6ebd53aa35591457d97d1 Mon Sep 17 00:00:00 2001 From: delarea Date: Wed, 6 Mar 2024 14:47:34 +0200 Subject: [PATCH 02/10] working double results --- artifactory/commands/npm/npmcommand.go | 2 +- artifactory/commands/npm/publish.go | 27 +++++++++++++++----------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/artifactory/commands/npm/npmcommand.go b/artifactory/commands/npm/npmcommand.go index f3d477c97..11bdafd80 100644 --- a/artifactory/commands/npm/npmcommand.go +++ b/artifactory/commands/npm/npmcommand.go @@ -193,7 +193,7 @@ func (nc *NpmCommand) setJsonOutput() error { return err } - // In case of --json=, the value of json is set to 'true', but the result from the command is not 'true' + // In case of --json=, the value of json is set to 'true', but the results from the command is not 'true' nc.jsonOutput = jsonOutput != "false" return nil } diff --git a/artifactory/commands/npm/publish.go b/artifactory/commands/npm/publish.go index 6ff88832a..66b768cbd 100644 --- a/artifactory/commands/npm/publish.go +++ b/artifactory/commands/npm/publish.go @@ -50,14 +50,14 @@ type NpmPublishCommandArgs struct { type NpmPublishCommand struct { configFilePath string commandName string - result *commandsutils.Result + results []*commandsutils.Result detailedSummary bool npmVersion *version.Version *NpmPublishCommandArgs } func NewNpmPublishCommand() *NpmPublishCommand { - return &NpmPublishCommand{NpmPublishCommandArgs: NewNpmPublishCommandArgs(), commandName: "rt_npm_publish", result: new(commandsutils.Result)} + return &NpmPublishCommand{NpmPublishCommandArgs: NewNpmPublishCommandArgs(), commandName: "rt_npm_publish", results: []*commandsutils.Result{}} } func NewNpmPublishCommandArgs() *NpmPublishCommandArgs { @@ -97,8 +97,8 @@ func (npc *NpmPublishCommand) SetScanOutputFormat(format format.OutputFormat) *N return npc } -func (npc *NpmPublishCommand) Result() *commandsutils.Result { - return npc.result +func (npc *NpmPublishCommand) Result() []*commandsutils.Result { + return npc.results } func (npc *NpmPublishCommand) IsDetailedSummary() bool { @@ -219,6 +219,7 @@ func (npc *NpmPublishCommand) CommandName() string { func (npc *NpmPublishCommand) preparePrerequisites() error { npc.packedFilesPath = make([]string, 0) + npc.results = make([]*commandsutils.Result, 0) currentDir, err := os.Getwd() if err != nil { return errorutils.CheckError(err) @@ -285,7 +286,7 @@ func (npc *NpmPublishCommand) getTarballDir() (string, error) { } func (npc *NpmPublishCommand) publish() (err error) { - for deployCount, packedFilePath := range npc.packedFilesPath { + for index, packedFilePath := range npc.packedFilesPath { log.Debug("Deploying npm package.") if err = npc.readPackageInfoFromTarball(packedFilePath); err != nil { return @@ -298,14 +299,16 @@ func (npc *NpmPublishCommand) publish() (err error) { Pattern(packedFilePath). Target(npc.repo + "/"). BuildSpec() - err = commandsutils.ConditionalUploadScanFunc(npc.serverDetails, fileSpec, 1, npc.scanOutputFormat) + if err = commandsutils.ConditionalUploadScanFunc(npc.serverDetails, fileSpec, 1, npc.scanOutputFormat); err != nil { + return + } } - err = errors.Join(err, npc.doDeploy(target, npc.serverDetails, packedFilePath, deployCount)) + err = errors.Join(err, npc.doDeploy(target, npc.serverDetails, packedFilePath, index)) } return } -func (npc *NpmPublishCommand) doDeploy(target string, artDetails *config.ServerDetails, packedFilePath string, deployCount int) error { +func (npc *NpmPublishCommand) doDeploy(target string, artDetails *config.ServerDetails, packedFilePath string, uploadCount int) error { servicesManager, err := utils.CreateServiceManager(artDetails, -1, 0, false) if err != nil { return err @@ -346,9 +349,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) + res := &commandsutils.Result{} + res.SetReader(summary.TransferDetailsReader) + res.SetFailCount(totalFailed) + res.SetSuccessCount(summary.TotalSucceeded) + npc.results = append(npc.results, res) } else { err = summary.TransferDetailsReader.Close() if err != nil { From caeeb522bbf44341e6d951ea0f34981afd5e5dd7 Mon Sep 17 00:00:00 2001 From: delarea Date: Wed, 6 Mar 2024 15:20:43 +0200 Subject: [PATCH 03/10] combine readers --- artifactory/commands/npm/npmcommand.go | 2 +- artifactory/commands/npm/publish.go | 33 +++++++++++++++++------- artifactory/commands/npm/publish_test.go | 12 +++++---- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/artifactory/commands/npm/npmcommand.go b/artifactory/commands/npm/npmcommand.go index 11bdafd80..f3d477c97 100644 --- a/artifactory/commands/npm/npmcommand.go +++ b/artifactory/commands/npm/npmcommand.go @@ -193,7 +193,7 @@ func (nc *NpmCommand) setJsonOutput() error { return err } - // In case of --json=, the value of json is set to 'true', but the results from the command is not 'true' + // In case of --json=, the value of json is set to 'true', but the result from the command is not 'true' nc.jsonOutput = jsonOutput != "false" return nil } diff --git a/artifactory/commands/npm/publish.go b/artifactory/commands/npm/publish.go index 66b768cbd..5fdf2e773 100644 --- a/artifactory/commands/npm/publish.go +++ b/artifactory/commands/npm/publish.go @@ -50,14 +50,14 @@ type NpmPublishCommandArgs struct { type NpmPublishCommand struct { configFilePath string commandName string - results []*commandsutils.Result + result *commandsutils.Result detailedSummary bool npmVersion *version.Version *NpmPublishCommandArgs } func NewNpmPublishCommand() *NpmPublishCommand { - return &NpmPublishCommand{NpmPublishCommandArgs: NewNpmPublishCommandArgs(), commandName: "rt_npm_publish", results: []*commandsutils.Result{}} + return &NpmPublishCommand{NpmPublishCommandArgs: NewNpmPublishCommandArgs(), commandName: "rt_npm_publish", result: new(commandsutils.Result)} } func NewNpmPublishCommandArgs() *NpmPublishCommandArgs { @@ -97,8 +97,8 @@ func (npc *NpmPublishCommand) SetScanOutputFormat(format format.OutputFormat) *N return npc } -func (npc *NpmPublishCommand) Result() []*commandsutils.Result { - return npc.results +func (npc *NpmPublishCommand) Result() *commandsutils.Result { + return npc.result } func (npc *NpmPublishCommand) IsDetailedSummary() bool { @@ -219,7 +219,6 @@ func (npc *NpmPublishCommand) CommandName() string { func (npc *NpmPublishCommand) preparePrerequisites() error { npc.packedFilesPath = make([]string, 0) - npc.results = make([]*commandsutils.Result, 0) currentDir, err := os.Getwd() if err != nil { return errorutils.CheckError(err) @@ -349,11 +348,15 @@ func (npc *NpmPublishCommand) doDeploy(target string, artDetails *config.ServerD } } if npc.detailedSummary { - res := &commandsutils.Result{} - res.SetReader(summary.TransferDetailsReader) - res.SetFailCount(totalFailed) - res.SetSuccessCount(summary.TotalSucceeded) - npc.results = append(npc.results, res) + npc.result.SetFailCount(npc.result.FailCount() + 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 err + } + } } else { err = summary.TransferDetailsReader.Close() if err != nil { @@ -374,6 +377,16 @@ func (npc *NpmPublishCommand) doDeploy(target string, artDetails *config.ServerD return nil } +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.") diff --git a/artifactory/commands/npm/publish_test.go b/artifactory/commands/npm/publish_test.go index 91174289e..3edd01b4e 100644 --- a/artifactory/commands/npm/publish_test.go +++ b/artifactory/commands/npm/publish_test.go @@ -9,10 +9,12 @@ 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) + for index, file := range npmPublish.packedFilesPath { + npmPublish.packedFilesPath[index] = filepath.Join("..", "testdata", "npm", "npm-example-0.0.3.tgz") + err := npmPublish.readPackageInfoFromTarball(file) + assert.NoError(t, err) - assert.Equal(t, "npm-example", npmPublish.packageInfo.Name) - assert.Equal(t, "0.0.3", npmPublish.packageInfo.Version) + assert.Equal(t, "npm-example", npmPublish.packageInfo.Name) + assert.Equal(t, "0.0.3", npmPublish.packageInfo.Version) + } } From 02f6dbcdcef851e9ebe9f55553ac7b92945a9e09 Mon Sep 17 00:00:00 2001 From: delarea Date: Wed, 6 Mar 2024 15:29:29 +0200 Subject: [PATCH 04/10] Fix static check --- artifactory/commands/npm/publish.go | 6 +++--- artifactory/utils/npm/pack_test.go | 6 +----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/artifactory/commands/npm/publish.go b/artifactory/commands/npm/publish.go index 5fdf2e773..d4285218a 100644 --- a/artifactory/commands/npm/publish.go +++ b/artifactory/commands/npm/publish.go @@ -285,7 +285,7 @@ func (npc *NpmPublishCommand) getTarballDir() (string, error) { } func (npc *NpmPublishCommand) publish() (err error) { - for index, packedFilePath := range npc.packedFilesPath { + for _, packedFilePath := range npc.packedFilesPath { log.Debug("Deploying npm package.") if err = npc.readPackageInfoFromTarball(packedFilePath); err != nil { return @@ -302,12 +302,12 @@ func (npc *NpmPublishCommand) publish() (err error) { return } } - err = errors.Join(err, npc.doDeploy(target, npc.serverDetails, packedFilePath, index)) + err = errors.Join(err, npc.doDeploy(target, npc.serverDetails, packedFilePath)) } return } -func (npc *NpmPublishCommand) doDeploy(target string, artDetails *config.ServerDetails, packedFilePath string, uploadCount int) 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 diff --git a/artifactory/utils/npm/pack_test.go b/artifactory/utils/npm/pack_test.go index 8b8533b4b..ea4419945 100644 --- a/artifactory/utils/npm/pack_test.go +++ b/artifactory/utils/npm/pack_test.go @@ -25,11 +25,7 @@ func TestGetPackageFileNameFromOutput(t *testing.T) { assert.NoError(t, err) return } - actualFilename, err := getPackageFileNameFromOutput(string(output)) - if err != nil { - assert.NoError(t, err) - return - } + actualFilename := getPackageFileNameFromOutput(string(output)) assert.Equal(t, test.expectedPackageFilename, actualFilename) }) } From 15eda5fe50e25de5ba2fbc50b2bea96f4f737b42 Mon Sep 17 00:00:00 2001 From: delarea Date: Wed, 6 Mar 2024 15:45:37 +0200 Subject: [PATCH 05/10] Add publish test for multiple files --- artifactory/commands/npm/publish_test.go | 19 +++++++++++------- .../testdata/npm/npm-example-0.0.4.tgz | Bin 0 -> 1500 bytes 2 files changed, 12 insertions(+), 7 deletions(-) create mode 100644 artifactory/commands/testdata/npm/npm-example-0.0.4.tgz diff --git a/artifactory/commands/npm/publish_test.go b/artifactory/commands/npm/publish_test.go index 3edd01b4e..9eae26445 100644 --- a/artifactory/commands/npm/publish_test.go +++ b/artifactory/commands/npm/publish_test.go @@ -9,12 +9,17 @@ import ( func TestReadPackageInfoFromTarball(t *testing.T) { npmPublish := NewNpmPublishCommand() - for index, file := range npmPublish.packedFilesPath { - npmPublish.packedFilesPath[index] = filepath.Join("..", "testdata", "npm", "npm-example-0.0.3.tgz") - err := npmPublish.readPackageInfoFromTarball(file) - assert.NoError(t, err) + npmPublish.packedFilesPath = append(npmPublish.packedFilesPath, filepath.Join("..", "testdata", "npm", "npm-example-0.0.3.tgz")) + npmPublish.packedFilesPath = append(npmPublish.packedFilesPath, filepath.Join("..", "testdata", "npm", "npm-example-0.0.4.tgz")) + + err := npmPublish.readPackageInfoFromTarball(npmPublish.packedFilesPath[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.packedFilesPath[1]) + assert.NoError(t, err) + assert.Equal(t, "npm-example", npmPublish.packageInfo.Name) + assert.Equal(t, "0.0.4", npmPublish.packageInfo.Version) - assert.Equal(t, "npm-example", npmPublish.packageInfo.Name) - assert.Equal(t, "0.0.3", npmPublish.packageInfo.Version) - } } diff --git a/artifactory/commands/testdata/npm/npm-example-0.0.4.tgz b/artifactory/commands/testdata/npm/npm-example-0.0.4.tgz new file mode 100644 index 0000000000000000000000000000000000000000..5d3f4db173ff30266a3edc55e0ea3ad46ae82f82 GIT binary patch literal 1500 zcmV<21ta<&iwFP!00002|Ls>>Z`(E$&a-~SLGs{BZQ73G1jw)fY0@qodI^#i!_aLg zEzvSliPT8Su2JN_?;J|96enp{tP3!}3}VS7&;2_WXFUAKV=+KYrzf7oy*`y zFygOV8qsdi_g3eJAfOi(8o`~o5@sPxdzP!vNv+y=xrY?C1D^w+7q&FgiF3iD_Wp3# zgU_u~t(;J<|8|v$792y%iBE2jC0wf34YDuDgo@g&b3z~dO7Qz#k?5NaOQc2e_Gmml z>OpI?f5e9PH=@&>?fPCTEAzj6V)ARX^SJpx9v~JDpMT9TdAZG%|A*cZr%d(1p%3(2;<;mF(j2jVFmjhKVjzC`FSF z_lWFI%(Sh2W-Sfo+7x}7KDGD|Ci5B&R#uZY4jh+_;UVF#^XEv zf4I&6M_POE`r;KlE0R9g+k-cl^9w1K!HIEl#zUvg3NoV?G7=VMd6Iz5vrHQYGi@N{ zipNsLfVBfkFj?R#0-eFwz*oB94B~ zvfw!Zi@X>K$7O;TQngc%ffKPQJegB`U~O6t5F^|Q#K8>tp`PzO&kCRyWq`lYkNE!t zqxYonXT$V8Ji!CM-`MU~LSJWTC9+c)UdQ1bS^jg5h-3f(CEwe*wCiN_U&?ljo=CMq z(Bmj>VXl{CTN$asH;Ctd!+XcUh{0KjVG6DFyr^53t+xP1 zGupvE^L{MAQRISMhul?3v}Su1Y91>yEBqoeplSaUtO;W zYRJvIuPMu0j`_$a_9u8h)voFS>rzkiYvcpjFD`VOc9F!!Wsi|624yQ*v2`2j0u=!n zBPDvf$O)?cZSo>{o7hES#<3$|Tfx#8R_5Lo)bZ{;DZy~Rw*IW^bK~T`9#&sPi-H4e zkwvJzsHVLh7Y&xT9gAjUIH7}{c|s+YRhHZl$$|-kE8%W2kxPp1wqkV zh*!m?m_DfdlmVUkb@f6LG9(VnIaf4z5>8ekDD(dU{Bu}SU*+)Te644xl zPSDCbYr&15e{5yy1Hz`^2J4bnjLo+>NPLA0kMD4y%H6ELMZD_SXQKrB$te+~>nLQX z0XmGASE`cpu+uWn{hPdY(NL{1Mr<~Rrlr}Yp@fy~&%*86uI<{chu7cW{td?f8UO&z C9^x|q literal 0 HcmV?d00001 From e9ebbfa66b6edf82897d60b721cb6e58b370ee67 Mon Sep 17 00:00:00 2001 From: delarea Date: Sun, 10 Mar 2024 11:48:56 +0200 Subject: [PATCH 06/10] CR --- artifactory/commands/npm/publish.go | 49 ++++++++++++------------ artifactory/commands/npm/publish_test.go | 8 ++-- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/artifactory/commands/npm/publish.go b/artifactory/commands/npm/publish.go index d4285218a..dac29dcd0 100644 --- a/artifactory/commands/npm/publish.go +++ b/artifactory/commands/npm/publish.go @@ -38,7 +38,7 @@ type NpmPublishCommandArgs struct { executablePath string workingDirectory string collectBuildInfo bool - packedFilesPath []string + packedFilePaths []string packageInfo *biutils.PackageInfo publishPath string tarballProvided bool @@ -173,11 +173,11 @@ func (npc *NpmPublishCommand) Run() (err error) { return err } // We should delete the tarball we created - return deleteCreatedTarballAndError(npc.packedFilesPath, err) + return deleteCreatedTarballAndError(npc.packedFilePaths, err) } if !npc.tarballProvided { - if err := deleteCreatedTarball(npc.packedFilesPath); err != nil { + if err := deleteCreatedTarball(npc.packedFilePaths); err != nil { return err } } @@ -218,7 +218,7 @@ func (npc *NpmPublishCommand) CommandName() string { } func (npc *NpmPublishCommand) preparePrerequisites() error { - npc.packedFilesPath = make([]string, 0) + npc.packedFilePaths = make([]string, 0) currentDir, err := os.Getwd() if err != nil { return errorutils.CheckError(err) @@ -263,9 +263,8 @@ func (npc *NpmPublishCommand) pack() error { return err } - for index, packageFileName := range packedFileNames { - npc.packedFilesPath = append(npc.packedFilesPath, filepath.Join(tarballDir, packageFileName)) - log.Debug("Created npm package at", npc.packedFilesPath[index]) + for _, packageFileName := range packedFileNames { + npc.packedFilePaths = append(npc.packedFilePaths, filepath.Join(tarballDir, packageFileName)) } return nil @@ -285,7 +284,7 @@ func (npc *NpmPublishCommand) getTarballDir() (string, error) { } func (npc *NpmPublishCommand) publish() (err error) { - for _, packedFilePath := range npc.packedFilesPath { + for _, packedFilePath := range npc.packedFilePaths { log.Debug("Deploying npm package.") if err = npc.readPackageInfoFromTarball(packedFilePath); err != nil { return @@ -348,18 +347,11 @@ func (npc *NpmPublishCommand) doDeploy(target string, artDetails *config.ServerD } } if npc.detailedSummary { - npc.result.SetFailCount(npc.result.FailCount() + 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 err - } + 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 } } @@ -377,6 +369,19 @@ 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) @@ -421,7 +426,7 @@ func (npc *NpmPublishCommand) setPackageInfo() error { } func (npc *NpmPublishCommand) readPackageInfoFromTarball(packedFilePath string) (err error) { - log.Debug("Extracting info from npm package:", npc.packedFilesPath) + log.Debug("Extracting info from npm package:", npc.packedFilePaths) tarball, err := os.Open(packedFilePath) if err != nil { return errorutils.CheckError(err) @@ -459,11 +464,7 @@ func (npc *NpmPublishCommand) readPackageInfoFromTarball(packedFilePath string) } func deleteCreatedTarballAndError(packedFilesPath []string, currentError error) error { - if err := deleteCreatedTarball(packedFilesPath); err != nil { - errorText := fmt.Sprintf("Two errors occurred: \n%s \n%s", currentError, err) - return errorutils.CheckErrorf(errorText) - } - return currentError + return errors.Join(currentError, deleteCreatedTarball(packedFilesPath)) } func deleteCreatedTarball(packedFilesPath []string) error { diff --git a/artifactory/commands/npm/publish_test.go b/artifactory/commands/npm/publish_test.go index 9eae26445..fa3e412c6 100644 --- a/artifactory/commands/npm/publish_test.go +++ b/artifactory/commands/npm/publish_test.go @@ -9,15 +9,15 @@ import ( func TestReadPackageInfoFromTarball(t *testing.T) { npmPublish := NewNpmPublishCommand() - npmPublish.packedFilesPath = append(npmPublish.packedFilesPath, filepath.Join("..", "testdata", "npm", "npm-example-0.0.3.tgz")) - npmPublish.packedFilesPath = append(npmPublish.packedFilesPath, filepath.Join("..", "testdata", "npm", "npm-example-0.0.4.tgz")) + 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.packedFilesPath[0]) + 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.packedFilesPath[1]) + 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) From e408f32663a192ba88a764abdf6c715968740160 Mon Sep 17 00:00:00 2001 From: delarea Date: Mon, 11 Mar 2024 17:24:28 +0200 Subject: [PATCH 07/10] remove unused test, the output from npm has changed but we test on out output --- artifactory/utils/npm/pack_test.go | 32 ------------------- .../utils/testdata/npm/npmPackOutputV6 | 28 ---------------- .../utils/testdata/npm/npmPackOutputV7 | 28 ---------------- 3 files changed, 88 deletions(-) delete mode 100644 artifactory/utils/npm/pack_test.go delete mode 100644 artifactory/utils/testdata/npm/npmPackOutputV6 delete mode 100644 artifactory/utils/testdata/npm/npmPackOutputV7 diff --git a/artifactory/utils/npm/pack_test.go b/artifactory/utils/npm/pack_test.go deleted file mode 100644 index ea4419945..000000000 --- a/artifactory/utils/npm/pack_test.go +++ /dev/null @@ -1,32 +0,0 @@ -package npm - -import ( - "github.com/stretchr/testify/assert" - "os" - "path/filepath" - "testing" -) - -const testdataDir = "../testdata/npm/" - -func TestGetPackageFileNameFromOutput(t *testing.T) { - tests := []struct { - testName string - outputTestDataFile string - expectedPackageFilename string - }{ - {"Get package filename for npm 6", "npmPackOutputV6", "npm-example-0.0.3.tgz"}, - {"Get package filename for npm 7", "npmPackOutputV7", "npm-example-ver0.0.3.tgz"}, - } - for _, test := range tests { - t.Run(test.testName, func(t *testing.T) { - output, err := os.ReadFile(filepath.Join(testdataDir, test.outputTestDataFile)) - if err != nil { - assert.NoError(t, err) - return - } - actualFilename := getPackageFileNameFromOutput(string(output)) - assert.Equal(t, test.expectedPackageFilename, actualFilename) - }) - } -} diff --git a/artifactory/utils/testdata/npm/npmPackOutputV6 b/artifactory/utils/testdata/npm/npmPackOutputV6 deleted file mode 100644 index b0eb2668b..000000000 --- a/artifactory/utils/testdata/npm/npmPackOutputV6 +++ /dev/null @@ -1,28 +0,0 @@ -> npm-example@0.0.3 prepack /Users/robin/proj/project-examples/npm-example -> echo pre-helloworld - -pre-helloworld - -> npm-example@0.0.3 postpack /Users/robin/proj/project-examples/npm-example -> echo post-helloworld - -post-helloworld -npm notice -npm notice 📦 npm-example@0.0.3 -npm notice === Tarball Contents === -npm notice 181B helloworld.js -npm notice 276B package.json -npm notice 2.8kB README.md -npm notice 5.5kB npm-example-ver0.0.3.tgz -npm notice 97B .jfrog/projects/npm.yaml -npm notice === Tarball Details === -npm notice name: npm-example -npm notice version: 0.0.3 -npm notice filename: npm-example-0.0.3.tgz -npm notice package size: 7.5 kB -npm notice unpacked size: 8.8 kB -npm notice shasum: fd0a95ccbb62ff833cd89cf4bb5296486c9a63aa -npm notice integrity: sha512-pMRH9mUXGZzeC[...]eJk8tQc1qSbRA== -npm notice total files: 5 -npm notice -npm-example-0.0.3.tgz \ No newline at end of file diff --git a/artifactory/utils/testdata/npm/npmPackOutputV7 b/artifactory/utils/testdata/npm/npmPackOutputV7 deleted file mode 100644 index 602a97930..000000000 --- a/artifactory/utils/testdata/npm/npmPackOutputV7 +++ /dev/null @@ -1,28 +0,0 @@ -> npm-example@ver0.0.3 prepack -> echo pre-helloworld - -pre-helloworld - -> npm-example@ver0.0.3 postpack -> echo post-helloworld - -post-helloworld -npm notice -npm notice 📦 npm-example@ver0.0.3 -npm notice === Tarball Contents === -npm notice 2.8kB README.md -npm notice 97B .jfrog/projects/npm.yaml -npm notice 181B helloworld.js -npm notice 3.5kB npm-example-ver0.0.3.tgz -npm notice 279B package.json -npm notice === Tarball Details === -npm notice name: npm-example -npm notice version: ver0.0.3 -npm notice filename: npm-example-ver0.0.3.tgz -npm notice package size: 5.5 kB -npm notice unpacked size: 6.8 kB -npm notice shasum: e3af25617b6c58c7f803d919949fc3d8993ce9dc -npm notice integrity: sha512-nTrTk6ph83jLL[...]ipNTJhWUci8Wg== -npm notice total files: 5 -npm notice -npm-example-ver0.0.3.tgz \ No newline at end of file From bb0aeb7385d13de370b8c44c3f92050f129dcebd Mon Sep 17 00:00:00 2001 From: delarea Date: Mon, 11 Mar 2024 18:40:52 +0200 Subject: [PATCH 08/10] remove deleteCreatedTarballAndError function --- artifactory/commands/npm/publish.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/artifactory/commands/npm/publish.go b/artifactory/commands/npm/publish.go index dac29dcd0..764fc1b73 100644 --- a/artifactory/commands/npm/publish.go +++ b/artifactory/commands/npm/publish.go @@ -173,7 +173,7 @@ func (npc *NpmPublishCommand) Run() (err error) { return err } // We should delete the tarball we created - return deleteCreatedTarballAndError(npc.packedFilePaths, err) + return errors.Join(err, deleteCreatedTarball(npc.packedFilePaths)) } if !npc.tarballProvided { @@ -463,10 +463,6 @@ func (npc *NpmPublishCommand) readPackageInfoFromTarball(packedFilePath string) } } -func deleteCreatedTarballAndError(packedFilesPath []string, currentError error) error { - return errors.Join(currentError, deleteCreatedTarball(packedFilesPath)) -} - func deleteCreatedTarball(packedFilesPath []string) error { for _, packedFilePath := range packedFilesPath { if err := os.Remove(packedFilePath); err != nil { From b970e50b81972577511deccbec4943bef0eca857 Mon Sep 17 00:00:00 2001 From: delarea Date: Tue, 12 Mar 2024 15:16:44 +0200 Subject: [PATCH 09/10] add pack test to verify current output --- artifactory/utils/npm/pack_test.go | 65 +++++++++++++++++++ .../npm-workspaces/module1/package.json | 12 ++++ .../npm-workspaces/module2/package.json | 12 ++++ tests/testdata/npm-workspaces/package.json | 13 ++++ 4 files changed, 102 insertions(+) create mode 100644 artifactory/utils/npm/pack_test.go create mode 100644 tests/testdata/npm-workspaces/module1/package.json create mode 100644 tests/testdata/npm-workspaces/module2/package.json create mode 100644 tests/testdata/npm-workspaces/package.json diff --git a/artifactory/utils/npm/pack_test.go b/artifactory/utils/npm/pack_test.go new file mode 100644 index 000000000..0634e8064 --- /dev/null +++ b/artifactory/utils/npm/pack_test.go @@ -0,0 +1,65 @@ +package npm + +import ( + biutils "github.com/jfrog/build-info-go/build/utils" + "github.com/jfrog/build-info-go/utils" + "github.com/jfrog/jfrog-cli-core/v2/utils/tests" + "github.com/jfrog/jfrog-client-go/utils/log" + testsUtils "github.com/jfrog/jfrog-client-go/utils/tests" + "github.com/stretchr/testify/assert" + "os" + "path/filepath" + "testing" +) + +const minimumWorkspacesNpmVersion = "7.24.2" + +func TestNpmPackWorkspaces(t *testing.T) { + + npmVersion, executablePath, err := biutils.GetNpmVersionAndExecPath(nil) + assert.NoError(t, err) + // In npm under v7 skip test + if npmVersion.Compare(minimumWorkspacesNpmVersion) > 0 { + log.Info("Test skipped as this function in not supported in npm version " + npmVersion.GetVersion()) + return + } + + tmpDir, createTempDirCallback := tests.CreateTempDirWithCallbackAndAssert(t) + defer createTempDirCallback() + + npmProjectPath := filepath.Join("..", "..", "..", "tests", "testdata", "npm-workspaces") + err = utils.CopyDir(npmProjectPath, tmpDir, true, nil) + assert.NoError(t, err) + + cwd, err := os.Getwd() + assert.NoError(t, err) + chdirCallback := testsUtils.ChangeDirWithCallback(t, cwd, tmpDir) + defer chdirCallback() + + packedFileNames, err := Pack([]string{"--workspaces", "--verbose"}, executablePath) + assert.NoError(t, err) + + expected := []string{"module1-1.0.0.tgz", "module2-1.0.0.tgz"} + assert.Equal(t, expected, packedFileNames) +} + +func TestNpmPack(t *testing.T) { + + _, executablePath, err := biutils.GetNpmVersionAndExecPath(nil) + tmpDir, createTempDirCallback := tests.CreateTempDirWithCallbackAndAssert(t) + defer createTempDirCallback() + npmProjectPath := filepath.Join("..", "..", "..", "tests", "testdata", "npm-workspaces") + err = utils.CopyDir(npmProjectPath, tmpDir, false, nil) + assert.NoError(t, err) + + cwd, err := os.Getwd() + assert.NoError(t, err) + chdirCallback := testsUtils.ChangeDirWithCallback(t, cwd, tmpDir) + defer chdirCallback() + + packedFileNames, err := Pack([]string{"--verbose"}, executablePath) + assert.NoError(t, err) + + expected := []string{"npm-pack-test-1.0.0.tgz"} + assert.Equal(t, expected, packedFileNames) +} diff --git a/tests/testdata/npm-workspaces/module1/package.json b/tests/testdata/npm-workspaces/module1/package.json new file mode 100644 index 000000000..c98ade642 --- /dev/null +++ b/tests/testdata/npm-workspaces/module1/package.json @@ -0,0 +1,12 @@ +{ + "name": "module1", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC" +} diff --git a/tests/testdata/npm-workspaces/module2/package.json b/tests/testdata/npm-workspaces/module2/package.json new file mode 100644 index 000000000..d584476e5 --- /dev/null +++ b/tests/testdata/npm-workspaces/module2/package.json @@ -0,0 +1,12 @@ +{ + "name": "module2", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC" +} diff --git a/tests/testdata/npm-workspaces/package.json b/tests/testdata/npm-workspaces/package.json new file mode 100644 index 000000000..59c936a79 --- /dev/null +++ b/tests/testdata/npm-workspaces/package.json @@ -0,0 +1,13 @@ +{ + "name": "npm-pack-test", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "workspaces": ["module1","module2"] +} From 9b0d3fff6a9b6736e684f7ebed88745e486106ca Mon Sep 17 00:00:00 2001 From: delarea Date: Tue, 12 Mar 2024 15:18:29 +0200 Subject: [PATCH 10/10] fix staticheck --- artifactory/utils/npm/pack_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/artifactory/utils/npm/pack_test.go b/artifactory/utils/npm/pack_test.go index 0634e8064..68ec6c5c8 100644 --- a/artifactory/utils/npm/pack_test.go +++ b/artifactory/utils/npm/pack_test.go @@ -46,6 +46,7 @@ func TestNpmPackWorkspaces(t *testing.T) { func TestNpmPack(t *testing.T) { _, executablePath, err := biutils.GetNpmVersionAndExecPath(nil) + assert.NoError(t, err) tmpDir, createTempDirCallback := tests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() npmProjectPath := filepath.Join("..", "..", "..", "tests", "testdata", "npm-workspaces")