Search in sources :

Example 1 with DistTag

use of org.commonjava.indy.pkg.npm.model.DistTag in project indy by Commonjava.

the class PackageMetadataGenerator method writePackageMetadata.

private boolean writePackageMetadata(List<StoreResource> firstLevelFiles, ArtifactStore store, String path, EventMetadata eventMetadata) throws IndyWorkflowException {
    logger.debug("writePackageMetadata, firstLevelFiles:{}, store:{}", firstLevelFiles, store.getKey());
    // Parse the path of the tar (e.g.: jquery/-/jquery-7.6.1.tgz or @types/jquery/-/jquery-2.2.3.tgz)
    // to get the version, then try to get the version metadata by the path (@scoped/)package/version
    List<PackagePath> packagePaths = firstLevelFiles.stream().map((res) -> new PackagePath(res.getPath())).sorted(Comparator.comparing(PackagePath::getVersion)).collect(Collectors.toList());
    if (packagePaths.size() == 0) {
        return false;
    }
    final Transfer metadataFile = fileManager.getTransfer(store, path);
    final PackageMetadata packageMetadata = new PackageMetadata();
    final IndyObjectMapper mapper = new IndyObjectMapper(true);
    List<String> keywords = new ArrayList<>();
    DistTag distTags = new DistTag();
    Map<String, VersionMetadata> versions = new LinkedHashMap<>();
    PackagePath latest = packagePaths.get(packagePaths.size() - 1);
    for (PackagePath packagePath : packagePaths) {
        String versionPath = packagePath.getVersionPath();
        logger.debug("Retrieving the version file {} from store {}", versionPath, store);
        Transfer metaFile = fileManager.retrieveRaw(store, versionPath, eventMetadata);
        if (metaFile == null) {
            // The metadata file (@scoped/)package/version for the specific version is missing, still need to extract it from tarball
            String tarPath = packagePath.getTarPath();
            Transfer tar = fileManager.retrieveRaw(store, tarPath, eventMetadata);
            if (tar == null) {
                logger.warn("Tarball file {} is missing in the store {}.", tarPath, store.getKey());
                continue;
            }
            logger.info("Extracting package metadata package.json from tarball {} and store it in {}/{}", tarPath, store.getKey(), versionPath);
            metaFile = extractMetaFileFromTarballAndStore(store, versionPath, tar);
            if (metaFile == null) {
                logger.warn("Package metadata is missing in tarball {}/{}.", store.getKey(), tarPath);
                continue;
            }
        }
        try (InputStream input = metaFile.openInputStream()) {
            VersionMetadata versionMetadata = mapper.readValue(input, VersionMetadata.class);
            if (versionMetadata == null) {
                continue;
            }
            versions.put(versionMetadata.getVersion(), versionMetadata);
            if (versionMetadata.getKeywords() != null) {
                for (String keyword : versionMetadata.getKeywords()) {
                    if (!keywords.contains(keyword)) {
                        keywords.add(keyword);
                    }
                }
            }
            // Set couple of attributes based on the latest version metadata
            if (packagePath.getVersion().equals(latest.getVersion())) {
                packageMetadata.setName(versionMetadata.getName());
                packageMetadata.setDescription(versionMetadata.getDescription());
                packageMetadata.setAuthor(versionMetadata.getAuthor());
                if (versionMetadata.getLicense() != null) {
                    packageMetadata.setLicense(versionMetadata.getLicense());
                }
                packageMetadata.setRepository(versionMetadata.getRepository());
                packageMetadata.setBugs(versionMetadata.getBugs());
                distTags.setLatest(versionMetadata.getVersion());
            }
            // Generate tarball url if missing
            if (versionMetadata.getDist() == null) {
                // here we use mock host. indy will amend it with the right hostname
                String tarball = "http://indy/" + packagePath.getTarPath();
                // logger.debug( "Generate dist tarball: {}", tarball );
                versionMetadata.setDist(new Dist(null, tarball));
            }
        } catch (IOException e) {
            logger.error("Get the version metadata error from path {}", versionPath, e);
            throw new IndyWorkflowException("Get the version metadata error from path {}", versionPath);
        }
    }
    if (!keywords.isEmpty()) {
        packageMetadata.setKeywords(keywords);
    }
    versions = sortVersions(versions);
    packageMetadata.setVersions(versions);
    packageMetadata.setDistTags(distTags);
    OutputStream stream = null;
    try {
        String output = mapper.writeValueAsString(packageMetadata);
        stream = metadataFile.openOutputStream(TransferOperation.GENERATE, true, eventMetadata);
        stream.write(output.getBytes(StandardCharsets.UTF_8));
    } catch (IOException e) {
        throw new IndyWorkflowException("Generating package metadata failure in store {}", store.getKey());
    } finally {
        closeQuietly(stream);
    }
    logger.debug("writePackageMetadata, DONE, store: {}", store.getKey());
    return true;
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) InputStream(java.io.InputStream) Dist(org.commonjava.indy.pkg.npm.model.Dist) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) DistTag(org.commonjava.indy.pkg.npm.model.DistTag) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) IndyObjectMapper(org.commonjava.indy.model.core.io.IndyObjectMapper) VersionMetadata(org.commonjava.indy.pkg.npm.model.VersionMetadata) PackageMetadata(org.commonjava.indy.pkg.npm.model.PackageMetadata) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Transfer(org.commonjava.maven.galley.model.Transfer)

