Search in sources :

Example 11 with DependencyBuilder

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

the class PipBuildInfoExtractor method createDependenciesFromAqlResult.

private Map<String, Dependency> createDependenciesFromAqlResult(AqlSearchResult searchResult, Map<String, String> fileToPackage, Log logger) {
    if (searchResult.getResults().isEmpty()) {
        return Collections.emptyMap();
    }
    Map<String, Dependency> dependenciesMap = new HashMap<>();
    for (AqlSearchResult.SearchEntry searchEntry : searchResult.getResults()) {
        if (!isResultComplete(searchEntry)) {
            continue;
        }
        // Avoid adding duplicated dependencies.
        if (dependenciesMap.containsKey(fileToPackage.get(searchEntry.getName()))) {
            continue;
        }
        // Add a new dependency only if searchEntry represents a package downloaded in this build execution.
        if (fileToPackage.containsKey(searchEntry.getName())) {
            Dependency curDep = new DependencyBuilder().id(searchEntry.getName()).md5(searchEntry.getActualMd5()).sha1(searchEntry.getActualSha1()).build();
            dependenciesMap.put(fileToPackage.get(searchEntry.getName()), curDep);
        }
    }
    Set<String> missingFiles = fileToPackage.keySet().stream().filter(x -> !dependenciesMap.containsKey(fileToPackage.get(x))).collect(Collectors.toSet());
    promptMissingChecksumFromArtifactory(missingFiles, logger);
    return dependenciesMap;
}
Also used : Arrays(java.util.Arrays) ArtifactoryManager(org.jfrog.build.extractor.clientConfiguration.client.artifactory.ArtifactoryManager) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) ModuleBuilder(org.jfrog.build.extractor.builder.ModuleBuilder) Collectors(java.util.stream.Collectors) DependencyBuilder(org.jfrog.build.extractor.builder.DependencyBuilder) ModuleType(org.jfrog.build.api.builder.ModuleType) ArrayList(java.util.ArrayList) Log(org.jfrog.build.api.util.Log) List(java.util.List) Module(org.jfrog.build.extractor.ci.Module) Map(java.util.Map) BuildInfo(org.jfrog.build.extractor.ci.BuildInfo) AqlSearchResult(org.jfrog.build.api.search.AqlSearchResult) Path(java.nio.file.Path) Collections(java.util.Collections) Dependency(org.jfrog.build.extractor.ci.Dependency) AqlSearchResult(org.jfrog.build.api.search.AqlSearchResult) HashMap(java.util.HashMap) DependencyBuilder(org.jfrog.build.extractor.builder.DependencyBuilder) Dependency(org.jfrog.build.extractor.ci.Dependency)

Example 12 with DependencyBuilder

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

the class DependenciesCacheTest method dependenciesCacheTest.

