use of com.synopsys.integration.detectable.detectable.codelocation.CodeLocation in project synopsys-detect by blackducksoftware.
the class YarnLockDetectableTest method assertExtraction.
@Override
public void assertExtraction(@NotNull Extraction extraction) {
Assertions.assertEquals(1, extraction.getCodeLocations().size());
CodeLocation codeLocation = extraction.getCodeLocations().get(0);
Assertions.assertEquals("babel", extraction.getProjectName());
Assertions.assertEquals("1.2.3", extraction.getProjectVersion());
NameVersionGraphAssert graphAssert = new NameVersionGraphAssert(Forge.NPMJS, codeLocation.getDependencyGraph());
graphAssert.hasRootSize(2);
graphAssert.hasRootDependency("async", "2.5.0");
graphAssert.hasRootDependency("lodash", "4.17.4");
graphAssert.hasParentChildRelationship("async", "2.5.0", "lodash", "4.17.4");
}
use of com.synopsys.integration.detectable.detectable.codelocation.CodeLocation in project synopsys-detect by blackducksoftware.
the class ExtractionUtil method assertAndGetCodeLocationNamed.
public static CodeLocation assertAndGetCodeLocationNamed(String name, Extraction extraction) {
CodeLocation found = null;
for (CodeLocation it : extraction.getCodeLocations()) {
if (it.getExternalId().isPresent() && it.getExternalId().get().getName().equals(name)) {
found = it;
}
}
Assertions.assertNotNull(found, "Missing code location with name: " + name);
return found;
}
use of com.synopsys.integration.detectable.detectable.codelocation.CodeLocation in project synopsys-detect by blackducksoftware.
the class CodeLocationConverter method toDetectCodeLocation.
public Map<CodeLocation, DetectCodeLocation> toDetectCodeLocation(File detectSourcePath, Extraction extraction, File overridePath, String overrideName) {
Map<CodeLocation, DetectCodeLocation> detectCodeLocations = new HashMap<>();
for (CodeLocation codeLocation : extraction.getCodeLocations()) {
File sourcePath = codeLocation.getSourcePath().orElse(overridePath);
ExternalId externalId;
if (!codeLocation.getExternalId().isPresent()) {
logger.debug("The detector was unable to determine an external id for this code location, so an external id will be created using the file path.");
String relativePath = FileNameUtils.relativize(detectSourcePath.getAbsolutePath(), sourcePath.getAbsolutePath());
if (StringUtils.isNotBlank(relativePath)) {
externalId = externalIdFactory.createPathExternalId(DETECT_FORGE, relativePath);
} else {
// Relativize from the parent.
externalId = externalIdFactory.createPathExternalId(DETECT_FORGE, FileNameUtils.relativizeParent(detectSourcePath.getAbsolutePath(), sourcePath.getAbsolutePath()));
}
logger.debug("The external id that was created is: {}", Arrays.asList(externalId.getExternalIdPieces()));
} else {
externalId = codeLocation.getExternalId().get();
}
Optional<String> dockerImageName = extraction.getMetaData(DockerExtractor.DOCKER_IMAGE_NAME_META_DATA);
DetectCodeLocation detectCodeLocation = dockerImageName.map(s -> DetectCodeLocation.forDocker(codeLocation.getDependencyGraph(), sourcePath, externalId, s)).orElseGet(() -> DetectCodeLocation.forCreator(codeLocation.getDependencyGraph(), sourcePath, externalId, overrideName));
detectCodeLocations.put(codeLocation, detectCodeLocation);
}
return detectCodeLocations;
}
use of com.synopsys.integration.detectable.detectable.codelocation.CodeLocation in project synopsys-detect by blackducksoftware.
the class DetectorTool method performDetectors.
public DetectorToolResult performDetectors(File directory, DetectorRuleSet detectorRuleSet, DetectorFinderOptions detectorFinderOptions, DetectorEvaluationOptions evaluationOptions, String projectDetector, List<DetectorType> requiredDetectors, FileFinder fileFinder) {
logger.debug("Initializing detector system.");
Optional<DetectorEvaluationTree> possibleRootEvaluation;
logger.debug("Starting detector file system traversal.");
possibleRootEvaluation = detectorFinder.findDetectors(directory, detectorRuleSet, detectorFinderOptions, fileFinder);
if (!possibleRootEvaluation.isPresent()) {
logger.error("The source directory could not be searched for detectors - detector tool failed.");
logger.error("Please ensure the provided source path is a directory and detect has access.");
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_CONFIGURATION, "Detector tool failed to run on the configured source path.");
return new DetectorToolResult();
}
DetectorEvaluationTree rootEvaluation = possibleRootEvaluation.get();
List<DetectorEvaluation> detectorEvaluations = rootEvaluation.allDescendentEvaluations();
logger.trace("Setting up detector events.");
// DetectorNameVersionHandler detectorNameVersionHandler = createNameVersionHandler(projectDetector);
DetectorEvaluatorBroadcaster eventBroadcaster = new DetectorEvaluatorBroadcaster(eventSystem);
DetectorEvaluator detectorEvaluator = new DetectorEvaluator(evaluationOptions, extractionEnvironmentProvider::createExtractionEnvironment);
detectorEvaluator.setDetectorEvaluatorListener(eventBroadcaster);
detectorEvaluator.registerPostApplicableCallback(detectorAggregateEvaluationResult -> {
detectorEventPublisher.publishApplicableCompleted(detectorAggregateEvaluationResult.getApplicableDetectorTypesRecursively());
detectorEventPublisher.publishSearchCompleted(detectorAggregateEvaluationResult.getEvaluationTree());
logger.info("");
});
detectorEvaluator.registerPostExtractableCallback(detectorAggregateEvaluationResult -> {
detectorEventPublisher.publishPreparationsCompleted(detectorAggregateEvaluationResult.getEvaluationTree());
logger.debug("Counting detectors that will be evaluated.");
Integer extractionCount = detectorAggregateEvaluationResult.getExtractionCount();
detectorEventPublisher.publishExtractionCount(extractionCount);
logger.debug("Total number of detectors: {}", extractionCount);
});
detectorEvaluator.registerPostExtractionCallback(detectorAggregateEvaluationResult -> detectorEventPublisher.publishExtractionsCompleted(detectorAggregateEvaluationResult.getEvaluationTree()));
DetectorAggregateEvaluationResult evaluationResult = detectorEvaluator.evaluate(rootEvaluation);
// TODO- finished extractions?
logger.debug("Finished detectors.");
printExplanations(rootEvaluation);
Map<DetectorType, StatusType> statusMap = extractStatus(detectorEvaluations);
publishStatusEvents(statusMap);
publishFileEvents(detectorEvaluations);
detectorIssuePublisher.publishEvents(statusEventPublisher, rootEvaluation);
publishMissingDetectorEvents(requiredDetectors, evaluationResult.getApplicableDetectorTypesRecursively());
Map<CodeLocation, DetectCodeLocation> codeLocationMap = createCodeLocationMap(detectorEvaluations, directory);
DetectorEvaluationNameVersionDecider detectorEvaluationNameVersionDecider = new DetectorEvaluationNameVersionDecider(new DetectorNameVersionDecider());
Optional<NameVersion> bomToolProjectNameVersion = detectorEvaluationNameVersionDecider.decideSuggestion(detectorEvaluations, projectDetector);
logger.debug("Finished evaluating detectors for project info.");
DetectorToolResult detectorToolResult = new DetectorToolResult(bomToolProjectNameVersion.orElse(null), new ArrayList<>(codeLocationMap.values()), evaluationResult.getApplicableDetectorTypes(), new HashSet<>(), rootEvaluation, codeLocationMap);
// Completed.
logger.debug("Finished running detectors.");
detectorEventPublisher.publishDetectorsComplete(detectorToolResult);
return detectorToolResult;
}
use of com.synopsys.integration.detectable.detectable.codelocation.CodeLocation in project synopsys-detect by blackducksoftware.
the class DetectableTool method extract.
public DetectableToolResult extract() {
// TODO: Move docker/bazel out of detectable and drop this notion of a detctable tool. Will simplify this logic and make this unneccessary.
DetectableResult extractable;
try {
extractable = detectable.extractable();
} catch (DetectableException e) {
extractable = new ExceptionDetectableResult(e);
}
if (!extractable.getPassed()) {
logger.error(String.format("Was not extractable: %s", extractable.toDescription()));
statusEventPublisher.publishIssue(new DetectIssue(DetectIssueType.DETECTABLE_TOOL, "Detectable Tool Issue", Arrays.asList(extractable.toDescription())));
statusEventPublisher.publishStatusSummary(new Status(name, StatusType.FAILURE));
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_GENERAL_ERROR, extractable.toDescription());
return DetectableToolResult.failed(extractable);
}
logger.debug("Extractable passed.");
ExtractionEnvironment extractionEnvironment = extractionEnvironmentProvider.createExtractionEnvironment(name);
Extraction extraction;
try {
extraction = detectable.extract(extractionEnvironment);
} catch (ExecutableFailedException | ExecutableRunnerException | JsonSyntaxException | IOException | CycleDetectedException | DetectableException | MissingExternalIdException | ParserConfigurationException | SAXException e) {
extraction = new Extraction.Builder().exception(e).build();
}
if (!extraction.isSuccess()) {
logger.error("Extraction was not success.");
List<String> errorMessages = collectErrorMessages(extraction);
statusEventPublisher.publishIssue(new DetectIssue(DetectIssueType.DETECTABLE_TOOL, "Detectable Tool Issue", errorMessages));
statusEventPublisher.publishStatusSummary(new Status(name, StatusType.FAILURE));
exitCodePublisher.publishExitCode(new ExitCodeRequest(ExitCodeType.FAILURE_GENERAL_ERROR, extraction.getDescription()));
return DetectableToolResult.failed();
} else {
logger.debug("Extraction success.");
statusEventPublisher.publishStatusSummary(new Status(name, StatusType.SUCCESS));
}
Map<CodeLocation, DetectCodeLocation> detectCodeLocationMap = codeLocationConverter.toDetectCodeLocation(sourcePath, extraction, sourcePath, name);
List<DetectCodeLocation> detectCodeLocations = new ArrayList<>(detectCodeLocationMap.values());
DockerTargetData dockerTargetData = DockerTargetData.fromExtraction(extraction);
DetectToolProjectInfo projectInfo = null;
if (StringUtils.isNotBlank(extraction.getProjectName()) || StringUtils.isNotBlank(extraction.getProjectVersion())) {
NameVersion nameVersion = new NameVersion(extraction.getProjectName(), extraction.getProjectVersion());
projectInfo = new DetectToolProjectInfo(detectTool, nameVersion);
}
logger.debug("Tool finished.");
return DetectableToolResult.success(detectCodeLocations, projectInfo, dockerTargetData);
}
Aggregations