-
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 fa287dc
Showing
7 changed files
with
261 additions
and
4 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
143 changes: 143 additions & 0 deletions
143
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,143 @@ | ||
package se.bjurr.violations.lib.parsers; | ||
|
||
import static se.bjurr.violations.lib.model.Violation.violationBuilder; | ||
import static se.bjurr.violations.lib.parsers.DiffParser.DiffState.ADDED; | ||
import static se.bjurr.violations.lib.parsers.DiffParser.DiffState.CHANGED; | ||
import static se.bjurr.violations.lib.parsers.DiffParser.DiffState.DELETED; | ||
|
||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import se.bjurr.violations.lib.ViolationsLogger; | ||
import se.bjurr.violations.lib.model.Violation; | ||
|
||
public class DiffParser implements ViolationsParser { | ||
public class CommonParsed { | ||
private final Integer startLineBefore; | ||
private final Integer endLineBefore; | ||
private final Integer startLineAfter; | ||
private final Integer endLineAfter; | ||
|
||
public CommonParsed(Integer startLineBefore, Integer endLineBefore, Integer startLineAfter, | ||
Integer endLineAfter) { | ||
super(); | ||
this.startLineBefore = startLineBefore; | ||
this.endLineBefore = endLineBefore; | ||
this.startLineAfter = startLineAfter; | ||
this.endLineAfter = endLineAfter; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "CommonParsed [startLineBefore=" + startLineBefore + ", endLineBefore=" + endLineBefore | ||
+ ", startLineAfter=" + startLineAfter + ", endLineAfter=" + endLineAfter + "]"; | ||
} | ||
|
||
} | ||
|
||
private static Logger LOG = Logger.getLogger(DiffParser.class.getSimpleName()); | ||
private static final String LINE_PATTERN_STR = "([0-9]+),?([0-9]+)?"; | ||
private static final Pattern LINE_PATTERN = Pattern.compile(LINE_PATTERN_STR); | ||
|
||
public enum DiffState { | ||
ADDED("a"), DELETED("d"), CHANGED("c"); | ||
|
||
private final Pattern pattern; | ||
|
||
private DiffState(String letter) { | ||
this.pattern = Pattern.compile("^" + LINE_PATTERN_STR + letter + LINE_PATTERN_STR); | ||
} | ||
|
||
public Pattern getScopePattern() { | ||
return pattern; | ||
} | ||
|
||
public boolean matches(String line) { | ||
return this.pattern.matcher(line).find(); | ||
} | ||
} | ||
|
||
@Override | ||
public Set<Violation> parseReportOutput(String reportContent, ViolationsLogger violationsLogger) throws Exception { | ||
Set<Violation> violations = new TreeSet<>(); | ||
Iterator<String> linesItr = Arrays.asList(reportContent.split("\\r?\\n")).iterator(); | ||
while (linesItr.hasNext()) { | ||
String line = linesItr.next(); | ||
if (ADDED.matches(line)) { | ||
violations.add(parseAdded(line, linesItr)); | ||
} else if (DELETED.matches(line)) { | ||
violations.add(parseDeleted(line, linesItr)); | ||
} else if (CHANGED.matches(line)) { | ||
violations.add(parseChanged(line, linesItr)); | ||
} else { | ||
LOG.log(Level.FINE, "Was unable to detect diff state " + line); | ||
} | ||
} | ||
return violations; | ||
} | ||
|
||
private CommonParsed parseCommon(String line) { | ||
String[] orignalAndNew = line.split("[acd]"); | ||
|
||
String originalLines = orignalAndNew[0]; | ||
Matcher originalMatcher = LINE_PATTERN.matcher(originalLines); | ||
int startLineBefore = Integer.parseInt(originalMatcher.group(0)); | ||
Integer endLineBefore = null; | ||
if (originalMatcher.groupCount() > 1) { | ||
endLineBefore = Integer.parseInt(originalMatcher.group(1)); | ||
} | ||
|
||
String newLines = orignalAndNew[1]; | ||
Matcher newMatcher = LINE_PATTERN.matcher(newLines); | ||
int startLineAfter = Integer.parseInt(newMatcher.group(0)); | ||
Integer endLineAfter = null; | ||
if (newMatcher.groupCount() > 1) { | ||
endLineAfter = Integer.parseInt(newMatcher.group(1)); | ||
} | ||
return new CommonParsed(startLineBefore, endLineBefore, startLineAfter, endLineAfter); | ||
} | ||
|
||
private Violation parseChanged(String line, Iterator<String> linesItr) { | ||
CommonParsed commonParsed = parseCommon(line); | ||
new StringBuilder(); | ||
while (linesItr.hasNext()) { | ||
line = linesItr.next(); | ||
if (line.equals("---")) { | ||
break; | ||
} | ||
} | ||
return violationBuilder() | ||
.setStartLine(commonParsed.startLineBefore) | ||
.setEndLine(commonParsed.endLineBefore) | ||
.setSpecific("START_LINE_AFTER", commonParsed.startLineAfter) | ||
.setSpecific("END_LINE_AFTER", commonParsed.endLineAfter) | ||
.build(); | ||
} | ||
|
||
private Violation parseDeleted(String line, Iterator<String> linesItr) { | ||
CommonParsed commonParsed = parseCommon(line); | ||
return violationBuilder() | ||
.setStartLine(commonParsed.startLineBefore) | ||
.setEndLine(commonParsed.endLineBefore) | ||
.setSpecific("START_LINE_AFTER", commonParsed.startLineAfter) | ||
.setSpecific("END_LINE_AFTER", commonParsed.endLineAfter) | ||
.build(); | ||
} | ||
|
||
private Violation parseAdded(String line, Iterator<String> linesItr) { | ||
CommonParsed commonParsed = parseCommon(line); | ||
return violationBuilder() | ||
.setStartLine(commonParsed.startLineBefore) | ||
.setEndLine(commonParsed.endLineBefore) | ||
.setSpecific("START_LINE_AFTER", commonParsed.startLineAfter) | ||
.setSpecific("END_LINE_AFTER", commonParsed.endLineAfter) | ||
.build(); | ||
} | ||
|
||
} |
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,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/diff-1\\.txt$") // | ||
.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); | ||
} | ||
} |
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 | ||
|
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,22 @@ | ||
0a1,6 | ||
> This is an important | ||
> notice! It should | ||
> therefore be located at | ||
> the beginning of this | ||
> document! | ||
> | ||
11,15d16 | ||
< This paragraph contains | ||
< text that is outdated. | ||
< It will be deleted in the | ||
< near future. | ||
< | ||
17c18 | ||
< check this dokument. On | ||
--- | ||
> check this document. On | ||
24a26,29 | ||
> | ||
> This paragraph contains | ||
> important new additions | ||
> to this document. |