Skip to content

Commit

Permalink
Add FlattenGraph for GraphNode (#719)
Browse files Browse the repository at this point in the history
  • Loading branch information
omerzi authored Apr 5, 2023
1 parent e0aec23 commit 93ab1ed
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
20 changes: 20 additions & 0 deletions xray/services/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services
import (
"encoding/json"
"github.com/jfrog/jfrog-client-go/utils/log"
"golang.org/x/exp/maps"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -189,6 +190,25 @@ type GraphNode struct {
Parent *GraphNode `json:"-"`
}

// FlattenGraph creates a map of dependencies from the given graph, and returns a flat graph of dependencies with one level.
func FlattenGraph(graph []*GraphNode) []*GraphNode {
allDependencies := map[string]*GraphNode{}
for _, node := range graph {
populateUniqueDependencies(node, allDependencies)
}
return []*GraphNode{{Id: "root", Nodes: maps.Values(allDependencies)}}
}

func populateUniqueDependencies(node *GraphNode, allDependencies map[string]*GraphNode) {
if _, exist := allDependencies[node.Id]; exist {
return
}
allDependencies[node.Id] = &GraphNode{Id: node.Id}
for _, dependency := range node.Nodes {
populateUniqueDependencies(dependency, allDependencies)
}
}

type OtherComponentIds struct {
Id string `json:"component_id,omitempty"`
Origin int `json:"origin,omitempty"`
Expand Down
31 changes: 31 additions & 0 deletions xray/services/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package services

import (
"fmt"
"github.com/jfrog/gofrog/datastructures"
"github.com/stretchr/testify/assert"
"testing"
)

Expand Down Expand Up @@ -47,3 +49,32 @@ func TestCreateScanGraphQueryParams(t *testing.T) {
})
}
}

func TestFlattenGraph(t *testing.T) {
nodeA := &GraphNode{Id: "A"}
nodeB := &GraphNode{Id: "B"}
nodeC := &GraphNode{Id: "C"}
nodeD := &GraphNode{Id: "D"}
nodeE := &GraphNode{Id: "E"}
nodeF := &GraphNode{Id: "F"}

// Set dependencies
nodeA.Nodes = []*GraphNode{nodeB, nodeC}
nodeB.Nodes = []*GraphNode{nodeC, nodeD}
nodeC.Nodes = []*GraphNode{nodeD}
nodeD.Nodes = []*GraphNode{nodeE, nodeF}
nodeF.Nodes = []*GraphNode{nodeA, nodeB, nodeC}

// Create graph
graph := []*GraphNode{nodeA, nodeB, nodeC}
flatGraph := FlattenGraph(graph)

// Check that the graph has been flattened correctly
assert.Equal(t, len(flatGraph[0].Nodes), 6)
set := datastructures.MakeSet[string]()
for _, node := range flatGraph[0].Nodes {
assert.Len(t, node.Nodes, 0)
assert.False(t, set.Exists(node.Id))
set.Add(node.Id)
}
}

0 comments on commit 93ab1ed

Please sign in to comment.