Search in sources :

Example 6 with RepositoryManagerResult

use of org.jboss.pnc.spi.repositorymanager.RepositoryManagerResult in project pnc by project-ncl.

the class IndyRepositorySession method extractBuildArtifacts.

/**
 * Retrieve tracking report from repository manager. Add each tracked download to the dependencies of the build
 * result. Add each tracked upload to the built artifacts of the build result. Promote uploaded artifacts to the
 * product-level storage. Finally delete the group associated with the completed build.
 *
 * @param liveBuild {@inheritDoc} - if true, the promotion of the collected artifacts and dependencies is done,
 *        tracking report is sealed and the build group is removed, if false it only reads the data without any
 *        actions
 */
@Override
public RepositoryManagerResult extractBuildArtifacts(final boolean liveBuild) throws RepositoryManagerException {
    TrackedContentDTO report = sealAndGetTrackingReport(liveBuild);
    Comparator<Artifact> comp = (one, two) -> one.getIdentifier().compareTo(two.getIdentifier());
    Uploads uploads = collectUploads(report);
    List<Artifact> uploadedArtifacts = uploads.getData();
    Collections.sort(uploadedArtifacts, comp);
    List<Artifact> downloadedArtifacts = null;
    String log = "";
    CompletionStatus status = CompletionStatus.SUCCESS;
    try {
        downloadedArtifacts = processDownloads(report, liveBuild);
        Collections.sort(downloadedArtifacts, comp);
    } catch (PromotionValidationException ex) {
        status = CompletionStatus.FAILED;
        log = ex.getMessage();
        logger.warn("Dependencies promotion failed. Error(s): {}", log);
        userLog.error("Dependencies promotion failed. Error(s): {}", log);
    }
    if (liveBuild) {
        deleteBuildGroup();
    }
    // if the promotion of dependencies succeeded...
    if (status == CompletionStatus.SUCCESS) {
        logger.info("Returning built artifacts / dependencies:\nUploads:\n  {}\n\nDownloads:\n  {}\n\n", StringUtils.join(uploads.getData(), "\n  "), StringUtils.join(downloadedArtifacts, "\n  "));
        if (liveBuild) {
            logger.info("BEGIN: promotion to build content set");
            StopWatch stopWatch = StopWatch.createStarted();
            try {
                promoteToBuildContentSet(uploads.getPromotion());
            } catch (PromotionValidationException ex) {
                status = CompletionStatus.FAILED;
                log = ex.getMessage();
                logger.warn("Built artifact promotion failed. Error(s): {}", log);
                userLog.error("Built artifact promotion failed. Error(s): {}", log);
            }
            logger.info("END: promotion to build content set, took: {} seconds", stopWatch.getTime(TimeUnit.SECONDS));
            stopWatch.reset();
        }
    }
    if (status == CompletionStatus.FAILED) {
        // prevent saving artifacts and dependencies to a failed build
        downloadedArtifacts = Collections.emptyList();
        uploadedArtifacts = Collections.emptyList();
    }
    return new IndyRepositoryManagerResult(uploadedArtifacts, downloadedArtifacts, buildContentId, log, status);
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Date(java.util.Date) ValidationResult(org.commonjava.indy.promote.model.ValidationResult) LoggerFactory(org.slf4j.LoggerFactory) AbstractPromoteResult(org.commonjava.indy.promote.model.AbstractPromoteResult) Map(java.util.Map) ConstraintViolation(javax.validation.ConstraintViolation) StoreKey(org.commonjava.indy.model.core.StoreKey) TargetRepository(org.jboss.pnc.model.TargetRepository) MAVEN_PKG_KEY(org.commonjava.indy.pkg.maven.model.MavenPackageTypeDescriptor.MAVEN_PKG_KEY) ArtifactPathInfo(org.commonjava.atlas.maven.ident.util.ArtifactPathInfo) PathsPromoteRequest(org.commonjava.indy.promote.model.PathsPromoteRequest) NpmPackageRef(org.commonjava.atlas.npm.ident.ref.NpmPackageRef) SimpleArtifactRef(org.commonjava.atlas.maven.ident.ref.SimpleArtifactRef) StringUtils.isEmpty(org.apache.commons.lang.StringUtils.isEmpty) RepositoryManagerException(org.jboss.pnc.spi.repositorymanager.RepositoryManagerException) TrackedContentDTO(org.commonjava.indy.folo.dto.TrackedContentDTO) Artifact(org.jboss.pnc.model.Artifact) StoreType(org.commonjava.indy.model.core.StoreType) Validator(javax.validation.Validator) Set(java.util.Set) StopWatch(org.apache.commons.lang3.time.StopWatch) Instant(java.time.Instant) IndyContentClientModule(org.commonjava.indy.client.core.module.IndyContentClientModule) IndyPromoteClientModule(org.commonjava.indy.promote.client.IndyPromoteClientModule) IOUtils(org.apache.commons.io.IOUtils) RepositoryConnectionInfo(org.jboss.pnc.spi.repositorymanager.model.RepositoryConnectionInfo) List(java.util.List) RepositorySession(org.jboss.pnc.spi.repositorymanager.model.RepositorySession) IndyFoloAdminClientModule(org.commonjava.indy.folo.client.IndyFoloAdminClientModule) ArtifactRef(org.commonjava.atlas.maven.ident.ref.ArtifactRef) IndyClientException(org.commonjava.indy.client.core.IndyClientException) TrackedContentEntryDTO(org.commonjava.indy.folo.dto.TrackedContentEntryDTO) NPM_PKG_KEY(org.commonjava.indy.pkg.npm.model.NPMPackageTypeDescriptor.NPM_PKG_KEY) PackageURL(com.github.packageurl.PackageURL) ArtifactQuality(org.jboss.pnc.enums.ArtifactQuality) ValidatorFactory(javax.validation.ValidatorFactory) HashMap(java.util.HashMap) SHARED_IMPORTS_ID(org.jboss.pnc.indyrepositorymanager.IndyRepositoryConstants.SHARED_IMPORTS_ID) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ReposiotryIdentifier(org.jboss.pnc.constants.ReposiotryIdentifier) MalformedPackageURLException(com.github.packageurl.MalformedPackageURLException) CollectionUtils(org.apache.commons.collections.CollectionUtils) RepositoryType(org.jboss.pnc.enums.RepositoryType) Validation(javax.validation.Validation) CompletionStatus(org.jboss.pnc.spi.coordinator.CompletionStatus) GENERIC_PKG_KEY(org.commonjava.indy.model.core.GenericPackageTypeDescriptor.GENERIC_PKG_KEY) Logger(org.slf4j.Logger) Indy(org.commonjava.indy.client.core.Indy) PathsPromoteResult(org.commonjava.indy.promote.model.PathsPromoteResult) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) HostedRepository(org.commonjava.indy.model.core.HostedRepository) RepositoryManagerResult(org.jboss.pnc.spi.repositorymanager.RepositoryManagerResult) PackageURLBuilder(com.github.packageurl.PackageURLBuilder) BuildCategory(org.jboss.pnc.enums.BuildCategory) Comparator(java.util.Comparator) NpmPackagePathInfo(org.commonjava.atlas.npm.ident.util.NpmPackagePathInfo) Collections(java.util.Collections) CompletionStatus(org.jboss.pnc.spi.coordinator.CompletionStatus) Artifact(org.jboss.pnc.model.Artifact) TrackedContentDTO(org.commonjava.indy.folo.dto.TrackedContentDTO) StopWatch(org.apache.commons.lang3.time.StopWatch)

