Search in sources :

Example 31 with DetectorEvaluationTree

use of com.synopsys.integration.detector.base.DetectorEvaluationTree in project synopsys-detect by blackducksoftware.

the class ExtractionEvaluator method extractionEvaluation.

public void extractionEvaluation(DetectorEvaluationTree detectorEvaluationTree) {
    logger.trace("Extracting detectors in the directory: {}", detectorEvaluationTree.getDirectory());
    for (DetectorEvaluation detectorEvaluation : detectorEvaluationTree.getOrderedEvaluations()) {
        if (detectorEvaluation.isExtractable() && detectorEvaluation.getExtractionEnvironment() != null) {
            logger.trace("Detector was searchable, applicable and extractable, will perform extraction: {}", detectorEvaluation.getDetectorRule().getDescriptiveName());
            Detectable detectable = detectorEvaluation.getDetectable();
            getDetectorEvaluatorListener().ifPresent(it -> it.extractionStarted(detectorEvaluation));
            try {
                Extraction extraction = detectable.extract(detectorEvaluation.getExtractionEnvironment());
                detectorEvaluation.setExtraction(extraction);
            } catch (Exception e) {
                detectorEvaluation.setExtraction(new Extraction.Builder().exception(e).build());
            }
            getDetectorEvaluatorListener().ifPresent(it -> it.extractionEnded(detectorEvaluation));
            logger.trace("Extraction result: {}", detectorEvaluation.wasExtractionSuccessful());
        }
    }
    for (DetectorEvaluationTree childDetectorEvaluationTree : detectorEvaluationTree.getChildren()) {
        extractionEvaluation(childDetectorEvaluationTree);
    }
}
Also used : DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) Detectable(com.synopsys.integration.detectable.Detectable) Extraction(com.synopsys.integration.detectable.extraction.Extraction) DetectorEvaluation(com.synopsys.integration.detector.base.DetectorEvaluation)

Example 32 with DetectorEvaluationTree

use of com.synopsys.integration.detector.base.DetectorEvaluationTree in project synopsys-detect by blackducksoftware.

the class ApplicableEvaluator method searchAndApplicableEvaluation.

public void searchAndApplicableEvaluation(DetectorEvaluationTree detectorEvaluationTree, Set<DetectorRule> appliedInParent) {
    logger.trace("Determining applicable detectors on the directory: {}", detectorEvaluationTree.getDirectory());
    Set<DetectorRule> appliedSoFar = new HashSet<>();
    for (DetectorEvaluation detectorEvaluation : detectorEvaluationTree.getOrderedEvaluations()) {
        getDetectorEvaluatorListener().ifPresent(it -> it.applicableStarted(detectorEvaluation));
        DetectorRule detectorRule = detectorEvaluation.getDetectorRule();
        logger.trace("Evaluating detector: {}", detectorRule.getDescriptiveName());
        SearchEnvironment searchEnvironment = new SearchEnvironment(detectorEvaluationTree.getDepthFromRoot(), getEvaluationOptions().getDetectorFilter(), getEvaluationOptions().isForceNested(), getEvaluationOptions().isFollowSymLinks(), appliedInParent, appliedSoFar);
        detectorEvaluation.setSearchEnvironment(searchEnvironment);
        DetectorResult searchableResult = detectorRuleSetEvaluator.evaluateSearchable(detectorEvaluationTree.getDetectorRuleSet(), detectorEvaluation.getDetectorRule(), searchEnvironment);
        detectorEvaluation.setSearchable(searchableResult);
        if (detectorEvaluation.isSearchable()) {
            logger.trace("Searchable passed, will continue evaluating.");
            // TODO: potential todo, this could be invoked as part of the rule - ie we make a DetectableEnvironmentCreatable and the file could be given to the creatable (detectorRule.createEnvironment(file)
            DetectableEnvironment detectableEnvironment = new DetectableEnvironment(detectorEvaluationTree.getDirectory());
            detectorEvaluation.setDetectableEnvironment(detectableEnvironment);
            Detectable detectable = detectorRule.createDetectable(detectableEnvironment);
            detectorEvaluation.setDetectable(detectable);
            DetectableResult applicable = detectable.applicable();
            DetectorResult applicableResult = new DetectorResult(applicable.getPassed(), applicable.toDescription(), applicable.getClass(), applicable.getExplanation(), applicable.getRelevantFiles());
            detectorEvaluation.setApplicable(applicableResult);
            if (detectorEvaluation.isApplicable()) {
                logger.trace("Found applicable detector: {}", detectorRule.getDescriptiveName());
                appliedSoFar.add(detectorRule);
            } else {
                logger.trace("Applicable did not pass: {}", detectorEvaluation.getApplicabilityMessage());
            }
        } else {
            logger.trace("Searchable did not pass: {}", detectorEvaluation.getSearchabilityMessage());
        }
        getDetectorEvaluatorListener().ifPresent(it -> it.applicableEnded(detectorEvaluation));
    }
    if (!appliedSoFar.isEmpty()) {
        // TODO: Perfect log level also matters here. To little and we may appear stuck, but we may also be flooding the logs.
        logger.debug("Found ({}) applicable detectors in: {}", appliedSoFar.size(), detectorEvaluationTree.getDirectory());
    }
    Set<DetectorRule> nextAppliedInParent = new HashSet<>();
    nextAppliedInParent.addAll(appliedInParent);
    nextAppliedInParent.addAll(appliedSoFar);
    for (DetectorEvaluationTree childDetectorEvaluationTree : detectorEvaluationTree.getChildren()) {
        searchAndApplicableEvaluation(childDetectorEvaluationTree, nextAppliedInParent);
    }
}
Also used : DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) DetectorRule(com.synopsys.integration.detector.rule.DetectorRule) Detectable(com.synopsys.integration.detectable.Detectable) DetectableResult(com.synopsys.integration.detectable.detectable.result.DetectableResult) DetectorResult(com.synopsys.integration.detector.result.DetectorResult) DetectorEvaluation(com.synopsys.integration.detector.base.DetectorEvaluation) DetectableEnvironment(com.synopsys.integration.detectable.DetectableEnvironment) HashSet(java.util.HashSet)

