Skip to content
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
36 changes: 36 additions & 0 deletions artifactory/commands/npm/artifactoryinstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package npm

import "github.com/jfrog/jfrog-client-go/utils/log"

type npmRtInstall struct {
*NpmCommand
}

func (nri *npmRtInstall) PrepareInstallPrerequisites(repo string) (err error) {
log.Debug("Executing npm install command using jfrog RT on repository: ", repo)
if err = nri.setArtifactoryAuth(); err != nil {
return err
}

if err = nri.setNpmAuthRegistry(repo); err != nil {
return err
}

return nri.setRestoreNpmrcFunc()
}

func (nri *npmRtInstall) Run() (err error) {
if err = nri.CreateTempNpmrc(); err != nil {
return
}
if err = nri.prepareBuildInfoModule(); err != nil {
return
}
err = nri.collectDependencies()
return
}

func (nri *npmRtInstall) RestoreNpmrc() (err error) {
// Restore the npmrc file, since we are using our own npmrc
return nri.restoreNpmrcFunc()
}
129 changes: 129 additions & 0 deletions artifactory/commands/npm/artifactoryupload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package npm

import (
"errors"
"fmt"
buildinfo "github.com/jfrog/build-info-go/entities"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-client-go/artifactory"
"github.com/jfrog/jfrog-client-go/artifactory/services"
specutils "github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/io/content"
)

type npmRtUpload struct {
*NpmPublishCommand
}

func (nru *npmRtUpload) upload() (err error) {
for _, packedFilePath := range nru.packedFilePaths {
if err = nru.readPackageInfoFromTarball(packedFilePath); err != nil {
return
}
target := fmt.Sprintf("%s/%s", nru.repo, nru.packageInfo.GetDeployPath())

// If requested, perform a Xray binary scan before deployment. If a FailBuildError is returned, skip the deployment.
if nru.xrayScan {
if err = performXrayScan(packedFilePath, nru.repo, nru.serverDetails, nru.scanOutputFormat); err != nil {
return
}
}
err = errors.Join(err, nru.doDeploy(target, nru.serverDetails, packedFilePath))
}
return
}

func (nru *npmRtUpload) getBuildArtifacts() ([]buildinfo.Artifact, error) {
return specutils.ConvertArtifactsDetailsToBuildInfoArtifacts(nru.artifactsDetailsReader)
}

func (nru *npmRtUpload) 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: packedFilePath, Target: target}
if err = nru.addDistTagIfSet(up.CommonParams); err != nil {
return err
}
var totalFailed int
if nru.collectBuildInfo || nru.detailedSummary {
if nru.collectBuildInfo {
up.BuildProps, err = nru.getBuildPropsForArtifact()
if err != nil {
return err
}
}
summary, err := servicesManager.UploadFilesWithSummary(artifactory.UploadServiceOptions{}, up)
if err != nil {
return err
}
totalFailed = summary.TotalFailed
if nru.collectBuildInfo {
nru.artifactsDetailsReader = summary.ArtifactsDetailsReader
} else {
err = summary.ArtifactsDetailsReader.Close()
if err != nil {
return err
}
}
if nru.detailedSummary {
if err = nru.setDetailedSummary(summary); err != nil {
return err
}
} else {
if err = summary.TransferDetailsReader.Close(); err != nil {
return err
}
}
} else {
_, totalFailed, err = servicesManager.UploadFiles(artifactory.UploadServiceOptions{}, up)
if err != nil {
return err
}
}

// We are deploying only one Artifact which have to be deployed, in case of failure we should fail
if totalFailed > 0 {
return errorutils.CheckErrorf("Failed to upload the npm package to Artifactory. See Artifactory logs for more details.")
}
return nil
}

func (nru *npmRtUpload) addDistTagIfSet(params *specutils.CommonParams) error {
if nru.distTag == "" {
return nil
}
props, err := specutils.ParseProperties(DistTagPropKey + "=" + nru.distTag)
if err != nil {
return err
}
params.TargetProps = props
return nil
}

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

func (nru *npmRtUpload) setDetailedSummary(summary *specutils.OperationSummary) (err error) {
nru.result.SetFailCount(nru.result.FailCount() + summary.TotalFailed)
nru.result.SetSuccessCount(nru.result.SuccessCount() + summary.TotalSucceeded)
if nru.result.Reader() == nil {
nru.result.SetReader(summary.TransferDetailsReader)
} else {
if err = nru.appendReader(summary); err != nil {
return
}
}
return
}
10 changes: 10 additions & 0 deletions artifactory/commands/npm/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type CommonArgs struct {
buildConfiguration *build.BuildConfiguration
npmArgs []string
serverDetails *config.ServerDetails
useNative bool
}

