-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
513d19e
commit 3197ccc
Showing
8 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/main/java/se/bjurr/violations/lib/parsers/DiffParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package se.bjurr.violations.lib.parsers; | ||
|
||
import static com.github.difflib.patch.DeltaType.CHANGE; | ||
import static com.github.difflib.patch.DeltaType.DELETE; | ||
import static com.github.difflib.patch.DeltaType.EQUAL; | ||
import static com.github.difflib.patch.DeltaType.INSERT; | ||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static java.util.stream.Collectors.joining; | ||
import static se.bjurr.violations.lib.model.SEVERITY.ERROR; | ||
import static se.bjurr.violations.lib.model.Violation.violationBuilder; | ||
import static se.bjurr.violations.lib.reports.Parser.DIFF; | ||
|
||
import com.github.difflib.patch.AbstractDelta; | ||
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 java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
import se.bjurr.violations.lib.ViolationsLogger; | ||
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<String> delta : patch.getDeltas()) { | ||
String fromString = toString(delta.getSource().getLines()); | ||
int fromLine = delta.getSource().getPosition(); | ||
String toString = toString(delta.getTarget().getLines()); | ||
int toLine = delta.getTarget().getPosition(); | ||
|
||
String message = ""; | ||
|
||
if (delta.getType() == CHANGE) { | ||
message = | ||
"Change line " | ||
+ fromLine | ||
+ ":\n\n" | ||
+ fromString | ||
+ "\n\nTo line " | ||
+ toLine | ||
+ ":\n\n" | ||
+ toString; | ||
} else if (delta.getType() == DELETE) { | ||
message = | ||
"Delete line " | ||
+ fromLine | ||
+ ":\n\n" | ||
+ fromString | ||
+ "\n\nTo line " | ||
+ toLine | ||
+ ":\n\n" | ||
+ toString; | ||
} else if (delta.getType() == EQUAL) { | ||
message = | ||
"Equal line " | ||
+ fromLine | ||
+ ":\n\n" | ||
+ fromString | ||
+ "\n\nTo line " | ||
+ toLine | ||
+ ":\n\n" | ||
+ toString; | ||
} else if (delta.getType() == INSERT) { | ||
message = | ||
"Insert line " | ||
+ fromLine | ||
+ ":\n\n" | ||
+ fromString | ||
+ "\n\nTo line " | ||
+ toLine | ||
+ ":\n\n" | ||
+ toString; | ||
} | ||
|
||
violations.add( | ||
violationBuilder() | ||
.setParser(DIFF) | ||
.setFile(file) | ||
.setStartLine(fromLine) | ||
.setMessage(message) | ||
.setSeverity(ERROR) | ||
.build()); | ||
} | ||
} | ||
return violations; | ||
} | ||
|
||
private String toString(List<String> lines) { | ||
return lines.stream().collect(joining("\n")).trim(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
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.ERROR; | ||
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 testChange() { | ||
final String rootFolder = getRootFolder(); | ||
|
||
final Set<Violation> actual = | ||
violationsApi() // | ||
.withPattern(".*/diff/0001-.*") // | ||
.inFolder(rootFolder) // | ||
.findAll(DIFF) // | ||
.violations(); | ||
|
||
assertThat(actual) // | ||
.hasSize(2); | ||
|
||
Violation v0 = new ArrayList<>(actual).get(0); | ||
assertThat(v0.getMessage()) // | ||
.contains("Changelog of Violations lib"); | ||
assertThat(v0.getFile()) // | ||
.isEqualTo("CHANGELOG.md"); | ||
assertThat(v0.getSeverity()) // | ||
.isEqualTo(ERROR); | ||
assertThat(v0.getStartLine()) // | ||
.isEqualTo(2); | ||
|
||
Violation v1 = new ArrayList<>(actual).get(1); | ||
assertThat(v1.getMessage()) // | ||
.contains("version = 1.127"); | ||
assertThat(v1.getFile()) // | ||
.isEqualTo("gradle.properties"); | ||
assertThat(v1.getSeverity()) // | ||
.isEqualTo(ERROR); | ||
assertThat(v1.getStartLine()) // | ||
.isEqualTo(0); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/test/resources/diff/0001-Gradle-Release-Plugin-new-version-commit-1.128-SNAPS.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "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 | ||
|