Skip to content

Commit

Permalink
[MCHECKSTYLE-449] Add support for SARIF output format
Browse files Browse the repository at this point in the history
This closes #136
  • Loading branch information
exiahuang authored and michael-o committed Jun 1, 2024
1 parent a29a294 commit 1ad6033
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -619,10 +620,16 @@ protected AuditListener getListener() throws MavenReportException {
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'.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -802,6 +803,21 @@ private AuditListener getListener() throws MojoFailureException, MojoExecutionEx
} 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'.");
Expand Down

0 comments on commit 1ad6033

Please sign in to comment.