Example 7 with RepositoryManagerResult

use of org.jboss.pnc.spi.repositorymanager.RepositoryManagerResult in project pnc by project-ncl.

the class DownloadTwoThenVerifyExtractedArtifactsContainThemTest method extractBuildArtifacts_ContainsTwoDownloads.

@Test
public void extractBuildArtifacts_ContainsTwoDownloads() throws Exception {
    String pomPath = "org/commonjava/indy/indy-core/0.17.0/indy-core-0.17.0.pom";
    String jarPath = "org/commonjava/indy/indy-core/0.17.0/indy-core-0.17.0.jar";
    String pomContent = "This is a pom test " + System.currentTimeMillis();
    String jarContent = "This is a jar test " + System.currentTimeMillis();
    // setup the expectation that the remote repo pointing at this server will request this file...and define its
    // content.
    server.expect(server.formatUrl(STORE, pomPath), 200, pomContent);
    server.expect(server.formatUrl(STORE, jarPath), 200, jarContent);
    // create a dummy non-chained build execution and repo session based on it
    BuildExecution execution = new TestBuildExecution();
    RepositorySession rc = driver.createBuildRepository(execution, accessToken, accessToken, RepositoryType.MAVEN, Collections.emptyMap(), false);
    assertThat(rc, notNullValue());
    String baseUrl = rc.getConnectionInfo().getDependencyUrl();
    // download the two files via the repo session's dependency URL, which will proxy the test http server
    // using the expectations above
    assertThat(download(UrlUtils.buildUrl(baseUrl, pomPath)), equalTo(pomContent));
    assertThat(download(UrlUtils.buildUrl(baseUrl, jarPath)), equalTo(jarContent));
    // extract the build artifacts, which should contain the two imported deps.
    // This will also trigger promoting imported artifacts into the shared-imports hosted repo
    RepositoryManagerResult repositoryManagerResult = rc.extractBuildArtifacts(true);
    List<Artifact> deps = repositoryManagerResult.getDependencies();
    System.out.println(deps);
    assertThat(deps, notNullValue());
    assertThat("Expected 2 dependencies, got: " + deps, deps.size(), equalTo(2));
    ProjectVersionRef pvr = new SimpleProjectVersionRef("org.commonjava.indy", "indy-core", "0.17.0");
    Set<String> refs = new HashSet<>();
    refs.add(new SimpleArtifactRef(pvr, "pom", null).toString());
    refs.add(new SimpleArtifactRef(pvr, "jar", null).toString());
    // check that both files are in the dep artifacts list using getIdentifier() to match on GAVT[C]
    for (Artifact artifact : deps) {
        assertThat(artifact + " is not in the expected list of deps: " + refs, refs.contains(artifact.getIdentifier()), equalTo(true));
    }
    Indy indy = driver.getIndy(accessToken);
    // check that the new imports are available from shared-imports
    assertAvailableInSharedImports(indy, pomContent, pomPath);
    assertAvailableInSharedImports(indy, jarContent, jarPath);
}
Also used : TestBuildExecution(org.jboss.pnc.indyrepositorymanager.fixture.TestBuildExecution) RepositoryManagerResult(org.jboss.pnc.spi.repositorymanager.RepositoryManagerResult) TestBuildExecution(org.jboss.pnc.indyrepositorymanager.fixture.TestBuildExecution) BuildExecution(org.jboss.pnc.spi.repositorymanager.BuildExecution) SimpleArtifactRef(org.commonjava.atlas.maven.ident.ref.SimpleArtifactRef) SimpleProjectVersionRef(org.commonjava.atlas.maven.ident.ref.SimpleProjectVersionRef) RepositorySession(org.jboss.pnc.spi.repositorymanager.model.RepositorySession) Artifact(org.jboss.pnc.model.Artifact) ProjectVersionRef(org.commonjava.atlas.maven.ident.ref.ProjectVersionRef) SimpleProjectVersionRef(org.commonjava.atlas.maven.ident.ref.SimpleProjectVersionRef) Indy(org.commonjava.indy.client.core.Indy) HashSet(java.util.HashSet) ContainerTest(org.jboss.pnc.test.category.ContainerTest) Test(org.junit.Test)

