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
38 changes: 2 additions & 36 deletions commands/curation/curationaudit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -885,13 +885,13 @@ func getTestCasesForDoCurationAudit() []testCase {
"/api/gems/ruby-yaml-repo/gems/activesupport-5.2.3.gem": true,
},

// Expect a report containing the exact blocked package.
// actionpack's subtree collapses every path to activesupport into one entry (XRAY-167222).
expectedResp: map[string]*CurationReport{
"Ruby-Project": {
packagesStatus: []*PackageStatus{
{
Action: "blocked",
ParentName: "actionview",
ParentName: "actionpack",
ParentVersion: "5.2.3",
BlockedPackageUrl: "/api/gems/ruby-yaml-repo/gems/activesupport-5.2.3.gem",
PackageName: "activesupport",
Expand All @@ -906,40 +906,6 @@ func getTestCasesForDoCurationAudit() []testCase {
},
},
},
{
Action: "blocked",
ParentName: "activesupport",
ParentVersion: "5.2.3",
BlockedPackageUrl: "/api/gems/ruby-yaml-repo/gems/activesupport-5.2.3.gem",
PackageName: "activesupport",
PackageVersion: "5.2.3",
DepRelation: "direct",
PkgType: "ruby",
BlockingReason: "Policy violations",
Policy: []Policy{
{
Policy: "pol1",
Condition: "cond1",
},
},
},
{
Action: "blocked",
ParentName: "rails-dom-testing",
ParentVersion: "2.3.0",
BlockedPackageUrl: "/api/gems/ruby-yaml-repo/gems/activesupport-5.2.3.gem",
PackageName: "activesupport",
PackageVersion: "5.2.3",
DepRelation: "indirect",
PkgType: "ruby",
BlockingReason: "Policy violations",
Policy: []Policy{
{
Policy: "pol1",
Condition: "cond1",
},
},
},
},
totalNumberOfPackages: 0, // Ignore package count for cross-platform compatibility
},
Expand Down
20 changes: 10 additions & 10 deletions sca/bom/buildinfo/technologies/gem/gem.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,9 @@ func getGemCmd(execPath, workingDir, cmd string, args ...string) *io.Command {
}

// calculateDependencies orchestrates the generation and parsing of Gemfile.lock to build the dependency graph.
// It first runs 'bundle lock' to ensure Gemfile.lock is up-to-date, then parses the lock file,
//
// dependencyTrees: A slice of top-level dependency nodes.
// uniqueDeps: A slice of unique dependency IDs found in the graph.
// It first runs 'bundle lock' to ensure Gemfile.lock is up-to-date, then parses the lock file.
// dependencyTrees must wrap direct deps in one project-root node, like every other tech builder does —
// curation-audit skips auditing whatever Id it sees as a "root".
func calculateDependencies(bundleExecPath, workingDir string, _ technologies.BuildInfoBomGeneratorParams) (dependencyTrees []*xrayUtils.GraphNode, uniqueDeps []string, err error) {
log.Debug("Ensuring Gemfile.lock is up to date using 'bundle lock'...")
if _, err = getGemCmd(bundleExecPath, workingDir, "lock").RunWithOutput(); err != nil {
Expand All @@ -187,16 +186,17 @@ func calculateDependencies(bundleExecPath, workingDir string, _ technologies.Bui

projectRootNode := buildFullGemDependencyGraph(*gemInput, workingDir)

directDeps := []*xrayUtils.GraphNode{}
dependencyTrees = []*xrayUtils.GraphNode{}
if projectRootNode != nil {
dependencyTrees = projectRootNode.Nodes
directDeps = projectRootNode.Nodes
dependencyTrees = []*xrayUtils.GraphNode{projectRootNode}
Comment thread
gauriy-tech marked this conversation as resolved.
Resolved
}

if dependencyTrees == nil {
dependencyTrees = []*xrayUtils.GraphNode{}
}
// From directDeps, not dependencyTrees — the root's own Id isn't a real package.
uniqueDeps = calculateUniqueDependencies(directDeps)

uniqueDeps = calculateUniqueDependencies(dependencyTrees)
log.Debug("Calculated dependency trees (children of root): %d trees found.", len(dependencyTrees))
log.Debug(fmt.Sprintf("Calculated gem dependency tree: %d direct dependencies, %d total nodes.", len(directDeps), len(uniqueDeps)))

return
}
Expand Down
21 changes: 15 additions & 6 deletions sca/bom/buildinfo/technologies/gem/gem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,30 @@ func TestBuildDependencyTree(t *testing.T) {
_, cleanUp := technologies.CreateTestWorkspace(t, filepath.Join("projects", "package-managers", "gem"))
defer cleanUp()
params := technologies.BuildInfoBomGeneratorParams{SkipAutoInstall: true}
actualTopLevelTrees, uniqueDeps, err := BuildDependencyTree(params)
assert.NoError(t, err, "BuildDependencyTree should not return an error")
expectedTopLevelTrees := expectedResult.Nodes
if !reflect.DeepEqual(expectedTopLevelTrees, actualTopLevelTrees) {
expectedJSON, err := utils.GetAsJsonString(expectedTopLevelTrees, true, false)
actualTrees, uniqueDeps, err := BuildDependencyTree(params)
require.NoError(t, err, "BuildDependencyTree should not return an error")

// Must return one project-root node wrapping the direct deps, not the deps themselves (XRAY-167222).
require.Len(t, actualTrees, 1, "BuildDependencyTree should return a single project-root node")
actualRoot := actualTrees[0]
assert.NotEmpty(t, actualRoot.Id, "the project-root node should carry the working directory name as its Id")

expectedDirectDeps := expectedResult.Nodes
actualDirectDeps := actualRoot.Nodes
if !reflect.DeepEqual(expectedDirectDeps, actualDirectDeps) {
expectedJSON, err := utils.GetAsJsonString(expectedDirectDeps, true, false)
if err != nil {
t.Fatalf("Failed to marshal expected dependency tree to JSON for error reporting: %v", err)
}

actualJSON, err := utils.GetAsJsonString(actualTopLevelTrees, true, false)
actualJSON, err := utils.GetAsJsonString(actualDirectDeps, true, false)
if err != nil {
t.Fatalf("Failed to marshal actual dependency tree to JSON for error reporting: %v", err)
}
t.Errorf("Dependency tree mismatch.\nExpected (JSON):\n%s\nGot (JSON):\n%s", expectedJSON, actualJSON)
}

// The root's own Id isn't a real package and must stay out of uniqueDeps.
assert.ElementsMatch(t, uniqueDeps, expectedUniqueDeps, "Unique dependencies mismatch. First is actual, Second is Expected")
}

Expand Down
Loading