Skip to content

Commit 45439c6

Browse files
committed
More logging
1 parent 81a63b1 commit 45439c6

File tree

4 files changed

+65
-17
lines changed

4 files changed

+65
-17
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.Line;
2222
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.Segment;
2323
import se.bjurr.violations.comments.lib.CommentsProvider;
24+
import se.bjurr.violations.comments.lib.ViolationsLogger;
2425
import se.bjurr.violations.comments.lib.model.ChangedFile;
2526
import se.bjurr.violations.comments.lib.model.Comment;
2627
import se.bjurr.violations.lib.util.Optional;
@@ -51,7 +52,8 @@ public BitbucketServerDiffResponse get() {
5152
}
5253

5354
public BitbucketServerCommentsProvider(
54-
final ViolationCommentsToBitbucketServerApi violationCommentsToBitbucketApi) {
55+
final ViolationCommentsToBitbucketServerApi violationCommentsToBitbucketApi,
56+
final ViolationsLogger violationsLogger) {
5557
final String bitbucketServerBaseUrl = violationCommentsToBitbucketApi.getBitbucketServerUrl();
5658
final String bitbucketServerProject = violationCommentsToBitbucketApi.getProjectKey();
5759
final String bitbucketServerRepo = violationCommentsToBitbucketApi.getRepoSlug();
@@ -66,6 +68,7 @@ public BitbucketServerCommentsProvider(
6668
final String proxyPassword = violationCommentsToBitbucketApi.getProxyPassword();
6769
client =
6870
new BitbucketServerClient(
71+
violationsLogger,
6972
bitbucketServerBaseUrl,
7073
bitbucketServerProject,
7174
bitbucketServerRepo,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ private void populateFromEnvironmentVariables() {
180180
public void toPullRequest() throws Exception {
181181
populateFromEnvironmentVariables();
182182
checkState();
183-
final CommentsProvider commentsProvider = new BitbucketServerCommentsProvider(this);
183+
final CommentsProvider commentsProvider =
184+
new BitbucketServerCommentsProvider(this, violationsLogger);
184185
createComments(violationsLogger, violations, BITBUCKET_MAX_COMMENT_SIZE, commentsProvider);
185186
}
186187

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

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
import se.bjurr.violations.comments.bitbucketserver.lib.client.BitbucketServerInvoker.Method;
1616
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.BitbucketServerComment;
1717
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.BitbucketServerDiffResponse;
18+
import se.bjurr.violations.comments.lib.ViolationsLogger;
1819

1920
public class BitbucketServerClient {
2021
private static BitbucketServerInvoker bitbucketServerInvoker = new BitbucketServerInvoker();
22+
private final ViolationsLogger violationsLogger;
2123

2224
@VisibleForTesting
2325
public static void setBitbucketServerInvoker(
@@ -36,6 +38,7 @@ public static void setBitbucketServerInvoker(
3638
private final ProxyConfig proxyInformation;
3739

3840
public BitbucketServerClient(
41+
final ViolationsLogger violationsLogger,
3942
final String bitbucketServerBaseUrl,
4043
final String bitbucketServerProject,
4144
final String bitbucketServerRepo,
@@ -47,6 +50,7 @@ public BitbucketServerClient(
4750
final Integer proxyHostPort,
4851
final String proxyUser,
4952
final String proxyPassword) {
53+
this.violationsLogger = violationsLogger;
5054
if (bitbucketServerBaseUrl.endsWith("/")) {
5155
this.bitbucketServerBaseUrl =
5256
bitbucketServerBaseUrl.substring(0, bitbucketServerBaseUrl.length() - 1);
@@ -90,11 +94,26 @@ private <T> T invokeAndParse(
9094
}
9195

9296
public List<String> pullRequestChanges() {
93-
return invokeAndParse(
94-
getBitbucketServerPullRequestBase() + "/changes?limit=999999",
95-
Method.GET,
96-
null,
97-
"$..path.toString");
97+
String url = getBitbucketServerPullRequestBase() + "/changes?limit=999999";
98+
final String json = doInvokeUrl(url, Method.GET, null);
99+
100+
String jsonPath = "$..path.toString";
101+
try {
102+
List<String> response = JsonPath.read(json, jsonPath);
103+
if (response.isEmpty()) {
104+
violationsLogger.log(
105+
"Found no changed files from "
106+
+ url
107+
+ " with JSONPath "
108+
+ jsonPath
109+
+ " in JSON:\n"
110+
+ json);
111+
}
112+
return response;
113+
} catch (final Exception e) {
114+
throw new RuntimeException(
115+
"Unable to parse diff response from " + url + " using " + jsonPath + "\n\n" + json, e);
116+
}
98117
}
99118

100119
public void pullRequestComment(final String message) {
@@ -142,15 +161,27 @@ public BitbucketServerComment pullRequestComment(
142161
public List<BitbucketServerComment> pullRequestComments(final String changedFile) {
143162
try {
144163
final String encodedChangedFile = encode(changedFile, UTF_8.name());
145-
final List<LinkedHashMap<?, ?>> parsed =
146-
invokeAndParse(
147-
getBitbucketServerPullRequestBase()
148-
+ "/comments?path="
149-
+ encodedChangedFile
150-
+ "&limit=999999",
151-
Method.GET,
152-
null,
153-
"$.values[*]");
164+
String url =
165+
getBitbucketServerPullRequestBase()
166+
+ "/comments?path="
167+
+ encodedChangedFile
168+
+ "&limit=999999";
169+
String jsonPath = "$.values[*]";
170+
171+
final String json = doInvokeUrl(url, Method.GET, null);
172+
List<LinkedHashMap<?, ?>> parsed = null;
173+
try {
174+
parsed = JsonPath.read(json, jsonPath);
175+
} catch (final Exception e) {
176+
throw new RuntimeException(
177+
"Unable to parse diff response from " + url + " using " + jsonPath + "\n\n" + json, e);
178+
}
179+
180+
if (parsed.isEmpty()) {
181+
violationsLogger.log(
182+
"Found no comments from " + url + " with JSONPath " + jsonPath + " in JSON:\n" + json);
183+
}
184+
154185
return toBitbucketServerComments(parsed);
155186
} catch (final UnsupportedEncodingException e) {
156187
throw new RuntimeException(e);
@@ -161,7 +192,12 @@ public BitbucketServerDiffResponse pullRequestDiff() {
161192
final String url = getBitbucketServerPullRequestBase() + "/diff?limit=999999";
162193
final String json = doInvokeUrl(url, BitbucketServerInvoker.Method.GET, null);
163194
try {
164-
return new Gson().fromJson(json, BitbucketServerDiffResponse.class);
195+
final BitbucketServerDiffResponse diff =
196+
new Gson().fromJson(json, BitbucketServerDiffResponse.class);
197+
if (diff.getDiffs().isEmpty()) {
198+
violationsLogger.log("Found no diffs from " + url + " in JSON:\n" + json);
199+
}
200+
return diff;
165201
} catch (final Exception e) {
166202
throw new RuntimeException("Unable to parse diff response from " + url + "\n\n" + json, e);
167203
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.google.common.io.Resources;
1111
import java.io.IOException;
1212
import java.util.List;
13+
import java.util.logging.Logger;
1314
import org.junit.Before;
1415
import org.junit.Test;
1516
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.BitbucketServerComment;
@@ -18,11 +19,18 @@
1819
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.DIFFTYPE;
1920
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.DiffHunk;
2021
import se.bjurr.violations.comments.bitbucketserver.lib.client.model.Segment;
22+
import se.bjurr.violations.comments.lib.ViolationsLogger;
2123

2224
public class BitbucketServerClientTest {
2325
private String mockedJson = null;
2426
private final BitbucketServerClient sut =
2527
new BitbucketServerClient(
28+
new ViolationsLogger() {
29+
@Override
30+
public void log(final String string) {
31+
Logger.getLogger(BitbucketServerClientTest.class.getSimpleName()).info(string);
32+
}
33+
},
2634
"bitbucketServerBaseUrl",
2735
"bitbucketServerProject",
2836
"bitbucketServerRepo",

0 commit comments

Comments
 (0)