1515import se .bjurr .violations .comments .bitbucketserver .lib .client .BitbucketServerInvoker .Method ;
1616import se .bjurr .violations .comments .bitbucketserver .lib .client .model .BitbucketServerComment ;
1717import se .bjurr .violations .comments .bitbucketserver .lib .client .model .BitbucketServerDiffResponse ;
18+ import se .bjurr .violations .comments .lib .ViolationsLogger ;
1819
1920public 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 }
0 commit comments