@Test
public void dependenciesCacheTest() {
    Map<String, Dependency> cacheMap = new HashMap<>();
    // Create depA.
    Dependency depA = new DependencyBuilder().id("depenA-1.0-A.zip").sha1("sha1A").md5("md5A").build();
    cacheMap.put("depA", depA);
    // Create depC.
    Dependency depC = new DependencyBuilder().id("depenC-3.4-C.gzip").sha1("sha1C").md5("md5C").build();
    cacheMap.put("depC", depC);
    // Test cache methods.
    Path projectDir = null;
    try {
        projectDir = TestUtils.createTempDir("pipCacheTest");
        // Read empty cache.
        DependenciesCache nullCache = DependenciesCache.getProjectDependenciesCache(projectDir, log);
        assertNull(nullCache, "Expecting null cache read.");
        // Write cache.
        DependenciesCache.updateDependenciesCache(cacheMap, projectDir);
        // Read existing cache.
        DependenciesCache readCache = DependenciesCache.getProjectDependenciesCache(projectDir, log);
        assertNotNull(readCache, "Expecting cache read.");
        // Validate cache content.
        Dependency depATest = readCache.getDependency("depA");
        assertEquals(depATest, depA, String.format("Actual: %s\nExpected: %s", mapper.writeValueAsString(depATest), mapper.writeValueAsString(depA)));
        Dependency depCTest = readCache.getDependency("depC");
        assertEquals(depCTest, depC, String.format("Actual: %s\nExpected: %s", mapper.writeValueAsString(depCTest), mapper.writeValueAsString(depC)));
        Dependency nonExistingDepTTest = readCache.getDependency("depT");
        assertNull(nonExistingDepTTest, String.format("Actual: %s\nExpected null", mapper.writeValueAsString(nonExistingDepTTest)));
    } catch (IOException e) {
        fail(ExceptionUtils.getStackTrace(e));
    } finally {
        if (projectDir != null) {
            FileUtils.deleteQuietly(projectDir.toFile());
        }
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) DependencyBuilder(org.jfrog.build.extractor.builder.DependencyBuilder) Dependency(org.jfrog.build.extractor.ci.Dependency) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 13 with DependencyBuilder

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

the class GoRun method addDependency.

/**
 * Each dependency is in format <name>:<version>.
 * We add only the pgk zip file as build's dependency.
 * The dependency's id is "module-name:version", and its type is "zip".
 * We locate each pkg zip file downloaded to local Go cache, and calculate the pkg checksum.
 */
private void addDependency(String dependencyId, String cachePath) throws Exception {
    String[] dependencyIdSplit = StringUtils.split(dependencyId, ":");
    String name = dependencyIdSplit[0];
    String version = "v" + dependencyIdSplit[1];
    String cachedPkgPath = cachePath + convertModuleNameToCachePathConvention(name) + File.separator + "@v" + File.separator + version + ".zip";
    File moduleZip = new File(cachedPkgPath);
    if (moduleZip.exists()) {
        Map<String, String> checksums = FileChecksumCalculator.calculateChecksums(moduleZip, MD5_ALGORITHM, SHA1_ALGORITHM, SHA256_ALGORITHM);
        Dependency dependency = new DependencyBuilder().id(name + ":" + version).md5(checksums.get(MD5_ALGORITHM)).sha1(checksums.get(SHA1_ALGORITHM)).sha256(checksums.get(SHA256_ALGORITHM)).type("zip").build();
        dependenciesList.add(dependency);
    }
}
Also used : DependencyBuilder(org.jfrog.build.extractor.builder.DependencyBuilder) Dependency(org.jfrog.build.extractor.ci.Dependency) File(java.io.File)

Example 14 with DependencyBuilder

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

the class DockerImage method setDependenciesAndArtifacts.

/**
 * Search the docker image in Artifactory and add all artifacts & dependencies into Module.
 */
private void setDependenciesAndArtifacts(ModuleBuilder moduleBuilder, ArtifactoryManager artifactoryManager) throws IOException {
    DockerLayer historyLayer = layers.getByDigest(imageId);
    if (historyLayer == null) {
        throw new IllegalStateException("Could not find the history docker layer: " + imageId + " for image: " + imageTag + " in Artifactory.");
    }
    int dependencyLayerNum = DockerUtils.getNumberOfDependentLayers(artifactoryManager.download(historyLayer.getFullPath()).getContent());
    LinkedHashSet<Dependency> dependencies = new LinkedHashSet<>();
    LinkedHashSet<Artifact> artifacts = new LinkedHashSet<>();
    // Filter out duplicate layers from manifest by using HashSet.
    // Docker manifest may hold 'empty layers', as a result, docker promote will fail to promote the same layer more than once.
    Iterator<String> it = DockerUtils.getLayersDigests(manifest).iterator();
    for (int i = 0; i < dependencyLayerNum; i++) {
        String digest = it.next();
        DockerLayer layer = layers.getByDigest(digest);
        Dependency dependency = new DependencyBuilder().id(layer.getFileName()).sha1(layer.getSha1()).build();
        dependencies.add(dependency);
        Artifact artifact = new ArtifactBuilder(layer.getFileName()).sha1(layer.getSha1()).remotePath(layer.getPath()).build();
        artifacts.add(artifact);
    }
    moduleBuilder.dependencies(new ArrayList<>(dependencies));
    while (it.hasNext()) {
        String digest = it.next();
        DockerLayer layer = layers.getByDigest(digest);
        if (layer == null) {
            continue;
        }
        Artifact artifact = new ArtifactBuilder(layer.getFileName()).sha1(layer.getSha1()).remotePath(layer.getPath()).build();
        artifacts.add(artifact);
    }
    moduleBuilder.artifacts(new ArrayList<>(artifacts));
}
Also used : Dependency(org.jfrog.build.extractor.ci.Dependency) ArtifactBuilder(org.jfrog.build.extractor.builder.ArtifactBuilder) Artifact(org.jfrog.build.extractor.ci.Artifact) DependencyBuilder(org.jfrog.build.extractor.builder.DependencyBuilder)

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