Example 2 with DistTag

use of org.commonjava.indy.pkg.npm.model.DistTag in project indy by Commonjava.

the class NPMMetadataTimeoutTest method test.

@Test
@Category(TimingDependent.class)
public void test() throws Exception {
    IndyObjectMapper mapper = new IndyObjectMapper(true);
    final PackageMetadata src = new PackageMetadata();
    final DistTag dts = new DistTag();
    dts.setBeta("1");
    src.setDistTags(dts);
    server.expect("GET", server.formatUrl(REPO, PATH_JQUERY), (req, resp) -> {
        resp.setStatus(200);
        mapper.writeValue(resp.getWriter(), src);
        resp.getWriter().flush();
    });
    server.expect("GET", server.formatUrl(REPO, PATH_BABEL_PARSER), (req, resp) -> {
        resp.setStatus(200);
        mapper.writeValue(resp.getWriter(), src);
        resp.getWriter().flush();
    });
    final RemoteRepository repo = new RemoteRepository(NPM_PKG_KEY, REPO, server.formatUrl(REPO));
    repo.setMetadataTimeoutSeconds(1);
    client.stores().create(repo, "adding npm remote repo", RemoteRepository.class);
    // First retrieval
    verifyMetadataBetaTag("1", PATH_JQUERY, repo);
    assertThat("Metadata not retrieved!", client.content().exists(repo.getKey(), PATH_JQUERY, true), equalTo(true));
    assertThat("Metadata not retrieved!", client.content().exists(repo.getKey(), PATH_JQUERY + "/package.json", true), equalTo(true));
    verifyMetadataBetaTag("1", PATH_BABEL_PARSER, repo);
    assertThat("Metadata not retrieved!", client.content().exists(repo.getKey(), PATH_BABEL_PARSER, true), equalTo(true));
    assertThat("Metadata not retrieved!", client.content().exists(repo.getKey(), PATH_BABEL_PARSER + "/package.json", true), equalTo(true));
    // wait for repo metadata timeout
    Thread.sleep(3000);
    assertThat("Metadata not cleaned up!", client.content().exists(repo.getKey(), PATH_JQUERY, true), equalTo(false));
    assertThat("Metadata not cleaned up!", client.content().exists(repo.getKey(), PATH_JQUERY + "/package.json", true), equalTo(false));
    assertThat("Metadata not cleaned up!", client.content().exists(repo.getKey(), PATH_BABEL_PARSER, true), equalTo(false));
    assertThat("Metadata not cleaned up!", client.content().exists(repo.getKey(), PATH_BABEL_PARSER + "/package.json", true), equalTo(false));
    logger.info("\n\n\n\nRE-REQUEST STARTS HERE\n\n\n\n");
    // Second retrieval
    dts.setBeta("2");
    verifyMetadataBetaTag("2", PATH_JQUERY, repo);
    verifyMetadataBetaTag("2", PATH_BABEL_PARSER, repo);
}
Also used : IndyObjectMapper(org.commonjava.indy.model.core.io.IndyObjectMapper) PackageMetadata(org.commonjava.indy.pkg.npm.model.PackageMetadata) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) DistTag(org.commonjava.indy.pkg.npm.model.DistTag) Category(org.junit.experimental.categories.Category) Test(org.junit.Test) AbstractContentManagementTest(org.commonjava.indy.ftest.core.AbstractContentManagementTest)

Example 3 with DistTag

use of org.commonjava.indy.pkg.npm.model.DistTag in project indy by Commonjava.

the class PackageMetadataMergerTest method mergeProviderWithDistTagVersionSorted.

