This is an automated email from the ASF dual-hosted git repository.

michaelo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-checkstyle-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 1ad6033  [MCHECKSTYLE-449] Add support for SARIF output format
1ad6033 is described below

commit 1ad603398fb30000e8db919dcd6c15338ecce9ad
Author: exiahuang <exia.hu...@outlook.com>
AuthorDate: Wed May 29 20:42:03 2024 +0900

    [MCHECKSTYLE-449] Add support for SARIF output format
    
    This closes #136
---
 .../plugins/checkstyle/AbstractCheckstyleReport.java   | 11 +++++++++--
 .../checkstyle/CheckstyleViolationCheckMojo.java       | 18 +++++++++++++++++-
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git 
a/src/main/java/org/apache/maven/plugins/checkstyle/AbstractCheckstyleReport.java
 
b/src/main/java/org/apache/maven/plugins/checkstyle/AbstractCheckstyleReport.java
index 51de7d9..e86b6d9 100644
--- 
a/src/main/java/org/apache/maven/plugins/checkstyle/AbstractCheckstyleReport.java
+++ 
b/src/main/java/org/apache/maven/plugins/checkstyle/AbstractCheckstyleReport.java
@@ -31,6 +31,7 @@ import java.util.Locale;
 import java.util.Map;
 
 import com.puppycrawl.tools.checkstyle.DefaultLogger;
+import com.puppycrawl.tools.checkstyle.SarifLogger;
 import com.puppycrawl.tools.checkstyle.XMLLogger;
 import com.puppycrawl.tools.checkstyle.api.AuditListener;
 import com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions;
@@ -314,7 +315,7 @@ public abstract class AbstractCheckstyleReport extends 
AbstractMavenReport {
 
     /**
      * Specifies the format of the output to be used when writing to the output
-     * file. Valid values are "<code>plain</code>" and "<code>xml</code>".
+     * file. Valid values are "<code>plain</code>", "<code>sarif</code>" and 
"<code>xml</code>".
      */
     @Parameter(property = "checkstyle.output.format", defaultValue = "xml")
     private String outputFileFormat;
@@ -619,10 +620,16 @@ public abstract class AbstractCheckstyleReport extends 
AbstractMavenReport {
                 listener = new XMLLogger(out, OutputStreamOptions.CLOSE);
             } else if ("plain".equals(outputFileFormat)) {
                 listener = new DefaultLogger(out, OutputStreamOptions.CLOSE);
+            } else if ("sarif".equals(outputFileFormat)) {
+                try {
+                    listener = new SarifLogger(out, OutputStreamOptions.CLOSE);
+                } catch (IOException e) {
+                    throw new MavenReportException("Failed to create 
SarifLogger", e);
+                }
             } else {
                 // TODO: failure if not a report
                 throw new MavenReportException(
-                        "Invalid output file format: (" + outputFileFormat + 
"). Must be 'plain' or 'xml'.");
+                        "Invalid output file format: (" + outputFileFormat + 
"). Must be 'plain', 'sarif' or 'xml'.");
             }
         }
 
diff --git 
a/src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java
 
b/src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java
index d6e3cb2..14c8443 100644
--- 
a/src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java
+++ 
b/src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java
@@ -34,6 +34,7 @@ import java.util.List;
 import java.util.Map;
 
 import com.puppycrawl.tools.checkstyle.DefaultLogger;
+import com.puppycrawl.tools.checkstyle.SarifLogger;
 import com.puppycrawl.tools.checkstyle.XMLLogger;
 import com.puppycrawl.tools.checkstyle.api.AuditListener;
 import com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions;
@@ -92,7 +93,7 @@ public class CheckstyleViolationCheckMojo extends 
AbstractMojo {
 
     /**
      * Specifies the format of the output to be used when writing to the output
-     * file. Valid values are "<code>plain</code>" and "<code>xml</code>".
+     * file. Valid values are "<code>plain</code>", "<code>sarif</code>" and 
"<code>xml</code>".
      */
     @Parameter(property = "checkstyle.output.format", defaultValue = "xml")
     private String outputFileFormat;
@@ -802,6 +803,21 @@ public class CheckstyleViolationCheckMojo extends 
AbstractMojo {
                 } catch (IOException e) {
                     throw new MojoExecutionException("Unable to create 
temporary file", e);
                 }
+            } else if ("sarif".equals(outputFileFormat)) {
+                try {
+                    // Write a sarif output file to the standard output file,
+                    // and write an XML output file to the temp directory that 
can be used to count violations
+                    outputXmlFile =
+                            Files.createTempFile("checkstyle-result", 
".xml").toFile();
+                    outputXmlFile.deleteOnExit();
+                    OutputStream xmlOut = getOutputStream(outputXmlFile);
+                    CompositeAuditListener compoundListener = new 
CompositeAuditListener();
+                    compoundListener.addListener(new XMLLogger(xmlOut, 
OutputStreamOptions.CLOSE));
+                    compoundListener.addListener(new SarifLogger(out, 
OutputStreamOptions.CLOSE));
+                    listener = compoundListener;
+                } catch (IOException e) {
+                    throw new MojoExecutionException("Unable to create 
temporary file", e);
+                }
             } else {
                 throw new MojoFailureException(
                         "Invalid output file format: (" + outputFileFormat + 
"). Must be 'plain' or 'xml'.");

Reply via email to