-
Notifications
You must be signed in to change notification settings - Fork 79
Detect property to exclude Cargo dependencies #1421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
zahidblackduck
wants to merge
21
commits into
master
Choose a base branch
from
dev/zahidblackduck/IDETECT-4326
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+321
−21
Draft
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
10883f5
property to exclude dev dependencies in cargo cli detector add
zahidblackduck 0354a8e
cargo cli extractor dev dependencies exclusion add
zahidblackduck 2830904
cargo lockfile detector dependency exclusion add
zahidblackduck 1ac92d4
detect rule factory edit
zahidblackduck 5344176
cargo version match util update
zahidblackduck fba1188
cargo version match util add
zahidblackduck f6e48d9
cargo lockfile dev,build dependency exclusion add
zahidblackduck b66b9f7
Merge conflict resolve with master
zahidblackduck 37c4ed6
cargo lock file dependency transformer refactor
zahidblackduck 9e1e4a4
wildcard imports cleared
zahidblackduck 95d1bac
method name refactor to express actual intent
zahidblackduck 1293fb9
doc description updated for new detect properties
zahidblackduck 1a0e93a
handle dependency exclusion if present in multiple sections of Cargo.…
zahidblackduck 0d8f6f2
handle dependency exclusion if present in multiple sections of Cargo.…
zahidblackduck d2a8a6c
properties doc updated with an example usage
zahidblackduck 5eb9b16
Merge remote-tracking branch 'origin/master' into dev/zahidblackduck/…
zahidblackduck d2be399
refactor dependency exclusion to use NameVersion without precedence f…
zahidblackduck 83ebdf1
remove code comment and empty line
zahidblackduck ae1ee8e
refactor condition to check actual dependency exclusion filter
zahidblackduck 383b99e
Merge remote-tracking branch 'origin/master' into dev/zahidblackduck/…
zahidblackduck 204b05f
parse dependencies to exclude map key change to NameVersion
zahidblackduck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
8 changes: 8 additions & 0 deletions
8
...main/java/com/blackduck/integration/detectable/detectables/cargo/CargoDependencyType.java
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.blackduck.integration.detectable.detectables.cargo; | ||
|
||
public enum CargoDependencyType { | ||
NORMAL, | ||
BUILD, | ||
DEV, | ||
PROC_MACRO | ||
} |
15 changes: 15 additions & 0 deletions
15
...n/java/com/blackduck/integration/detectable/detectables/cargo/CargoDetectableOptions.java
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.blackduck.integration.detectable.detectables.cargo; | ||
|
||
import com.blackduck.integration.detectable.detectable.util.EnumListFilter; | ||
|
||
public class CargoDetectableOptions { | ||
private final EnumListFilter<CargoDependencyType> dependencyTypeFilter; | ||
|
||
public CargoDetectableOptions(EnumListFilter<CargoDependencyType> dependencyTypeFilter) { | ||
this.dependencyTypeFilter = dependencyTypeFilter; | ||
} | ||
|
||
public EnumListFilter<CargoDependencyType> getDependencyTypeFilter() { | ||
return dependencyTypeFilter; | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -5,9 +5,14 @@ | |
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import com.blackduck.integration.detectable.detectable.util.EnumListFilter; | ||
import com.blackduck.integration.detectable.detectables.cargo.data.CargoLockPackageData; | ||
import org.apache.commons.io.FileUtils; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
|
@@ -39,26 +44,81 @@ public CargoExtractor( | |
this.cargoLockPackageTransformer = cargoLockPackageTransformer; | ||
} | ||
|
||
public Extraction extract(File cargoLockFile, @Nullable File cargoTomlFile) throws IOException, DetectableException, MissingExternalIdException { | ||
public Extraction extract(File cargoLockFile, @Nullable File cargoTomlFile, CargoDetectableOptions cargoDetectableOptions) throws IOException, DetectableException, MissingExternalIdException { | ||
CargoLockData cargoLockData = new Toml().read(cargoLockFile).to(CargoLockData.class); | ||
List<CargoLockPackage> packages = cargoLockData.getPackages() | ||
.orElse(new ArrayList<>()).stream() | ||
List<CargoLockPackageData> cargoLockPackageDataList = cargoLockData.getPackages().orElse(new ArrayList<>()); | ||
List<CargoLockPackageData> filteredPackages = cargoLockPackageDataList; | ||
String cargoTomlContents = FileUtils.readFileToString(cargoTomlFile, StandardCharsets.UTF_8); | ||
|
||
if (isDependencyExclusionEnabled(cargoDetectableOptions)) { | ||
Map<String, String> excludableDependencyMap = cargoTomlParser.parseDependenciesToExclude(cargoTomlContents, cargoDetectableOptions.getDependencyTypeFilter()); | ||
filteredPackages = excludeDependencies(cargoLockPackageDataList, excludableDependencyMap); | ||
} | ||
|
||
List<CargoLockPackage> packages = filteredPackages.stream() | ||
.map(cargoLockPackageDataTransformer::transform) | ||
.collect(Collectors.toList()); | ||
|
||
DependencyGraph graph = cargoLockPackageTransformer.transformToGraph(packages); | ||
|
||
Optional<NameVersion> projectNameVersion = Optional.empty(); | ||
if (cargoTomlFile != null) { | ||
String cargoTomlContents = FileUtils.readFileToString(cargoTomlFile, StandardCharsets.UTF_8); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously we were checking whether the file is not null before reading it. Now, we don't. Also, I think something like this should happen:
|
||
projectNameVersion = cargoTomlParser.parseNameVersionFromCargoToml(cargoTomlContents); | ||
} | ||
|
||
CodeLocation codeLocation = new CodeLocation(graph); //TODO: Consider for producing a ProjectDependencyGraph | ||
|
||
return new Extraction.Builder() | ||
.success(codeLocation) | ||
.nameVersionIfPresent(projectNameVersion) | ||
.build(); | ||
} | ||
|
||
private boolean isDependencyExclusionEnabled(CargoDetectableOptions options) { | ||
if (options == null) { | ||
return false; | ||
} | ||
|
||
EnumListFilter<CargoDependencyType> filter = options.getDependencyTypeFilter(); | ||
return filter != null && !filter.shouldIncludeAll(); | ||
} | ||
|
||
private List<CargoLockPackageData> excludeDependencies( | ||
List<CargoLockPackageData> packages, | ||
Map<String, String> excludableDependencyMap | ||
) { | ||
Set<String> excludedNames = new HashSet<>(); | ||
|
||
List<CargoLockPackageData> filtered = packages.stream() | ||
.filter(pkg -> { | ||
String name = pkg.getName().orElse(null); | ||
String version = pkg.getVersion().orElse(null); | ||
if (name == null || version == null) return true; | ||
|
||
if (excludableDependencyMap.containsKey(name)) { | ||
String constraint = excludableDependencyMap.get(name); | ||
boolean matches = constraint == null || VersionUtils.versionMatches(constraint, version); | ||
if (matches) { | ||
excludedNames.add(name); | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}) | ||
.collect(Collectors.toList()); | ||
|
||
return filtered.stream() | ||
.map(pkg -> new CargoLockPackageData( | ||
pkg.getName().orElse(null), | ||
pkg.getVersion().orElse(null), | ||
pkg.getSource().orElse(null), | ||
pkg.getChecksum().orElse(null), | ||
pkg.getDependencies() | ||
.orElse(new ArrayList<>()) | ||
.stream() | ||
.filter(dep -> !excludedNames.contains(dep)) | ||
.collect(Collectors.toList()) | ||
)) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you describe situations in which PROC_MACRO exclusion is useful? And I guess it's not applicable to lock file extractions?