Example 33 with DetectorEvaluationTree

use of com.synopsys.integration.detector.base.DetectorEvaluationTree in project synopsys-detect by blackducksoftware.

the class DetectorIssuePublisher method publishEvents.

private void publishEvents(StatusEventPublisher statusEventPublisher, List<DetectorEvaluationTree> trees) {
    String spacer = "\t";
    for (DetectorEvaluationTree tree : trees) {
        List<DetectorEvaluation> excepted = DetectorEvaluationUtils.filteredChildren(tree, DetectorEvaluation::wasExtractionException);
        List<DetectorEvaluation> failed = DetectorEvaluationUtils.filteredChildren(tree, DetectorEvaluation::wasExtractionFailure);
        List<DetectorEvaluation> notExtractable = DetectorEvaluationUtils.filteredChildren(tree, evaluation -> evaluation.isApplicable() && !evaluation.isExtractable());
        List<String> messages = new ArrayList<>();
        addIfNotEmpty(messages, "Not Extractable: ", spacer, notExtractable, DetectorEvaluation::getExtractabilityMessage);
        addIfNotEmpty(messages, "Failure: ", spacer, failed, detectorEvaluation -> detectorEvaluation.getExtraction().getDescription());
        addIfNotEmpty(messages, "Exception: ", spacer, excepted, detectorEvaluation -> ExceptionUtil.oneSentenceDescription(detectorEvaluation.getExtraction().getError()));
        if (messages.size() > 0) {
            messages.add(0, tree.getDirectory().toString());
            statusEventPublisher.publishIssue(new DetectIssue(DetectIssueType.DETECTOR, "Detector Issue", messages));
        }
    }
}
Also used : DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) DetectIssue(com.synopsys.integration.detect.workflow.status.DetectIssue) ArrayList(java.util.ArrayList) DetectorEvaluation(com.synopsys.integration.detector.base.DetectorEvaluation)

Example 34 with DetectorEvaluationTree

use of com.synopsys.integration.detector.base.DetectorEvaluationTree in project synopsys-detect by blackducksoftware.

the class DetectorToolTest method createEvaluationTree.

