Search in sources :

Example 11 with IContentId

use of org.eclipse.dash.licenses.IContentId in project dash-licenses by eclipse.

the class MavenIdParserTests method testEclipseFeature.

@Test
public void testEclipseFeature() {
    IContentId value = parser.parseId("org.eclipse.acceleo.features:org.eclipse.acceleo.doc:eclipse-feature:3.7.10-SNAPSHOT:provided");
    // TODO The default values for type and source are obviously bogus in this case
    assertEquals("maven", value.getType());
    assertEquals("mavencentral", value.getSource());
    assertEquals("org.eclipse.acceleo.features", value.getNamespace());
    assertEquals("org.eclipse.acceleo.doc", value.getName());
    assertEquals("3.7.10-SNAPSHOT", value.getVersion());
}
Also used : IContentId(org.eclipse.dash.licenses.IContentId) Test(org.junit.jupiter.api.Test)

Example 12 with IContentId

use of org.eclipse.dash.licenses.IContentId in project dash-licenses by eclipse.

the class PackageLockFileReaderTests method testV2Format.

@Test
void testV2Format() throws IOException {
    try (InputStream input = this.getClass().getResourceAsStream(PACKAGE_LOCK_V2_JSON)) {
        PackageLockFileReader reader = new PackageLockFileReader(input);
        // This "test" is a little... abridged. At least this test proves
        // that we're getting something in the right format from the reader
        // without having to enumerate all 574 (I think) records).
        String[] expected = { "npm/npmjs/@babel/code-frame/7.12.13", "npm/npmjs/@babel/compat-data/7.13.15", "npm/npmjs/@babel/core/7.13.15" };
        String[] found = reader.getContentIds().stream().limit(3).map(IContentId::toString).sorted().toArray(String[]::new);
        assertArrayEquals(expected, found);
    }
}
Also used : PackageLockFileReader(org.eclipse.dash.licenses.cli.PackageLockFileReader) IContentId(org.eclipse.dash.licenses.IContentId) InputStream(java.io.InputStream) Test(org.junit.jupiter.api.Test)

Example 13 with IContentId

use of org.eclipse.dash.licenses.IContentId in project dash-licenses by eclipse.

the class EclipseFoundationSupport method queryLicenseData.

@Override
public void queryLicenseData(Collection<IContentId> ids, Consumer<IContentData> consumer) {
    if (ids.isEmpty())
        return;
    String url = settings.getLicenseCheckUrl();
    if (url.isBlank()) {
        logger.debug("Bypassing Eclipse Foundation.");
        return;
    }
    logger.info("Querying Eclipse Foundation for license data for {} items.", ids.size());
    String form = encodeRequestPayload(ids);
    int code = httpClientService.post(url, "application/x-www-form-urlencoded", form, response -> {
        AtomicInteger counter = new AtomicInteger();
        JsonReader reader = Json.createReader(new StringReader(response));
        JsonObject read = (JsonObject) reader.read();
        JsonObject approved = read.getJsonObject("approved");
        if (approved != null)
            approved.forEach((key, each) -> {
                FoundationData data = new FoundationData(each.asJsonObject());
                logger.debug("EF approved: {} ({}) score: {} {} {}", data.getId(), data.getRule(), data.getScore(), data.getLicense(), data.getAuthority());
                consumer.accept(data);
                counter.incrementAndGet();
            });
        JsonObject restricted = read.getJsonObject("restricted");
        if (restricted != null)
            restricted.forEach((key, each) -> {
                FoundationData data = new FoundationData(each.asJsonObject());
                logger.debug("EF restricted: {} score: {} {} {}", data.getId(), data.getScore(), data.getLicense(), data.getAuthority());
                consumer.accept(data);
                counter.incrementAndGet();
            });
        logger.info("Found {} items.", counter.get());
    });
    if (code != 200) {
        logger.error("Error response from the Eclipse Foundation {}", code);
        throw new RuntimeException("Received an error response from the Eclipse Foundation.");
    }
}
Also used : Logger(org.slf4j.Logger) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) IContentId(org.eclipse.dash.licenses.IContentId) ISettings(org.eclipse.dash.licenses.ISettings) StandardCharsets(java.nio.charset.StandardCharsets) Json(jakarta.json.Json) Consumer(java.util.function.Consumer) Inject(javax.inject.Inject) IContentData(org.eclipse.dash.licenses.IContentData) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) URLEncoder(java.net.URLEncoder) StringReader(java.io.StringReader) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JsonObject(jakarta.json.JsonObject) IHttpClientService(org.eclipse.dash.licenses.http.IHttpClientService) JsonReader(jakarta.json.JsonReader) JsonArray(jakarta.json.JsonArray) ILicenseDataProvider(org.eclipse.dash.licenses.ILicenseDataProvider) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StringReader(java.io.StringReader) JsonReader(jakarta.json.JsonReader) JsonObject(jakarta.json.JsonObject)

Example 14 with IContentId

use of org.eclipse.dash.licenses.IContentId in project dash-licenses by eclipse.

the class Main method main.

