Skip to content

Commit 40c69f6

Browse files
committed
Handling deleted files in PR:s #1
* And using violations lib 1.22
1 parent 9209e4c commit 40c69f6

File tree

5 files changed

+1977
-11
lines changed

5 files changed

+1977
-11
lines changed

CHANGELOG.md

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,76 @@
22
# Changelog
33
## Unreleased
44
### No issue
5-
**Initial**
6-
[ac4bb0a4224cc39](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/ac4bb0a4224cc39) Tomas Bjerre *2016-07-26 09:21:47*
5+
**Handling deleted files in PR:s #1**
6+
* And using violations lib 1.22
7+
[e1c2e84b34a589e](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/e1c2e84b34a589e) Tomas Bjerre *2016-12-21 17:16:02*
8+
**violations lib 1.22**
9+
[9209e4c5df3a33c](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/9209e4c5df3a33c) Tomas Bjerre *2016-12-21 16:58:03*
10+
## 1.16
11+
### No issue
12+
**More details if response cannot be parsed**
13+
[c2f0453d2880e2f](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/c2f0453d2880e2f) Tomas Bjerre *2016-12-12 17:09:40*
14+
## 1.15
15+
### No issue
16+
**Codenarc, Cpd, Gendarme, Jcereport, Simian, ZptLint**
17+
[e407e22ac35361b](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/e407e22ac35361b) Tomas Bjerre *2016-11-06 06:57:28*
18+
## 1.14
19+
### No issue
20+
**Commenting only ADDED, and adding context integer**
21+
[8e074647617c76d](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/8e074647617c76d) Tomas Bjerre *2016-11-01 19:56:02*
22+
## 1.13
23+
### No issue
24+
**Moving multiple/single commenting logic to lib**
25+
[576f30c6aeafa05](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/576f30c6aeafa05) Tomas Bjerre *2016-11-01 16:58:18*
26+
## 1.12
27+
### No issue
28+
**Including only changed parts in big comment**
29+
[38956ecff098aec](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/38956ecff098aec) Tomas Bjerre *2016-10-31 21:17:02*
30+
## 1.11
31+
### No issue
32+
**Including only changed parts in big comment**
33+
[2921f6970c5f10b](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/2921f6970c5f10b) Tomas Bjerre *2016-10-31 21:12:21*
34+
**Adding diff endpoint to client**
35+
[a7c57c68c22bbc1](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/a7c57c68c22bbc1) Tomas Bjerre *2016-10-31 20:12:41*
36+
## 1.10
37+
### No issue
38+
**Markdown in comments and not commenting unchanged files**
39+
[ea4ca5639f75022](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/ea4ca5639f75022) Tomas Bjerre *2016-10-27 17:00:54*
40+
## 1.9
41+
### No issue
42+
**Handling CSS Lint reports without line or evidence**
43+
[2ad392e6446091f](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/2ad392e6446091f) Tomas Bjerre *2016-10-26 15:53:42*
44+
## 1.8
45+
### No issue
46+
**Changing rule format in PyLint to CODE(codeName)**
47+
[138665e1f556abd](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/138665e1f556abd) Tomas Bjerre *2016-10-25 18:14:27*
48+
## 1.7
49+
### No issue
50+
**PyLint parser**
51+
[e5cb74d799e022e](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/e5cb74d799e022e) Tomas Bjerre *2016-10-24 17:29:59*
52+
## 1.6
53+
### No issue
54+
**Supporting StyleCop and fixing FxCop**
55+
[b318ab5bbe6647d](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/b318ab5bbe6647d) Tomas Bjerre *2016-10-03 17:09:38*
56+
## 1.5
57+
### No issue
58+
**FXCop**
59+
[6e1c3db034115ae](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/6e1c3db034115ae) Tomas Bjerre *2016-10-01 11:39:06*
60+
## 1.4
61+
### No issue
62+
**Avoiding crash when unable to remove comment**
63+
[1bd7386d09197c0](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/1bd7386d09197c0) Tomas Bjerre *2016-08-19 05:04:02*
64+
## 1.3
65+
### No issue
66+
**Avoiding commenting on line 0, it fails**
67+
[b9e9581e16b69ab](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/b9e9581e16b69ab) Tomas Bjerre *2016-08-02 17:29:11*
68+
## 1.2
69+
### No issue
70+
**Removing any trailing slash from base url**
71+
[504ee8501ccb5f1](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/504ee8501ccb5f1) Tomas Bjerre *2016-08-02 17:14:58*
72+
## 1.1
73+
### No issue
74+
**Not trying to delete PR comments**
75+
* Looks like its not supported in API
76+
[f00b2b738497013](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/f00b2b738497013) Tomas Bjerre *2016-07-29 19:50:15*
777

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package se.bjurr.violations.comments.bitbucketserver.lib;
22

