Skip to content

Commit

Permalink
[NCL-8681] Retrieve the license information from Build Finder and add…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
vibe13 committed Jul 1, 2024
1 parent 8f8c562 commit efb4bf1
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
<version.org.infinispan>13.0.10.Final</version.org.infinispan>
<version.org.jacoco>0.8.12</version.org.jacoco>
<version.org.jboss.pnc.build.finder>2.3.2-SNAPSHOT</version.org.jboss.pnc.build.finder>
<version.org.jboss.pnc.pnc-api>3.0.0</version.org.jboss.pnc.pnc-api>
<version.org.jboss.pnc.pnc-api>3.0.3</version.org.jboss.pnc.pnc-api>
<version.org.jboss.resteasy>6.2.9.Final</version.org.jboss.resteasy>
<version.org.junit-pioneer>2.2.0</version.org.junit-pioneer>
<version.org.sonatype.plugins.nexus-staging-maven-plugin>1.7.0</version.org.sonatype.plugins.nexus-staging-maven-plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@
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;

import org.jboss.pnc.api.deliverablesanalyzer.dto.Artifact;
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;
Expand All @@ -60,6 +64,52 @@ public static FinderResult createFinderResult(String id, URL url, Map<BuildSyste
.build();
}

private static void setLicenseInformation(Artifact.ArtifactBuilder builder, KojiLocalArchive localArchive) {
Set<LicenseInfo> 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();
Expand Down Expand Up @@ -108,6 +158,9 @@ private static Collection<Artifact> 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());
Expand Down Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit efb4bf1

Please sign in to comment.