Search in sources :

Example 6 with DependencyBuilder

use of org.jfrog.build.extractor.builder.DependencyBuilder in project build-info by JFrogDev.

the class DockerImage method setDependencies.

private void setDependencies(ModuleBuilder moduleBuilder) throws IOException {
    LinkedHashSet<Dependency> dependencies = new LinkedHashSet<>();
    // Docker manifest may hold 'empty layers', as a result, docker promote will fail to promote the same layer more than once.
    for (String digest : DockerUtils.getLayersDigests(manifest)) {
        DockerLayer layer = layers.getByDigest(digest);
        Dependency dependency = new DependencyBuilder().id(layer.getFileName()).sha1(layer.getSha1()).build();
        dependencies.add(dependency);
    }
    moduleBuilder.dependencies(new ArrayList<>(dependencies));
}
Also used : DependencyBuilder(org.jfrog.build.extractor.builder.DependencyBuilder) Dependency(org.jfrog.build.extractor.ci.Dependency)

Example 7 with DependencyBuilder

use of org.jfrog.build.extractor.builder.DependencyBuilder in project build-info by JFrogDev.

the class GradleModuleExtractor method calculateDependencies.

private List<Dependency> calculateDependencies(Project project, String moduleId) throws Exception {
    ArtifactoryDependencyResolutionListener artifactoryDependencyResolutionListener = project.getRootProject().getPlugins().getPlugin(ArtifactoryPlugin.class).getArtifactoryDependencyResolutionListener();
    Map<String, String[][]> requestedByMap = artifactoryDependencyResolutionListener.getModulesHierarchyMap().get(moduleId);
    Set<Configuration> configurationSet = project.getConfigurations();
    List<Dependency> dependencies = newArrayList();
    for (Configuration configuration : configurationSet) {
        if (configuration.getState() != Configuration.State.RESOLVED) {
            log.info("Artifacts for configuration '{}' were not all resolved, skipping", configuration.getName());
            continue;
        }
        ResolvedConfiguration resolvedConfiguration = configuration.getResolvedConfiguration();
        Set<ResolvedArtifact> resolvedArtifactSet = resolvedConfiguration.getResolvedArtifacts();
        for (final ResolvedArtifact artifact : resolvedArtifactSet) {
            File file = artifact.getFile();
            if (file.exists()) {
                ModuleVersionIdentifier id = artifact.getModuleVersion().getId();
                final String depId = getModuleIdString(id.getGroup(), id.getName(), id.getVersion());
                // if it's already in the dependencies list just add the current scope
                Dependency existingDependency = dependencies.stream().filter(input -> input.getId().equals(depId)).findAny().orElse(null);
                if (existingDependency != null) {
                    Set<String> existingScopes = existingDependency.getScopes();
                    existingScopes.add(configuration.getName());
                    existingDependency.setScopes(existingScopes);
                } else {
                    DependencyBuilder dependencyBuilder = new DependencyBuilder().type(getTypeString(artifact.getType(), artifact.getClassifier(), artifact.getExtension())).id(depId).scopes(Sets.newHashSet(configuration.getName()));
                    if (requestedByMap != null) {
                        dependencyBuilder.requestedBy(requestedByMap.get(depId));
                    }
                    if (file.isFile()) {
                        // In recent gradle builds (3.4+) subproject dependencies are represented by a dir not jar.
                        Map<String, String> checksums = FileChecksumCalculator.calculateChecksums(file, MD5_ALGORITHM, SHA1_ALGORITHM, SHA256_ALGORITHM);
                        dependencyBuilder.md5(checksums.get(MD5_ALGORITHM)).sha1(checksums.get(SHA1_ALGORITHM)).sha256(checksums.get(SHA256_ALGORITHM));
                    }
                    dependencies.add(dependencyBuilder.build());
                }
            }
        }
    }
    return dependencies;
}
Also used : ResolvedArtifact(org.gradle.api.artifacts.ResolvedArtifact) Configuration(org.gradle.api.artifacts.Configuration) ResolvedConfiguration(org.gradle.api.artifacts.ResolvedConfiguration) ArtifactoryClientConfiguration(org.jfrog.build.extractor.clientConfiguration.ArtifactoryClientConfiguration) BuildInfoExtractorUtils.getModuleIdString(org.jfrog.build.extractor.BuildInfoExtractorUtils.getModuleIdString) BuildInfoExtractorUtils.getTypeString(org.jfrog.build.extractor.BuildInfoExtractorUtils.getTypeString) Dependency(org.jfrog.build.extractor.ci.Dependency) ArtifactoryPlugin(org.jfrog.gradle.plugin.artifactory.ArtifactoryPlugin) ModuleVersionIdentifier(org.gradle.api.artifacts.ModuleVersionIdentifier) ResolvedConfiguration(org.gradle.api.artifacts.ResolvedConfiguration) DependencyBuilder(org.jfrog.build.extractor.builder.DependencyBuilder) File(java.io.File) ArtifactoryDependencyResolutionListener(org.jfrog.gradle.plugin.artifactory.extractor.listener.ArtifactoryDependencyResolutionListener)