private DetectorEvaluationTree createEvaluationTree(Extraction extraction, DetectableResult extractionResult, File directory, DetectorRule<GoModCliDetectable> rule, DetectorRuleSet detectorRuleSet) {
    DetectorEvaluation detectorEvaluation = new DetectorEvaluation(rule);
    DetectorResult extractableResult = new DetectorResult(extractionResult.getPassed(), extractionResult.toDescription(), extractionResult.getClass(), Collections.emptyList(), Collections.emptyList());
    detectorEvaluation.setExtractable(extractableResult);
    detectorEvaluation.setExtraction(extraction);
    detectorEvaluation.setApplicable(new DetectorResult(true, "", Collections.emptyList(), Collections.emptyList()));
    detectorEvaluation.setSearchable(new DetectorResult(true, "", Collections.emptyList(), Collections.emptyList()));
    detectorEvaluation.setDetectableEnvironment(new DetectableEnvironment(new File("")));
    return new DetectorEvaluationTree(directory, 0, detectorRuleSet, Collections.singletonList(detectorEvaluation), new HashSet<>());
}
Also used : DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) DetectorResult(com.synopsys.integration.detector.result.DetectorResult) DetectorEvaluation(com.synopsys.integration.detector.base.DetectorEvaluation) File(java.io.File) DetectableEnvironment(com.synopsys.integration.detectable.DetectableEnvironment)

Example 35 with DetectorEvaluationTree

use of com.synopsys.integration.detector.base.DetectorEvaluationTree in project synopsys-detect by blackducksoftware.

the class ExtractionSummaryReporter method writeSummary.

public void writeSummary(ReportWriter writer, DetectorEvaluationTree rootEvaluation, Map<CodeLocation, DetectCodeLocation> detectableMap, Map<DetectCodeLocation, String> codeLocationNameMap, boolean writeCodeLocationNames) {
    ReporterUtils.printHeader(writer, "Extraction results:");
    boolean printedAny = false;
    for (DetectorEvaluationTree it : rootEvaluation.asFlatList()) {
        List<DetectorEvaluation> success = DetectorEvaluationUtils.filteredChildren(it, DetectorEvaluation::wasExtractionSuccessful);
        List<DetectorEvaluation> exception = DetectorEvaluationUtils.filteredChildren(it, DetectorEvaluation::wasExtractionException);
        List<DetectorEvaluation> failed = DetectorEvaluationUtils.filteredChildren(it, DetectorEvaluation::wasExtractionFailure);
        if (success.size() > 0 || exception.size() > 0 || failed.size() > 0) {
            writer.writeLine(it.getDirectory().toString());
            List<String> codeLocationNames = findCodeLocationNames(it, detectableMap, codeLocationNameMap);
            writer.writeLine("\tCode locations: " + codeLocationNames.size());
            if (writeCodeLocationNames) {
                codeLocationNames.forEach(name -> writer.writeLine("\t\t" + name));
            }
            writeEvaluationsIfNotEmpty(writer, "\tSuccess: ", success);
            writeEvaluationsIfNotEmpty(writer, "\tFailure: ", failed);
            writeEvaluationsIfNotEmpty(writer, "\tException: ", exception);
            printedAny = true;
        }
    }
    if (!printedAny) {
        writer.writeLine("There were no extractions to be summarized - no code locations were generated or no detectors were evaluated.");
    }
    ReporterUtils.printFooter(writer);
}
Also used : DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) DetectorEvaluation(com.synopsys.integration.detector.base.DetectorEvaluation)

Aggregations

DetectorEvaluationTree (com.synopsys.integration.detector.base.DetectorEvaluationTree)37 DetectorEvaluation (com.synopsys.integration.detector.base.DetectorEvaluation)28 File (java.io.File)18 Test (org.junit.jupiter.api.Test)17 DetectableEnvironment (com.synopsys.integration.detectable.DetectableEnvironment)11 Detectable (com.synopsys.integration.detectable.Detectable)10 DetectorRule (com.synopsys.integration.detector.rule.DetectorRule)9 DetectorResult (com.synopsys.integration.detector.result.DetectorResult)8 DetectorRuleSet (com.synopsys.integration.detector.rule.DetectorRuleSet)8 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)8 ExtractionEnvironment (com.synopsys.integration.detectable.extraction.ExtractionEnvironment)7 Predicate (java.util.function.Predicate)7 Mockito (org.mockito.Mockito)7 DetectableResult (com.synopsys.integration.detectable.detectable.result.DetectableResult)6 List (java.util.List)6 DetectableException (com.synopsys.integration.detectable.detectable.exception.DetectableException)5 Extraction (com.synopsys.integration.detectable.extraction.Extraction)5 ArrayList (java.util.ArrayList)5 Collections (java.util.Collections)5 HashSet (java.util.HashSet)5