Search in sources :

Example 6 with UploadTarget

use of com.synopsys.integration.blackduck.codelocation.upload.UploadTarget in project synopsys-detect by blackducksoftware.

the class DetectRapidScanService method performUpload.

public List<HttpUrl> performUpload(UploadBatch uploadBatch, RapidScanOptions rapidScanOptions, @Nullable File rapidScanConfig) throws IntegrationException, IOException {
    List<HttpUrl> allScanUrls = new LinkedList<>();
    for (UploadTarget uploadTarget : uploadBatch.getUploadTargets()) {
        HttpUrl url = bdio2FileUploadService.uploadFile(directoryManager.getRapidOutputDirectory(), uploadTarget, rapidScanOptions, rapidScanConfig);
        logger.info("Uploaded Rapid Scan: {}", url);
        allScanUrls.add(url);
    }
    return allScanUrls;
}
Also used : UploadTarget(com.synopsys.integration.blackduck.codelocation.upload.UploadTarget) HttpUrl(com.synopsys.integration.rest.HttpUrl) LinkedList(java.util.LinkedList)

Example 7 with UploadTarget

use of com.synopsys.integration.blackduck.codelocation.upload.UploadTarget in project synopsys-detect by blackducksoftware.

the class RapidScanUploadService method uploadFiles.

private HttpUrl uploadFiles(UploadTarget uploadTarget, List<BdioFileContent> bdioFiles, RapidScanOptions rapidScanOptions, @Nullable NameVersion nameVersion, @Nullable File rapidScanConfig, @Nullable File rapidScanWorkingDirectory) throws IntegrationException, IOException {
    if (bdioFiles.isEmpty()) {
        throw new IllegalArgumentException("BDIO files cannot be empty.");
    }
    BdioFileContent header = bdioFiles.stream().filter(content -> content.getFileName().equals(FILE_NAME_BDIO_HEADER_JSONLD)).findFirst().orElseThrow(() -> new BlackDuckIntegrationException("Cannot find BDIO header file" + FILE_NAME_BDIO_HEADER_JSONLD + "."));
    List<BdioFileContent> remainingFiles = bdioFiles.stream().filter(content -> !content.getFileName().equals(FILE_NAME_BDIO_HEADER_JSONLD)).collect(Collectors.toList());
    int count = remainingFiles.size();
    logger.debug("BDIO upload file count = " + count);
    BlackDuckRequestBuilderEditor editor = builder -> {
        builder.addHeader(RapidCompareMode.HEADER_NAME, rapidScanOptions.getCompareMode().getHeaderValue());
        if (nameVersion != null) {
            builder.addHeader(Bdio2StreamUploader.PROJECT_NAME_HEADER, nameVersion.getName()).addHeader(Bdio2StreamUploader.VERSION_NAME_HEADER, nameVersion.getVersion());
        }
    };
    HttpUrl url;
    if (rapidScanConfig != null) {
        url = bdio2Uploader.startWithConfig(zip(uploadTarget, rapidScanConfig, header, rapidScanWorkingDirectory), editor);
    } else {
        url = bdio2Uploader.start(header, editor);
    }
    for (BdioFileContent content : remainingFiles) {
        bdio2Uploader.append(url, count, content, editor);
    }
    bdio2Uploader.finish(url, count, editor);
    return url;
}
Also used : FileNameUtils(org.apache.commons.compress.utils.FileNameUtils) IntegrationException(com.synopsys.integration.exception.IntegrationException) DataService(com.synopsys.integration.blackduck.service.DataService) ApiDiscovery(com.synopsys.integration.blackduck.api.generated.discovery.ApiDiscovery) HashMap(java.util.HashMap) IntLogger(com.synopsys.integration.log.IntLogger) HttpUrl(com.synopsys.integration.rest.HttpUrl) NameVersion(com.synopsys.integration.util.NameVersion) RapidScanOptions(com.synopsys.integration.detect.workflow.blackduck.developer.RapidScanOptions) Charset(java.nio.charset.Charset) Map(java.util.Map) BdioFileContent(com.synopsys.integration.blackduck.bdio2.model.BdioFileContent) Path(java.nio.file.Path) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) RapidCompareMode(com.synopsys.integration.detect.configuration.enumeration.RapidCompareMode) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Collectors(java.util.stream.Collectors) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable) Bdio2ContentExtractor(com.synopsys.integration.blackduck.bdio2.util.Bdio2ContentExtractor) List(java.util.List) BlackDuckRequestBuilderEditor(com.synopsys.integration.blackduck.service.request.BlackDuckRequestBuilderEditor) Bdio2StreamUploader(com.synopsys.integration.blackduck.bdio2.Bdio2StreamUploader) BlackDuckIntegrationException(com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException) DetectZipUtil(com.synopsys.integration.detect.util.DetectZipUtil) UploadTarget(com.synopsys.integration.blackduck.codelocation.upload.UploadTarget) BlackDuckRequestBuilderEditor(com.synopsys.integration.blackduck.service.request.BlackDuckRequestBuilderEditor) BdioFileContent(com.synopsys.integration.blackduck.bdio2.model.BdioFileContent) BlackDuckIntegrationException(com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException) HttpUrl(com.synopsys.integration.rest.HttpUrl)

