From efb4bf18fb7a70b5d8a073327b03a6c03db05472 Mon Sep 17 00:00:00 2001 From: Andrea Vibelli Date: Mon, 1 Jul 2024 17:01:09 +0200 Subject: [PATCH] [NCL-8681] Retrieve the license information from Build Finder and add the licenses to the FinderResult (#643) * Retrieve the license information from Build Finder and add the licenses to the FinderResult * Add more logs to the wiremock invocations for easier debugging * Update to latest pnc-api * Refactoring after review --- pom.xml | 2 +- .../model/FinderResultCreator.java | 56 +++++++++++++++++++ .../AnalyzeResourceWithMockedBrewTest.java | 31 ++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 85030a8f..7b77f4b4 100644 --- a/pom.xml +++ b/pom.xml @@ -99,7 +99,7 @@ 13.0.10.Final 0.8.12 2.3.2-SNAPSHOT - 3.0.0 + 3.0.3 6.2.9.Final 2.2.0 1.7.0 diff --git a/src/main/java/org/jboss/pnc/deliverablesanalyzer/model/FinderResultCreator.java b/src/main/java/org/jboss/pnc/deliverablesanalyzer/model/FinderResultCreator.java index b9f3a5b8..86230cb6 100644 --- a/src/main/java/org/jboss/pnc/deliverablesanalyzer/model/FinderResultCreator.java +++ b/src/main/java/org/jboss/pnc/deliverablesanalyzer/model/FinderResultCreator.java @@ -25,7 +25,9 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; import javax.ws.rs.BadRequestException; @@ -33,8 +35,10 @@ import org.jboss.pnc.api.deliverablesanalyzer.dto.Build; import org.jboss.pnc.api.deliverablesanalyzer.dto.BuildSystemType; import org.jboss.pnc.api.deliverablesanalyzer.dto.FinderResult; +import org.jboss.pnc.api.deliverablesanalyzer.dto.LicenseInfo; import org.jboss.pnc.api.deliverablesanalyzer.dto.MavenArtifact; import org.jboss.pnc.api.deliverablesanalyzer.dto.NPMArtifact; +import org.jboss.pnc.api.enums.LicenseSource; import org.jboss.pnc.build.finder.core.BuildSystem; import org.jboss.pnc.build.finder.core.BuildSystemInteger; import org.jboss.pnc.build.finder.core.Checksum; @@ -60,6 +64,52 @@ public static FinderResult createFinderResult(String id, URL url, Map licenses = Optional.ofNullable(localArchive.getLicenses()) + .orElse(Collections.emptySet()) + .stream() + .map(FinderResultCreator::toDTO) + .collect(Collectors.toSet()); + + builder.licenses(licenses); + } + + private static LicenseInfo toDTO(org.jboss.pnc.build.finder.core.LicenseInfo license) { + LicenseInfo.LicenseInfoBuilder licenseBuilder = LicenseInfo.builder() + .comments(license.getComments()) + .distribution(license.getDistribution()) + .name(license.getName()) + .spdxLicenseId(license.getSpdxLicenseId()) + .url(license.getUrl()); + + org.jboss.pnc.build.finder.core.LicenseSource source = license.getSource(); + if (source == null) { + throw new IllegalArgumentException("License source cannot be null"); + } + + switch (source) { + case UNKNOWN: + licenseBuilder.source(LicenseSource.UNKNOWN); + break; + case POM: + licenseBuilder.source(LicenseSource.POM); + break; + case POM_XML: + licenseBuilder.source(LicenseSource.POM_XML); + break; + case BUNDLE_LICENSE: + licenseBuilder.source(LicenseSource.BUNDLE_LICENSE); + break; + case TEXT: + licenseBuilder.source(LicenseSource.TEXT); + break; + default: + throw new IllegalArgumentException("Unknown license source " + source); + } + + return licenseBuilder.build(); + } + private static void setCommonArtifactFields(Artifact.ArtifactBuilder builder, KojiLocalArchive archive) { KojiArchiveInfo archiveInfo = archive.getArchive(); long size = archiveInfo.getSize(); @@ -108,6 +158,9 @@ private static Collection createNotFoundArtifacts(KojiLocalArchive loc localArchive.getArchive().setFilename(filename); setCommonArtifactFields(builder, localArchive); + // Add the new license information provided by Build Finder + setLicenseInformation(builder, localArchive); + builder.archiveFilenames(List.of(filename)).archiveUnmatchedFilenames(localArchive.getUnmatchedFilenames()); artifacts.add(builder.build()); @@ -196,6 +249,9 @@ private static Artifact createArtifact(KojiLocalArchive localArchive, BuildSyste builder.builtFromSource(localArchive.isBuiltFromSource() && !imported); setCommonArtifactFields(builder, localArchive); + // Add the new license information provided by Build Finder + setLicenseInformation(builder, localArchive); + builder.archiveFilenames(localArchive.getFilenames()) .archiveUnmatchedFilenames(localArchive.getUnmatchedFilenames()); diff --git a/src/test/java/org/jboss/pnc/deliverablesanalyzer/rest/AnalyzeResourceWithMockedBrewTest.java b/src/test/java/org/jboss/pnc/deliverablesanalyzer/rest/AnalyzeResourceWithMockedBrewTest.java index 5f8f02dc..4a8bf6b8 100644 --- a/src/test/java/org/jboss/pnc/deliverablesanalyzer/rest/AnalyzeResourceWithMockedBrewTest.java +++ b/src/test/java/org/jboss/pnc/deliverablesanalyzer/rest/AnalyzeResourceWithMockedBrewTest.java @@ -38,6 +38,9 @@ import org.junitpioneer.jupiter.SetSystemProperty; import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.http.HttpHeaders; +import com.github.tomakehurst.wiremock.http.RequestListener; +import com.github.tomakehurst.wiremock.http.RequestMethod; import io.quarkus.test.junit.QuarkusTest; import io.restassured.response.Response; @@ -67,20 +70,48 @@ public void beforeEach() { public AnalyzeResourceWithMockedBrewTest() throws URISyntaxException { } + public class LoggingRequestListener implements RequestListener { + + @Override + public void requestReceived( + com.github.tomakehurst.wiremock.http.Request request, + com.github.tomakehurst.wiremock.http.Response response) { + + RequestMethod method = request.getMethod(); + String url = request.getUrl(); + String body = request.getBodyAsString(); + int port = request.getPort(); + HttpHeaders headers = request.getHeaders(); + String protocol = request.getProtocol(); + + LOGGER.info( + "Received request: method={}, url={}, protocol={}, port={}, headers={}, body={}", + method, + url, + protocol, + port, + headers, + body); + } + } + @Test @SetSystemProperty(key = "org.spdx.useJARLicenseInfoOnly", value = "true") public void analyzeTestOKSimple() throws InterruptedException { // given // callback + wiremock.addMockServiceRequestListener(new LoggingRequestListener()); wiremock.stubFor(post(urlEqualTo(callbackRelativePath)).willReturn(aResponse().withStatus(HTTP_OK))); // Remote servers stubs WireMockServer pncServer = new WireMockServer( options().port(8083).usingFilesUnderClasspath("analyzeTestOKSimple/pnc")); + pncServer.addMockServiceRequestListener(new LoggingRequestListener()); pncServer.start(); WireMockServer brewHub = new WireMockServer( options().port(8084).usingFilesUnderClasspath("analyzeTestOKSimple/brewHub")); + brewHub.addMockServiceRequestListener(new LoggingRequestListener()); brewHub.start(); // when