@Test
public void mergeProviderWithDistTagVersionSorted() throws Exception {
    String path = "jquery";
    HostedRepository h1 = new HostedRepository(NPM_PKG_KEY, "test-hosted-1");
    Transfer t1 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h1), path));
    initTestData(t1, VERSION_META + "package-dist-tag-version.json");
    Group g = new Group(NPM_PKG_KEY, "test-group", h1.getKey());
    List<Transfer> sources = Arrays.asList(t1);
    PackageMetadata provided = new PackageMetadata("jquery");
    DistTag distTag = new DistTag();
    distTag.putTag(DistTag.LATEST, "3.2.3-rc.3");
    distTag.putTag(DistTag.STABLE, "2.0");
    distTag.putTag(DistTag.BETA, "3.2.1-beta.2");
    distTag.putTag(DistTag.DEV, "3.2.3-alpha");
    provided.setDistTags(distTag);
    TestPackageMetadataProvider testProvider = new TestPackageMetadataProvider(provided);
    byte[] output = new PackageMetadataMerger(Collections.singletonList(testProvider), mapper).merge(sources, g, path);
    PackageMetadata merged = mapper.readValue(IOUtils.toString(new ByteArrayInputStream(output)), PackageMetadata.class);
    assertThat(merged.getDistTags().getLatest(), equalTo("3.2.3-rc.3"));
    assertThat(merged.getDistTags().getStable(), equalTo("3.2.1"));
    assertThat(merged.getDistTags().getBeta(), equalTo("3.2.1-beta.2"));
    assertThat(merged.getDistTags().getDev(), equalTo("3.2.3-alpha"));
}
Also used : Group(org.commonjava.indy.model.core.Group) ByteArrayInputStream(java.io.ByteArrayInputStream) PackageMetadata(org.commonjava.indy.pkg.npm.model.PackageMetadata) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) DistTag(org.commonjava.indy.pkg.npm.model.DistTag) HostedRepository(org.commonjava.indy.model.core.HostedRepository) Test(org.junit.Test)

Example 4 with DistTag

use of org.commonjava.indy.pkg.npm.model.DistTag in project indy by Commonjava.

the class NPMMetadataRevisitTimeoutTest method test.

@Test
@Category(TimingDependent.class)
public void test() throws Exception {
    IndyObjectMapper mapper = new IndyObjectMapper(true);
    final PackageMetadata src = new PackageMetadata();
    final DistTag dts = new DistTag();
    dts.setBeta("1");
    src.setDistTags(dts);
    server.expect("GET", server.formatUrl(REPO, PATH), (req, resp) -> {
        resp.setStatus(200);
        mapper.writeValue(resp.getWriter(), src);
        resp.getWriter().flush();
    });
    final int METADATA_TIMEOUT_SECOND = 3;
    final int TIMEOUT_WAIT_MILLI_S = 1000;
    final RemoteRepository repo = new RemoteRepository(NPM_PKG_KEY, REPO, server.formatUrl(REPO));
    logger.info("\n\nCreate repo: {}\n\n", repo);
    repo.setMetadataTimeoutSeconds(METADATA_TIMEOUT_SECOND);
    client.stores().create(repo, "adding npm remote repo", RemoteRepository.class);
    logger.info("\n\nFirst retrieval\n\n");
    // First retrieval
    verifyMetadataBetaTag("1", repo);
    dts.setBeta("2");
    // wait for repo metadata timeout
    Thread.sleep(TIMEOUT_WAIT_MILLI_S * 2);
    logger.info("\n\nSecond retrieval: try to reset timeout\n\n");
    // Second retrieval when not timeout for first
    verifyMetadataBetaTag("1", repo);
    // wait for repo metadata timeout
    Thread.sleep(TIMEOUT_WAIT_MILLI_S * 2);
    assertThat("Metadata not cleaned up!", client.content().exists(repo.getKey(), PATH + "/package.json", true), equalTo(false));
    logger.info("\n\nThird retrieval: check if timeout happened\n\n");
    // Third retrieval
    verifyMetadataBetaTag("2", repo);
}
Also used : IndyObjectMapper(org.commonjava.indy.model.core.io.IndyObjectMapper) PackageMetadata(org.commonjava.indy.pkg.npm.model.PackageMetadata) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) DistTag(org.commonjava.indy.pkg.npm.model.DistTag) Category(org.junit.experimental.categories.Category) Test(org.junit.Test) AbstractContentManagementTest(org.commonjava.indy.ftest.core.AbstractContentManagementTest)

Aggregations

DistTag (org.commonjava.indy.pkg.npm.model.DistTag)4 PackageMetadata (org.commonjava.indy.pkg.npm.model.PackageMetadata)4 IndyObjectMapper (org.commonjava.indy.model.core.io.IndyObjectMapper)3 Test (org.junit.Test)3 AbstractContentManagementTest (org.commonjava.indy.ftest.core.AbstractContentManagementTest)2 RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)2 Transfer (org.commonjava.maven.galley.model.Transfer)2 Category (org.junit.experimental.categories.Category)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)1 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)1 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)1 Group (org.commonjava.indy.model.core.Group)1 HostedRepository (org.commonjava.indy.model.core.HostedRepository)1 Dist (org.commonjava.indy.pkg.npm.model.Dist)1