forked from dependency-check/dependency-check-sonar-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support dependency-check 8.0.0 (dependency-check#758)
* Support dependency-check 8.0.0 * Add Unit tests for includedby
- Loading branch information
Showing
35 changed files
with
14,088 additions
and
6,609 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,9 @@ | |
import org.sonar.dependencycheck.parser.element.Vulnerability; | ||
import org.sonar.dependencycheck.reason.DependencyReason; | ||
import org.sonar.dependencycheck.reason.Language; | ||
import org.sonar.dependencycheck.reason.SoftwareDependency; | ||
import org.sonar.dependencycheck.reason.maven.MavenDependency; | ||
import org.sonar.dependencycheck.reason.npm.NPMDependency; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
|
||
|
@@ -108,36 +111,26 @@ public static Float severityToScore(String severity, Configuration config) { | |
return DependencyCheckUtils.severityToScore(severity, severityBlocker, severityCritical, severityMajor, severityMinor); | ||
} | ||
|
||
public static Optional<Identifier> getMavenIdentifier(@NonNull Dependency dependency) { | ||
public static Optional<MavenDependency> getMavenDependency(@NonNull Dependency dependency) { | ||
Optional<Collection<Identifier>> packages = dependency.getPackages(); | ||
if (packages.isPresent()) { | ||
for (Identifier identifier : packages.get()) { | ||
if (Identifier.isMavenPackage(identifier)) { | ||
return Optional.of(identifier); | ||
Optional<SoftwareDependency> softwareDependency = DependencyCheckUtils.convertToSoftwareDependency(identifier.getId()); | ||
if (softwareDependency.isPresent() && softwareDependency.get() instanceof MavenDependency) { | ||
return Optional.of((MavenDependency) softwareDependency.get()); | ||
} | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
public static Optional<Identifier> getNPMIdentifier(@NonNull Dependency dependency) { | ||
public static Optional<NPMDependency> getNPMDependency(@NonNull Dependency dependency) { | ||
Optional<Collection<Identifier>> packages = dependency.getPackages(); | ||
if (packages.isPresent()) { | ||
for (Identifier identifier : packages.get()) { | ||
if (Identifier.isNPMPackage(identifier)) { | ||
return Optional.of(identifier); | ||
} | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
public static Optional<Identifier> getJavaScriptIdentifier(@NonNull Dependency dependency) { | ||
Optional<Collection<Identifier>> packages = dependency.getPackages(); | ||
if (packages.isPresent()) { | ||
for (Identifier identifier : packages.get()) { | ||
if (Identifier.isJavaScriptPackage(identifier)) { | ||
return Optional.of(identifier); | ||
Optional<SoftwareDependency> softwareDependency = DependencyCheckUtils.convertToSoftwareDependency(identifier.getId()); | ||
if (softwareDependency.isPresent() && softwareDependency.get() instanceof NPMDependency) { | ||
return Optional.of((NPMDependency) softwareDependency.get()); | ||
} | ||
} | ||
} | ||
|
@@ -208,4 +201,52 @@ public static Optional<DependencyReason> getBestDependencyReason(@NonNull Depend | |
} | ||
return dependencyReasons.stream().sorted(comparatorFileLength).sorted(comparatorTextRange).findFirst(); | ||
} | ||
|
||
/** | ||
* | ||
* @param reference | ||
* @return | ||
*/ | ||
public static Optional<SoftwareDependency> convertToSoftwareDependency(@NonNull String reference) { | ||
if (StringUtils.isNotBlank(reference)) { | ||
if (reference.contains("maven")) { | ||
return convertToMavenDependency(reference); | ||
} else if (reference.contains("npm") || reference.contains("javascript")) { | ||
return convertToNPMDependency(reference); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
private static Optional<SoftwareDependency> convertToMavenDependency(@NonNull String reference) { | ||
// pkg:maven/struts/[email protected] -> struts/[email protected] | ||
String dependency = StringUtils.substringAfter(reference, "/"); | ||
String groupId = StringUtils.substringBefore(dependency, "/"); | ||
String artifactId = StringUtils.substringBetween(dependency, "/", "@"); | ||
if (StringUtils.isAnyBlank(groupId, artifactId)) { | ||
return Optional.empty(); | ||
} | ||
String version = StringUtils.substringAfter(dependency, "@"); | ||
return Optional.of(new MavenDependency(groupId, artifactId, StringUtils.isBlank(version) ? null : version)); | ||
} | ||
|
||
private static Optional<SoftwareDependency> convertToNPMDependency(@NonNull String reference) { | ||
// pkg:npm/[email protected] -> [email protected] | ||
// pkg:npm/mime -> mime | ||
String dependency = StringUtils.substringAfter(reference, "/"); | ||
String name = StringUtils.substringBefore(dependency, "@"); | ||
if (StringUtils.isBlank(name)) { | ||
return Optional.empty(); | ||
} | ||
String version = StringUtils.substringAfter(dependency, "@"); | ||
return Optional.of(new NPMDependency(name, StringUtils.isBlank(version) ? null : version)); | ||
} | ||
|
||
public static boolean isMavenDependency(@NonNull SoftwareDependency dep) { | ||
return dep instanceof MavenDependency; | ||
} | ||
|
||
public static boolean isNPMDependency(@NonNull SoftwareDependency dep) { | ||
return dep instanceof NPMDependency; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.