Search in sources :

Example 1 with DetectCodeLocation

use of com.blackducksoftware.integration.hub.detect.model.DetectCodeLocation in project hub-detect by blackducksoftware.

the class DetectProjectManager method createDetectProject.

public DetectProject createDetectProject() throws IntegrationException {
    final DetectProject detectProject = new DetectProject();
    final EnumSet<BomToolType> applicableBomTools = EnumSet.noneOf(BomToolType.class);
    for (final BomTool bomTool : bomTools) {
        final BomToolType bomToolType = bomTool.getBomToolType();
        final String bomToolTypeString = bomToolType.toString();
        try {
            if (!detectConfiguration.shouldRun(bomTool)) {
                logger.debug(String.format("Skipping %s.", bomToolTypeString));
                continue;
            }
            logger.info(String.format("%s applies given the current configuration.", bomToolTypeString));
            bomToolSummaryResults.put(bomTool.getBomToolType(), Result.FAILURE);
            foundAnyBomTools = true;
            final List<DetectCodeLocation> codeLocations = bomTool.extractDetectCodeLocations(detectProject);
            if (codeLocations != null && codeLocations.size() > 0) {
                bomToolSummaryResults.put(bomTool.getBomToolType(), Result.SUCCESS);
                detectProject.addAllDetectCodeLocations(codeLocations);
                applicableBomTools.add(bomToolType);
            } else {
                logger.error(String.format("Did not find any projects from %s even though it applied.", bomToolTypeString));
            }
        } catch (final Exception e) {
            // any bom tool failure should not prevent other bom tools from running
            logger.error(String.format("%s threw an Exception: %s", bomToolTypeString, e.getMessage()));
            // log the stacktrace if and only if running at trace level
            if (logger.isTraceEnabled()) {
                logger.error("Exception details: ", e);
            }
        }
    }
    // we've gone through all applicable bom tools so we now have the
    // complete metadata to phone home
    detectPhoneHomeManager.startPhoneHome(applicableBomTools);
    final String prefix = detectConfiguration.getProjectCodeLocationPrefix();
    final String suffix = detectConfiguration.getProjectCodeLocationSuffix();
    // ensure that the project name is set, use some reasonable defaults
    detectProject.setProjectDetails(getProjectName(detectProject.getProjectName()), getProjectVersionName(detectProject.getProjectVersionName()), prefix, suffix);
    if (!foundAnyBomTools) {
        logger.info(String.format("No package managers were detected - will register %s for signature scanning of %s/%s", detectConfiguration.getSourcePath(), detectProject.getProjectName(), detectProject.getProjectVersionName()));
        hubSignatureScanner.registerPathToScan(ScanPathSource.DETECT_SOURCE, detectConfiguration.getSourceDirectory());
    } else if (detectConfiguration.getHubSignatureScannerSnippetMode()) {
        logger.info(String.format("Snippet mode is enabled - will register %s for signature scanning of %s/%s", detectConfiguration.getSourcePath(), detectProject.getProjectName(), detectProject.getProjectVersionName()));
        hubSignatureScanner.registerPathToScan(ScanPathSource.SNIPPET_SOURCE, detectConfiguration.getSourceDirectory());
    }
    if (StringUtils.isBlank(detectConfiguration.getAggregateBomName())) {
        detectProject.processDetectCodeLocations(logger, detectFileManager, bdioFileNamer, codeLocationNameService);
        for (final BomToolType bomToolType : detectProject.getFailedBomTools()) {
            bomToolSummaryResults.put(bomToolType, Result.FAILURE);
        }
    }
    return detectProject;
}
Also used : DetectProject(com.blackducksoftware.integration.hub.detect.model.DetectProject) BomTool(com.blackducksoftware.integration.hub.detect.bomtool.BomTool) DetectCodeLocation(com.blackducksoftware.integration.hub.detect.model.DetectCodeLocation) BomToolType(com.blackducksoftware.integration.hub.detect.model.BomToolType) DetectUserFriendlyException(com.blackducksoftware.integration.hub.detect.exception.DetectUserFriendlyException) IOException(java.io.IOException) IntegrationException(com.blackducksoftware.integration.exception.IntegrationException)

Example 2 with DetectCodeLocation

use of com.blackducksoftware.integration.hub.detect.model.DetectCodeLocation in project hub-detect by blackducksoftware.

the class GradleReportParser method parseDependencies.

