Skip to content

Commit

Permalink
change report columns
Browse files Browse the repository at this point in the history
  • Loading branch information
danhermann committed May 7, 2018
1 parent 9b0f504 commit 741215c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,68 @@
package org.logstash.dependencies;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Objects;
import java.util.SortedSet;

class Dependency implements Comparable<Dependency> {
public static final String RUBY_TYPE = "ruby";
public static final String JAVA_TYPE = "java";

String type;
String name;
String version;
String license;
String url;
String spdxLicense;

// optional
String licenseUrl;
/**
* Returns an object array representing this dependency as a CSV record according
* to the format requested here: https://github.com/elastic/logstash/issues/8725
*/
Object[] toCsvReportRecord() {
return new String[] {name, version, "", url, spdxLicense, ""};
}

/**
* Reads dependencies from the specified stream using the Ruby dependency report format
* and adds them to the supplied set.
*/
static void addDependenciesFromRubyReport(InputStream stream, SortedSet<Dependency> dependencies)
throws IOException {
Reader in = new InputStreamReader(stream);
for (CSVRecord record : CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(in)) {
dependencies.add(Dependency.fromRubyCsvRecord(record));
}
}

/**
* Reads dependencies from the specified stream using the Java dependency report format
* and adds them to the supplied set.
*/
static void addDependenciesFromJavaReport(InputStream stream, SortedSet<Dependency> dependencies)
throws IOException {
Reader in = new InputStreamReader(stream);
for (CSVRecord record : CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(in)) {
dependencies.add(Dependency.fromJavaCsvRecord(record));
}
}

public static Dependency fromRubyCsvRecord(CSVRecord record) {
private static Dependency fromRubyCsvRecord(CSVRecord record) {
Dependency d = new Dependency();

// name, version, url, license
d.type = RUBY_TYPE;
d.name = record.get(0);
d.version = record.get(1);
d.license = record.get(3);

return d;
}

public static Dependency fromJavaCsvRecord(CSVRecord record) {
private static Dependency fromJavaCsvRecord(CSVRecord record) {
Dependency d = new Dependency();

// artifact,moduleUrl,moduleLicense,moduleLicenseUrl
d.type = JAVA_TYPE;

String nameAndVersion = record.get(0);
int colonIndex = nameAndVersion.indexOf(':');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class ReportGenerator {

final String UNKNOWN_LICENSE = "UNKNOWN";
final Collection<Dependency> UNKNOWN_LICENSES = new ArrayList<Dependency>();
final String[] CSV_HEADERS = {"name", "version", "revision", "url", "license", "copyright"};

public boolean generateReport(
InputStream licenseMappingStream,
Expand All @@ -38,10 +39,10 @@ public boolean generateReport(
Writer output) throws IOException {

SortedSet<Dependency> dependencies = new TreeSet<>();
readRubyDependenciesReport(rubyDependenciesStream, dependencies);
Dependency.addDependenciesFromRubyReport(rubyDependenciesStream, dependencies);

for (InputStream stream : javaDependenciesStreams) {
readJavaDependenciesReport(stream, dependencies);
Dependency.addDependenciesFromJavaReport(stream, dependencies);
}

Map<String, LicenseUrlPair> licenseMapping = new HashMap<>();
Expand All @@ -68,9 +69,9 @@ public boolean generateReport(
}

try (CSVPrinter csvPrinter = new CSVPrinter(output,
CSVFormat.DEFAULT.withHeader("dependencyName", "dependencyVersion", "url", "license"))) {
CSVFormat.DEFAULT.withHeader(CSV_HEADERS))) {
for (Dependency dependency : dependencies) {
csvPrinter.printRecord(dependency.name, dependency.version, dependency.url, dependency.spdxLicense);
csvPrinter.printRecord(dependency.toCsvReportRecord());
}
csvPrinter.flush();
}
Expand All @@ -93,22 +94,6 @@ public boolean generateReport(
return UNKNOWN_LICENSES.size() == 0;
}

private void readRubyDependenciesReport(InputStream stream, SortedSet<Dependency> dependencies)
throws IOException {
Reader in = new InputStreamReader(stream);
for (CSVRecord record : CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(in)) {
dependencies.add(Dependency.fromRubyCsvRecord(record));
}
}

private void readJavaDependenciesReport(InputStream stream, SortedSet<Dependency> dependencies)
throws IOException {
Reader in = new InputStreamReader(stream);
for (CSVRecord record : CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(in)) {
dependencies.add(Dependency.fromJavaCsvRecord(record));
}
}

private void readAcceptableLicenses(InputStream stream, List<String> acceptableLicenses)
throws IOException {
Reader in = new InputStreamReader(stream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public void testReportWithMissingLicenses() throws IOException {

// verify that the two components in the test input with missing licenses are
// listed in the output with no license, i.e., an empty license field followed by CR/LF
assertTrue(output.toString().contains("commons-io:commons-io,2.5,,\r\n"));
assertTrue(output.toString().contains("filesize,0.0.4,,\r\n"));
assertTrue(output.toString().contains("commons-io:commons-io,2.5,,,,\r\n"));
assertTrue(output.toString().contains("filesize,0.0.4,,,,\r\n"));
}

@Test
Expand All @@ -47,8 +47,8 @@ public void testReportWithUnacceptableLicenses() throws IOException {

// verify that the two components in the test input with unacceptable licenses are
// listed in the output with no license, i.e., an empty license field followed by CR/LF
assertTrue(output.toString().contains("com.fasterxml.jackson.core:jackson-core,2.7.3,,\r\n"));
assertTrue(output.toString().contains("bundler,1.16.0,,\r\n"));
assertTrue(output.toString().contains("com.fasterxml.jackson.core:jackson-core,2.7.3,,,,\r\n"));
assertTrue(output.toString().contains("bundler,1.16.0,,,,\r\n"));
}

@Test
Expand All @@ -60,8 +60,8 @@ public void testReportWithMissingUrls() throws IOException {

// verify that the two components in the test input with missing URLs are
// listed in the output with no license, i.e., an empty license field followed by CR/LF
assertTrue(output.toString().contains("org.codehaus.janino:commons-compiler,3.0.8,,\r\n"));
assertTrue(output.toString().contains("json-parser,,,\r\n"));
assertTrue(output.toString().contains("org.codehaus.janino:commons-compiler,3.0.8,,,,\r\n"));
assertTrue(output.toString().contains("json-parser,,,,,\r\n"));
}

private static boolean runReportGenerator(String licenseMappingPath, StringWriter output)
Expand Down
40 changes: 20 additions & 20 deletions tools/dependencies-report/src/test/resources/expectedOutput.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
dependencyName,dependencyVersion,url,license
bundler,1.16.0,https://rubygems.org/gems/bundler/versions/1.16.0,MIT
bundler,1.16.1,https://rubygems.org/gems/bundler/versions/1.16.1,MIT
com.fasterxml.jackson.core:jackson-core,2.7.3,https://github.com/FasterXML/jackson-core/tree/jackson-core-2.7.3,Apache-2.0
com.fasterxml.jackson.core:jackson-core,2.9.1,https://github.com/FasterXML/jackson-core/tree/jackson-core-2.9.1,Apache-2.0
com.fasterxml.jackson.core:jackson-core,2.9.4,https://github.com/FasterXML/jackson-core/tree/jackson-core-2.9.4,Apache-2.0
com.google.errorprone:javac-shaded,9-dev-r4023-3,http://repo1.maven.org/maven2/com/google/errorprone/javac-shaded/9-dev-r4023-3/,EPL-1.0
commons-io:commons-io,2.5,https://commons.apache.org/proper/commons-io/index.html,Apache-2.0
control.js,,https://github.com/zombieleet/control,MIT
filesize,0.0.4,https://rubygems.org/gems/filesize/versions/0.0.4,MIT
gradle.plugin.com.github.jk1:gradle-license-report,0.7.1,https://github.com/jk1/Gradle-License-Report,Apache-2.0
jar-dependencies,0.3.10,https://rubygems.org/gems/jar-dependencies/versions/0.3.10,Apache-2.0
jar-dependencies,0.3.11,https://rubygems.org/gems/jar-dependencies/versions/0.3.11,MIT
jar-dependencies,0.3.12,https://rubygems.org/gems/jar-dependencies/versions/0.3.12,MIT
jruby-openssl,0.9.20,https://rubygems.org/gems/jruby-openssl/versions/0.9.20-java,Apache-2.0
jruby-openssl,0.9.21,https://rubygems.org/gems/jruby-openssl/versions/0.9.21-java,Apache-2.0
jruby-readline,1.1.1,https://rubygems.org/gems/jruby-readline/versions/1.1.1-java,Apache-2.0
json-parser,,https://rubygems.org/gems/json-parser/versions/0.0.1,Apache-2.0
junit:junit,4.12,https://github.com/junit-team/junit4,Apache-2.0
org.codehaus.janino:commons-compiler,3.0.8,https://github.com/janino-compiler/janino,BSD-3-Clause-Attribution
name,version,revision,url,license,copyright
bundler,1.16.0,,https://rubygems.org/gems/bundler/versions/1.16.0,MIT,
bundler,1.16.1,,https://rubygems.org/gems/bundler/versions/1.16.1,MIT,
com.fasterxml.jackson.core:jackson-core,2.7.3,,https://github.com/FasterXML/jackson-core/tree/jackson-core-2.7.3,Apache-2.0,
com.fasterxml.jackson.core:jackson-core,2.9.1,,https://github.com/FasterXML/jackson-core/tree/jackson-core-2.9.1,Apache-2.0,
com.fasterxml.jackson.core:jackson-core,2.9.4,,https://github.com/FasterXML/jackson-core/tree/jackson-core-2.9.4,Apache-2.0,
com.google.errorprone:javac-shaded,9-dev-r4023-3,,http://repo1.maven.org/maven2/com/google/errorprone/javac-shaded/9-dev-r4023-3/,EPL-1.0,
commons-io:commons-io,2.5,,https://commons.apache.org/proper/commons-io/index.html,Apache-2.0,
control.js,,,https://github.com/zombieleet/control,MIT,
filesize,0.0.4,,https://rubygems.org/gems/filesize/versions/0.0.4,MIT,
gradle.plugin.com.github.jk1:gradle-license-report,0.7.1,,https://github.com/jk1/Gradle-License-Report,Apache-2.0,
jar-dependencies,0.3.10,,https://rubygems.org/gems/jar-dependencies/versions/0.3.10,Apache-2.0,
jar-dependencies,0.3.11,,https://rubygems.org/gems/jar-dependencies/versions/0.3.11,MIT,
jar-dependencies,0.3.12,,https://rubygems.org/gems/jar-dependencies/versions/0.3.12,MIT,
jruby-openssl,0.9.20,,https://rubygems.org/gems/jruby-openssl/versions/0.9.20-java,Apache-2.0,
jruby-openssl,0.9.21,,https://rubygems.org/gems/jruby-openssl/versions/0.9.21-java,Apache-2.0,
jruby-readline,1.1.1,,https://rubygems.org/gems/jruby-readline/versions/1.1.1-java,Apache-2.0,
json-parser,,,https://rubygems.org/gems/json-parser/versions/0.0.1,Apache-2.0,
junit:junit,4.12,,https://github.com/junit-team/junit4,Apache-2.0,
org.codehaus.janino:commons-compiler,3.0.8,,https://github.com/janino-compiler/janino,BSD-3-Clause-Attribution,

0 comments on commit 741215c

Please sign in to comment.