Search in sources :

Example 1 with PackagistPackage

use of com.synopsys.integration.detectable.detectables.packagist.model.PackagistPackage in project synopsys-detect by blackducksoftware.

the class PackagistParser method getDependencyGraphFromProject.

// TODO: Why are we dealing with JsonObjects rather than Gson straight to classes? Is this to avoid TypeAdapters? If so... smh JM-01/2022
public PackagistParseResult getDependencyGraphFromProject(String composerJsonText, String composerLockText) throws MissingExternalIdException {
    LazyExternalIdDependencyGraphBuilder builder = new LazyExternalIdDependencyGraphBuilder();
    JsonObject composerJsonObject = new JsonParser().parse(composerJsonText).getAsJsonObject();
    NameVersion projectNameVersion = parseNameVersionFromJson(composerJsonObject);
    JsonObject composerLockObject = new JsonParser().parse(composerLockText).getAsJsonObject();
    List<PackagistPackage> models = convertJsonToModel(composerLockObject);
    List<NameVersion> rootPackages = parseDependencies(composerJsonObject);
    models.forEach(it -> {
        ExternalId id = externalIdFactory.createNameVersionExternalId(Forge.PACKAGIST, it.getNameVersion().getName(), it.getNameVersion().getVersion());
        LazyId dependencyId = LazyId.fromName(it.getNameVersion().getName());
        builder.setDependencyInfo(dependencyId, it.getNameVersion().getName(), it.getNameVersion().getVersion(), id);
        if (isRootPackage(it.getNameVersion(), rootPackages)) {
            builder.addChildToRoot(dependencyId);
        }
        it.getDependencies().forEach(child -> {
            if (existsInPackages(child, models)) {
                LazyId childId = LazyId.fromName(child.getName());
                builder.addChildWithParent(childId, dependencyId);
            } else {
                logger.warn("Dependency was not found in packages list but found a require that used it: " + child.getName());
            }
        });
    });
    DependencyGraph graph = builder.build();
    CodeLocation codeLocation;
    if (projectNameVersion.getName() == null || projectNameVersion.getVersion() == null) {
        codeLocation = new CodeLocation(graph);
    } else {
        codeLocation = new CodeLocation(graph, externalIdFactory.createNameVersionExternalId(Forge.PACKAGIST, projectNameVersion.getName(), projectNameVersion.getVersion()));
    }
    return new PackagistParseResult(projectNameVersion.getName(), projectNameVersion.getVersion(), codeLocation);
}
Also used : PackagistPackage(com.synopsys.integration.detectable.detectables.packagist.model.PackagistPackage) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) NameVersion(com.synopsys.integration.util.NameVersion) PackagistParseResult(com.synopsys.integration.detectable.detectables.packagist.model.PackagistParseResult) LazyExternalIdDependencyGraphBuilder(com.synopsys.integration.bdio.graph.builder.LazyExternalIdDependencyGraphBuilder) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) JsonObject(com.google.gson.JsonObject) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) JsonParser(com.google.gson.JsonParser) LazyId(com.synopsys.integration.bdio.graph.builder.LazyId)

Example 2 with PackagistPackage

use of com.synopsys.integration.detectable.detectables.packagist.model.PackagistPackage in project synopsys-detect by blackducksoftware.

the class PackagistParser method convertJsonToModel.

private List<PackagistPackage> convertJsonToModel(JsonArray packagesProperty) {
    List<PackagistPackage> packages = new ArrayList<>();
    packagesProperty.forEach(it -> {
        if (it.isJsonObject()) {
            JsonObject itObject = it.getAsJsonObject();
            NameVersion nameVersion = parseNameVersionFromJson(itObject);
            List<NameVersion> dependencies = parseDependencies(itObject);
            packages.add(new PackagistPackage(nameVersion, dependencies));
        }
    });
    return packages;
}
Also used : PackagistPackage(com.synopsys.integration.detectable.detectables.packagist.model.PackagistPackage) NameVersion(com.synopsys.integration.util.NameVersion) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject)

Aggregations

JsonObject (com.google.gson.JsonObject)2 PackagistPackage (com.synopsys.integration.detectable.detectables.packagist.model.PackagistPackage)2 NameVersion (com.synopsys.integration.util.NameVersion)2 JsonParser (com.google.gson.JsonParser)1 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)1 LazyExternalIdDependencyGraphBuilder (com.synopsys.integration.bdio.graph.builder.LazyExternalIdDependencyGraphBuilder)1 LazyId (com.synopsys.integration.bdio.graph.builder.LazyId)1 ExternalId (com.synopsys.integration.bdio.model.externalid.ExternalId)1 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)1 PackagistParseResult (com.synopsys.integration.detectable.detectables.packagist.model.PackagistParseResult)1 ArrayList (java.util.ArrayList)1