Search in sources :

Example 1 with ExitCodeRequest

use of com.synopsys.integration.detect.lifecycle.shutdown.ExitCodeRequest 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);
}
Also used : ExecutableFailedException(com.synopsys.integration.detectable.detectable.executable.ExecutableFailedException) NameVersion(com.synopsys.integration.util.NameVersion) CycleDetectedException(com.synopsys.integration.detectable.util.CycleDetectedException) ArrayList(java.util.ArrayList) SAXException(org.xml.sax.SAXException) DetectIssue(com.synopsys.integration.detect.workflow.status.DetectIssue) ExtractionEnvironment(com.synopsys.integration.detectable.extraction.ExtractionEnvironment) Extraction(com.synopsys.integration.detectable.extraction.Extraction) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DockerTargetData(com.synopsys.integration.detect.lifecycle.run.data.DockerTargetData) Status(com.synopsys.integration.detect.workflow.status.Status) DetectCodeLocation(com.synopsys.integration.detect.workflow.codelocation.DetectCodeLocation) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) ExitCodeRequest(com.synopsys.integration.detect.lifecycle.shutdown.ExitCodeRequest) IOException(java.io.IOException) MissingExternalIdException(com.synopsys.integration.bdio.graph.builder.MissingExternalIdException) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) JsonSyntaxException(com.google.gson.JsonSyntaxException) DetectCodeLocation(com.synopsys.integration.detect.workflow.codelocation.DetectCodeLocation) DetectableResult(com.synopsys.integration.detectable.detectable.result.DetectableResult) ExceptionDetectableResult(com.synopsys.integration.detectable.detectable.result.ExceptionDetectableResult) ExceptionDetectableResult(com.synopsys.integration.detectable.detectable.result.ExceptionDetectableResult) DetectToolProjectInfo(com.synopsys.integration.detect.workflow.project.DetectToolProjectInfo) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException)

Example 2 with ExitCodeRequest

use of com.synopsys.integration.detect.lifecycle.shutdown.ExitCodeRequest in project synopsys-detect by blackducksoftware.

the class ExitManager method exit.

public ExitResult exit(ExitOptions exitOptions) {
    long startTime = exitOptions.getStartTime();
    boolean forceSuccessExit = exitOptions.shouldForceSuccessExit();
    boolean shouldExit = exitOptions.shouldExit();
    // Generally, when requesting a failure status, an exit code is also requested, but if it is not, we default to an unknown error.
    if (statusManager.hasAnyFailure()) {
        eventSystem.publishEvent(Event.ExitCode, new ExitCodeRequest(ExitCodeType.FAILURE_UNKNOWN_ERROR, "A failure status was requested by one or more of Detect's tools."));
    }
    // Find the final (as requested) exit code
    ExitCodeType finalExitCode = exitCodeManager.getWinningExitCode();
    // Print detect's status
    statusManager.logDetectResults(new Slf4jIntLogger(logger), finalExitCode);
    // Print duration of run
    long endTime = System.currentTimeMillis();
    String duration = DurationFormatUtils.formatPeriod(startTime, endTime, "HH'h' mm'm' ss's' SSS'ms'");
    logger.info("Detect duration: {}", duration);
    // Exit with formal exit code
    if (finalExitCode != ExitCodeType.SUCCESS && forceSuccessExit) {
        logger.warn("Forcing success: Exiting with exit code 0. Ignored exit code was {}.", finalExitCode.getExitCode());
    } else if (finalExitCode != ExitCodeType.SUCCESS) {
        logger.error("Exiting with code {} - {}", finalExitCode.getExitCode(), finalExitCode);
    }
    if (!shouldExit) {
        logger.info("Would normally exit({}) but it is overridden.", finalExitCode.getExitCode());
    }
    return new ExitResult(finalExitCode, forceSuccessExit, shouldExit);
}
Also used : ExitCodeType(com.synopsys.integration.detect.configuration.enumeration.ExitCodeType) Slf4jIntLogger(com.synopsys.integration.log.Slf4jIntLogger) ExitCodeRequest(com.synopsys.integration.detect.lifecycle.shutdown.ExitCodeRequest)

Example 3 with ExitCodeRequest

use of com.synopsys.integration.detect.lifecycle.shutdown.ExitCodeRequest in project synopsys-detect by blackducksoftware.

