Skip to content

Commit 8e07464

Browse files
committed
Commenting only ADDED, and adding context integer
1 parent a8cdf02 commit 8e07464

File tree

3 files changed

+94
-5
lines changed

3 files changed

+94
-5
lines changed

src/main/java/se/bjurr/violations/comments/bitbucketserver/lib/BitbucketServerCommentsProvider.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
import static com.google.common.collect.Lists.newArrayList;
55
import static java.util.concurrent.TimeUnit.SECONDS;
66
import static se.bjurr.violations.comments.bitbucketserver.lib.client.model.DIFFTYPE.ADDED;
7-
import static se.bjurr.violations.comments.bitbucketserver.lib.client.model.DIFFTYPE.CONTEXT;
87

98
import java.util.ArrayList;
109
import java.util.List;
1110

1211
import org.slf4j.Logger;
1312
import org.slf4j.LoggerFactory;
1413

14+
import com.google.common.annotations.VisibleForTesting;
1515
import com.google.common.base.Supplier;
1616

1717
import se.bjurr.violations.comments.bitbucketserver.lib.client.BitbucketServerClient;
@@ -41,6 +41,12 @@ public BitbucketServerDiffResponse get() {
4141

4242
private final ViolationCommentsToBitbucketServerApi violationCommentsToBitbucketApi;
4343

44+
@VisibleForTesting
45+
BitbucketServerCommentsProvider() {
46+
client = null;
47+
violationCommentsToBitbucketApi = null;
48+
}
49+
4450
public BitbucketServerCommentsProvider(ViolationCommentsToBitbucketServerApi violationCommentsToBitbucketApi) {
4551
String bitbucketServerBaseUrl = violationCommentsToBitbucketApi.getBitbucketServerUrl();
4652
String bitbucketServerProject = violationCommentsToBitbucketApi.getProjectKey();
@@ -107,14 +113,23 @@ public void removeComments(List<Comment> comments) {
107113

108114
@Override
109115
public boolean shouldComment(ChangedFile changedFile, Integer changedLine) {
110-
for (BitbucketServerDiff diff : diffResponse.get().getDiffs()) {
116+
if (!violationCommentsToBitbucketApi.getCommentOnlyChangedContent()) {
117+
return true;
118+
}
119+
int context = violationCommentsToBitbucketApi.getCommentOnlyChangedContentContext();
120+
List<BitbucketServerDiff> diffs = diffResponse.get().getDiffs();
121+
return shouldComment(changedFile, changedLine, context, diffs);
122+
}
123+
124+
@VisibleForTesting
125+
boolean shouldComment(ChangedFile changedFile, Integer changedLine, int context, List<BitbucketServerDiff> diffs) {
126+
for (BitbucketServerDiff diff : diffs) {
111127
if (diff.getDestination().getToString().equals(changedFile.getFilename())) {
112128
for (DiffHunk hunk : diff.getHunks()) {
113129
for (Segment segment : hunk.getSegments()) {
114-
if (segment.getType() == ADDED //
115-
|| segment.getType() == CONTEXT) {
130+
if (segment.getType() == ADDED) {
116131
for (Line line : segment.getLines()) {
117-
if (line.getDestination() == changedLine) {
132+
if (line.getDestination() >= changedLine - context && line.getDestination() <= changedLine + context) {
118133
return true;
119134
}
120135
}

src/main/java/se/bjurr/violations/comments/bitbucketserver/lib/ViolationCommentsToBitbucketServerApi.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static ViolationCommentsToBitbucketServerApi violationCommentsToBitbucket
3030
private String username;
3131
private List<Violation> violations;
3232
private boolean commentOnlyChangedContent = false;
33+
private int commentOnlyChangedContentContext;
3334

3435
private ViolationCommentsToBitbucketServerApi() {
3536

@@ -56,6 +57,10 @@ public boolean getCommentOnlyChangedContent() {
5657
return commentOnlyChangedContent;
5758
}
5859

60+
public int getCommentOnlyChangedContentContext() {
61+
return commentOnlyChangedContentContext;
62+
}
63+
5964
public boolean getCreateCommentWithAllSingleFileComments() {
6065
return createCommentWithAllSingleFileComments;
6166
}
@@ -118,6 +123,12 @@ public ViolationCommentsToBitbucketServerApi withCommentOnlyChangedContent(boole
118123
return this;
119124
}
120125

126+
public ViolationCommentsToBitbucketServerApi withCommentOnlyChangedContentContext(
127+
int commentOnlyChangedContentContext) {
128+
this.commentOnlyChangedContentContext = commentOnlyChangedContentContext;
129+
return this;
130+
}
131+
121132
public ViolationCommentsToBitbucketServerApi withCreateCommentWithAllSingleFileComments(
122133
boolean createCommentWithAllSingleFileComments) {
123134
this.createCommentWithAllSingleFileComments = createCommentWithAllSingleFileComments;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package se.bjurr.violations.comments.bitbucketserver.lib;
2+
3+
import static com.google.common.collect.Lists.newArrayList;
4+
import static org.assertj.core.api.Assertions.assertThat;
5+
6+
import java.util.List;
7+
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.BitbucketServerDiff;
12+
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.DIFFTYPE;
13+
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.DiffDestination;
14+
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.DiffHunk;
15+
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.Line;
16+
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.Segment;
17+
import se.bjurr.violations.comments.lib.model.ChangedFile;
18+
19+
public class BitbucketServerCommentsProviderTest {
20+
21+
private BitbucketServerCommentsProvider sut;
22+
23+
@Before
24+
public void before() {
25+
sut = new BitbucketServerCommentsProvider();
26+
}
27+
28+
@Test
29+
public void testThatOnlyAddedCanBeCommented() {
30+
ChangedFile changedFile = new ChangedFile("filename", null);
31+
List<BitbucketServerDiff> diffs = newArrayList();
32+
DiffDestination destination = new DiffDestination("filename");
33+
List<DiffHunk> hunks = newArrayList();
34+
List<Segment> segments = newArrayList();
35+
List<Line> lines = newArrayList();
36+
lines.add(new Line(10));
37+
segments.add(new Segment(DIFFTYPE.ADDED, lines));
38+
hunks.add(new DiffHunk(segments));
39+
BitbucketServerDiff diff = new BitbucketServerDiff(destination, hunks);
40+
diffs.add(diff);
41+
42+
Integer changedLine = 10;
43+
int context = 0;
44+
boolean actual = sut.shouldComment(changedFile, changedLine, context, diffs);
45+
assertThat(actual).isTrue();
46+
47+
changedLine = 11;
48+
context = 0;
49+
actual = sut.shouldComment(changedFile, changedLine, context, diffs);
50+
assertThat(actual).isFalse();
51+
52+
changedLine = 11;
53+
context = 1;
54+
actual = sut.shouldComment(changedFile, changedLine, context, diffs);
55+
assertThat(actual).isTrue();
56+
57+
changedLine = 9;
58+
context = 1;
59+
actual = sut.shouldComment(changedFile, changedLine, context, diffs);
60+
assertThat(actual).isTrue();
61+
}
62+
63+
}

0 commit comments

Comments
 (0)