func (ca *CommonArgs) SetServerDetails(serverDetails *config.ServerDetails) *CommonArgs {
Expand All @@ -31,3 +32,12 @@ func (ca *CommonArgs) SetRepo(repo string) *CommonArgs {
ca.repo = repo
return ca
}

func (ca *CommonArgs) UseNative() bool {
return ca.useNative
}

func (ca *CommonArgs) SetUseNative(useNpmRc bool) *CommonArgs {
ca.useNative = useNpmRc
return ca
}
41 changes: 41 additions & 0 deletions artifactory/commands/npm/installstrategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package npm

import "github.com/jfrog/jfrog-client-go/utils/log"

type Installer interface {
PrepareInstallPrerequisites(repo string) error
Run() error
RestoreNpmrc() error
}

type NpmInstallStrategy struct {
strategy Installer
strategyName string
}

// Get npm implementation
func NewNpmInstallStrategy(useNativeClient bool, npmCommand *NpmCommand) *NpmInstallStrategy {
npi := NpmInstallStrategy{}
if useNativeClient {
npi.strategy = &npmInstall{npmCommand}
npi.strategyName = "native"
} else {
npi.strategy = &npmRtInstall{npmCommand}
npi.strategyName = "artifactory"
}
return &npi
}

func (npi *NpmInstallStrategy) PrepareInstallPrerequisites(repo string) error {
log.Debug("Using strategy for preparing install prerequisites: ", npi.strategyName)
return npi.strategy.PrepareInstallPrerequisites(repo)
}

func (npi *NpmInstallStrategy) Install() error {
log.Debug("Using strategy for npm install: ", npi.strategyName)
return npi.strategy.Run()
}

func (npi *NpmInstallStrategy) RestoreNpmrc() error {
return npi.strategy.RestoreNpmrc()
}
23 changes: 8 additions & 15 deletions artifactory/commands/npm/npmcommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type NpmCommand struct {
configFilePath string
collectBuildInfo bool
buildInfoModule *build.NpmModule
installHandler *NpmInstallStrategy
}

func NewNpmCommand(cmdName string, collectBuildInfo bool) *NpmCommand {
Expand Down Expand Up @@ -168,15 +169,15 @@ func (nc *NpmCommand) PreparePrerequisites(repo string) error {
return err
}
log.Debug("Working directory set to:", nc.workingDirectory)
if err = nc.setArtifactoryAuth(); err != nil {
return err
}

if err = nc.setNpmAuthRegistry(repo); err != nil {
_, useNative, err := coreutils.ExtractUseNativeFromArgs(nc.npmArgs)
if err != nil {
return err
}
nc.SetUseNative(useNative)
nc.installHandler = NewNpmInstallStrategy(nc.UseNative(), nc)

return nc.setRestoreNpmrcFunc()
return nc.installHandler.PrepareInstallPrerequisites(repo)
}

func (nc *NpmCommand) setNpmAuthRegistry(repo string) (err error) {
Expand Down Expand Up @@ -309,17 +310,9 @@ func (nc *NpmCommand) Run() (err error) {
return
}
defer func() {
err = errors.Join(err, nc.restoreNpmrcFunc())
err = errors.Join(err, nc.installHandler.RestoreNpmrc())
}()
if err = nc.CreateTempNpmrc(); err != nil {
return
}

if err = nc.prepareBuildInfoModule(); err != nil {
return
}

err = nc.collectDependencies()
err = nc.installHandler.Install()
return
}

Expand Down
25 changes: 25 additions & 0 deletions artifactory/commands/npm/npminstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package npm

import "github.com/jfrog/jfrog-client-go/utils/log"

type npmInstall struct {
*NpmCommand
}

func (ni *npmInstall) PrepareInstallPrerequisites(repo string) error {
log.Debug("Skipping npm install preparation on repository: ", repo)
return nil
}

func (ni *npmInstall) Run() (err error) {
if err = ni.prepareBuildInfoModule(); err != nil {
return
}
err = ni.collectDependencies()
return
}

func (ni *npmInstall) RestoreNpmrc() error {
// No need to restore the npmrc file, since we are using user's npmrc
return nil
}
Loading