88import com .google .common .annotations .VisibleForTesting ;
99import com .google .gson .Gson ;
1010import com .jayway .jsonpath .JsonPath ;
11+ import java .io .File ;
1112import java .io .UnsupportedEncodingException ;
1213import java .util .LinkedHashMap ;
1314import java .util .List ;
@@ -62,18 +63,23 @@ public BitbucketServerClient(
6263 new ProxyConfig (proxyHostNameOrIp , proxyHostPort , proxyUser , proxyPassword );
6364 }
6465
65- private String getBitbucketServerPulLRequestBase () {
66- return bitbucketServerBaseUrl
67- + "/rest/api/1.0/projects/"
66+ private String getBitbucketServerApiBase () {
67+ return bitbucketServerBaseUrl + "/rest/api/1.0" ;
68+ }
69+
70+ private String getBitbucketServerPullRequestBase () {
71+ return getBitbucketServerApiBase ()
72+ + "/projects/"
6873 + bitbucketServerProject
6974 + "/repos/"
7075 + bitbucketServerRepo
7176 + "/pull-requests/"
7277 + bitbucketServerPullRequestId ;
7378 }
7479
75- private <T > T invokeAndParse (final String url , final String jsonPath ) {
76- final String json = doInvokeUrl (url , BitbucketServerInvoker .Method .GET , null );
80+ private <T > T invokeAndParse (
81+ final String url , final Method method , final String postContent , final String jsonPath ) {
82+ final String json = doInvokeUrl (url , method , postContent );
7783
7884 try {
7985 return JsonPath .read (json , jsonPath );
@@ -85,24 +91,27 @@ private <T> T invokeAndParse(final String url, final String jsonPath) {
8591
8692 public List <String > pullRequestChanges () {
8793 return invokeAndParse (
88- getBitbucketServerPulLRequestBase () + "/changes?limit=999999" , "$..path.toString" );
94+ getBitbucketServerPullRequestBase () + "/changes?limit=999999" ,
95+ Method .GET ,
96+ null ,
97+ "$..path.toString" );
8998 }
9099
91100 public void pullRequestComment (final String message ) {
92101 final String postContent = "{ \" text\" : \" " + safeJson (message ) + "\" }" ;
93102 doInvokeUrl (
94- getBitbucketServerPulLRequestBase () + "/comments" ,
103+ getBitbucketServerPullRequestBase () + "/comments" ,
95104 BitbucketServerInvoker .Method .POST ,
96105 postContent );
97106 }
98107
99- private String doInvokeUrl (final String string , final Method method , final String postContent ) {
108+ private String doInvokeUrl (final String url , final Method method , final String postContent ) {
100109 if (isNullOrEmpty (bitbucketServerUser ) || isNullOrEmpty (bitbucketServerPassword )) {
101110 return bitbucketServerInvoker .invokeUrl (
102- string , method , postContent , bitbucketPersonalAccessToken , proxyInformation );
111+ url , method , postContent , bitbucketPersonalAccessToken , proxyInformation );
103112 } else {
104113 return bitbucketServerInvoker .invokeUrl (
105- string ,
114+ url ,
106115 method ,
107116 postContent ,
108117 bitbucketServerUser ,
@@ -111,33 +120,40 @@ private String doInvokeUrl(final String string, final Method method, final Strin
111120 }
112121 }
113122
114- public void pullRequestComment (final String changedFile , int line , final String message ) {
123+ public BitbucketServerComment pullRequestComment (final String changedFile , int line , final String message ) {
115124 if (line == 0 ) {
116125 line = 1 ;
117126 }
118- final String postContent =
127+ final String commentPostContent =
119128 "{ \" text\" : \" "
120129 + safeJson (message )
121130 + "\" , \" anchor\" : { \" line\" : "
122131 + line
123132 + ", \" lineType\" : \" ADDED\" , \" fileType\" : \" TO\" , \" path\" : \" "
124133 + changedFile
125134 + "\" }}" ;
126- doInvokeUrl (
127- getBitbucketServerPulLRequestBase () + "/comments" ,
128- BitbucketServerInvoker .Method .POST ,
129- postContent );
135+
136+ final LinkedHashMap <?, ?> parsed =
137+ invokeAndParse (
138+ getBitbucketServerPullRequestBase () + "/comments" ,
139+ BitbucketServerInvoker .Method .POST ,
140+ commentPostContent ,
141+ "$" );
142+
143+ return toBitbucketServerComment (parsed );
130144 }
131145
132146 public List <BitbucketServerComment > pullRequestComments (final String changedFile ) {
133147 try {
134148 final String encodedChangedFile = encode (changedFile , UTF_8 .name ());
135149 final List <LinkedHashMap <?, ?>> parsed =
136150 invokeAndParse (
137- getBitbucketServerPulLRequestBase ()
151+ getBitbucketServerPullRequestBase ()
138152 + "/comments?path="
139153 + encodedChangedFile
140154 + "&limit=999999" ,
155+ Method .GET ,
156+ null ,
141157 "$.values[*]" );
142158 return toBitbucketServerComments (parsed );
143159 } catch (final UnsupportedEncodingException e ) {
@@ -146,7 +162,7 @@ public List<BitbucketServerComment> pullRequestComments(final String changedFile
146162 }
147163
148164 public BitbucketServerDiffResponse pullRequestDiff () {
149- final String url = getBitbucketServerPulLRequestBase () + "/diff?limit=999999" ;
165+ final String url = getBitbucketServerPullRequestBase () + "/diff?limit=999999" ;
150166 final String json = doInvokeUrl (url , BitbucketServerInvoker .Method .GET , null );
151167 try {
152168 return new Gson ().fromJson (json , BitbucketServerDiffResponse .class );
@@ -157,7 +173,7 @@ public BitbucketServerDiffResponse pullRequestDiff() {
157173
158174 public void pullRequestRemoveComment (final Integer commentId , final Integer commentVersion ) {
159175 doInvokeUrl (
160- getBitbucketServerPulLRequestBase ()
176+ getBitbucketServerPullRequestBase ()
161177 + "/comments/"
162178 + commentId
163179 + "?version="
@@ -166,6 +182,22 @@ public void pullRequestRemoveComment(final Integer commentId, final Integer comm
166182 null );
167183 }
168184
185+ public void commentCreateTask (
186+ final BitbucketServerComment comment , String changedFile , int line ) {
187+ final String changedFileName = new File (changedFile ).getName ();
188+
189+ final String taskPostContent =
190+ "{ \" anchor\" : { \" id\" : "
191+ + comment .getId ()
192+ + ", \" type\" : \" COMMENT\" }, \" text\" : \" [Violation] "
193+ + changedFileName
194+ + " L"
195+ + line
196+ + "\" }}" ;
197+
198+ doInvokeUrl (getBitbucketServerApiBase () + "/tasks" , Method .POST , taskPostContent );
199+ }
200+
169201 @ VisibleForTesting
170202 String safeJson (final String message ) {
171203 return message .replaceAll ("\\ \\ " , "\\ \\ \\ \\ " ).replaceAll ("\" " , "" ).replaceAll ("\n " , "\\ \\ n" );
@@ -175,11 +207,16 @@ private List<BitbucketServerComment> toBitbucketServerComments(
175207 final List <LinkedHashMap <?, ?>> parsed ) {
176208 final List <BitbucketServerComment > transformed = newArrayList ();
177209 for (final LinkedHashMap <?, ?> from : parsed ) {
178- final Integer version = (Integer ) from .get ("version" );
179- final String text = (String ) from .get ("text" );
180- final Integer id = (Integer ) from .get ("id" );
181- transformed .add (new BitbucketServerComment (version , text , id ));
210+ transformed .add (toBitbucketServerComment (from ));
182211 }
183212 return transformed ;
184213 }
214+
215+ private BitbucketServerComment toBitbucketServerComment (LinkedHashMap <?, ?> parsed ) {
216+ final Integer version = (Integer ) parsed .get ("version" );
217+ final String text = (String ) parsed .get ("text" );
218+ final Integer id = (Integer ) parsed .get ("id" );
219+
220+ return new BitbucketServerComment (version , text , id );
221+ }
185222}
0 commit comments