the class ExitManagerTest method testForceExit.

@Test
public void testForceExit() {
    long startTime = System.currentTimeMillis();
    EventSystem eventSystem = new EventSystem();
    DetectStatusManager statusManager = new DetectStatusManager(eventSystem);
    ExceptionUtility exitCodeUtility = new ExceptionUtility();
    ExitCodeManager exitCodeManager = new ExitCodeManager(eventSystem, exitCodeUtility);
    exitCodeManager.addExitCodeRequest(new ExitCodeRequest(ExitCodeType.FAILURE_CONFIGURATION, "JUnit failure code."));
    ExitManager exitManager = new ExitManager(eventSystem, exitCodeManager, statusManager);
    ExitOptions exitOptions = new ExitOptions(startTime, true, true);
    ExitResult exitResult = exitManager.exit(exitOptions);
    assertEquals(startTime, exitOptions.getStartTime());
    assertTrue(exitResult.shouldForceSuccess());
    assertTrue(exitResult.shouldPerformExit());
    assertEquals(ExitCodeType.FAILURE_CONFIGURATION, exitResult.getExitCodeType());
}
Also used : ExitCodeRequest(com.synopsys.integration.detect.lifecycle.shutdown.ExitCodeRequest) ExceptionUtility(com.synopsys.integration.detect.lifecycle.shutdown.ExceptionUtility) EventSystem(com.synopsys.integration.detect.workflow.event.EventSystem) DetectStatusManager(com.synopsys.integration.detect.workflow.status.DetectStatusManager) ExitCodeManager(com.synopsys.integration.detect.lifecycle.shutdown.ExitCodeManager) Test(org.junit.jupiter.api.Test)

Example 4 with ExitCodeRequest

use of com.synopsys.integration.detect.lifecycle.shutdown.ExitCodeRequest in project synopsys-detect by blackducksoftware.

the class ExitManagerTest method testSkipExit.

@Test
public void testSkipExit() {
    long startTime = System.currentTimeMillis();
    EventSystem eventSystem = new EventSystem();
    DetectStatusManager statusManager = new DetectStatusManager(eventSystem);
    ExceptionUtility exitCodeUtility = new ExceptionUtility();
    ExitCodeManager exitCodeManager = new ExitCodeManager(eventSystem, exitCodeUtility);
    exitCodeManager.addExitCodeRequest(new ExitCodeRequest(ExitCodeType.FAILURE_CONFIGURATION, "JUnit failure code."));
    ExitManager exitManager = new ExitManager(eventSystem, exitCodeManager, statusManager);
    ExitOptions exitOptions = new ExitOptions(startTime, false, false);
    ExitResult exitResult = exitManager.exit(exitOptions);
    assertEquals(startTime, exitOptions.getStartTime());
    assertFalse(exitResult.shouldForceSuccess());
    assertFalse(exitResult.shouldPerformExit());
    assertEquals(ExitCodeType.FAILURE_CONFIGURATION, exitResult.getExitCodeType());
}
Also used : ExitCodeRequest(com.synopsys.integration.detect.lifecycle.shutdown.ExitCodeRequest) ExceptionUtility(com.synopsys.integration.detect.lifecycle.shutdown.ExceptionUtility) EventSystem(com.synopsys.integration.detect.workflow.event.EventSystem) DetectStatusManager(com.synopsys.integration.detect.workflow.status.DetectStatusManager) ExitCodeManager(com.synopsys.integration.detect.lifecycle.shutdown.ExitCodeManager) Test(org.junit.jupiter.api.Test)

Example 5 with ExitCodeRequest

use of com.synopsys.integration.detect.lifecycle.shutdown.ExitCodeRequest in project synopsys-detect by blackducksoftware.

the class DetectorTool method publishMissingDetectorEvents.

