Skip to content

Commit

Permalink
CodeClimate export
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Sep 7, 2019
1 parent 8b1fa13 commit 2586dc5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ temp
.gradle
.couscous
couscous.phar
violations-maven-plugin-example/code-climate-file.json
violations-maven-plugin-example/violations-file.json
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ A number of **parsers** have been implemented. Some **parsers** can parse output
| [_CPPLint_](https://github.com/theandrewdavis/cpplint) | `CPPLINT` |
| [_CSSLint_](https://github.com/CSSLint/csslint) | `CSSLINT` |
| [_Checkstyle_](http://checkstyle.sourceforge.net/) | `CHECKSTYLE` |
| [_CodeClimate_](https://codeclimate.com/) | `CODECLIMATE` |
| [_CodeNarc_](http://codenarc.sourceforge.net/) | `CODENARC` |
| [_Detekt_](https://github.com/arturbosch/detekt) | `CHECKSTYLE` | With `--output-format xml`.
| [_DocFX_](http://dotnet.github.io/docfx/) | `DOCFX` |
Expand Down Expand Up @@ -121,7 +122,7 @@ A number of **parsers** have been implemented. Some **parsers** can parse output
| [_SbtScalac_](http://www.scala-sbt.org/) | `SBTSCALAC` |
| [_Scalastyle_](http://www.scalastyle.org/) | `CHECKSTYLE` |
| [_Simian_](http://www.harukizaemon.com/simian/) | `SIMIAN` |
| [_Sonar_](https://www.sonarqube.org/) | `SONAR` | With `mvn sonar:sonar -Dsonar.analysis.mode=preview -Dsonar.report.export.path=sonar-report.json`. Removed in 7.7, see [SONAR-11670](https://jira.sonarsource.com/browse/SONAR-11670).
| [_Sonar_](https://www.sonarqube.org/) | `SONAR` | With `mvn sonar:sonar -Dsonar.analysis.mode=preview -Dsonar.report.export.path=sonar-report.json`. Removed in 7.7, see [SONAR-11670](https://jira.sonarsource.com/browse/SONAR-11670) but can be retrieved with: `curl --silent 'http://sonar-server/api/issues/search?componentKeys=unique-key&resolved=false' \| jq -f sonar-report-builder.jq > sonar-report.json`.
| [_Spotbugs_](https://spotbugs.github.io/) | `FINDBUGS` |
| [_StyleCop_](https://stylecop.codeplex.com/) | `STYLECOP` |
| [_SwiftLint_](https://github.com/realm/SwiftLint) | `CHECKSTYLE` | With `--reporter checkstyle`.
Expand Down Expand Up @@ -180,7 +181,10 @@ The plugin needs to run after any static code analysis tools, so put it after th
<maxViolations>99999999</maxViolations>
<!-- Will print violations found in diff -->
<printViolations>true</printViolations>
<!-- Will create a CodeClimate JSON report. -->
<codeClimateFile>code-climate-file.json</codeClimateFile>
<!-- Will create a normalized JSON report. -->
<violationsFile>violations-file.json</violationsFile>
<!-- Diff configuration, remove if you dont want to report violations
for files changed between specific revisions. -->
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<changelog>1.60</changelog>
<violations>1.22</violations>
<violations-lib>1.96</violations-lib>
<violations>1.23</violations>
<violations-lib>1.101</violations-lib>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package se.bjurr.violations.maven.plugin;

import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static java.nio.file.StandardOpenOption.WRITE;
import static org.apache.maven.plugins.annotations.LifecyclePhase.VERIFY;
import static se.bjurr.violations.git.ViolationsReporterApi.violationsReporterApi;
import static se.bjurr.violations.lib.ViolationsApi.violationsApi;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import javax.script.ScriptException;
Expand All @@ -16,7 +22,9 @@
import se.bjurr.violations.git.ViolationsReporterDetailLevel;
import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;
import se.bjurr.violations.lib.model.codeclimate.CodeClimateTransformer;
import se.bjurr.violations.lib.util.Filtering;
import se.bjurr.violations.violationslib.com.google.gson.GsonBuilder;

@Mojo(name = "violations", defaultPhase = VERIFY)
public class ViolationCommentsMojo extends AbstractMojo {
Expand Down Expand Up @@ -72,6 +80,12 @@ public class ViolationCommentsMojo extends AbstractMojo {
@Parameter(property = "maxSeverityColumnWidth", required = false, defaultValue = "0")
private int maxSeverityColumnWidth;

@Parameter(property = "codeClimateFile", required = false)
private File codeClimateFile;

@Parameter(property = "violationsFile", required = false)
private File violationsFile;

@Override
public void execute() throws MojoExecutionException {
try {
Expand All @@ -98,10 +112,27 @@ private void doExecute() throws Exception, ScriptException {
allParsedViolationsInDiff.addAll(getAllViolationsInDiff(parsedViolations));
}

if (this.codeClimateFile != null) {
createJsonFile(
CodeClimateTransformer.fromViolations(allParsedViolations), this.codeClimateFile);
}
if (this.violationsFile != null) {
createJsonFile(allParsedViolations, this.violationsFile);
}
checkGlobalViolations(allParsedViolations);
checkDiffViolations(allParsedViolationsInDiff);
}

private void createJsonFile(final Object object, final File file) throws IOException {
final String codeClimateReport = new GsonBuilder().setPrettyPrinting().create().toJson(object);
Files.write(
file.toPath(),
codeClimateReport.getBytes(StandardCharsets.UTF_8),
TRUNCATE_EXISTING,
CREATE,
WRITE);
}

private void checkGlobalViolations(final List<Violation> violations) throws ScriptException {
final boolean tooManyViolations = violations.size() > maxViolations;
if (!tooManyViolations && !printViolations) {
Expand Down
6 changes: 5 additions & 1 deletion violations-maven-plugin-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.6</maven.compiler.target>
<maven.compiler.source>1.6</maven.compiler.source>
<violations.version>1.21-SNAPSHOT</violations.version>
<violations.version>1.21-SNAPSHOT</violations.version>
</properties>
<build>
<plugins>
Expand All @@ -32,6 +32,10 @@
<maxSeverityColumnWidth>0</maxSeverityColumnWidth>
<maxLineColumnWidth>0</maxLineColumnWidth>
<maxMessageColumnWidth>50</maxMessageColumnWidth>
<!-- Will create a CodeClimate JSON report. -->
<codeClimateFile>code-climate-file.json</codeClimateFile>
<!-- Will create a normalized JSON report. -->
<violationsFile>violations-file.json</violationsFile>

<!-- Global configuration, remove if you dont want to report violations
for the entire repo. -->
Expand Down

0 comments on commit 2586dc5

Please sign in to comment.