Example 8 with DependencyBuilder

use of org.jfrog.build.extractor.builder.DependencyBuilder in project build-info by JFrogDev.

the class BuildExtractorUtilsTest method testBuildToJson.

public void testBuildToJson() throws IOException {
    String[] requestedByA = new String[] { "parentA", "b", "moduleId" };
    String[] requestedByB = new String[] { "parentB", "d", "moduleId" };
    Dependency dependencyA = new DependencyBuilder().id("depA").addRequestedBy(requestedByA).addRequestedBy(requestedByB).build();
    Module module = new ModuleBuilder().id("moduleId").addDependency(dependencyA).build();
    BuildInfo buildInfo = new BuildInfoBuilder("buildId").number("12").started("34").addModule(module).build();
    // Serialize and deserialize again
    BuildInfo actualBuildInfo = jsonStringToBuildInfo(buildInfoToJsonString(buildInfo));
    // Check buildInfo
    assertEquals(actualBuildInfo.getName(), buildInfo.getName());
    assertEquals(actualBuildInfo.getNumber(), buildInfo.getNumber());
    assertEquals(actualBuildInfo.getStarted(), buildInfo.getStarted());
    // Check module
    Module actualModule = actualBuildInfo.getModule(module.getId());
    assertNotNull(actualModule);
    // Check dependency
    assertEquals(actualModule.getDependencies().size(), 1);
    Dependency actualDependency = actualModule.getDependencies().get(0);
    assertEquals(actualDependency.getId(), dependencyA.getId());
    // Check requestedBy
    String[][] requestedBy = actualDependency.getRequestedBy();
    assertEquals(ArrayUtils.getLength(requestedBy), 2);
    assertEquals(requestedBy[0], requestedByA);
    assertEquals(requestedBy[1], requestedByB);
}
Also used : BuildInfoBuilder(org.jfrog.build.extractor.builder.BuildInfoBuilder) DependencyBuilder(org.jfrog.build.extractor.builder.DependencyBuilder) ModuleBuilder(org.jfrog.build.extractor.builder.ModuleBuilder)

Example 9 with DependencyBuilder

use of org.jfrog.build.extractor.builder.DependencyBuilder in project build-info by JFrogDev.

the class NpmExtractorConsumer method createDependencyFromAqlResult.

/**
 * Create 'Dependency' from name and version of 'npmPackageInfo'. Try to retrieve sha1 and md5 from Artifactory.
 *
 * @param npmPackageInfo - The npm package information.
 * @param id             - The id of the dependency to create.
 * @return Dependency or null in case of an exception, or in case the dependency does not exist in Artifactory.
 */
