From 582711cde4f0d4ba671b1221c1bccbff38ee5499 Mon Sep 17 00:00:00 2001 From: Tomas Bjerre Date: Sun, 6 Sep 2020 09:03:03 +0200 Subject: [PATCH] DiffParser #95 --- README.md | 1 + build.gradle | 2 + .../violations/lib/parsers/DiffParser.java | 57 +++++++++++++++++++ .../bjurr/violations/lib/reports/Parser.java | 3 + .../violations/lib/reports/Reporter.java | 13 +++-- .../se/bjurr/violations/lib/DiffTest.java | 42 ++++++++++++++ ...lugin-new-version-commit-1.128-SNAPS.patch | 41 +++++++++++++ 7 files changed, 155 insertions(+), 4 deletions(-) create mode 100644 src/main/java/se/bjurr/violations/lib/parsers/DiffParser.java create mode 100644 src/test/java/se/bjurr/violations/lib/DiffTest.java create mode 100644 src/test/resources/diff/0001-Gradle-Release-Plugin-new-version-commit-1.128-SNAPS.patch diff --git a/README.md b/README.md index ad489274..93d4448b 100644 --- a/README.md +++ b/README.md @@ -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` | diff --git a/build.gradle b/build.gradle index 74ac4014..2d08813c 100644 --- a/build.gradle +++ b/build.gradle @@ -21,6 +21,7 @@ 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' @@ -28,6 +29,7 @@ dependencies { } 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') } diff --git a/src/main/java/se/bjurr/violations/lib/parsers/DiffParser.java b/src/main/java/se/bjurr/violations/lib/parsers/DiffParser.java new file mode 100644 index 00000000..3839bd43 --- /dev/null +++ b/src/main/java/se/bjurr/violations/lib/parsers/DiffParser.java @@ -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 parseReportOutput(String reportContent, ViolationsLogger violationsLogger) throws Exception { + Set 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 patch = diffFile.getPatch(); + for (AbstractDelta delta : patch.getDeltas()) { + if (delta.getType() == DeltaType.CHANGE) { + @SuppressWarnings("unchecked") + ChangeDelta changeDelta = (ChangeDelta)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; + } + +} diff --git a/src/main/java/se/bjurr/violations/lib/reports/Parser.java b/src/main/java/se/bjurr/violations/lib/reports/Parser.java index 86f2ff22..3e285777 100644 --- a/src/main/java/se/bjurr/violations/lib/reports/Parser.java +++ b/src/main/java/se/bjurr/violations/lib/reports/Parser.java @@ -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; @@ -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; @@ -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()), // diff --git a/src/main/java/se/bjurr/violations/lib/reports/Reporter.java b/src/main/java/se/bjurr/violations/lib/reports/Reporter.java index 9ebab6dc..f0c00a7c 100644 --- a/src/main/java/se/bjurr/violations/lib/reports/Reporter.java +++ b/src/main/java/se/bjurr/violations/lib/reports/Reporter.java @@ -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, diff --git a/src/test/java/se/bjurr/violations/lib/DiffTest.java b/src/test/java/se/bjurr/violations/lib/DiffTest.java new file mode 100644 index 00000000..ab2c711f --- /dev/null +++ b/src/test/java/se/bjurr/violations/lib/DiffTest.java @@ -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 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); + } +} diff --git a/src/test/resources/diff/0001-Gradle-Release-Plugin-new-version-commit-1.128-SNAPS.patch b/src/test/resources/diff/0001-Gradle-Release-Plugin-new-version-commit-1.128-SNAPS.patch new file mode 100644 index 00000000..d1e5c30f --- /dev/null +++ b/src/test/resources/diff/0001-Gradle-Release-Plugin-new-version-commit-1.128-SNAPS.patch @@ -0,0 +1,41 @@ +From 513d19e33d0a78b778bc2cd33551a300e7605975 Mon Sep 17 00:00:00 2001 +From: Tomas Bjerre +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 "suggested change" *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 "suggested change" *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 +