public DetectCodeLocation parseDependencies(final DetectProject detectProject, final InputStream dependenciesInputStream) {
    clearState();
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(dependenciesInputStream, StandardCharsets.UTF_8))) {
        while (reader.ready()) {
            final String line = reader.readLine();
            /**
             * The meta data section will be at the end of the file after all of the "gradle dependencies" output
             */
            if (line.startsWith("DETECT META DATA START")) {
                processingMetaData = true;
                continue;
            }
            if (line.startsWith("DETECT META DATA END")) {
                processingMetaData = false;
                continue;
            }
            if (processingMetaData) {
                processMetaDataLine(line);
                continue;
            }
            if (StringUtils.isBlank(line)) {
                clearConfigurationState();
                continue;
            }
            final Dependency nextDependency = gradleReportConfigurationParser.parseDependency(externalIdFactory, line);
            if (nextDependency == null) {
                continue;
            }
            final int lineTreeLevel = gradleReportConfigurationParser.getTreeLevel();
            if (lineTreeLevel == previousTreeLevel + 1) {
                nodeStack.push(previousNode);
            } else if (lineTreeLevel < previousTreeLevel) {
                for (int times = 0; times < (previousTreeLevel - lineTreeLevel); times++) {
                    nodeStack.pop();
                }
            } else if (lineTreeLevel != previousTreeLevel) {
                logger.error(String.format("The tree level (%s) and this line (%s) with count %s can't be reconciled.", previousTreeLevel, line, lineTreeLevel));
            }
            if (nodeStack.size() == 0) {
                graph.addChildToRoot(nextDependency);
            } else {
                graph.addChildWithParents(nextDependency, nodeStack.peek());
            }
            previousNode = nextDependency;
            previousTreeLevel = lineTreeLevel;
        }
    } catch (final Exception e) {
        logger.error("Exception parsing gradle output: " + e.getMessage());
    }
    detectProject.setProjectNameIfNotSet(rootProjectName);
    detectProject.setProjectVersionNameIfNotSet(rootProjectVersionName);
    final ExternalId id = externalIdFactory.createMavenExternalId(projectGroup, projectName, projectVersionName);
    final DetectCodeLocation detectCodeLocation = new DetectCodeLocation.Builder(BomToolType.GRADLE, projectSourcePath, id, graph).bomToolProjectName(projectName).bomToolProjectVersionName(projectVersionName).build();
    return detectCodeLocation;
}
Also used : InputStreamReader(java.io.InputStreamReader) DetectCodeLocation(com.blackducksoftware.integration.hub.detect.model.DetectCodeLocation) ExternalId(com.blackducksoftware.integration.hub.bdio.model.externalid.ExternalId) BufferedReader(java.io.BufferedReader) Dependency(com.blackducksoftware.integration.hub.bdio.model.dependency.Dependency)

Example 3 with DetectCodeLocation

use of com.blackducksoftware.integration.hub.detect.model.DetectCodeLocation in project hub-detect by blackducksoftware.

the class DetectProjectManager method createBdioFiles.

public List<File> createBdioFiles(final DetectProject detectProject) throws DetectUserFriendlyException {
    final List<File> bdioFiles = new ArrayList<>();
    final MutableDependencyGraph aggregateDependencyGraph = simpleBdioFactory.createMutableDependencyGraph();
    if (StringUtils.isBlank(detectConfiguration.getAggregateBomName())) {
        for (final String codeLocationNameString : detectProject.getCodeLocationNameStrings()) {
            final DetectCodeLocation detectCodeLocation = detectProject.getDetectCodeLocation(codeLocationNameString);
            final String bdioFileName = detectProject.getBdioFilename(codeLocationNameString);
            final SimpleBdioDocument simpleBdioDocument = createSimpleBdioDocument(codeLocationNameString, detectProject.getProjectName(), detectProject.getProjectVersionName(), detectCodeLocation);
            final File outputFile = new File(detectConfiguration.getBdioOutputDirectoryPath(), bdioFileName);
            if (outputFile.exists()) {
                final boolean deleteSuccess = outputFile.delete();
                logger.debug(String.format("%s deleted: %b", outputFile.getAbsolutePath(), deleteSuccess));
            }
            writeBdioFile(outputFile, simpleBdioDocument);
            bdioFiles.add(outputFile);
        }
    } else {
        for (final DetectCodeLocation detectCodeLocation : detectProject.getDetectCodeLocations()) {
            if (detectCodeLocation.getDependencyGraph() == null) {
                logger.warn(String.format("Dependency graph is null for code location %s", detectCodeLocation.getSourcePath()));
                continue;
            }
            if (detectCodeLocation.getDependencyGraph().getRootDependencies().size() <= 0) {
                logger.warn(String.format("Could not find any dependencies for code location %s", detectCodeLocation.getSourcePath()));
            }
            aggregateDependencyGraph.addGraphAsChildrenToRoot(detectCodeLocation.getDependencyGraph());
        }
        final SimpleBdioDocument aggregateBdioDocument = createAggregateSimpleBdioDocument(detectProject.getProjectName(), detectProject.getProjectVersionName(), aggregateDependencyGraph);
        final String filename = String.format("%s.jsonld", integrationEscapeUtil.escapeForUri(detectConfiguration.getAggregateBomName()));
        final File aggregateBdioFile = new File(detectConfiguration.getOutputDirectory(), filename);
        if (aggregateBdioFile.exists()) {
            final boolean deleteSuccess = aggregateBdioFile.delete();
            logger.debug(String.format("%s deleted: %b", aggregateBdioFile.getAbsolutePath(), deleteSuccess));
        }
        writeBdioFile(aggregateBdioFile, aggregateBdioDocument);
    }
    return bdioFiles;
}
Also used : MutableDependencyGraph(com.blackducksoftware.integration.hub.bdio.graph.MutableDependencyGraph) DetectCodeLocation(com.blackducksoftware.integration.hub.detect.model.DetectCodeLocation) ArrayList(java.util.ArrayList) File(java.io.File) SimpleBdioDocument(com.blackducksoftware.integration.hub.bdio.model.SimpleBdioDocument)

Aggregations

DetectCodeLocation (com.blackducksoftware.integration.hub.detect.model.DetectCodeLocation)3 IntegrationException (com.blackducksoftware.integration.exception.IntegrationException)1 MutableDependencyGraph (com.blackducksoftware.integration.hub.bdio.graph.MutableDependencyGraph)1 SimpleBdioDocument (com.blackducksoftware.integration.hub.bdio.model.SimpleBdioDocument)1 Dependency (com.blackducksoftware.integration.hub.bdio.model.dependency.Dependency)1 ExternalId (com.blackducksoftware.integration.hub.bdio.model.externalid.ExternalId)1 BomTool (com.blackducksoftware.integration.hub.detect.bomtool.BomTool)1 DetectUserFriendlyException (com.blackducksoftware.integration.hub.detect.exception.DetectUserFriendlyException)1 BomToolType (com.blackducksoftware.integration.hub.detect.model.BomToolType)1 DetectProject (com.blackducksoftware.integration.hub.detect.model.DetectProject)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1