Skip to content

Commit 4db31be

Browse files
committed
Getting diff of each file to avoid truncation #7
1 parent 6830253 commit 4db31be

File tree

4 files changed

+34
-26
lines changed

4 files changed

+34
-26
lines changed

CHANGELOG.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ Changelog of Violation comments to bitbucket server lib.
130130

131131

132132
## 1.52
133-
### GitHub [#5](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/pull/5) Add optional creation of Bitbucket tasks for violation comments.
133+
### GitHub [#5](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/pull/5) Add optional creation of Bitbucket tasks for violation comments. *enhancement*
134134

135135
**Formatting after merge**
136136

@@ -198,7 +198,7 @@ Changelog of Violation comments to bitbucket server lib.
198198

199199

200200
## 1.46
201-
### GitHub #47
201+
### GitHub #47
202202

203203
**Personal Access Tokens**
204204

@@ -285,7 +285,7 @@ Changelog of Violation comments to bitbucket server lib.
285285

286286

287287
## 1.36
288-
### Jira JENKINS-48648
288+
### Jira JENKINS-48648
289289

290290
**Using UTF-8 in Bitbucket client**
291291

@@ -364,7 +364,7 @@ Changelog of Violation comments to bitbucket server lib.
364364

365365

366366
## 1.28
367-
### GitHub [#2](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/issues/2) Paths with spaces fail
367+
### GitHub [#2](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/issues/2) Paths with spaces fail *bug*
368368

369369
**URL encoding paths**
370370

@@ -468,7 +468,7 @@ Changelog of Violation comments to bitbucket server lib.
468468

469469

470470
## 1.17
471-
### GitHub [#1](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/issues/1) NullPointerException when running the Jenkins plugin
471+
### GitHub [#1](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/issues/1) NullPointerException when running the Jenkins plugin
472472

473473
**Handling deleted files in PR:s**
474474

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

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

33
import static com.google.common.base.Strings.isNullOrEmpty;
4-
import static com.google.common.base.Suppliers.memoizeWithExpiration;
4+
import static com.google.common.cache.CacheBuilder.newBuilder;
55
import static com.google.common.collect.Lists.newArrayList;
6-
import static java.util.concurrent.TimeUnit.SECONDS;
6+
import static java.util.concurrent.TimeUnit.MINUTES;
77
import static java.util.logging.Level.SEVERE;
88
import static se.bjurr.violations.comments.bitbucketserver.lib.client.model.DIFFTYPE.ADDED;
99

1010
import com.google.common.annotations.VisibleForTesting;
11-
import com.google.common.base.Supplier;
11+
import com.google.common.cache.CacheLoader;
12+
import com.google.common.cache.LoadingCache;
1213
import java.util.ArrayList;
1314
import java.util.List;
1415
import se.bjurr.violations.comments.bitbucketserver.lib.client.BitbucketServerClient;
@@ -29,16 +30,16 @@ public class BitbucketServerCommentsProvider implements CommentsProvider {
2930

3031
private final BitbucketServerClient client;
3132

32-
private final Supplier<BitbucketServerDiffResponse> diffResponse =
33-
memoizeWithExpiration(
34-
new Supplier<BitbucketServerDiffResponse>() {
35-
@Override
36-
public BitbucketServerDiffResponse get() {
37-
return client.pullRequestDiff();
38-
}
39-
},
40-
10,
41-
SECONDS);
33+
private final LoadingCache<String, BitbucketServerDiffResponse> diffResponse =
34+
newBuilder()
35+
.maximumSize(100)
36+
.expireAfterWrite(2, MINUTES)
37+
.build(
38+
new CacheLoader<String, BitbucketServerDiffResponse>() {
39+
public BitbucketServerDiffResponse load(String path) {
40+
return client.pullRequestDiff(path);
41+
}
42+
});
4243

4344
private final ViolationCommentsToBitbucketServerApi violationCommentsToBitbucketApi;
4445
private final ViolationsLogger violationsLogger;
@@ -149,8 +150,14 @@ public boolean shouldComment(final ChangedFile changedFile, final Integer change
149150
return true;
150151
}
151152
final int context = violationCommentsToBitbucketApi.getCommentOnlyChangedContentContext();
152-
final List<BitbucketServerDiff> diffs = diffResponse.get().getDiffs();
153-
return shouldComment(changedFile, changedLine, context, diffs);
153+
try {
154+
final List<BitbucketServerDiff> diffs =
155+
diffResponse.get(changedFile.getFilename()).getDiffs();
156+
return shouldComment(changedFile, changedLine, context, diffs);
157+
} catch (Exception e) {
158+
violationsLogger.log(SEVERE, "Was unable to get diff from " + changedFile.getFilename(), e);
159+
return false;
160+
}
154161
}
155162

