@@ -17,6 +17,7 @@ public static void setBitbucketServerInvoker(BitbucketServerInvoker bitbucketSer
1717 }
1818
1919 private final String bitbucketServerBaseUrl ;
20+
2021 private final String bitbucketServerPassword ;
2122 private final String bitbucketServerProject ;
2223 private final Integer bitbucketServerPullRequestId ;
@@ -37,14 +38,29 @@ public BitbucketServerClient(String bitbucketServerBaseUrl, String bitbucketServ
3738 this .bitbucketServerUser = bitbucketServerUser ;
3839 }
3940
41+ private String getBitbucketServerPulLRequestBase () {
42+ return bitbucketServerBaseUrl + "/rest/api/1.0/projects/" + bitbucketServerProject + "/repos/" + bitbucketServerRepo
43+ + "/pull-requests/" + bitbucketServerPullRequestId ;
44+ }
45+
46+ private <T > T invokeAndParse (String url , String jsonPath ) {
47+ String json = bitbucketServerInvoker .invokeUrl (url , BitbucketServerInvoker .Method .GET , null , bitbucketServerUser ,
48+ bitbucketServerPassword );
49+ try {
50+ return JsonPath .read (json , jsonPath );
51+ } catch (Exception e ) {
52+ throw e ;
53+ }
54+ }
55+
4056 public List <String > pullRequestChanges () {
4157 return invokeAndParse (getBitbucketServerPulLRequestBase () + "/changes?limit=999999" , "$..path.toString" );
4258 }
4359
4460 public void pullRequestComment (String message ) {
4561 String postContent = "{ \" text\" : \" " + safeJson (message ) + "\" }" ;
4662 bitbucketServerInvoker .invokeUrl (getBitbucketServerPulLRequestBase () + "/comments" ,
47- BitbucketServerInvoker .Method .POST , postContent , this . bitbucketServerUser , this . bitbucketServerPassword );
63+ BitbucketServerInvoker .Method .POST , postContent , bitbucketServerUser , bitbucketServerPassword );
4864 }
4965
5066 public void pullRequestComment (String changedFile , int line , String message ) {
@@ -54,38 +70,24 @@ public void pullRequestComment(String changedFile, int line, String message) {
5470 String postContent = "{ \" text\" : \" " + safeJson (message ) + "\" , \" anchor\" : { \" line\" : " + line
5571 + ", \" lineType\" : \" ADDED\" , \" fileType\" : \" TO\" , \" path\" : \" " + changedFile + "\" }}" ;
5672 bitbucketServerInvoker .invokeUrl (getBitbucketServerPulLRequestBase () + "/comments" ,
57- BitbucketServerInvoker .Method .POST , postContent , this . bitbucketServerUser , this . bitbucketServerPassword );
73+ BitbucketServerInvoker .Method .POST , postContent , bitbucketServerUser , bitbucketServerPassword );
5874 }
5975
6076 public List <BitbucketServerComment > pullRequestComments (String changedFile ) {
61- List <LinkedHashMap <?, ?>> parsed = invokeAndParse (getBitbucketServerPulLRequestBase () + "/comments?path="
62- + changedFile + "&limit=999999" , "$.values[*]" );
77+ List <LinkedHashMap <?, ?>> parsed = invokeAndParse (
78+ getBitbucketServerPulLRequestBase () + "/comments?path=" + changedFile + "&limit=999999" , "$.values[*]" );
6379 return toBitbucketServerComments (parsed );
6480 }
6581
6682 public void pullRequestRemoveComment (Integer commentId , Integer commentVersion ) {
67- bitbucketServerInvoker .invokeUrl (getBitbucketServerPulLRequestBase () + "/comments/" + commentId + "?version="
68- + commentVersion , BitbucketServerInvoker . Method . DELETE , null , this . bitbucketServerUser ,
69- this . bitbucketServerPassword );
83+ bitbucketServerInvoker .invokeUrl (
84+ getBitbucketServerPulLRequestBase () + "/comments/" + commentId + "?version=" + commentVersion ,
85+ BitbucketServerInvoker . Method . DELETE , null , bitbucketServerUser , bitbucketServerPassword );
7086 }
7187
72- private String getBitbucketServerPulLRequestBase () {
73- return this .bitbucketServerBaseUrl + "/rest/api/1.0/projects/" + this .bitbucketServerProject + "/repos/"
74- + this .bitbucketServerRepo + "/pull-requests/" + this .bitbucketServerPullRequestId ;
75- }
76-
77- private <T > T invokeAndParse (String url , String jsonPath ) {
78- String json = bitbucketServerInvoker .invokeUrl (url , BitbucketServerInvoker .Method .GET , null ,
79- this .bitbucketServerUser , this .bitbucketServerPassword );
80- try {
81- return JsonPath .read (json , jsonPath );
82- } catch (Exception e ) {
83- throw e ;
84- }
85- }
86-
87- private String safeJson (String message ) {
88- return message .replaceAll ("\" " , "" ).replaceAll ("\n " , "\\ \\ n" );
88+ @ VisibleForTesting
89+ String safeJson (String message ) {
90+ return message .replaceAll ("\\ \\ " , "\\ \\ \\ \\ " ).replaceAll ("\" " , "" ).replaceAll ("\n " , "\\ \\ n" );
8991 }
9092
9193 private List <BitbucketServerComment > toBitbucketServerComments (List <LinkedHashMap <?, ?>> parsed ) {
0 commit comments