Example 8 with RepositoryManagerResult

use of org.jboss.pnc.spi.repositorymanager.RepositoryManagerResult in project pnc by project-ncl.

the class ExcludeInternalRepoByRegexTest method extractBuildArtifacts_ContainsTwoDownloads.

@Test
public void extractBuildArtifacts_ContainsTwoDownloads() throws Exception {
    // create a remote repo pointing at our server fixture's 'repo/test' directory.
    indy.stores().create(new RemoteRepository(MAVEN_PKG_KEY, INTERNAL, server.formatUrl(INTERNAL)), "Creating internal test remote repo", RemoteRepository.class);
    indy.stores().create(new RemoteRepository(MAVEN_PKG_KEY, EXTERNAL, server.formatUrl(EXTERNAL)), "Creating external test remote repo", RemoteRepository.class);
    StoreKey publicKey = new StoreKey(MAVEN_PKG_KEY, StoreType.group, PUBLIC_GROUP_ID);
    StoreKey internalKey = new StoreKey(MAVEN_PKG_KEY, StoreType.remote, INTERNAL);
    StoreKey externalKey = new StoreKey(MAVEN_PKG_KEY, StoreType.remote, EXTERNAL);
    Group publicGroup = indy.stores().load(publicKey, Group.class);
    if (publicGroup == null) {
        publicGroup = new Group(MAVEN_PKG_KEY, PUBLIC_GROUP_ID, internalKey, externalKey);
        indy.stores().create(publicGroup, "creating public group", Group.class);
    } else {
        publicGroup.setConstituents(Arrays.asList(internalKey, externalKey));
        indy.stores().update(publicGroup, "adding test remotes to public group");
    }
    String internalPath = "org/foo/internal/1.0/internal-1.0.pom";
    String externalPath = "org/foo/external/1.1/external-1.1.pom";
    String content = "This is a test " + System.currentTimeMillis();
    // setup the expectation that the remote repo pointing at this server will request this file...and define its
    // content.
    server.expect(server.formatUrl(INTERNAL, internalPath), 200, content);
    server.expect(server.formatUrl(EXTERNAL, externalPath), 200, content);
    // create a dummy non-chained build execution and repo session based on it
    BuildExecution execution = new TestBuildExecution();
    RepositorySession rc = driver.createBuildRepository(execution, accessToken, accessToken, RepositoryType.MAVEN, Collections.emptyMap(), false);
    assertThat(rc, notNullValue());
    String baseUrl = rc.getConnectionInfo().getDependencyUrl();
    // download the two files via the repo session's dependency URL, which will proxy the test http server
    // using the expectations above
    assertThat(download(UrlUtils.buildUrl(baseUrl, internalPath)), equalTo(content));
    assertThat(download(UrlUtils.buildUrl(baseUrl, externalPath)), equalTo(content));
    // extract the build artifacts, which should contain the two imported deps.
    // This will also trigger promoting imported artifacts into the shared-imports hosted repo
    RepositoryManagerResult repositoryManagerResult = rc.extractBuildArtifacts(true);
    List<Artifact> deps = repositoryManagerResult.getDependencies();
    System.out.println(deps);
    assertThat(deps, notNullValue());
    assertThat(deps.size(), equalTo(2));
    Indy indy = driver.getIndy(accessToken);
    StoreKey sharedImportsKey = new StoreKey(MAVEN_PKG_KEY, StoreType.hosted, SHARED_IMPORTS_ID);
    // check that the imports from external locations are available from shared-imports
    InputStream stream = indy.content().get(sharedImportsKey, externalPath);
    String downloaded = IOUtils.toString(stream, (String) null);
    assertThat(downloaded, equalTo(content));
    stream.close();
    // check that the imports from internal/trusted locations are NOT available from shared-imports
    stream = indy.content().get(sharedImportsKey, internalPath);
    assertThat(stream, nullValue());
}
Also used : Group(org.commonjava.indy.model.core.Group) TestBuildExecution(org.jboss.pnc.indyrepositorymanager.fixture.TestBuildExecution) InputStream(java.io.InputStream) RepositoryManagerResult(org.jboss.pnc.spi.repositorymanager.RepositoryManagerResult) Indy(org.commonjava.indy.client.core.Indy) TestBuildExecution(org.jboss.pnc.indyrepositorymanager.fixture.TestBuildExecution) BuildExecution(org.jboss.pnc.spi.repositorymanager.BuildExecution) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) StoreKey(org.commonjava.indy.model.core.StoreKey) RepositorySession(org.jboss.pnc.spi.repositorymanager.model.RepositorySession) Artifact(org.jboss.pnc.model.Artifact) ContainerTest(org.jboss.pnc.test.category.ContainerTest) Test(org.junit.Test)