private void publishMissingDetectorEvents(List<DetectorType> requiredDetectors, Set<DetectorType> applicable) {
    Set<DetectorType> missingDetectors = requiredDetectors.stream().filter(it -> !applicable.contains(it)).collect(Collectors.toSet());
    if (!missingDetectors.isEmpty()) {
        String missingDetectorDisplay = missingDetectors.stream().map(Enum::toString).collect(Collectors.joining(","));
        logger.error("One or more required detector types were not found: {}", missingDetectorDisplay);
        exitCodePublisher.publishExitCode(new ExitCodeRequest(ExitCodeType.FAILURE_DETECTOR_REQUIRED));
    }
}
Also used : DetectorNameVersionDecider(com.synopsys.integration.detect.workflow.nameversion.DetectorNameVersionDecider) StatusEventPublisher(com.synopsys.integration.detect.workflow.status.StatusEventPublisher) DetectorEvaluationUtils(com.synopsys.integration.detect.workflow.report.util.DetectorEvaluationUtils) DetectorStatus(com.synopsys.integration.detect.workflow.status.DetectorStatus) ExtractionEnvironmentProvider(com.synopsys.integration.detect.tool.detector.extraction.ExtractionEnvironmentProvider) DetectorFinder(com.synopsys.integration.detector.finder.DetectorFinder) UnrecognizedPaths(com.synopsys.integration.detect.workflow.status.UnrecognizedPaths) LoggerFactory(org.slf4j.LoggerFactory) DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) ReportConstants(com.synopsys.integration.detect.workflow.report.util.ReportConstants) FileFinder(com.synopsys.integration.common.util.finder.FileFinder) DetectorFinderOptions(com.synopsys.integration.detector.finder.DetectorFinderOptions) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) NameVersion(com.synopsys.integration.util.NameVersion) EventSystem(com.synopsys.integration.detect.workflow.event.EventSystem) Map(java.util.Map) DetectorType(com.synopsys.integration.detector.base.DetectorType) DetectorRuleSet(com.synopsys.integration.detector.rule.DetectorRuleSet) DetectCodeLocation(com.synopsys.integration.detect.workflow.codelocation.DetectCodeLocation) ExitCodeType(com.synopsys.integration.detect.configuration.enumeration.ExitCodeType) Logger(org.slf4j.Logger) EnumMap(java.util.EnumMap) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) File(java.io.File) StatusType(com.synopsys.integration.detect.workflow.status.StatusType) DetectorEvaluation(com.synopsys.integration.detector.base.DetectorEvaluation) List(java.util.List) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) DetectorEvaluator(com.synopsys.integration.detector.evaluation.DetectorEvaluator) DetectorEvaluationOptions(com.synopsys.integration.detector.evaluation.DetectorEvaluationOptions) Optional(java.util.Optional) ExitCodePublisher(com.synopsys.integration.detect.lifecycle.shutdown.ExitCodePublisher) DetectorAggregateEvaluationResult(com.synopsys.integration.detector.evaluation.DetectorAggregateEvaluationResult) DetectorEvaluationNameVersionDecider(com.synopsys.integration.detect.workflow.nameversion.DetectorEvaluationNameVersionDecider) ExitCodeRequest(com.synopsys.integration.detect.lifecycle.shutdown.ExitCodeRequest) DetectorType(com.synopsys.integration.detector.base.DetectorType) ExitCodeRequest(com.synopsys.integration.detect.lifecycle.shutdown.ExitCodeRequest)

Aggregations

ExitCodeRequest (com.synopsys.integration.detect.lifecycle.shutdown.ExitCodeRequest)6 ExitCodeType (com.synopsys.integration.detect.configuration.enumeration.ExitCodeType)3 EventSystem (com.synopsys.integration.detect.workflow.event.EventSystem)3 ExceptionUtility (com.synopsys.integration.detect.lifecycle.shutdown.ExceptionUtility)2 ExitCodeManager (com.synopsys.integration.detect.lifecycle.shutdown.ExitCodeManager)2 ExitCodePublisher (com.synopsys.integration.detect.lifecycle.shutdown.ExitCodePublisher)2 DetectCodeLocation (com.synopsys.integration.detect.workflow.codelocation.DetectCodeLocation)2 DetectIssue (com.synopsys.integration.detect.workflow.status.DetectIssue)2 DetectStatusManager (com.synopsys.integration.detect.workflow.status.DetectStatusManager)2 StatusEventPublisher (com.synopsys.integration.detect.workflow.status.StatusEventPublisher)2 StatusType (com.synopsys.integration.detect.workflow.status.StatusType)2 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)2 NameVersion (com.synopsys.integration.util.NameVersion)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Test (org.junit.jupiter.api.Test)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 MissingExternalIdException (com.synopsys.integration.bdio.graph.builder.MissingExternalIdException)1