Skip to content

Commit

Permalink
DiffParser #95
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Sep 6, 2020
1 parent 513d19e commit 582711c
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ A number of **parsers** have been implemented. Some **parsers** can parse output
| [_PHPCS_](https://github.com/squizlabs/PHP_CodeSniffer) | `CHECKSTYLE` | With `phpcs api.php --report=checkstyle`.
| [_PHPPMD_](https://phpmd.org/) | `PMD` | With `phpmd api.php xml ruleset.xml`.
| [_PMD_](https://pmd.github.io/) | `PMD` |
| [_Patch_](https://en.wikipedia.org/wiki/Diff) | `DIFF` | Often called 'patch'.
| [_Pep8_](https://github.com/PyCQA/pycodestyle) | `FLAKE8` |
| [_PerlCritic_](https://github.com/Perl-Critic) | `PERLCRITIC` |
| [_PiTest_](http://pitest.org/) | `PITEST` |
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ apply from: project.buildscript.classLoader.getResource('release.gradle').toURI(

dependencies {
compile 'com.google.code.gson:gson:2.8.2'
compile 'io.github.java-diff-utils:java-diff-utils:4.7'
compileOnly 'com.github.spotbugs:spotbugs-annotations:3.1.12'
testCompile 'junit:junit:4.12'
testCompile 'org.assertj:assertj-core:2.3.0'
testCompile 'uk.co.jemos.podam:podam:7.2.1.RELEASE'
}

shadowJar {
relocate 'io.github', safeJavaIdentidier(project.group + '.' + project.name + '.io.github')
relocate 'com.google', safeJavaIdentidier(project.group + '.' + project.name + '.com.google')
relocate 'com.jakewharton', safeJavaIdentidier(project.group + '.' + project.name + '.com.jakewharton')
}
57 changes: 57 additions & 0 deletions src/main/java/se/bjurr/violations/lib/parsers/DiffParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package se.bjurr.violations.lib.parsers;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.joining;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.reports.Parser.DIFF;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Set;
import java.util.TreeSet;

import com.github.difflib.patch.AbstractDelta;
import com.github.difflib.patch.ChangeDelta;
import com.github.difflib.patch.DeltaType;
import com.github.difflib.patch.Patch;
import com.github.difflib.unifieddiff.UnifiedDiff;
import com.github.difflib.unifieddiff.UnifiedDiffFile;
import com.github.difflib.unifieddiff.UnifiedDiffReader;

import se.bjurr.violations.lib.ViolationsLogger;
import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;

public class DiffParser implements ViolationsParser {

@Override
public Set<Violation> parseReportOutput(String reportContent, ViolationsLogger violationsLogger) throws Exception {
Set<Violation> violations = new TreeSet<>();
InputStream reportContentStream = new ByteArrayInputStream(reportContent.getBytes(UTF_8));
UnifiedDiff parsedDiff = UnifiedDiffReader.parseUnifiedDiff(reportContentStream);
for(UnifiedDiffFile diffFile : parsedDiff.getFiles()) {
String file = diffFile.getFromFile();
Patch<String> patch = diffFile.getPatch();
for (AbstractDelta<?> delta : patch.getDeltas()) {
if (delta.getType() == DeltaType.CHANGE) {
@SuppressWarnings("unchecked")
ChangeDelta<String> changeDelta = (ChangeDelta<String>)delta;
String fromString = changeDelta.getSource().getLines().stream().collect(joining("\n"));
int fromLine = changeDelta.getSource().getPosition();
String toString = changeDelta.getTarget().getLines().stream().collect(joining("\n"));
changeDelta.getTarget().getPosition();

violations.add(violationBuilder()
.setParser(DIFF)
.setFile(file)
.setStartLine(fromLine)
.setMessage(fromString+"\n\nTo:\n\n"+toString)
.setSeverity(SEVERITY.ERROR)
.build());
}
}
}
return violations;
}

}
3 changes: 3 additions & 0 deletions src/main/java/se/bjurr/violations/lib/reports/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Logger;

import se.bjurr.violations.lib.ViolationsLogger;
import se.bjurr.violations.lib.model.Violation;
import se.bjurr.violations.lib.parsers.AndroidLintParser;
Expand Down Expand Up @@ -39,6 +40,7 @@
import se.bjurr.violations.lib.parsers.MyPyParser;
import se.bjurr.violations.lib.parsers.PCLintParser;
import se.bjurr.violations.lib.parsers.PMDParser;
import se.bjurr.violations.lib.parsers.DiffParser;
import se.bjurr.violations.lib.parsers.PerlCriticParser;
import se.bjurr.violations.lib.parsers.PiTestParser;
import se.bjurr.violations.lib.parsers.ProtoLintParser;
Expand Down Expand Up @@ -79,6 +81,7 @@ public enum Parser {
KOTLINGRADLE(new KotlinGradleParser()), //
MSCPP(new MSCPPParser()), //
MYPY(new MyPyParser()), //
DIFF(new DiffParser()), //
GOLINT(new GoLintParser()), //
GOOGLEERRORPRONE(new GoogleErrorProneParser()), //
PERLCRITIC(new PerlCriticParser()), //
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/se/bjurr/violations/lib/reports/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,15 @@ public enum Reporter {
MYPY("MyPy", Parser.MYPY, "https://pypi.python.org/pypi/mypy-lang", ""),
MSCPP("MSCpp", Parser.MSCPP, "https://visualstudio.microsoft.com/vs/features/cplusplus/", ""),
NULLAWAY(
"NullAway",
Parser.GOOGLEERRORPRONE,
"https://github.com/uber/NullAway",
"Same format as Google Error Prone."),
"NullAway",
Parser.GOOGLEERRORPRONE,
"https://github.com/uber/NullAway",
"Same format as Google Error Prone."),
DIFF(
"Patch",
Parser.DIFF,
"https://en.wikipedia.org/wiki/Diff",
"Unidiff, often used by the `patch` program."),
PCLINT(
"PCLint",
Parser.PCLINT,
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/se/bjurr/violations/lib/DiffTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package se.bjurr.violations.lib;

import static org.assertj.core.api.Assertions.assertThat;
import static se.bjurr.violations.lib.TestUtils.getRootFolder;
import static se.bjurr.violations.lib.ViolationsApi.violationsApi;
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.reports.Parser.DIFF;

import java.util.ArrayList;
import java.util.Set;

import org.junit.Test;

import se.bjurr.violations.lib.model.Violation;

public class DiffTest {

@Test
public void testThatGoVetViolationsCanBeParsed() {
final String rootFolder = getRootFolder();

final Set<Violation> actual =
violationsApi() //
.withPattern(".*/diff/0001-.*") //
.inFolder(rootFolder) //
.findAll(DIFF) //
.violations();

assertThat(actual) //
.hasSize(3);

Violation violation0 = new ArrayList<>(actual).get(0);
assertThat(violation0.getMessage()) //
.isEqualTo("this is a message");
assertThat(violation0.getFile()) //
.isEqualTo("my_file.go");
assertThat(violation0.getSeverity()) //
.isEqualTo(INFO);
assertThat(violation0.getStartLine()) //
.isEqualTo(46);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
From 513d19e33d0a78b778bc2cd33551a300e7605975 Mon Sep 17 00:00:00 2001
From: Tomas Bjerre <[email protected]>
Date: Sun, 6 Sep 2020 07:26:13 +0200
Subject: [PATCH] [Gradle Release Plugin] - new version commit:
'1.128-SNAPSHOT'.

---
CHANGELOG.md | 9 +++++++++
gradle.properties | 2 +-
2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7b2cb23..a1ee761 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,15 @@

Changelog of Violations lib.

+## 1.127
+### GitHub [#95](https://github.com/tomasbjerre/violations-lib/issues/95) Add support for &quot;suggested change&quot; *enhancement*
+
+**Moving PatchParser from violation-comments-lib**
+
+
+[5c52a3933792d14](https://github.com/tomasbjerre/violations-lib/commit/5c52a3933792d14) Tomas Bjerre *2020-09-06 05:24:56*
+
+
## 1.126
### GitHub [#95](https://github.com/tomasbjerre/violations-lib/issues/95) Add support for &quot;suggested change&quot; *enhancement*

diff --git a/gradle.properties b/gradle.properties
index aca4c1a..eda4430 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -1 +1 @@
-version = 1.127
+version = 1.128-SNAPSHOT
--
2.25.1

0 comments on commit 582711c

Please sign in to comment.