156163
@VisibleForTesting

src/main/java/se/bjurr/violations/comments/bitbucketserver/lib/client/BitbucketServerClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ public List<BitbucketServerComment> pullRequestComments(final String changedFile
202202
}
203203
}
204204

205-
public BitbucketServerDiffResponse pullRequestDiff() {
206-
final String url = getBitbucketServerPullRequestBase() + "/diff?limit=999999";
205+
public BitbucketServerDiffResponse pullRequestDiff(final String path) {
206+
final String url = getBitbucketServerPullRequestBase() + "/diff/" + path;
207207
final String json = doInvokeUrl(url, BitbucketServerInvoker.Method.GET, null);
208208
try {
209209
final BitbucketServerDiffResponse diff =

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public void log(final Level level, final String string, final Throwable t) {
5050
null,
5151
null);
5252
private String invoked;
53+
private String path = "anypath";
5354

5455
@Before
5556
public void before() {
@@ -143,7 +144,7 @@ public void testPullRequestCommentsOnFileWithSpaces() {
143144
@Test
144145
public void testPullRequestDiffDiffDeleted() {
145146
mockJson("pull-request-changes-3-deleted.json");
146-
final BitbucketServerDiffResponse response = sut.pullRequestDiff();
147+
final BitbucketServerDiffResponse response = sut.pullRequestDiff(path);
147148
assertThat(response) //
148149
.isNotNull();
149150
assertThat(response.getDiffs()) //
@@ -153,7 +154,7 @@ public void testPullRequestDiffDiffDeleted() {
153154
@Test
154155
public void testPullRequestDiffDiffTypes() {
155156
mockJson("pull-request-changes-2.json");
156-
final BitbucketServerDiffResponse response = sut.pullRequestDiff();
157+
final BitbucketServerDiffResponse response = sut.pullRequestDiff(path);
157158
assertThat(response) //
158159
.isNotNull();
159160
assertThat(response.getDiffs()) //
@@ -174,7 +175,7 @@ public void testPullRequestDiffDiffTypes() {
174175
@Test
175176
public void testPullRequestDiffPerFileTestCpp() {
176177
mockJson("pull-request-changes-2.json");
177-
final BitbucketServerDiffResponse response = sut.pullRequestDiff();
178+
final BitbucketServerDiffResponse response = sut.pullRequestDiff(path);
178179
assertThat(response) //
179180
.isNotNull();
180181
final List<BitbucketServerDiff> diffs = filterByFile(response, "cpp/test.cpp");
@@ -192,7 +193,7 @@ public void testPullRequestDiffPerFileTestCpp() {
192193
@Test
193194
public void testPullRequestDiffPerFileTravisYml() {
194195
mockJson("pull-request-changes-2.json");
195-
final BitbucketServerDiffResponse response = sut.pullRequestDiff();
196+
final BitbucketServerDiffResponse response = sut.pullRequestDiff(path);
196197
assertThat(response) //
197198
.isNotNull();
198199
final List<BitbucketServerDiff> diffs = filterByFile(response, ".travis.yml");

0 commit comments

Comments
 (0)