private Dependency createDependencyFromAqlResult(NpmPackageInfo npmPackageInfo, String id) {
    String aql = String.format(NPM_AQL_FORMAT, npmPackageInfo.getName(), npmPackageInfo.getVersion());
    AqlSearchResult searchResult;
    try {
        searchResult = artifactoryManager.searchArtifactsByAql(aql);
        if (searchResult.getResults().isEmpty()) {
            return null;
        }
        DependencyBuilder builder = new DependencyBuilder();
        AqlSearchResult.SearchEntry searchEntry = searchResult.getResults().get(0);
        return builder.id(id).addScope(npmPackageInfo.getScope()).md5(searchEntry.getActualMd5()).sha1(searchEntry.getActualSha1()).build();
    } catch (IOException e) {
        log.error(ExceptionUtils.getStackTrace(e), e);
        return null;
    }
}
Also used : AqlSearchResult(org.jfrog.build.api.search.AqlSearchResult) DependencyBuilder(org.jfrog.build.extractor.builder.DependencyBuilder) IOException(java.io.IOException)

Example 10 with DependencyBuilder

use of org.jfrog.build.extractor.builder.DependencyBuilder in project build-info by JFrogDev.

the class NugetRun method collectDependenciesFromProjectAssets.

private List<Dependency> collectDependenciesFromProjectAssets(String projectAssetsPath) throws Exception {
    File projectAssets = new File(projectAssetsPath);
    List<Dependency> dependenciesList = new ArrayList<>();
    NugetProjectAssets assets = new NugetProjectAssets();
    assets.readProjectAssets(projectAssets);
    for (Map.Entry<String, NugetProjectAssets.Library> entry : assets.getLibraries().entrySet()) {
        String pkgKey = entry.getKey();
        NugetProjectAssets.Library library = entry.getValue();
        if (library.getType().equals("project")) {
            continue;
        }
        File nupkg = new File(assets.getPackagesPath(), library.getNupkgFilePath());
        if (nupkg.exists()) {
            Map<String, String> checksums = FileChecksumCalculator.calculateChecksums(nupkg, MD5_ALGORITHM, SHA1_ALGORITHM, SHA256_ALGORITHM);
            Dependency dependency = new DependencyBuilder().id(pkgKey.replace('/', ':')).md5(checksums.get(MD5_ALGORITHM)).sha1(checksums.get(SHA1_ALGORITHM)).sha256(checksums.get(SHA256_ALGORITHM)).build();
            dependenciesList.add(dependency);
        } else {
            if (isPackagePartOfTargetDependencies(library.getPath(), assets.getTargets())) {
                logger.warn(String.format("The file %s doesn't exist in the NuGet cache directory but it does exist as a target in the assets files. %s", nupkg.getPath(), ABSENT_NUPKG_WARN_MSG));
                continue;
            }
            throw new Exception(String.format("The file %s doesn't exist in the NuGet cache directory.", nupkg.getPath()));
        }
    }
    return dependenciesList;
}
Also used : Dependency(org.jfrog.build.extractor.ci.Dependency) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DependencyBuilder(org.jfrog.build.extractor.builder.DependencyBuilder) File(java.io.File) NugetProjectAssets(org.jfrog.build.extractor.nuget.types.NugetProjectAssets)

Aggregations

DependencyBuilder (org.jfrog.build.extractor.builder.DependencyBuilder)14 Dependency (org.jfrog.build.extractor.ci.Dependency)9 File (java.io.File)6 IOException (java.io.IOException)4 BuildInfoExtractorUtils.getModuleIdString (org.jfrog.build.extractor.BuildInfoExtractorUtils.getModuleIdString)3 BuildInfoExtractorUtils.getTypeString (org.jfrog.build.extractor.BuildInfoExtractorUtils.getTypeString)3 ModuleBuilder (org.jfrog.build.extractor.builder.ModuleBuilder)3 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 AqlSearchResult (org.jfrog.build.api.search.AqlSearchResult)2 Module (org.jfrog.build.extractor.ci.Module)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1