Skip to content

Commit

Permalink
Feature/gradle license report reader (#284)
Browse files Browse the repository at this point in the history
Co-authored-by: ohecker <8004361+ohecker@users.noreply.github.com>
  • Loading branch information
duph97 and ohecker authored Sep 3, 2024
1 parent 2c69d41 commit efeda28
Show file tree
Hide file tree
Showing 9 changed files with 357 additions and 258 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* SPDX-License-Identifier: Apache-2.0
*/

package com.devonfw.tools.solicitor.reader.gradle;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Component;

import com.devonfw.tools.solicitor.common.PackageURLHelper;
import com.devonfw.tools.solicitor.common.SolicitorRuntimeException;
import com.devonfw.tools.solicitor.model.inventory.ApplicationComponent;
import com.devonfw.tools.solicitor.model.masterdata.Application;
import com.devonfw.tools.solicitor.model.masterdata.UsagePattern;
import com.devonfw.tools.solicitor.reader.AbstractReader;
import com.devonfw.tools.solicitor.reader.Reader;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

/**
* A {@link Reader} which reads data generated by the <a href="https://github.com/jk1/Gradle-License-Report">Gradle
* License Report Plugin</a>.
*/
@Component
public class GradleLicenseReportReader extends AbstractReader implements Reader {

/**
* The supported type of this {@link Reader}.
*/
public static final String SUPPORTED_TYPE = "gradle-license-report-json";

/** {@inheritDoc} */
@Override
public Set<String> getSupportedTypes() {

return Collections.singleton(SUPPORTED_TYPE);
}

/** {@inheritDoc} */
@Override
public void readInventory(String type, String sourceUrl, Application application, UsagePattern usagePattern,
String repoType, String packageType, Map<String, String> configuration) {

int components = 0;
int licenses = 0;

// According to tutorial https://github.com/FasterXML/jackson-databind/
ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
try {
Map<String, List> report = mapper.readValue(this.inputStreamFactory.createInputStreamFor(sourceUrl), Map.class);
List<Map<String, String>> dependencies = report.get("dependencies");

// Extract groupId and artifactId
for (Map<String, String> dependency : dependencies) {
String[] dependencyParts = dependency.get("moduleName").split(":");
if (dependencyParts.length != 2) {
throw new SolicitorRuntimeException(
"Could not extract groupId, artifactId from moduleName: '" + dependency.get("moduleName") + "'");
}

ApplicationComponent appComponent = getModelFactory().newApplicationComponent();
appComponent.setApplication(application);
appComponent.setGroupId(dependencyParts[0]);
appComponent.setArtifactId(dependencyParts[1]);
appComponent.setVersion(dependency.get("moduleVersion"));

// Extract the first element from moduleUrls if available
Object urlsObject = dependency.get("moduleUrls");
if (urlsObject instanceof List<?>) {
List<?> urlsList = (List<?>) urlsObject;
if (!urlsList.isEmpty() && urlsList.get(0) instanceof String) {
appComponent.setOssHomepage((String) urlsList.get(0));
}
}

appComponent.setUsagePattern(usagePattern);
appComponent.setRepoType(repoType);
appComponent.setPackageUrl(PackageURLHelper
.fromMavenCoordinates(dependencyParts[0], dependencyParts[1], dependency.get("moduleVersion")).toString());

// Extract and process moduleLicenses
Object licensesObject = dependency.get("moduleLicenses");
if (licensesObject instanceof List<?>) {
List<?> licensesList = (List<?>) licensesObject;
for (Object licenseObject : licensesList) {
if (licenseObject instanceof Map<?, ?>) {
Map<?, ?> licenseMap = (Map<?, ?>) licenseObject;
String licenseName = (String) licenseMap.get("moduleLicense");
String licenseUrl = (String) licenseMap.get("moduleLicenseUrl");
addRawLicense(appComponent, licenseName, licenseUrl, sourceUrl);
}
}
}

components++;
licenses++;
}

doLogging(sourceUrl, application, components, licenses);

} catch (IOException e) {
throw new SolicitorRuntimeException("Could not read Gradle License Report inventory source '" + sourceUrl + "'",
e);
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.devonfw.tools.solicitor.common.DeprecationChecker;
import com.devonfw.tools.solicitor.common.PackageURLHelper;
import com.devonfw.tools.solicitor.common.SolicitorRuntimeException;
import com.devonfw.tools.solicitor.model.inventory.ApplicationComponent;
Expand All @@ -38,6 +40,14 @@ public class GradleReader2 extends AbstractReader implements Reader {
*/
public static final String SUPPORTED_TYPE = "gradle2";

private DeprecationChecker deprecationChecker;

@Autowired
public void setDeprecationChecker(DeprecationChecker deprecationChecker) {

this.deprecationChecker = deprecationChecker;
}

/** {@inheritDoc} */
@Override
public Set<String> getSupportedTypes() {
Expand All @@ -50,6 +60,10 @@ public Set<String> getSupportedTypes() {
public void readInventory(String type, String sourceUrl, Application application, UsagePattern usagePattern,
String repoType, String packageType, Map<String, String> configuration) {

this.deprecationChecker.check(false,
"Support for the 'Gradle License Plugin' via the 'gradle2' Reader is deprecated. Use the 'Gradle License Report' with"
+ " Reader 'gradle-license-report-json' instead. See https://github.com/devonfw/solicitor/issues/283");

int components = 0;
int licenses = 0;
LicenseSummary ls = new LicenseSummary();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.devonfw.tools.solicitor.reader.gradle;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;

import org.junit.jupiter.api.Test;

import com.devonfw.tools.solicitor.common.FileInputStreamFactory;
import com.devonfw.tools.solicitor.model.ModelFactory;
import com.devonfw.tools.solicitor.model.impl.ModelFactoryImpl;
import com.devonfw.tools.solicitor.model.inventory.ApplicationComponent;
import com.devonfw.tools.solicitor.model.masterdata.Application;
import com.devonfw.tools.solicitor.model.masterdata.UsagePattern;

class GradleLicenseReportReaderTest {

Application application;

public GradleLicenseReportReaderTest() {

ModelFactory modelFactory = new ModelFactoryImpl();
this.application = modelFactory.newApplication("testApp", "0.0.0.TEST", "1.1.2111", "http://bla.com", "Java8");
GradleLicenseReportReader gr = new GradleLicenseReportReader();
gr.setModelFactory(modelFactory);
gr.setInputStreamFactory(new FileInputStreamFactory());
gr.readInventory("gradle-license-report-json", "src/test/resources/gradleLicenseReport.json", this.application,
UsagePattern.STATIC_LINKING, null, null, null);
}

@Test
void testGetSupportedTypes() {

GradleLicenseReportReader cut = new GradleLicenseReportReader();

assertEquals(1, cut.getSupportedTypes().size());
assertTrue(cut.getSupportedTypes().contains("gradle-license-report-json"));
}

@Test
void testFindArtifact() {

List<ApplicationComponent> lapc = this.application.getApplicationComponents();
boolean found = false;
for (ApplicationComponent ap : lapc) {
if (ap.getGroupId().equals("androidx.activity") && //
ap.getArtifactId().equals("activity") && //
ap.getVersion().equals("1.2.4")) {
found = true;
assertEquals("pkg:maven/androidx.activity/activity@1.2.4", ap.getPackageUrl());
break;
}
}
assertEquals(3, lapc.size());
assertTrue(found);
}

@Test
void testFindLicense() {

List<ApplicationComponent> lapc = this.application.getApplicationComponents();
boolean found = false;
for (ApplicationComponent ap : lapc) {
if (ap.getArtifactId().equals("istack-commons-runtime")
&& ap.getRawLicenses().get(0).getDeclaredLicense().equals("GPL2 w/ CPE")) {
found = true;
break;
}
}
assertTrue(found);
}

@Test
void testMinimalInformation() {

List<ApplicationComponent> lapc = this.application.getApplicationComponents();
boolean found = false;
for (ApplicationComponent ap : lapc) {
if (ap.getArtifactId().equals("annotation") && ap.getOssHomepage() == null && ap.getRawLicenses().isEmpty()) {
found = true;
break;
}
}
assertTrue(found);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.devonfw.tools.solicitor.common.DeprecationChecker;
import com.devonfw.tools.solicitor.common.FileInputStreamFactory;
import com.devonfw.tools.solicitor.model.ModelFactory;
import com.devonfw.tools.solicitor.model.impl.ModelFactoryImpl;
Expand All @@ -31,6 +32,14 @@ public GradleReader2Tests() {

this.application = modelFactory.newApplication("testApp", "0.0.0.TEST", "1.1.2111", "http://bla.com", "Java8");
GradleReader2 gr = new GradleReader2();
gr.setDeprecationChecker(new DeprecationChecker() {

@Override
public void check(boolean warnOnly, String detailsString) {

// do nothing...
}
});
gr.setModelFactory(modelFactory);
gr.setInputStreamFactory(new FileInputStreamFactory());
gr.readInventory("gradle2", "src/test/resources/licenseReport.json", this.application, UsagePattern.DYNAMIC_LINKING,
Expand Down
Loading

0 comments on commit efeda28

Please sign in to comment.