-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
12 changed files
with
332 additions
and
45 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/main/java/pl/touk/sputnik/connector/gerrit/CommentFilter.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,20 @@ | ||
package pl.touk.sputnik.connector.gerrit; | ||
|
||
public interface CommentFilter { | ||
|
||
CommentFilter EMPTY_COMMENT_FILTER = new CommentFilter() { | ||
|
||
@Override | ||
public boolean include(String filePath, int line) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void init() { | ||
} | ||
}; | ||
|
||
boolean include(String filePath, int line); | ||
|
||
void init(); | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/pl/touk/sputnik/connector/gerrit/FileDiff.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,30 @@ | ||
package pl.touk.sputnik.connector.gerrit; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class FileDiff { | ||
|
||
private final String fileName; | ||
|
||
private final Set<Integer> modifiedLines = new HashSet<>(); | ||
|
||
public FileDiff(String fileName) { | ||
this.fileName = fileName; | ||
} | ||
|
||
public void addHunk(int firstLine, int count) { | ||
for (int i = firstLine; i < firstLine + count; i++) { | ||
modifiedLines.add(i); | ||
} | ||
} | ||
|
||
public String getFileName() { | ||
return fileName; | ||
} | ||
|
||
public Set<Integer> getModifiedLines() { | ||
return Collections.unmodifiableSet(modifiedLines); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/pl/touk/sputnik/connector/gerrit/FileDiffBuilder.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,30 @@ | ||
package pl.touk.sputnik.connector.gerrit; | ||
|
||
import com.google.gerrit.extensions.common.DiffInfo.ContentEntry; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
|
||
@Slf4j | ||
public class FileDiffBuilder { | ||
|
||
@NotNull | ||
public FileDiff build(@NotNull String fileKey, @NotNull List<ContentEntry> content) { | ||
FileDiff fileDiff = new FileDiff(fileKey); | ||
int currentLine = 1; | ||
for (ContentEntry diffHunk : content) { | ||
if (diffHunk.skip != null) { | ||
currentLine += diffHunk.skip; | ||
} | ||
if (diffHunk.ab != null) { | ||
currentLine += diffHunk.ab.size(); | ||
} | ||
if (diffHunk.b != null) { | ||
fileDiff.addHunk(currentLine, diffHunk.b.size()); | ||
currentLine += diffHunk.b.size(); | ||
} | ||
} | ||
return fileDiff; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/pl/touk/sputnik/connector/gerrit/GerritCommentFilter.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,55 @@ | ||
package pl.touk.sputnik.connector.gerrit; | ||
|
||
import com.google.gerrit.extensions.api.GerritApi; | ||
import com.google.gerrit.extensions.api.changes.FileApi; | ||
import com.google.gerrit.extensions.api.changes.RevisionApi; | ||
import com.google.gerrit.extensions.common.FileInfo; | ||
import com.google.gerrit.extensions.restapi.RestApiException; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
public class GerritCommentFilter implements CommentFilter { | ||
|
||
private final GerritApi gerritApi; | ||
|
||
private final GerritPatchset patchset; | ||
|
||
private Map<String, FileDiff> modifiedLines; | ||
|
||
public GerritCommentFilter(GerritApi gerritApi, GerritPatchset patchset) { | ||
this.gerritApi = gerritApi; | ||
this.patchset = patchset; | ||
} | ||
|
||
@Override | ||
public boolean include(String filePath, int line) { | ||
FileDiff diff = modifiedLines == null ? null : modifiedLines.get(filePath); | ||
return diff != null && diff.getModifiedLines().contains(line); | ||
} | ||
|
||
@Override | ||
public void init() { | ||
log.info("Query all file diffs to only include comments on modified lines"); | ||
modifiedLines = new HashMap<>(); | ||
FileDiffBuilder fileDiffBuilder = new FileDiffBuilder(); | ||
try { | ||
RevisionApi revision = gerritApi.changes() | ||
.id(patchset.getChangeId()) | ||
.revision(patchset.getRevisionId()); | ||
|
||
for (Map.Entry<String, FileInfo> file : revision.files().entrySet()) { | ||
log.info("Query file diff for {}", file.getKey()); | ||
FileApi fileApi = revision.file(file.getKey()); | ||
FileDiff fileDiff = fileDiffBuilder.build(file.getKey(), fileApi.diff().content); | ||
modifiedLines.put(fileDiff.getFileName(), fileDiff); | ||
log.info("Query file diff for {} finished", file.getKey()); | ||
} | ||
} catch (RestApiException e) { | ||
throw new GerritException("Error when retrieve modified lines", e); | ||
} | ||
log.info("Query all file diffs finished"); | ||
} | ||
} |
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
84 changes: 84 additions & 0 deletions
84
src/test/java/pl/touk/sputnik/connector/gerrit/FileDiffBuilderTest.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,84 @@ | ||
package pl.touk.sputnik.connector.gerrit; | ||
|
||
import com.google.gerrit.extensions.common.DiffInfo; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class FileDiffBuilderTest { | ||
|
||
private static final String FILE_KEY = "test.java"; | ||
|
||
@Test | ||
void shouldMoveWithHunkSkip() { | ||
List<DiffInfo.ContentEntry> content = new ArrayList<>(); | ||
content.add(buildContentEntry(0, 0, 0, 10)); | ||
content.add(buildContentEntry(0, 0, 2, 0)); | ||
|
||
FileDiff fileDiff = new FileDiffBuilder().build(FILE_KEY, content); | ||
|
||
assertThat(fileDiff.getFileName()).isEqualTo(FILE_KEY); | ||
assertThat(fileDiff.getModifiedLines()).containsExactlyInAnyOrder(11, 12); | ||
} | ||
|
||
@Test | ||
void shouldNotMoveWithHunkA() { | ||
List<DiffInfo.ContentEntry> content = new ArrayList<>(); | ||
content.add(buildContentEntry(10, 0, 0, 0)); | ||
content.add(buildContentEntry(0, 0, 2, 0)); | ||
|
||
FileDiff fileDiff = new FileDiffBuilder().build(FILE_KEY, content); | ||
|
||
assertThat(fileDiff.getFileName()).isEqualTo(FILE_KEY); | ||
assertThat(fileDiff.getModifiedLines()).containsExactlyInAnyOrder(1, 2); | ||
} | ||
|
||
@Test | ||
void shouldMoveWithHunkAB() { | ||
List<DiffInfo.ContentEntry> content = new ArrayList<>(); | ||
content.add(buildContentEntry(0, 10, 0, 0)); | ||
content.add(buildContentEntry(0, 0, 2, 0)); | ||
|
||
FileDiff fileDiff = new FileDiffBuilder().build(FILE_KEY, content); | ||
|
||
assertThat(fileDiff.getFileName()).isEqualTo(FILE_KEY); | ||
assertThat(fileDiff.getModifiedLines()).containsExactlyInAnyOrder(11, 12); | ||
} | ||
|
||
@Test | ||
void shouldComputeComplexHunks() { | ||
List<DiffInfo.ContentEntry> content = new ArrayList<>(); | ||
content.add(buildContentEntry(10, 0, 5, 1)); | ||
content.add(buildContentEntry(3, 9, 2, 20)); | ||
|
||
FileDiff fileDiff = new FileDiffBuilder().build(FILE_KEY, content); | ||
|
||
assertThat(fileDiff.getFileName()).isEqualTo(FILE_KEY); | ||
assertThat(fileDiff.getModifiedLines()).containsExactlyInAnyOrder(2, 3, 4, 5, 6, 36, 37); | ||
} | ||
|
||
/** | ||
* We build ContentEntry instance because it is a final class | ||
*/ | ||
private DiffInfo.ContentEntry buildContentEntry(int aSize, int abSize, int bSize, int skip) { | ||
DiffInfo.ContentEntry diffHunk = new DiffInfo.ContentEntry(); | ||
diffHunk.a = buildListMock(aSize); | ||
diffHunk.ab = buildListMock(abSize); | ||
diffHunk.b = buildListMock(bSize); | ||
diffHunk.skip = skip; | ||
return diffHunk; | ||
} | ||
|
||
private List<String> buildListMock(int size) { | ||
List<String> list = mock(List.class); | ||
when(list.size()).thenReturn(size); | ||
return list; | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.