-
Notifications
You must be signed in to change notification settings - Fork 36
Added new npm publish and install implementation #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.