Skip to content

Commit d0b778d

Browse files
author
Antoine Lange
committed
feat: Support report prefix filename (#2634)
1 parent 2328da1 commit d0b778d

File tree

3 files changed

+54
-24
lines changed

3 files changed

+54
-24
lines changed

ant/src/main/java/org/owasp/dependencycheck/taskdefs/Check.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ public class Check extends Update {
223223
* report.
224224
*/
225225
private String reportOutputDirectory = ".";
226+
/**
227+
* Specifies a prefix of the filename for the generated Dependency-Check
228+
* report.
229+
*/
230+
private String reportPrefixName = "dependency-check";
226231
/**
227232
* If using the JUNIT report format the junitFailOnCVSS sets the CVSS score
228233
* threshold that is considered a failure. The default is 0.
@@ -610,6 +615,24 @@ public void setReportOutputDirectory(String reportOutputDirectory) {
610615
this.reportOutputDirectory = reportOutputDirectory;
611616
}
612617

618+
/**
619+
* Get the value of reportPrefixName.
620+
*
621+
* @return the value of reportPrefixName
622+
*/
623+
public String getReportPrefixName() {
624+
return reportPrefixName;
625+
}
626+
627+
/**
628+
* Set the value of reportPrefixName.
629+
*
630+
* @param reportPrefixName new value of reportPrefixName
631+
*/
632+
public void setReportPrefixName(String reportPrefixName) {
633+
this.reportPrefixName = reportPrefixName;
634+
}
635+
613636
/**
614637
* Get the value of failBuildOnCVSS.
615638
*
@@ -2118,7 +2141,7 @@ protected void executeWithContextClassloader() throws BuildException {
21182141
final ExceptionCollection exceptions = callExecuteAnalysis(engine);
21192142
if (exceptions == null || !exceptions.isFatal()) {
21202143
for (String format : getReportFormats()) {
2121-
engine.writeReports(getProjectName(), new File(reportOutputDirectory), format, exceptions);
2144+
engine.writeReports(getProjectName(), new File(reportOutputDirectory), reportPrefixName, format, exceptions);
21222145
}
21232146
if (this.failBuildOnCVSS <= 10) {
21242147
checkForFailure(engine.getDependencies());

core/src/main/java/org/owasp/dependencycheck/Engine.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,11 +1190,11 @@ private void throwFatalExceptionCollection(String message, @NotNull final Throwa
11901190
* @param format the report format (see {@link ReportGenerator.Format})
11911191
* @throws ReportException thrown if there is an error generating the report
11921192
* @deprecated use
1193-
* {@link #writeReports(java.lang.String, java.io.File, java.lang.String, org.owasp.dependencycheck.exception.ExceptionCollection)}
1193+
* {@link #writeReports(java.lang.String, java.io.File, java.lang.String, java.lang.String, org.owasp.dependencycheck.exception.ExceptionCollection)}
11941194
*/
11951195
@Deprecated
11961196
public void writeReports(String applicationName, File outputDir, String format) throws ReportException {
1197-
writeReports(applicationName, null, null, null, outputDir, format, null);
1197+
writeReports(applicationName, null, null, null, outputDir, "dependency-check", format, null);
11981198
}
11991199

12001200
//CSOFF: LineLength
@@ -1204,13 +1204,14 @@ public void writeReports(String applicationName, File outputDir, String format)
12041204
* @param applicationName the name of the application/project
12051205
* @param outputDir the path to the output directory (can include the full
12061206
* file name if the format is not ALL)
1207+
* @param reportPrefixName the prefix of the report filename
12071208
* @param format the report format (see {@link ReportGenerator.Format})
12081209
* @param exceptions a collection of exceptions that may have occurred
12091210
* during the analysis
12101211
* @throws ReportException thrown if there is an error generating the report
12111212
*/
1212-
public void writeReports(String applicationName, File outputDir, String format, ExceptionCollection exceptions) throws ReportException {
1213-
writeReports(applicationName, null, null, null, outputDir, format, exceptions);
1213+
public void writeReports(String applicationName, File outputDir, String reportPrefixName, String format, ExceptionCollection exceptions) throws ReportException {
1214+
writeReports(applicationName, null, null, null, outputDir, reportPrefixName, format, exceptions);
12141215
}
12151216
//CSON: LineLength
12161217

@@ -1226,13 +1227,13 @@ public void writeReports(String applicationName, File outputDir, String format,
12261227
* @param format the report format (see {@link ReportGenerator.Format})
12271228
* @throws ReportException thrown if there is an error generating the report
12281229
* @deprecated use
1229-
* {@link #writeReports(String, String, String, String, File, String, ExceptionCollection)}
1230+
* {@link #writeReports(String, String, String, String, File, String, String, ExceptionCollection)}
12301231
*/
12311232
@Deprecated
12321233
public synchronized void writeReports(String applicationName, @Nullable final String groupId,
12331234
@Nullable final String artifactId, @Nullable final String version,
12341235
@NotNull final File outputDir, String format) throws ReportException {
1235-
writeReports(applicationName, groupId, artifactId, version, outputDir, format, null);
1236+
writeReports(applicationName, groupId, artifactId, version, outputDir, "dependency-check", format, null);
12361237
}
12371238

12381239
//CSOFF: LineLength
@@ -1245,14 +1246,16 @@ public synchronized void writeReports(String applicationName, @Nullable final St
12451246
* @param version the Maven version
12461247
* @param outputDir the path to the output directory (can include the full
12471248
* file name if the format is not ALL)
1249+
* @param reportPrefixName the prefix of the report filename
12481250
* @param format the report format (see {@link ReportGenerator.Format})
12491251
* @param exceptions a collection of exceptions that may have occurred
12501252
* during the analysis
12511253
* @throws ReportException thrown if there is an error generating the report
12521254
*/
12531255
public synchronized void writeReports(String applicationName, @Nullable final String groupId,
12541256
@Nullable final String artifactId, @Nullable final String version,
1255-
@NotNull final File outputDir, String format, ExceptionCollection exceptions) throws ReportException {
1257+
@NotNull final File outputDir, @NotNull String reportPrefixName,
1258+
String format, ExceptionCollection exceptions) throws ReportException {
12561259
if (mode == Mode.EVIDENCE_COLLECTION) {
12571260
throw new UnsupportedOperationException("Cannot generate report in evidence collection mode.");
12581261
}
@@ -1261,7 +1264,7 @@ public synchronized void writeReports(String applicationName, @Nullable final St
12611264
final ReportGenerator r = new ReportGenerator(applicationName, groupId, artifactId, version,
12621265
dependencies, getAnalyzers(), prop, settings, exceptions);
12631266
try {
1264-
r.write(outputDir.getAbsolutePath(), format);
1267+
r.write(outputDir.getAbsolutePath(), reportPrefixName, format);
12651268
} catch (ReportException ex) {
12661269
final String msg = String.format("Error generating the report for %s", applicationName);
12671270
LOGGER.debug(msg, ex);

core/src/main/java/org/owasp/dependencycheck/reporting/ReportGenerator.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.fasterxml.jackson.core.JsonParser;
2323
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2424
import org.apache.commons.io.FilenameUtils;
25+
import org.apache.commons.lang3.StringUtils;
2526
import org.apache.commons.text.WordUtils;
2627
import org.apache.velocity.VelocityContext;
2728
import org.apache.velocity.app.VelocityEngine;
@@ -308,13 +309,14 @@ private VelocityEngine createVelocityEngine() {
308309
* Writes the dependency-check report to the given output location.
309310
*
310311
* @param outputLocation the path where the reports should be written
312+
* @param reportPrefixName the prefix of the report filename
311313
* @param format the format the report should be written in (a valid member
312314
* of {@link Format}) or even the path to a custom velocity template
313315
* (either fully qualified or the template name on the class path).
314316
* @throws ReportException is thrown if there is an error creating out the
315317
* reports
316318
*/
317-
public void write(String outputLocation, String format) throws ReportException {
319+
public void write(String outputLocation, String reportPrefixName, String format) throws ReportException {
318320
Format reportFormat = null;
319321
try {
320322
reportFormat = Format.valueOf(format.toUpperCase());
@@ -323,9 +325,9 @@ public void write(String outputLocation, String format) throws ReportException {
323325
}
324326

325327
if (reportFormat != null) {
326-
write(outputLocation, reportFormat);
328+
write(outputLocation, reportPrefixName, reportFormat);
327329
} else {
328-
File out = getReportFile(outputLocation, null);
330+
File out = getReportFile(outputLocation, reportPrefixName, null);
329331
if (out.isDirectory()) {
330332
out = new File(out, FilenameUtils.getBaseName(format));
331333
LOGGER.warn("Writing non-standard VSL output to a directory using template name as file name.");
@@ -340,20 +342,21 @@ public void write(String outputLocation, String format) throws ReportException {
340342
* Writes the dependency-check report(s).
341343
*
342344
* @param outputLocation the path where the reports should be written
345+
* @param reportPrefixName the prefix of the report filename
343346
* @param format the format the report should be written in (see
344347
* {@link Format})
345348
* @throws ReportException is thrown if there is an error creating out the
346349
* reports
347350
*/
348-
public void write(String outputLocation, Format format) throws ReportException {
351+
public void write(String outputLocation, String reportPrefixName, Format format) throws ReportException {
349352
if (format == Format.ALL) {
350353
for (Format f : Format.values()) {
351354
if (f != Format.ALL) {
352-
write(outputLocation, f);
355+
write(outputLocation, reportPrefixName, f);
353356
}
354357
}
355358
} else {
356-
final File out = getReportFile(outputLocation, format);
359+
final File out = getReportFile(outputLocation, reportPrefixName, format);
357360
final String templateName = format.toString().toLowerCase() + "Report";
358361
LOGGER.info("Writing {} report to: {}", format, out.getAbsolutePath());
359362
processTemplate(templateName, out);
@@ -375,38 +378,39 @@ public void write(String outputLocation, Format format) throws ReportException {
375378
* will generate the correct name for the given output format.
376379
*
377380
* @param outputLocation the specified output location
381+
* @param reportPrefixName the prefix of the report filename
378382
* @param format the report format
379383
* @return the report File
380384
*/
381-
public static File getReportFile(String outputLocation, Format format) {
385+
public static File getReportFile(String outputLocation, String reportPrefixName, Format format) {
382386
File outFile = new File(outputLocation);
383387
if (outFile.getParentFile() == null) {
384388
outFile = new File(".", outputLocation);
385389
}
386390
final String pathToCheck = outputLocation.toLowerCase();
387391
if (format == Format.XML && !pathToCheck.endsWith(".xml")) {
388-
return new File(outFile, "dependency-check-report.xml");
392+
return new File(outFile, reportPrefixName + "-report.xml");
389393
}
390394
if (format == Format.HTML && !pathToCheck.endsWith(".html") && !pathToCheck.endsWith(".htm")) {
391-
return new File(outFile, "dependency-check-report.html");
395+
return new File(outFile, reportPrefixName + "-report.html");
392396
}
393397
if (format == Format.JENKINS && !pathToCheck.endsWith(".html") && !pathToCheck.endsWith(".htm")) {
394-
return new File(outFile, "dependency-check-jenkins.html");
398+
return new File(outFile, reportPrefixName + "-jenkins.html");
395399
}
396400
if (format == Format.JSON && !pathToCheck.endsWith(".json")) {
397-
return new File(outFile, "dependency-check-report.json");
401+
return new File(outFile, reportPrefixName + "-report.json");
398402
}
399403
if (format == Format.CSV && !pathToCheck.endsWith(".csv")) {
400-
return new File(outFile, "dependency-check-report.csv");
404+
return new File(outFile, reportPrefixName + "-report.csv");
401405
}
402406
if (format == Format.JUNIT && !pathToCheck.endsWith(".xml")) {
403-
return new File(outFile, "dependency-check-junit.xml");
407+
return new File(outFile, reportPrefixName + "-report.xml");
404408
}
405409
if (format == Format.SARIF && !pathToCheck.endsWith(".sarif")) {
406-
return new File(outFile, "dependency-check-report.sarif");
410+
return new File(outFile, reportPrefixName + "-report.sarif");
407411
}
408412
if (format == Format.GITLAB && !pathToCheck.endsWith(".json")) {
409-
return new File(outFile, "dependency-check-gitlab.json");
413+
return new File(outFile, reportPrefixName + "-gitlab.json");
410414
}
411415
return outFile;
412416
}

0 commit comments

Comments
 (0)