Example 9 with RepositoryManagerResult

use of org.jboss.pnc.spi.repositorymanager.RepositoryManagerResult in project pnc by project-ncl.

the class ImportDepVerifyPromotionToSharedImportsTest method extractBuildArtifactsTriggersImportPromotion.

@Test
public void extractBuildArtifactsTriggersImportPromotion() throws Exception {
    String path = "/org/myproj/myproj/1.0/myproj-1.0.pom";
    String content = "This is a test " + System.currentTimeMillis();
    // setup the expectation that the remote repo pointing at this server will request this file...and define its
    // content.
    server.expect(server.formatUrl(STORE, path), 200, content);
    // create a dummy non-chained build execution and repo session based on it
    BuildExecution execution = new TestBuildExecution();
    RepositorySession session = driver.createBuildRepository(execution, accessToken, accessToken, RepositoryType.MAVEN, Collections.emptyMap(), false);
    // simulate a build resolving an artifact via the Indy remote repository.
    assertThat(download(UrlUtils.buildUrl(session.getConnectionInfo().getDependencyUrl(), path)), equalTo(content));
    // now, extract the build artifacts. This will trigger promotion of imported stuff to shared-imports.
    RepositoryManagerResult result = session.extractBuildArtifacts(true);
    // do some sanity checks while we're here
    List<Artifact> deps = result.getDependencies();
    assertThat(deps.size(), equalTo(1));
    Artifact a = deps.get(0);
    assertThat(a.getFilename(), equalTo(new File(path).getName()));
    // end result: you should be able to download this artifact from shared-imports now.
    StoreKey sharedImportsKey = new StoreKey(MAVEN_PKG_KEY, StoreType.hosted, SHARED_IMPORTS_ID);
    assertThat(download(indy.content().contentUrl(sharedImportsKey, path)), equalTo(content));
}
Also used : TestBuildExecution(org.jboss.pnc.indyrepositorymanager.fixture.TestBuildExecution) RepositoryManagerResult(org.jboss.pnc.spi.repositorymanager.RepositoryManagerResult) TestBuildExecution(org.jboss.pnc.indyrepositorymanager.fixture.TestBuildExecution) BuildExecution(org.jboss.pnc.spi.repositorymanager.BuildExecution) RepositorySession(org.jboss.pnc.spi.repositorymanager.model.RepositorySession) File(java.io.File) StoreKey(org.commonjava.indy.model.core.StoreKey) Artifact(org.jboss.pnc.model.Artifact) Test(org.junit.Test) ContainerTest(org.jboss.pnc.test.category.ContainerTest)