Example 8 with UploadTarget

use of com.synopsys.integration.blackduck.codelocation.upload.UploadTarget in project synopsys-detect by blackducksoftware.

the class RapidModeUploadOperation method run.

public List<HttpUrl> run(BdioResult bdioResult, RapidScanOptions rapidScanOptions, @Nullable File rapidScanConfig) throws IntegrationException, IOException {
    logger.info("Begin Rapid Mode Scan");
    UploadBatch uploadBatch = new UploadBatch();
    for (UploadTarget uploadTarget : bdioResult.getUploadTargets()) {
        logger.debug(String.format("Uploading %s", uploadTarget.getUploadFile().getName()));
        uploadBatch.addUploadTarget(uploadTarget);
    }
    List<HttpUrl> results = rapidScanService.performUpload(uploadBatch, rapidScanOptions, rapidScanConfig);
    logger.debug("Rapid scan url count: {}", results.size());
    return results;
}
Also used : UploadTarget(com.synopsys.integration.blackduck.codelocation.upload.UploadTarget) UploadBatch(com.synopsys.integration.blackduck.codelocation.upload.UploadBatch) HttpUrl(com.synopsys.integration.rest.HttpUrl)

Example 9 with UploadTarget

use of com.synopsys.integration.blackduck.codelocation.upload.UploadTarget in project blackduck-common by blackducksoftware.

the class CodeLocationServiceTestIT method testMappingWithProjectCodeCreator.

@Test
@Disabled
// ejk 2020-09-17 disabling this until I can figure out a better way to create the required elements for the test to pass
void testMappingWithProjectCodeCreator() throws IntegrationException, InterruptedException {
    /*
        This test requires a project/version: code_location_mapping_test_donotdelete/code_location_mapping_test_donotdelete
        Also, it requires a user, project_code_scanner, with the Project Code Scanner role on the above project.
         */
    String codeLocationName = "bdio to be mapped";
    File bdioFile = new File(getClass().getResource("/bdio/bdio_without_project.jsonld").getFile());
    UploadTarget uploadTarget = UploadTarget.createDefault(new NameVersion("inaccurate", "inaccurate"), codeLocationName, bdioFile);
    BdioUploadService bdioUploadService = blackDuckServices.blackDuckServicesFactory.createBdioUploadService();
    bdioUploadService.uploadBdio(uploadTarget);
    UserView projectCodeScanner = blackDuckServices.blackDuckServicesFactory.createUserGroupService().getUserByUsername("project_code_scanner").get();
    Optional<CodeLocationView> codeLocationView = blackDuckServices.codeLocationService.getCodeLocationByName(codeLocationName);
    int attempts = 0;
    while (!codeLocationView.isPresent()) {
        attempts++;
        if (attempts > 15) {
            fail("code location not created fast enough");
        }
        Thread.sleep(5000);
        codeLocationView = blackDuckServices.codeLocationService.getCodeLocationByName(codeLocationName);
    }
    assertTrue(StringUtils.isBlank(codeLocationView.get().getMappedProjectVersion()));
    HttpUrl codeLocationUrl = codeLocationView.get().getHref();
    // now use the project code scanner user
    BufferedIntLogger logger = new BufferedIntLogger();
    BlackDuckServerConfigBuilder projectCodeScannerBuilder = BlackDuckServerConfig.newBuilder();
    projectCodeScannerBuilder.setUrl(intHttpClientTestHelper.getProperty(TestingPropertyKey.TEST_BLACK_DUCK_SERVER_URL));
    projectCodeScannerBuilder.setUsername("project_code_scanner");
    projectCodeScannerBuilder.setPassword("super_secure_password");
    projectCodeScannerBuilder.setTrustCert(true);
    BlackDuckServicesFactory specialFactory = projectCodeScannerBuilder.build().createBlackDuckServicesFactory(logger);
    Optional<ProjectVersionWrapper> projectVersionWrapper = specialFactory.createProjectService().getProjectVersion(new NameVersion("code_location_mapping_test_donotdelete", "code_location_mapping_test_donotdelete"));
    assertTrue(projectVersionWrapper.isPresent());
    CodeLocationService specialCodeLocationService = specialFactory.createCodeLocationService();
    specialCodeLocationService.mapCodeLocation(codeLocationUrl, projectVersionWrapper.get().getProjectVersionView());
    codeLocationView = blackDuckServices.codeLocationService.getCodeLocationByName(codeLocationName);
    assertTrue(codeLocationView.isPresent());
    assertEquals(projectVersionWrapper.get().getProjectVersionView().getHref().string(), codeLocationView.get().getMappedProjectVersion());
    blackDuckServices.blackDuckApiClient.delete(codeLocationView.get());
}
Also used : BlackDuckServerConfigBuilder(com.synopsys.integration.blackduck.configuration.BlackDuckServerConfigBuilder) UploadTarget(com.synopsys.integration.blackduck.codelocation.upload.UploadTarget) NameVersion(com.synopsys.integration.util.NameVersion) UserView(com.synopsys.integration.blackduck.api.generated.view.UserView) BlackDuckServicesFactory(com.synopsys.integration.blackduck.service.BlackDuckServicesFactory) BufferedIntLogger(com.synopsys.integration.log.BufferedIntLogger) HttpUrl(com.synopsys.integration.rest.HttpUrl) CodeLocationView(com.synopsys.integration.blackduck.api.generated.view.CodeLocationView) BdioUploadService(com.synopsys.integration.blackduck.codelocation.bdiolegacy.BdioUploadService) File(java.io.File) ProjectVersionWrapper(com.synopsys.integration.blackduck.service.model.ProjectVersionWrapper) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 10 with UploadTarget

