Skip to content

Commit

Permalink
Move depTree logic out from 'xrayutils.go' file (#1180)
Browse files Browse the repository at this point in the history
  • Loading branch information
asafambar authored May 22, 2024
1 parent 9914ee1 commit a62576f
Showing 1 changed file with 0 additions and 62 deletions.
62 changes: 0 additions & 62 deletions utils/xray/xrayutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
clientconfig "github.com/jfrog/jfrog-client-go/config"
"github.com/jfrog/jfrog-client-go/xray"
xrayUtils "github.com/jfrog/jfrog-client-go/xray/services/utils"
)

func CreateXrayServiceManager(serviceDetails *config.ServerDetails) (*xray.XrayServicesManager, error) {
Expand All @@ -20,64 +19,3 @@ func CreateXrayServiceManager(serviceDetails *config.ServerDetails) (*xray.XrayS
}
return xray.New(serviceConfig)
}

func CreateXrayServiceManagerAndGetVersion(serviceDetails *config.ServerDetails) (*xray.XrayServicesManager, string, error) {
xrayManager, err := CreateXrayServiceManager(serviceDetails)
if err != nil {
return nil, "", err
}
xrayVersion, err := xrayManager.GetVersion()
if err != nil {
return nil, "", err
}
return xrayManager, xrayVersion, nil
}

const maxUniqueAppearances = 10

type DepTreeNode struct {
Types *[]string `json:"types"`
Children []string `json:"children"`
}

func toNodeTypesMap(depMap map[string]DepTreeNode) map[string][]string {
mapOfTypes := map[string][]string{}
for nodId, value := range depMap {
mapOfTypes[nodId] = nil
if value.Types != nil {
mapOfTypes[nodId] = *value.Types
}
}
return mapOfTypes
}

func BuildXrayDependencyTree(treeHelper map[string]DepTreeNode, nodeId string) (*xrayUtils.GraphNode, map[string][]string) {
rootNode := &xrayUtils.GraphNode{
Id: nodeId,
Nodes: []*xrayUtils.GraphNode{},
}
dependencyAppearances := map[string]int8{}
populateXrayDependencyTree(rootNode, treeHelper, dependencyAppearances)
return rootNode, toNodeTypesMap(treeHelper)
}

func populateXrayDependencyTree(currNode *xrayUtils.GraphNode, treeHelper map[string]DepTreeNode, dependencyAppearances map[string]int8) {
dependencyAppearances[currNode.Id]++
if _, ok := treeHelper[currNode.Id]; !ok {
treeHelper[currNode.Id] = DepTreeNode{}
}
// Recursively create & append all node's dependencies.
for _, childDepId := range treeHelper[currNode.Id].Children {
childNode := &xrayUtils.GraphNode{
Id: childDepId,
Nodes: []*xrayUtils.GraphNode{},
Parent: currNode,
Types: treeHelper[childDepId].Types,
}
if dependencyAppearances[childDepId] >= maxUniqueAppearances || childNode.NodeHasLoop() {
continue
}
currNode.Nodes = append(currNode.Nodes, childNode)
populateXrayDependencyTree(childNode, treeHelper, dependencyAppearances)
}
}

0 comments on commit a62576f

Please sign in to comment.