Example 10 with RepositoryManagerResult

use of org.jboss.pnc.spi.repositorymanager.RepositoryManagerResult in project pnc by project-ncl.

the class UploadTwoThenVerifyExtractedArtifactsContainThemTest method extractBuildArtifacts_ContainsTwoUploads.

@Test
public void extractBuildArtifacts_ContainsTwoUploads() throws Exception {
    // create a dummy non-chained build execution and repo session based on it
    BuildExecution execution = new TestBuildExecution();
    RepositorySession rc = driver.createBuildRepository(execution, accessToken, accessToken, RepositoryType.MAVEN, Collections.emptyMap(), false);
    assertThat(rc, notNullValue());
    String baseUrl = rc.getConnectionInfo().getDeployUrl();
    String pomPath = "org/commonjava/indy/indy-core/0.17.0/indy-core-0.17.0.pom";
    String jarPath = "org/commonjava/indy/indy-core/0.17.0/indy-core-0.17.0.jar";
    CloseableHttpClient client = HttpClientBuilder.create().build();
    // this simulates a build deploying one jar and its associated POM
    for (String path : new String[] { pomPath, jarPath }) {
        final String url = UrlUtils.buildUrl(baseUrl, path);
        assertThat("Failed to upload: " + url, ArtifactUploadUtils.put(client, url, "This is a test"), equalTo(true));
    }
    // extract the "built" artifacts we uploaded above.
    RepositoryManagerResult repositoryManagerResult = rc.extractBuildArtifacts(true);
    // check that both files are present in extracted result
    List<Artifact> artifacts = repositoryManagerResult.getBuiltArtifacts();
    System.out.println(artifacts);
    assertThat(artifacts, notNullValue());
    assertThat(artifacts.size(), equalTo(2));
    ProjectVersionRef pvr = new SimpleProjectVersionRef("org.commonjava.indy", "indy-core", "0.17.0");
    Set<String> refs = new HashSet<>();
    refs.add(new SimpleArtifactRef(pvr, "pom", null).toString());
    refs.add(new SimpleArtifactRef(pvr, "jar", null).toString());
    // check that the artifact getIdentifier() stores GAVT[C] information in the standard Maven rendering
    for (Artifact artifact : artifacts) {
        assertThat(artifact + " is not in the expected list of built artifacts: " + refs, refs.contains(artifact.getIdentifier()), equalTo(true));
    }
    // check that we can download the two files from the build repository
    for (String path : new String[] { pomPath, jarPath }) {
        StoreKey hostedKey = new StoreKey(MAVEN_PKG_KEY, StoreType.hosted, rc.getBuildRepositoryId());
        final String url = indy.content().contentUrl(hostedKey, path);
        boolean downloaded = client.execute(new HttpGet(url), response -> {
            try {
                return response.getStatusLine().getStatusCode() == 200;
            } finally {
                if (response instanceof CloseableHttpResponse) {
                    IOUtils.closeQuietly((CloseableHttpResponse) response);
                }
            }
        });
        assertThat("Failed to download: " + url, downloaded, equalTo(true));
    }
    client.close();
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) TestBuildExecution(org.jboss.pnc.indyrepositorymanager.fixture.TestBuildExecution) RepositoryManagerResult(org.jboss.pnc.spi.repositorymanager.RepositoryManagerResult) HttpGet(org.apache.http.client.methods.HttpGet) TestBuildExecution(org.jboss.pnc.indyrepositorymanager.fixture.TestBuildExecution) BuildExecution(org.jboss.pnc.spi.repositorymanager.BuildExecution) SimpleArtifactRef(org.commonjava.atlas.maven.ident.ref.SimpleArtifactRef) SimpleProjectVersionRef(org.commonjava.atlas.maven.ident.ref.SimpleProjectVersionRef) RepositorySession(org.jboss.pnc.spi.repositorymanager.model.RepositorySession) StoreKey(org.commonjava.indy.model.core.StoreKey) Artifact(org.jboss.pnc.model.Artifact) ProjectVersionRef(org.commonjava.atlas.maven.ident.ref.ProjectVersionRef) SimpleProjectVersionRef(org.commonjava.atlas.maven.ident.ref.SimpleProjectVersionRef) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HashSet(java.util.HashSet) ContainerTest(org.jboss.pnc.test.category.ContainerTest) Test(org.junit.Test)

