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
60 changes: 60 additions & 0 deletions artifactory/commands/yarn/yarn.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (yc *YarnCommand) SetArgs(args []string) *YarnCommand {
func (yc *YarnCommand) Run() (err error) {
log.Info("Running Yarn...")
if err = yc.validateSupportedCommand(); err != nil {
log.Debug("Error occurred while validating the command with args: ", yc.yarnArgs, " with error: ", err)
return
}

Expand All @@ -88,13 +89,19 @@ func (yc *YarnCommand) Run() (err error) {
var filteredYarnArgs []string
yc.threads, _, _, _, filteredYarnArgs, yc.buildConfiguration, err = extractYarnOptionsFromArgs(yc.yarnArgs)
if err != nil {
log.Debug("Error occurred while extracting yarn opts: ", err)
return
}

if err = yc.preparePrerequisites(); err != nil {
return
}

err = verifyYarnVersion(yc.executablePath, filteredYarnArgs)
if err != nil {
return err
}

var missingDepsChan chan string
var missingDependencies []string
if yc.collectBuildInfo {
Expand Down Expand Up @@ -157,6 +164,27 @@ func (yc *YarnCommand) validateSupportedCommand() error {
return errorutils.CheckErrorf("The command 'jfrog rt yarn npm %s' is not supported.", npmCommand)
}
}
// validate 'yarn set version *' command
err := validateSupportedVersion(arg, yc.yarnArgs, index)
if err != nil {
return err
}
}
return nil
}

// validateSupportedVersion checks if the version to be set is supported.
// currently version 4 is not supported.
func validateSupportedVersion(arg string, yarnArgs []string, index int) error {
if arg == "set" && len(yarnArgs) > index {
setCommand := yarnArgs[index+1]
if setCommand == "version" && len(yarnArgs) > index+2 {
versionCommand := yarnArgs[index+2]
err := yarn.IsVersionSupported(versionCommand)
if err != nil {
return err
}
}
}
return nil
}
Expand Down Expand Up @@ -270,6 +298,34 @@ func GetYarnAuthDetails(server *config.ServerDetails, repo string) (registry, np
return
}

func verifyYarnVersion(executablePath string, filteredYarnArgs []string) error {
if skipVersionCheck(filteredYarnArgs) {

log.Debug("Skipping yarn version verification")
return nil
}
err := yarn.IsInstalledYarnVersionSupported(executablePath)
log.Debug("Yarn version verified")
if err != nil {
return err
}
log.Debug("Successfully verified yarn version")
return nil
}

func skipVersionCheck(filteredYarnArgs []string) bool {
// Allow 'yarn set version' command - (this will help to downgrade and upgrade yarn version)
if len(filteredYarnArgs) >= 2 && filteredYarnArgs[0] == "set" && filteredYarnArgs[1] == "version" {
return true
}

// Allow '--version' to check current version
if len(filteredYarnArgs) >= 1 && filteredYarnArgs[0] == "--version" {
return true
}
return false
}

func setArtifactoryAuth(server *config.ServerDetails) (auth.ServiceDetails, error) {
authArtDetails, err := server.CreateArtAuthConfig()
if err != nil {
Expand Down Expand Up @@ -328,6 +384,10 @@ func updateScopeRegistries(execPath, registry, npmAuthIdent, npmAuthToken string
if err != nil {
return err
}
// If npmScopesStr is "undefined" it means that the npmScopes configuration does not exist in case using yarn version 4.
if npmScopesStr == "undefined" {
return nil
}
npmScopesMap := make(map[string]yarnNpmScope)
err = json.Unmarshal([]byte(npmScopesStr), &npmScopesMap)
if err != nil {
Expand Down
25 changes: 22 additions & 3 deletions artifactory/commands/yarn/yarn_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package yarn

import (
"os"
"testing"

"github.com/jfrog/jfrog-client-go/utils/tests"
"github.com/stretchr/testify/assert"
"os"
"testing"
)

func TestValidateSupportedCommand(t *testing.T) {
Expand All @@ -22,6 +21,9 @@ func TestValidateSupportedCommand(t *testing.T) {
{[]string{"npm", "tag", "list"}, false},
{[]string{"npm", "info", "package-name"}, true},
{[]string{"npm", "whoami"}, true},
{[]string{"--version"}, true},
{[]string{"set", "version", "4.0.1"}, false},
{[]string{"set", "version", "3.2.1"}, true},
}

for _, testCase := range testCases {
Expand Down Expand Up @@ -73,3 +75,20 @@ func TestExtractAuthValuesFromNpmAuth(t *testing.T) {
assert.Equal(t, testCase.expectedExtractedAuthToken, actualExtractedAuthToken)
}
}

func TestSkipVersionCheck(t *testing.T) {
testCases := []struct {
args []string
expected bool
}{
{[]string{"set", "version", "1.22.10"}, true},
{[]string{"--version"}, true},
{[]string{"install"}, false},
{[]string{"add", "lodash"}, false},
}

for _, testCase := range testCases {
result := skipVersionCheck(testCase.args)
assert.Equal(t, testCase.expected, result, "Test args:", testCase.args)
}
}
8 changes: 1 addition & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,6 @@ require (
sigs.k8s.io/yaml v1.4.0 // indirect
)

//replace github.com/jfrog/jfrog-cli-core/v2 => github.com/oshratZairi/jfrog-cli-core/v2 v2.56.9-0.20241127142944-b39d0cc8f1c1
//replace github.com/jfrog/jfrog-cli-core/v2 => github.com/oshratZairi/jfrog-cli-core/v2 v2.31.1-0.20241211104546-3e12a85de116

replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20250212021126-e5223ab616af
replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20250221100045-5b6a23a37852

replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20250219065446-e17342f27e44

//replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240811150357-12a9330a2d67
//replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20240811142930-ab9715567376
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ github.com/jfrog/build-info-go v1.10.9 h1:mdJ+wlLw2ReFsqC7rifJVlRYLEqYk38uXDYAOZ
github.com/jfrog/build-info-go v1.10.9/go.mod h1:JcISnovFXKx3wWf3p1fcMmlPdt6adxScXvoJN4WXqIE=
github.com/jfrog/gofrog v1.7.6 h1:QmfAiRzVyaI7JYGsB7cxfAJePAZTzFz0gRWZSE27c6s=
github.com/jfrog/gofrog v1.7.6/go.mod h1:ntr1txqNOZtHplmaNd7rS4f8jpA5Apx8em70oYEe7+4=
github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20250212021126-e5223ab616af h1:XnyhhyARP1YCzYGdhk8ovpyZV+hSchiTGVTuejrZXsI=
github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20250212021126-e5223ab616af/go.mod h1:Hx2houXADsNv0eRh4w5XCS2uSsPNPn1OmiSDdwGFB7g=
github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20250221100045-5b6a23a37852 h1:tz6j/XO+BDoemr2LvQHN16ZHEG6dHT+79A+O+AvxXfk=
github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20250221100045-5b6a23a37852/go.mod h1:VAVY5umw94aXf+yGzKCoEqijeUjIUNv+ikJUeQkd9tw=
github.com/jfrog/jfrog-client-go v1.28.1-0.20250219065446-e17342f27e44 h1:Q2S65q8vk4Yb0B95XGCkcJJCUmJoKsDO5w/A1l6guJ8=
github.com/jfrog/jfrog-client-go v1.28.1-0.20250219065446-e17342f27e44/go.mod h1:hDoUcW6LZme83YNFbdSRImV+di1x0GP+Nlw7fctkwtE=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand Down
Loading