From 640a917ded9c200cbd4468f50bb1812dafae7f24 Mon Sep 17 00:00:00 2001 From: shanty Date: Thu, 21 Mar 2024 12:55:27 -0600 Subject: [PATCH 1/5] Fix sonar cloud complaint about using static access for parent class --- .../detectable/detectables/yarn/YarnTransformer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/yarn/YarnTransformer.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/yarn/YarnTransformer.java index 26edc01b14..b279c9d3ad 100644 --- a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/yarn/YarnTransformer.java +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/yarn/YarnTransformer.java @@ -134,7 +134,7 @@ private DependencyGraph buildGraphForProject( if (shouldInclude(entryName, entry.getVersion())) { LazyId id = generateComponentDependencyId(entryName, entry.getVersion()); graphBuilder.setDependencyInfo(id, entryName, entry.getVersion(), generateComponentExternalId(entryName, entry.getVersion())); - ExternalIdDependencyGraphBuilder.LazyDependencyInfo parentInfo = graphBuilder.checkAndHandleMissingExternalId(lazyBuilderHandler, id); + LazyExternalIdDependencyGraphBuilder.LazyDependencyInfo parentInfo = graphBuilder.checkAndHandleMissingExternalId(lazyBuilderHandler, id); Dependency parent = new Dependency(parentInfo.getName(), parentInfo.getVersion(), parentInfo.getExternalId(), null); mutableDependencyGraph.addDirectDependency(parent); collectYarnDependencies(lazyBuilderHandler, graphBuilder, mutableDependencyGraph, yarnLockResult, entry, resolvedEntryIdVersionMap, parent); From 5ef3911289ad171c4fee340659886fc7d8f4a2cb Mon Sep 17 00:00:00 2001 From: shanty Date: Thu, 21 Mar 2024 13:07:24 -0600 Subject: [PATCH 2/5] Attempt at refactoring to reduce method complexity complaint by sonar --- .../detectables/maven/cli/MavenCodeLocationPackager.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/maven/cli/MavenCodeLocationPackager.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/maven/cli/MavenCodeLocationPackager.java index 565e9b3131..9f22ad909b 100644 --- a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/maven/cli/MavenCodeLocationPackager.java +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/maven/cli/MavenCodeLocationPackager.java @@ -202,6 +202,10 @@ private void populateGraphDependencies(ExcludedIncludedWildcardFilter scopeFilte dependencyParentStack.push(dependency); } } + shadedDependenciesStuff(dependency); + } + + private void shadedDependenciesStuff(ScopedDependency dependency) { if(!shadedDependenciesConverted.isEmpty() && shadedDependenciesConverted.containsKey(dependency.getExternalId())) { for(Dependency childDependency: shadedDependenciesConverted.get(dependency.getExternalId())) { currentGraph.addParentWithChild(dependency, childDependency); From 066eedfa8c56508a0f634b7ecaa1e98acb085715 Mon Sep 17 00:00:00 2001 From: shanty Date: Thu, 21 Mar 2024 13:26:52 -0600 Subject: [PATCH 3/5] Update method name as per Dev's suggestion --- .../detectables/maven/cli/MavenCodeLocationPackager.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/maven/cli/MavenCodeLocationPackager.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/maven/cli/MavenCodeLocationPackager.java index 9f22ad909b..a2b1db8fb4 100644 --- a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/maven/cli/MavenCodeLocationPackager.java +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/maven/cli/MavenCodeLocationPackager.java @@ -4,7 +4,6 @@ import java.util.*; import java.util.regex.Pattern; -import com.synopsys.integration.detectable.detectables.projectinspector.ProjectInspectorParser; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -202,10 +201,10 @@ private void populateGraphDependencies(ExcludedIncludedWildcardFilter scopeFilte dependencyParentStack.push(dependency); } } - shadedDependenciesStuff(dependency); + addShadedDependenciesToGraph(dependency); } - private void shadedDependenciesStuff(ScopedDependency dependency) { + private void addShadedDependenciesToGraph(ScopedDependency dependency) { if(!shadedDependenciesConverted.isEmpty() && shadedDependenciesConverted.containsKey(dependency.getExternalId())) { for(Dependency childDependency: shadedDependenciesConverted.get(dependency.getExternalId())) { currentGraph.addParentWithChild(dependency, childDependency); From bcc98a197c2349caf8928da89fa14ab9aeabfd2c Mon Sep 17 00:00:00 2001 From: shanty Date: Thu, 21 Mar 2024 13:39:22 -0600 Subject: [PATCH 4/5] Attempt at reducing method complexity from 22 to 17 --- .../detectables/yarn/YarnTransformer.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/yarn/YarnTransformer.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/yarn/YarnTransformer.java index b279c9d3ad..95d15fc981 100644 --- a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/yarn/YarnTransformer.java +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/yarn/YarnTransformer.java @@ -176,20 +176,23 @@ private void collectYarnDependencies( dependencyVersion = dependency.getVersion(); } LazyId stringDependencyId = generateComponentDependencyId(dependency.getName(), dependencyVersion); - if (yarnDependencyTypeFilter.shouldInclude(YarnDependencyType.NON_PRODUCTION) || !dependency.isOptional()) { - graphBuilder.setDependencyInfo(stringDependencyId, dependency.getName(), dependencyVersion, generateComponentExternalId(dependency.getName(), dependencyVersion)); - //graphBuilder.addChildWithParent(stringDependencyId, id); - LazyDependencyInfo childInfo = graphBuilder.checkAndHandleMissingExternalId(lazyBuilderHandler, stringDependencyId); - Dependency child = new Dependency(childInfo.getName(), childInfo.getVersion(), childInfo.getExternalId(), null); - mutableDependencyGraph.addChildWithParent(child, parent); - - } else { - logger.trace("Excluding optional dependency: {}", stringDependencyId); - } + moveToMethod(dependency, dependencyVersion, parent,lazyBuilderHandler, graphBuilder, mutableDependencyGraph, stringDependencyId); } } } + private void moveToMethod(YarnLockDependency dependency, String dependencyVersion, Dependency parent, LazyBuilderMissingExternalIdHandler lazyBuilderHandler, ExternalIdDependencyGraphBuilder graphBuilder, BasicDependencyGraph mutableDependencyGraph,LazyId stringDependencyId) throws MissingExternalIdException { + if (yarnDependencyTypeFilter.shouldInclude(YarnDependencyType.NON_PRODUCTION) || !dependency.isOptional()) { + graphBuilder.setDependencyInfo(stringDependencyId, dependency.getName(), dependencyVersion, generateComponentExternalId(dependency.getName(), dependencyVersion)); + LazyDependencyInfo childInfo = graphBuilder.checkAndHandleMissingExternalId(lazyBuilderHandler, stringDependencyId); + Dependency child = new Dependency(childInfo.getName(), childInfo.getVersion(), childInfo.getExternalId(), null); + mutableDependencyGraph.addChildWithParent(child, parent); + + } else { + logger.trace("Excluding optional dependency: {}", stringDependencyId); + } + } + private boolean isWorkspace(YarnWorkspaces yarnWorkspaces, YarnLockDependency dependency) { Optional dependencyWorkspace = yarnWorkspaces.lookup(dependency); return dependencyWorkspace.isPresent(); From 711a7305334c280170d481f9befd84b56f1e789a Mon Sep 17 00:00:00 2001 From: shanty Date: Thu, 21 Mar 2024 14:09:31 -0600 Subject: [PATCH 5/5] Reduce method complexity from 17 to less than 15 to resolve sonar cloud issue. --- .../detectables/yarn/YarnTransformer.java | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/yarn/YarnTransformer.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/yarn/YarnTransformer.java index 95d15fc981..e9b7af2862 100644 --- a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/yarn/YarnTransformer.java +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/yarn/YarnTransformer.java @@ -158,30 +158,35 @@ private void collectYarnDependencies( ) throws MissingExternalIdException { for (YarnLockDependency dependency : entry.getDependencies()) { if (!isWorkspace(yarnLockResult.getWorkspaceData(), dependency)) { - Map idVersionMap = resolvedEntryIdVersionMap.get(dependency.getName()); - String dependencyVersion; - if (idVersionMap != null) { - dependencyVersion = idVersionMap.get(dependency.getVersion()); - if (dependencyVersion == null) { - if (idVersionMap.values().isEmpty()) { - logger.warn("Dependency {} with version definition {} not found in the Yarn map entries {}", dependency.getName(), dependency.getVersion(), idVersionMap.toString()); - dependencyVersion = dependency.getVersion(); - } else { - // 1. Choose first version. - dependencyVersion = (String) idVersionMap.values().toArray()[0]; - } - // 2. Try to auto-resolve to one of the versions. - } - } else { + String dependencyVersion = getDependencyVersion(resolvedEntryIdVersionMap, dependency); + LazyId stringDependencyId = generateComponentDependencyId(dependency.getName(), dependencyVersion); + includeNonProductionOrOptionalIfNeeded(dependency, dependencyVersion, parent,lazyBuilderHandler, graphBuilder, mutableDependencyGraph, stringDependencyId); + } + } + } + + private String getDependencyVersion(Map> resolvedEntryIdVersionMap, YarnLockDependency dependency) { + Map idVersionMap = resolvedEntryIdVersionMap.get(dependency.getName()); + String dependencyVersion; + if (idVersionMap != null) { + dependencyVersion = idVersionMap.get(dependency.getVersion()); + if (dependencyVersion == null) { + if (idVersionMap.values().isEmpty()) { + logger.warn("Dependency {} with version definition {} not found in the Yarn map entries {}", dependency.getName(), dependency.getVersion(), idVersionMap.toString()); dependencyVersion = dependency.getVersion(); + } else { + // 1. Choose first version. + dependencyVersion = (String) idVersionMap.values().toArray()[0]; } - LazyId stringDependencyId = generateComponentDependencyId(dependency.getName(), dependencyVersion); - moveToMethod(dependency, dependencyVersion, parent,lazyBuilderHandler, graphBuilder, mutableDependencyGraph, stringDependencyId); + // 2. Try to auto-resolve to one of the versions. } + } else { + dependencyVersion = dependency.getVersion(); } + return dependencyVersion; } - private void moveToMethod(YarnLockDependency dependency, String dependencyVersion, Dependency parent, LazyBuilderMissingExternalIdHandler lazyBuilderHandler, ExternalIdDependencyGraphBuilder graphBuilder, BasicDependencyGraph mutableDependencyGraph,LazyId stringDependencyId) throws MissingExternalIdException { + private void includeNonProductionOrOptionalIfNeeded(YarnLockDependency dependency, String dependencyVersion, Dependency parent, LazyBuilderMissingExternalIdHandler lazyBuilderHandler, ExternalIdDependencyGraphBuilder graphBuilder, BasicDependencyGraph mutableDependencyGraph, LazyId stringDependencyId) throws MissingExternalIdException { if (yarnDependencyTypeFilter.shouldInclude(YarnDependencyType.NON_PRODUCTION) || !dependency.isOptional()) { graphBuilder.setDependencyInfo(stringDependencyId, dependency.getName(), dependencyVersion, generateComponentExternalId(dependency.getName(), dependencyVersion)); LazyDependencyInfo childInfo = graphBuilder.checkAndHandleMissingExternalId(lazyBuilderHandler, stringDependencyId);