Skip to content

Commit 1ad6033

Browse files
exiahuangmichael-o
authored andcommitted
[MCHECKSTYLE-449] Add support for SARIF output format
This closes #136
1 parent a29a294 commit 1ad6033

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/main/java/org/apache/maven/plugins/checkstyle/AbstractCheckstyleReport.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.Map;
3232

3333
import com.puppycrawl.tools.checkstyle.DefaultLogger;
34+
import com.puppycrawl.tools.checkstyle.SarifLogger;
3435
import com.puppycrawl.tools.checkstyle.XMLLogger;
3536
import com.puppycrawl.tools.checkstyle.api.AuditListener;
3637
import com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions;
@@ -314,7 +315,7 @@ public abstract class AbstractCheckstyleReport extends AbstractMavenReport {
314315

315316
/**
316317
* Specifies the format of the output to be used when writing to the output
317-
* file. Valid values are "<code>plain</code>" and "<code>xml</code>".
318+
* file. Valid values are "<code>plain</code>", "<code>sarif</code>" and "<code>xml</code>".
318319
*/
319320
@Parameter(property = "checkstyle.output.format", defaultValue = "xml")
320321
private String outputFileFormat;
@@ -619,10 +620,16 @@ protected AuditListener getListener() throws MavenReportException {
619620
listener = new XMLLogger(out, OutputStreamOptions.CLOSE);
620621
} else if ("plain".equals(outputFileFormat)) {
621622
listener = new DefaultLogger(out, OutputStreamOptions.CLOSE);
623+
} else if ("sarif".equals(outputFileFormat)) {
624+
try {
625+
listener = new SarifLogger(out, OutputStreamOptions.CLOSE);
626+
} catch (IOException e) {
627+
throw new MavenReportException("Failed to create SarifLogger", e);
628+
}
622629
} else {
623630
// TODO: failure if not a report
624631
throw new MavenReportException(
625-
"Invalid output file format: (" + outputFileFormat + "). Must be 'plain' or 'xml'.");
632+
"Invalid output file format: (" + outputFileFormat + "). Must be 'plain', 'sarif' or 'xml'.");
626633
}
627634
}
628635

src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.Map;
3535

3636
import com.puppycrawl.tools.checkstyle.DefaultLogger;
37+
import com.puppycrawl.tools.checkstyle.SarifLogger;
3738
import com.puppycrawl.tools.checkstyle.XMLLogger;
3839
import com.puppycrawl.tools.checkstyle.api.AuditListener;
3940
import com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions;
@@ -92,7 +93,7 @@ public class CheckstyleViolationCheckMojo extends AbstractMojo {
9293

9394
/**
9495
* Specifies the format of the output to be used when writing to the output
95-
* file. Valid values are "<code>plain</code>" and "<code>xml</code>".
96+
* file. Valid values are "<code>plain</code>", "<code>sarif</code>" and "<code>xml</code>".
9697
*/
9798
@Parameter(property = "checkstyle.output.format", defaultValue = "xml")
9899
private String outputFileFormat;
@@ -802,6 +803,21 @@ private AuditListener getListener() throws MojoFailureException, MojoExecutionEx
802803
} catch (IOException e) {
803804
throw new MojoExecutionException("Unable to create temporary file", e);
804805
}
806+
} else if ("sarif".equals(outputFileFormat)) {
807+
try {
808+
// Write a sarif output file to the standard output file,
809+
// and write an XML output file to the temp directory that can be used to count violations
810+
outputXmlFile =
811+
Files.createTempFile("checkstyle-result", ".xml").toFile();
812+
outputXmlFile.deleteOnExit();
813+
OutputStream xmlOut = getOutputStream(outputXmlFile);
814+
CompositeAuditListener compoundListener = new CompositeAuditListener();
815+
compoundListener.addListener(new XMLLogger(xmlOut, OutputStreamOptions.CLOSE));
816+
compoundListener.addListener(new SarifLogger(out, OutputStreamOptions.CLOSE));
817+
listener = compoundListener;
818+
} catch (IOException e) {
819+
throw new MojoExecutionException("Unable to create temporary file", e);
820+
}
805821
} else {
806822
throw new MojoFailureException(
807823
"Invalid output file format: (" + outputFileFormat + "). Must be 'plain' or 'xml'.");

0 commit comments

Comments
 (0)