public static void main(String[] args) {
    CommandLineSettings settings = CommandLineSettings.getSettings(args);
    if (!settings.isValid()) {
        CommandLineSettings.printUsage(System.out);
        System.exit(1);
    }
    if (settings.isShowHelp()) {
        CommandLineSettings.printHelp(System.out);
        System.exit(0);
    }
    Injector injector = Guice.createInjector(new LicenseToolModule(settings));
    LicenseChecker checker = injector.getInstance(LicenseChecker.class);
    List<IResultsCollector> collectors = new ArrayList<>();
    // TODO Set up collectors based on command line parameters
    IResultsCollector primaryCollector = new NeedsReviewCollector();
    collectors.add(primaryCollector);
    String summaryPath = settings.getSummaryFilePath();
    if (summaryPath != null) {
        try {
            collectors.add(new CSVCollector(getWriter(summaryPath)));
        } catch (FileNotFoundException e1) {
            System.out.println("Can't write to " + summaryPath);
            System.exit(1);
        }
    }
    if (settings.isReview()) {
        collectors.add(new CreateReviewRequestCollector(injector.getInstance(GitLabSupport.class), (id, url) -> {
        }));
    }
    Arrays.stream(settings.getFileNames()).forEach(name -> {
        IDependencyListReader reader = null;
        try {
            reader = getReader(name);
        } catch (FileNotFoundException e) {
            System.out.println(String.format("The file \"%s\" does not exist.", name));
            CommandLineSettings.printUsage(System.out);
            System.exit(1);
        }
        if (reader != null) {
            Collection<IContentId> dependencies = reader.getContentIds();
            checker.getLicenseData(dependencies).forEach((id, licenseData) -> {
                collectors.forEach(collector -> collector.accept(licenseData));
            });
        }
    });
    collectors.forEach(IResultsCollector::close);
    System.exit(primaryCollector.getStatus());
}
Also used : OutputStream(java.io.OutputStream) GitLabSupport(org.eclipse.dash.licenses.review.GitLabSupport) Arrays(java.util.Arrays) Collection(java.util.Collection) FileOutputStream(java.io.FileOutputStream) IContentId(org.eclipse.dash.licenses.IContentId) FileInputStream(java.io.FileInputStream) InputStreamReader(java.io.InputStreamReader) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) Injector(com.google.inject.Injector) List(java.util.List) CreateReviewRequestCollector(org.eclipse.dash.licenses.review.CreateReviewRequestCollector) LicenseToolModule(org.eclipse.dash.licenses.context.LicenseToolModule) Guice(com.google.inject.Guice) FileReader(java.io.FileReader) LicenseChecker(org.eclipse.dash.licenses.LicenseChecker) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) LicenseChecker(org.eclipse.dash.licenses.LicenseChecker) LicenseToolModule(org.eclipse.dash.licenses.context.LicenseToolModule) IContentId(org.eclipse.dash.licenses.IContentId) Injector(com.google.inject.Injector) CreateReviewRequestCollector(org.eclipse.dash.licenses.review.CreateReviewRequestCollector)

Example 15 with IContentId

use of org.eclipse.dash.licenses.IContentId in project dash-licenses by eclipse.

the class YarnLockFileReaderTests method testSingleEntry.

@Test
void testSingleEntry() throws IOException {
    // @formatter:off
    String contents = "spawn-command@^0.0.2-1:\n" + "  version \"0.0.2-1\"\n" + "  resolved \"https://registry.yarnpkg.com/spawn-command/-/spawn-command-0.0.2-1.tgz#62f5e9466981c1b796dc5929937e11c9c6921bd0\"\n" + "  integrity sha1-YvXpRmmBwbeW3Fkpk34RycaSG9A=\n" + "";
    // @formatter:on
    var ids = new YarnLockFileReader(new StringReader(contents)).getContentIds();
    IContentId id = ids.get(0);
    assertEquals("-", id.getNamespace());
    assertEquals("spawn-command", id.getName());
    assertEquals("0.0.2-1", id.getVersion());
}
Also used : IContentId(org.eclipse.dash.licenses.IContentId) StringReader(java.io.StringReader) YarnLockFileReader(org.eclipse.dash.licenses.cli.YarnLockFileReader) Test(org.junit.jupiter.api.Test)

Aggregations

IContentId (org.eclipse.dash.licenses.IContentId)26 Test (org.junit.jupiter.api.Test)23 StringReader (java.io.StringReader)4 LicenseData (org.eclipse.dash.licenses.LicenseData)4 YarnLockFileReader (org.eclipse.dash.licenses.cli.YarnLockFileReader)3 Guice (com.google.inject.Guice)2 Injector (com.google.inject.Injector)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 StandardCharsets (java.nio.charset.StandardCharsets)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 ISettings (org.eclipse.dash.licenses.ISettings)2 LicenseChecker (org.eclipse.dash.licenses.LicenseChecker)2 PackageLockFileReader (org.eclipse.dash.licenses.cli.PackageLockFileReader)2 Disabled (org.junit.jupiter.api.Disabled)2 Json (jakarta.json.Json)1