use of com.synopsys.integration.blackduck.codelocation.upload.UploadTarget in project blackduck-docker-inspector by blackducksoftware.

the class BlackDuckClient method uploadBdio.

public void uploadBdio(File bdioFile, String codeLocationName, NameVersion projectAndVersion) throws IntegrationException {
    if (config.isOfflineMode()) {
        logger.info("Upload of BDIO has been disabled by offline mode");
        return;
    }
    logger.info("Uploading BDIO files.");
    BlackDuckHttpClient httpConnection = createHttpConnection(intLogger);
    BlackDuckServicesFactory blackDuckServicesFactory = createBlackDuckServicesFactory(intLogger, httpConnection);
    BdioUploadService bdioUploadService = blackDuckServicesFactory.createBdioUploadService();
    UploadBatch uploadBatch = new UploadBatch();
    UploadTarget uploadTarget = UploadTarget.createDefault(projectAndVersion, codeLocationName, bdioFile);
    logger.info(String.format("uploading %s", uploadTarget.getUploadFile().getName()));
    uploadBatch.addUploadTarget(uploadTarget);
    BdioUploadCodeLocationCreationRequest uploadRequest = bdioUploadService.createUploadRequest(uploadBatch);
    CodeLocationCreationData<UploadBatchOutput> bdioUploadResults = bdioUploadService.uploadBdio(uploadRequest);
    bdioUploadResults.getOutput().getOutputs().stream().forEach(o -> logger.debug(String.format("\tUpload %s: output: %s%n", o.getCodeLocationName(), o.getResponse().orElse("unknown"))));
    logger.info(String.format("Uploaded bdio file %s to %s", bdioFile.getName(), config.getBlackDuckUrl()));
}
Also used : UploadBatchOutput(com.synopsys.integration.blackduck.codelocation.upload.UploadBatchOutput) UploadTarget(com.synopsys.integration.blackduck.codelocation.upload.UploadTarget) BlackDuckHttpClient(com.synopsys.integration.blackduck.http.client.BlackDuckHttpClient) BdioUploadService(com.synopsys.integration.blackduck.codelocation.bdiolegacy.BdioUploadService) BdioUploadCodeLocationCreationRequest(com.synopsys.integration.blackduck.codelocation.bdiolegacy.BdioUploadCodeLocationCreationRequest) UploadBatch(com.synopsys.integration.blackduck.codelocation.upload.UploadBatch) BlackDuckServicesFactory(com.synopsys.integration.blackduck.service.BlackDuckServicesFactory)

Aggregations

UploadTarget (com.synopsys.integration.blackduck.codelocation.upload.UploadTarget)10 HttpUrl (com.synopsys.integration.rest.HttpUrl)4 File (java.io.File)4 ArrayList (java.util.ArrayList)4 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)3 UploadBatch (com.synopsys.integration.blackduck.codelocation.upload.UploadBatch)3 HashMap (java.util.HashMap)3 ExternalId (com.synopsys.integration.bdio.model.externalid.ExternalId)2 BdioUploadService (com.synopsys.integration.blackduck.codelocation.bdiolegacy.BdioUploadService)2 BlackDuckServicesFactory (com.synopsys.integration.blackduck.service.BlackDuckServicesFactory)2 DetectUserFriendlyException (com.synopsys.integration.detect.configuration.DetectUserFriendlyException)2 AggregateCodeLocation (com.synopsys.integration.detect.workflow.bdio.AggregateCodeLocation)2 BdioResult (com.synopsys.integration.detect.workflow.bdio.BdioResult)2 BdioCodeLocation (com.synopsys.integration.detect.workflow.codelocation.BdioCodeLocation)2 DetectCodeLocation (com.synopsys.integration.detect.workflow.codelocation.DetectCodeLocation)2 DetectCodeLocationNamesResult (com.synopsys.integration.detect.workflow.codelocation.DetectCodeLocationNamesResult)2 NameVersion (com.synopsys.integration.util.NameVersion)2 IOException (java.io.IOException)2 BdioMetadata (com.blackducksoftware.bdio2.BdioMetadata)1 Project (com.blackducksoftware.bdio2.model.Project)1