Skip to content

Commit

Permalink
CodeClimate and exports
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Sep 7, 2019
1 parent 07fe711 commit 1b197c1
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 12 deletions.
38 changes: 29 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The runnable can be found in [Maven Central](http://search.maven.org/#search%7Cg

Run it with:

```shell
```bash
npx violations-command-line -s ERROR -mv 0 \
-v "CHECKSTYLE" "." ".*checkstyle/main\.xml$" "Checkstyle" \
-v "JSHINT" "." ".*jshint/report\.xml$" "JSHint"
Expand All @@ -20,6 +20,16 @@ npx violations-command-line -s ERROR -mv 0 \
It can parse results from static code analysis and:

* Report violations in the build log.
* Export to a normalized JSON format.
```bash
npx violations-command-line -vf violations-report.json \
-v "CHECKSTYLE" "." ".*checkstyle/main\.xml$" "Checkstyle"
```
* Export to CodeClimate JSON.
```bash
npx violations-command-line -cc code-climate-report.json \
-v "CHECKSTYLE" "." ".*checkstyle/main\.xml$" "Checkstyle"
```
* Optionally fail the build depending on violations found.

A snippet of the output may look like this:
Expand Down Expand Up @@ -87,6 +97,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 @@ -132,7 +143,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 All @@ -146,6 +157,11 @@ Missing a format? Open an issue [here](https://github.com/tomasbjerre/violations
# Usage

```shell
-code-climate, -cc <path> Create a CodeClimate
file with all the
violations.
<path>: a file path
Default: /home/bjerre/workspace/violations/violations-command-line/build/libs/.
-detail-level, -dl <ViolationsReporterDetailLevel> Verbosity
<ViolationsReporterDetailLevel>: {VERBOSE | COMPACT | PER_FILE_COMPACT}
Default: VERBOSE
Expand Down Expand Up @@ -174,7 +190,7 @@ Missing a format? Open an issue [here](https://github.com/tomasbjerre/violations
Default:
-git-repo, -gr <path> Where to look for Git.
<path>: a file path
Default: /home/bjerre/workspace/violations-command-line/build/libs/.
Default: /home/bjerre/workspace/violations/violations-command-line/build/libs/.
-h, --help <argument-to-print-help-for> <argument-to-print-help-for>: an argument to print help for
Default: If no specific parameter is given the whole usage text is given
-max-line-column-width, -mlcw <integer> 0 means no limit
Expand Down Expand Up @@ -218,20 +234,24 @@ Missing a format? Open an issue [here](https://github.com/tomasbjerre/violations
CLANG, CPD, CPPCHECK,
CPPLINT, CSSLINT, FINDBUGS,
FLAKE8, FXCOP, GENDARME, IAR,
JCREPORT, JSHINT, LINT,
JCREPORT, JSLINT, JUNIT, LINT,
KLOCWORK, KOTLINMAVEN,
KOTLINGRADLE, MSCPP, MYPY, GOLINT,
GOOGLEERRORPRONE, PERLCRITIC, PITEST,
PMD, PYDOCSTYLE, PYLINT,
RESHARPER, SBTSCALAC, SIMIAN,
SONAR, STYLECOP, XMLLINT,
YAMLLINT, ZPTLINT, DOCFX, PCLINT

Example: -v "JSHINT"
"." ".*/jshint.xml$"
"JSHint" [Supports Multiple occurrences]
YAMLLINT, ZPTLINT, DOCFX,
PCLINT, CODECLIMATE
Example: -
v "JSHINT" "." ".
*/jshint.xml$" "JSHint" [Supports Multiple occurrences]
<string>: any string
Default: Empty list
-violations-file, -vf <path> Create a JSON file
with all the violations.
<path>: a file path
Default: /home/bjerre/workspace/violations/violations-command-line/build/libs/.
```
Checkout the [Violations Lib](https://github.com/tomasbjerre/violations-lib) for more documentation.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ apply from: project.buildscript.classLoader.getResource('violations.gradle').toU


dependencies {
compile 'se.bjurr.violations:violations-git-lib:1.22'
compile 'se.bjurr.violations:violations-git-lib:1.23'
compile 'se.softhouse:jargo:0.1.1'
compile 'org.slf4j:slf4j-simple:1.8.0-beta2'
testCompile 'junit:junit:4.12'
Expand Down
48 changes: 46 additions & 2 deletions src/main/java/se/bjurr/violations/main/Runner.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package se.bjurr.violations.main;

import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static java.nio.file.StandardOpenOption.WRITE;
import static se.bjurr.violations.git.ViolationsReporterApi.violationsReporterApi;
import static se.bjurr.violations.git.ViolationsReporterDetailLevel.VERBOSE;
import static se.bjurr.violations.lib.ViolationsApi.violationsApi;
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.codeclimate.CodeClimateTransformer.fromViolations;
import static se.softhouse.jargo.Arguments.booleanArgument;
import static se.softhouse.jargo.Arguments.enumArgument;
import static se.softhouse.jargo.Arguments.fileArgument;
Expand All @@ -14,6 +18,9 @@
import static se.softhouse.jargo.CommandLineParser.withArguments;

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.Arrays;
import java.util.List;
Expand All @@ -25,6 +32,7 @@
import se.bjurr.violations.lib.model.Violation;
import se.bjurr.violations.lib.reports.Parser;
import se.bjurr.violations.lib.util.Filtering;
import se.bjurr.violations.violationslib.com.google.gson.GsonBuilder;
import se.softhouse.jargo.Argument;
import se.softhouse.jargo.ArgumentException;
import se.softhouse.jargo.ParsedArguments;
Expand All @@ -48,6 +56,8 @@ public class Runner {
private int maxSeverityColumnWidth;
private int maxLineColumnWidth;
private int maxMessageColumnWidth;
private File codeClimateFile;
private File violationsFile;

public void main(final String args[]) throws Exception {
final Argument<?> helpArgument = helpArgument("-h", "--help");
Expand All @@ -70,7 +80,14 @@ public void main(final String args[]) throws Exception {
.defaultValue(INFO)
.description("Minimum severity level to report.")
.build();

final Argument<File> codeClimateFileArg =
fileArgument("-code-climate", "-cc")
.description("Create a CodeClimate file with all the violations.")
.build();
final Argument<File> violationsFileArg =
fileArgument("-violations-file", "-vf")
.description("Create a JSON file with all the violations.")
.build();
final Argument<Integer> maxViolationsArg =
integerArgument("-max-violations", "-mv")
.defaultValue(Integer.MAX_VALUE)
Expand Down Expand Up @@ -156,6 +173,8 @@ public void main(final String args[]) throws Exception {
helpArgument, //
violationsArg, //
minSeverityArg, //
codeClimateFileArg, //
violationsFileArg, //
detailLevelArg, //
printViolationsArg, //
diffFrom, //
Expand Down Expand Up @@ -190,7 +209,16 @@ public void main(final String args[]) throws Exception {
this.maxLineColumnWidth = parsed.get(maxLineColumnWidth);
this.maxMessageColumnWidth = parsed.get(maxMessageColumnWidth);
this.gitRepo = parsed.get(gitRepoArg);

if (parsed.wasGiven(codeClimateFileArg)) {
this.codeClimateFile = parsed.get(codeClimateFileArg);
} else {
this.codeClimateFile = null;
}
if (parsed.wasGiven(violationsFileArg)) {
this.violationsFile = parsed.get(violationsFileArg);
} else {
this.violationsFile = null;
}
if (parsed.wasGiven(showDebugInfo)) {
System.out.println(
"Given parameters:\n"
Expand Down Expand Up @@ -220,10 +248,26 @@ public void gitChangelogPluginTasks() throws Exception {
allParsedViolationsInDiff.addAll(getAllViolationsInDiff(parsedViolations));
}

if (this.codeClimateFile != null) {
createJsonFile(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

0 comments on commit 1b197c1

Please sign in to comment.