Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MCHECKSTYLE-449] Add support for SARIF output format #136

Merged
merged 1 commit into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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