Aggregations

RepositoryManagerResult (org.jboss.pnc.spi.repositorymanager.RepositoryManagerResult)27 Artifact (org.jboss.pnc.model.Artifact)14 RepositorySession (org.jboss.pnc.spi.repositorymanager.model.RepositorySession)14 Test (org.junit.Test)14 TestBuildExecution (org.jboss.pnc.indyrepositorymanager.fixture.TestBuildExecution)12 BuildExecution (org.jboss.pnc.spi.repositorymanager.BuildExecution)11 ContainerTest (org.jboss.pnc.test.category.ContainerTest)11 StoreKey (org.commonjava.indy.model.core.StoreKey)9 BuildDriverResult (org.jboss.pnc.spi.builddriver.BuildDriverResult)7 File (java.io.File)6 CompletionStatus (org.jboss.pnc.spi.coordinator.CompletionStatus)6 SimpleArtifactRef (org.commonjava.atlas.maven.ident.ref.SimpleArtifactRef)5 BuildResult (org.jboss.pnc.spi.BuildResult)5 BuildExecutionConfiguration (org.jboss.pnc.spi.executor.BuildExecutionConfiguration)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 HashSet (java.util.HashSet)4 ProjectVersionRef (org.commonjava.atlas.maven.ident.ref.ProjectVersionRef)4 SimpleProjectVersionRef (org.commonjava.atlas.maven.ident.ref.SimpleProjectVersionRef)4 Indy (org.commonjava.indy.client.core.Indy)4 IndyFoloContentClientModule (org.commonjava.indy.folo.client.IndyFoloContentClientModule)4