3+
import static com.google.common.base.Strings.isNullOrEmpty;
34
import static com.google.common.base.Suppliers.memoizeWithExpiration;
45
import static com.google.common.collect.Lists.newArrayList;
56
import static java.util.concurrent.TimeUnit.SECONDS;
@@ -18,6 +19,7 @@
1819
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.BitbucketServerComment;
1920
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.BitbucketServerDiff;
2021
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.BitbucketServerDiffResponse;
22+
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.DiffDestination;
2123
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.DiffHunk;
2224
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.Line;
2325
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.Segment;
@@ -29,6 +31,8 @@ public class BitbucketServerCommentsProvider implements CommentsProvider {
2931

3032
private static final Logger LOG = LoggerFactory.getLogger(BitbucketServerCommentsProvider.class);
3133

34+
private final BitbucketServerClient client;
35+
3236
private final Supplier<BitbucketServerDiffResponse> diffResponse = memoizeWithExpiration(
3337
new Supplier<BitbucketServerDiffResponse>() {
3438
@Override
@@ -37,8 +41,6 @@ public BitbucketServerDiffResponse get() {
3741
}
3842
}, 10, SECONDS);
3943

40-
private final BitbucketServerClient client;
41-
4244
private final ViolationCommentsToBitbucketServerApi violationCommentsToBitbucketApi;
4345

4446
@VisibleForTesting
@@ -124,13 +126,21 @@ public boolean shouldComment(ChangedFile changedFile, Integer changedLine) {
124126
@VisibleForTesting
125127
boolean shouldComment(ChangedFile changedFile, Integer changedLine, int context, List<BitbucketServerDiff> diffs) {
126128
for (BitbucketServerDiff diff : diffs) {
127-
if (diff.getDestination().getToString().equals(changedFile.getFilename())) {
128-
for (DiffHunk hunk : diff.getHunks()) {
129-
for (Segment segment : hunk.getSegments()) {
130-
if (segment.getType() == ADDED) {
131-
for (Line line : segment.getLines()) {
132-
if (line.getDestination() >= changedLine - context && line.getDestination() <= changedLine + context) {
133-
return true;
129+
DiffDestination destination = diff.getDestination();
130+
if (destination != null) {
131+
String destinationToString = destination.getToString();
132+
if (!isNullOrEmpty(destinationToString)) {
133+
if (destinationToString.equals(changedFile.getFilename())) {
134+
if (diff.getHunks() != null) {
135+
for (DiffHunk hunk : diff.getHunks()) {
136+
for (Segment segment : hunk.getSegments()) {
137+
if (segment.getType() == ADDED) {
138+
for (Line line : segment.getLines()) {
139+
if (line.getDestination() >= changedLine - context && line.getDestination() <= changedLine + context) {
140+
return true;
141+
}
142+
}
143+
}
134144
}
135145
}
136146
}

src/test/java/se/bjurr/violations/comments/bitbucketserver/lib/BitbucketServerCommentsProviderTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ public void before() {
2525
sut = new BitbucketServerCommentsProvider();
2626
}
2727

28+
@Test
29+
public void testThatDeletedFilesCanBeHandled() {
30+
ChangedFile changedFile = new ChangedFile("filename", null);
31+
List<BitbucketServerDiff> diffs = newArrayList();
32+
List<DiffHunk> hunks = newArrayList();
33+
List<Segment> segments = newArrayList();
34+
List<Line> lines = newArrayList();
35+
lines.add(new Line(10));
36+
segments.add(new Segment(DIFFTYPE.ADDED, lines));
37+
hunks.add(new DiffHunk(segments));
38+
BitbucketServerDiff diff = new BitbucketServerDiff(null, hunks);
39+
diffs.add(diff);
40+
41+
Integer changedLine = 10;
42+
int context = 0;
43+
boolean actual = sut.shouldComment(changedFile, changedLine, context, diffs);
44+
assertThat(actual).isFalse();
45+
}
46+
2847
@Test
2948
public void testThatOnlyAddedCanBeCommented() {
3049
ChangedFile changedFile = new ChangedFile("filename", null);

src/test/java/se/bjurr/violations/comments/bitbucketserver/lib/client/BitbucketServerClientTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ public void testPullRequestCommentsOnFile() {
8989
assertThat(actual.get(0).getVersion()).isEqualTo(0);
9090
}
9191

92+
@Test
93+
public void testPullRequestDiffDiffDeleted() {
94+
mockJson("pull-request-changes-3-deleted.json");
95+
BitbucketServerDiffResponse response = sut.pullRequestDiff();
96+
assertThat(response)//
97+
.isNotNull();
98+
assertThat(response.getDiffs())//
99+
.hasSize(12);
100+
}
101+
92102
@Test
93103
public void testPullRequestDiffDiffTypes() {
94104
mockJson("pull-request-changes-2.json");

0 commit comments

Comments
 (0)