Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
bhanurp committed Feb 26, 2025
2 parents 60cd17a + 44220ef commit df3b2da
Show file tree
Hide file tree
Showing 20 changed files with 512 additions and 32 deletions.
7 changes: 4 additions & 3 deletions artifactory/utils/commandsummary/commandsummary.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ const (
OutputDirName = "jfrog-command-summary"
finalMarkdownFileName = "markdown.md"
// Filenames formats
SarifFileFormat = "*.sarif"
DataFileFormat = "*-data"
NoneScannedResult = "default"
SarifFileFormat = "*.sarif"
DataFileFormat = "*-data"
NoneScannedResult = "default"
Evidence Index = "evidence"
)

type CommandSummary struct {
Expand Down
2 changes: 1 addition & 1 deletion artifactory/utils/yarn/configget.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
)

// This method runs "yarn config set" command and sets the yarn configuration.
// This method runs "yarn config get" command and sets the yarn configuration.
func ConfigGet(key, executablePath string, jsonOutput bool) (string, error) {
var flags []string = nil
if jsonOutput {
Expand Down
39 changes: 39 additions & 0 deletions artifactory/utils/yarn/versionVerify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package yarn

import (
gofrogcmd "github.com/jfrog/gofrog/io"
"github.com/jfrog/gofrog/version"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"strings"
)

const unsupportedYarnVersion = "4.0.0"

func IsInstalledYarnVersionSupported(executablePath string) error {
versionGetCmdConfig := getVersionCmdConfig(executablePath)
output, err := gofrogcmd.RunCmdOutput(versionGetCmdConfig)
if err != nil {
return errorutils.CheckError(err)
}
yarnVersion := strings.TrimSpace(output)
return IsVersionSupported(yarnVersion)
}

func IsVersionSupported(versionStr string) error {
yarnVersion := version.NewVersion(versionStr)
if yarnVersion.Compare(unsupportedYarnVersion) <= 0 {
return errorutils.CheckErrorf("Yarn version 4 is not supported. The current version is: " + versionStr +
". Please downgrade to a compatible version to continue")
}
return nil
}

func getVersionCmdConfig(executablePath string) *YarnConfig {
return &YarnConfig{
Executable: executablePath,
Command: []string{"--version"},
CommandFlags: nil,
StrWriter: nil,
ErrWriter: nil,
}
}
26 changes: 26 additions & 0 deletions artifactory/utils/yarn/versionVerify_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package yarn

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestIsVersionSupported(t *testing.T) {
tests := []struct {
versionStr string
expectErr bool
}{
{"3.9.0", false},
{"4.0.0", true},
{"4.1.0", true},
}

for _, test := range tests {
err := IsVersionSupported(test.versionStr)
if test.expectErr {
assert.Error(t, err, "Expected an error for version: %s", test.versionStr)
} else {
assert.NoError(t, err, "Did not expect an error for version: %s", test.versionStr)
}
}
}
5 changes: 5 additions & 0 deletions common/build/buildinfoproperties.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ const Repo = "repo"
const SnapshotRepo = "snapshotRepo"
const ReleaseRepo = "releaseRepo"

const DisableSnapshots = "disableSnapshots"
const SnapshotsUpdatePolicy = "snapshotsUpdatePolicy"

const ServerId = "serverId"
const Url = "url"
const Username = "username"
Expand Down Expand Up @@ -123,6 +126,8 @@ var mavenConfigMapping = map[string]string{
"buildInfoConfig.artifactoryResolutionEnabled": "buildInfoConfig.artifactoryResolutionEnabled",
"resolve.repoKey": ResolverPrefix + ReleaseRepo,
"resolve.downSnapshotRepoKey": ResolverPrefix + SnapshotRepo,
"resolve.snapshots.disabled": ResolverPrefix + DisableSnapshots,
"resolve.snapshots.updatePolicy": ResolverPrefix + SnapshotsUpdatePolicy,
"publish.repoKey": DeployerPrefix + ReleaseRepo,
"publish.snapshot.repoKey": DeployerPrefix + SnapshotRepo,
"publish.includePatterns": DeployerPrefix + IncludePatterns,
Expand Down
81 changes: 81 additions & 0 deletions common/cliutils/spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package cliutils

import (
speccore "github.com/jfrog/jfrog-cli-core/v2/common/spec"
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"strings"
)

func GetSpec(c *components.Context, isDownload, overrideFieldsIfSet bool) (specFiles *speccore.SpecFiles, err error) {
specFiles, err = speccore.CreateSpecFromFile(c.GetStringFlagValue("spec"), coreutils.SpecVarsStringToMap(c.GetStringFlagValue("spec-vars")))
if err != nil {
return nil, err
}
if isDownload {
trimPatternPrefix(specFiles)
}
if overrideFieldsIfSet {
overrideSpecFields(c, specFiles)
}
return
}

func overrideSpecFields(c *components.Context, specFiles *speccore.SpecFiles) {
for i := 0; i < len(specFiles.Files); i++ {
OverrideFieldsIfSet(specFiles.Get(i), c)
}
}

func trimPatternPrefix(specFiles *speccore.SpecFiles) {
for i := 0; i < len(specFiles.Files); i++ {
specFiles.Get(i).Pattern = strings.TrimPrefix(specFiles.Get(i).Pattern, "/")
}
}

func OverrideFieldsIfSet(spec *speccore.File, c *components.Context) {
overrideArrayIfSet(&spec.Exclusions, c, "exclusions")
overrideArrayIfSet(&spec.SortBy, c, "sort-by")
overrideIntIfSet(&spec.Offset, c, "offset")
overrideIntIfSet(&spec.Limit, c, "limit")
overrideStringIfSet(&spec.SortOrder, c, "sort-order")
overrideStringIfSet(&spec.Props, c, "props")
overrideStringIfSet(&spec.TargetProps, c, "target-props")
overrideStringIfSet(&spec.ExcludeProps, c, "exclude-props")
overrideStringIfSet(&spec.Build, c, "build")
overrideStringIfSet(&spec.Project, c, "project")
overrideStringIfSet(&spec.ExcludeArtifacts, c, "exclude-artifacts")
overrideStringIfSet(&spec.IncludeDeps, c, "include-deps")
overrideStringIfSet(&spec.Bundle, c, "bundle")
overrideStringIfSet(&spec.Recursive, c, "recursive")
overrideStringIfSet(&spec.Flat, c, "flat")
overrideStringIfSet(&spec.Explode, c, "explode")
overrideStringIfSet(&spec.BypassArchiveInspection, c, "bypass-archive-inspection")
overrideStringIfSet(&spec.Regexp, c, "regexp")
overrideStringIfSet(&spec.IncludeDirs, c, "include-dirs")
overrideStringIfSet(&spec.ValidateSymlinks, c, "validate-symlinks")
overrideStringIfSet(&spec.Symlinks, c, "symlinks")
overrideStringIfSet(&spec.Transitive, c, "transitive")
overrideStringIfSet(&spec.PublicGpgKey, c, "gpg-key")
}

// If `fieldName` exist in the cli args, read it to `field` as a string.
func overrideStringIfSet(field *string, c *components.Context, fieldName string) {
if c.IsFlagSet(fieldName) {
*field = c.GetStringFlagValue(fieldName)
}
}

// If `fieldName` exist in the cli args, read it to `field` as an array split by `;`.
func overrideArrayIfSet(field *[]string, c *components.Context, fieldName string) {
if c.IsFlagSet(fieldName) {
*field = append([]string{}, strings.Split(c.GetStringFlagValue(fieldName), ";")...)
}
}

// If `fieldName` exist in the cli args, read it to `field` as a int.
func overrideIntIfSet(field *int, c *components.Context, fieldName string) {
if c.IsFlagSet(fieldName) {
*field, _ = c.GetIntFlagValue(fieldName)
}
}
128 changes: 128 additions & 0 deletions common/cliutils/summary/summary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package summary

import (
"encoding/json"
clientutils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/log"
"strings"
)

type StatusType int

const (
Success StatusType = iota
Failure
)

var StatusTypes = []string{
"success",
"failure",
}

func (statusType StatusType) MarshalJSON() ([]byte, error) {
return json.Marshal(StatusTypes[statusType])
}

func (statusType *StatusType) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
switch strings.ToLower(s) {
default:
*statusType = Failure
case "success":
*statusType = Success

}
return nil
}

func NewBuildInfoSummary(success, failed int, sha256 string, err error) *BuildInfoSummary {
summaryReport := GetSummaryReport(success, failed, false, err)
buildInfoSummary := BuildInfoSummary{Summary: *summaryReport, Sha256Array: []Sha256{}}
if success == 1 {
buildInfoSummary.AddSha256(sha256)
}
return &buildInfoSummary
}

func (summary *Summary) Marshal() ([]byte, error) {
return json.Marshal(summary)
}

func (bis *BuildInfoSummary) Marshal() ([]byte, error) {
return json.Marshal(bis)
}

type Summary struct {
Status StatusType `json:"status"`
Totals *Totals `json:"totals"`
}

type Totals struct {
Success int `json:"success"`
Failure int `json:"failure"`
}

type BuildInfoSummary struct {
Summary
Sha256Array []Sha256 `json:"files"`
}

type Sha256 struct {
Sha256Str string `json:"sha256"`
}

func (bis *BuildInfoSummary) AddSha256(sha256Str string) {
sha256 := Sha256{Sha256Str: sha256Str}
bis.Sha256Array = append(bis.Sha256Array, sha256)
}

func GetSummaryReport(success, failed int, failNoOp bool, err error) *Summary {
summary := &Summary{Totals: &Totals{}}
if err != nil || failed > 0 || (success == 0 && failNoOp) {
summary.Status = Failure
} else {
summary.Status = Success
}
summary.Totals.Success = success
summary.Totals.Failure = failed
return summary
}

func PrintBuildInfoSummaryReport(succeeded bool, sha256 string, originalErr error) error {
success, failed := 1, 0
if !succeeded {
success, failed = 0, 1
}
buildInfoSummary, mErr := CreateBuildInfoSummaryReportString(success, failed, sha256, originalErr)
if mErr != nil {
return summaryPrintError(mErr, originalErr)
}
log.Output(buildInfoSummary)
return summaryPrintError(mErr, originalErr)
}

func CreateBuildInfoSummaryReportString(success, failed int, sha256 string, err error) (string, error) {
buildInfoSummary := NewBuildInfoSummary(success, failed, sha256, err)
buildInfoSummaryContent, mErr := buildInfoSummary.Marshal()
if errorutils.CheckError(mErr) != nil {
return "", mErr
}
return clientutils.IndentJson(buildInfoSummaryContent), mErr
}

// Print summary report.
// a given non-nil error will pass through and be returned as is if no other errors are raised.
// In case of a nil error, the current function error will be returned.
func summaryPrintError(summaryError, originalError error) error {
if originalError != nil {
if summaryError != nil {
log.Error(summaryError)
}
return originalError
}
return summaryError
}
6 changes: 5 additions & 1 deletion common/commands/configfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const (
deploymentSnapshotsRepo = "repo-deploy-snapshots"
includePatterns = "include-patterns"
excludePatterns = "exclude-patterns"
disableSnapshots = "disable-snapshots"
snapshotsUpdatePolicy = "snapshots-update-policy"

// Gradle flags
usesPlugin = "uses-plugin"
Expand Down Expand Up @@ -251,14 +253,16 @@ func WithDeployerRepo(repoId string) ConfigOption {
// Populate Maven related configuration from cli flags
func (configFile *ConfigFile) populateMavenConfigFromFlags(c *cli.Context) {
configFile.Resolver.SnapshotRepo = c.String(resolutionSnapshotsRepo)
configFile.Resolver.DisableSnapshots = c.Bool(disableSnapshots)
configFile.Resolver.SnapshotsUpdatePolicy = c.String(snapshotsUpdatePolicy)
configFile.Resolver.ReleaseRepo = c.String(resolutionReleasesRepo)
configFile.Deployer.SnapshotRepo = c.String(deploymentSnapshotsRepo)
configFile.Deployer.ReleaseRepo = c.String(deploymentReleasesRepo)
configFile.Deployer.IncludePatterns = c.String(includePatterns)
configFile.Deployer.ExcludePatterns = c.String(excludePatterns)
configFile.UseWrapper = c.Bool(useWrapper)
configFile.Interactive = configFile.Interactive && !isAnyFlagSet(c, resolutionSnapshotsRepo, resolutionReleasesRepo,
deploymentSnapshotsRepo, deploymentReleasesRepo, includePatterns, excludePatterns)
disableSnapshots, snapshotsUpdatePolicy, deploymentSnapshotsRepo, deploymentReleasesRepo, includePatterns, excludePatterns)
}

func WithResolverSnapshotRepo(repoId string) ConfigOption {
Expand Down
46 changes: 46 additions & 0 deletions common/commands/flag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package commands

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/urfave/cli"
)

// This test demonstrates an existing bug in the urfave/cli library.
// It highlights that both BoolT and Bool flags have their default values set to false.
// Ideally, the BoolT flag should have a default value of true.
func TestBoolVsBoolTFlag(t *testing.T) {
tests := []struct {
name string
args []string
shouldUseBoolT bool
expectValue bool
}{
{"Resolving flag value using Bool (default false)", []string{"cmd"}, false, false},
{"Resolving flag value using Bool (default false)", []string{"cmd"}, true, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
app := &cli.App{
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "myflag",
Usage: "Test boolean flag",
},
},
Action: func(c *cli.Context) error {
if tt.shouldUseBoolT {
assert.Equal(t, tt.expectValue, c.BoolT("myflag"), "Expected %v, got %v", tt.expectValue, c.BoolT("myflag"))
} else {
assert.Equal(t, tt.expectValue, c.Bool("myflag"), "Expected %v, got %v", tt.expectValue, c.Bool("myflag"))
}
return nil
},
}

_ = app.Run(tt.args)
})
}
}
Loading

0 comments on commit df3b2da

Please sign in to comment.