99import io .tpd .quboo .sonarplugin .pojos .Paging ;
1010import io .tpd .quboo .sonarplugin .pojos .Users ;
1111import io .tpd .quboo .sonarplugin .settings .QubooProperties ;
12+ import io .tpd .quboo .sonarplugin .util .QubooCache ;
1213import okhttp3 .*;
1314import org .sonar .api .ce .posttask .PostProjectAnalysisTask ;
1415import org .sonar .api .platform .Server ;
@@ -68,50 +69,78 @@ public void finished(ProjectAnalysis analysis) {
6869 }
6970
7071 private void sendIssuesToQuboo (final IssuesWrapper allIssues , final String qubooKey , final String qubooSecret ) throws Exception {
71- final Request request = new Request .Builder ()
72- .url (QubooPlugin .QUBOO_SERVER + "/updater/issues" )
73- .header (QubooPlugin .QUBOO_HEADER_ACCESS_KEY , qubooKey )
74- .header (QubooPlugin .QUBOO_HEADER_SECRET_KEY , qubooSecret )
75- .post (RequestBody .create (MediaType .get ("application/json" ), mapper .writeValueAsString (allIssues )))
76- .build ();
77- final Response response = http .newCall (request ).execute ();
78- final String body = response .body ().string ();
79- log .info ("Response " + response .code () + " | " + body );
72+ if (!allIssues .getIssues ().isEmpty ()) {
73+ final Request request = new Request .Builder ()
74+ .url (QubooPlugin .QUBOO_SERVER + "/updater/issues" )
75+ .header (QubooPlugin .QUBOO_HEADER_ACCESS_KEY , qubooKey )
76+ .header (QubooPlugin .QUBOO_HEADER_SECRET_KEY , qubooSecret )
77+ .post (RequestBody .create (MediaType .get ("application/json" ), mapper .writeValueAsString (allIssues )))
78+ .build ();
79+ log .info ("Sending {} issues to Quboo, url: {}" , allIssues .getIssues ().size (), request .url ().toString ());
80+ final Response response = http .newCall (request ).execute ();
81+ final String body = response .body ().string ();
82+ log .info ("Response " + response .code () + " | " + body );
83+ if (response .isSuccessful ()) {
84+ QubooCache .INSTANCE .toCache (allIssues );
85+ }
86+ } else {
87+ log .info ("No new issues to send to Quboo. Skipping..." );
88+ }
8089 }
8190
82- private IssuesWrapper getIssues (final String token ) throws Exception {
91+ private IssuesWrapper getIssues (final String token ) {
8392 int pageNumber = 1 ;
8493 boolean moreData = true ;
8594 IssuesWrapper wrapper = new IssuesWrapper ();
8695 while (moreData ) {
87- final Request .Builder request = new Request .Builder ()
88- .url (server .getPublicRootUrl () + "/api/issues/search?assigned=true&ps=200&p=" + pageNumber ).get ();
89- addAuthorizationIfNeeded (request , token );
90- final Request r = request .build ();
91- log .info ("Quboo plugin getting issues from {}" , r .url ().toString ());
92- final Response response = http .newCall (r ).execute ();
93- final String body = response .body ().string ();
94- final Issues issues = mapper .readValue (body , Issues .class );
95- log .info ("Quboo plugin got {} issues" , issues .getIssues ().size ());
96- wrapper .filterAndAddIssues (issues , server .getVersion ());
97- moreData = moreData (issues .getPaging (), issues .getIssues ().size ());
98- pageNumber ++;
96+ try {
97+ final Request .Builder request = new Request .Builder ()
98+ .url (server .getPublicRootUrl () + "/api/issues/search?assigned=true&ps=200&p=" + pageNumber ).get ();
99+ addAuthorizationIfNeeded (request , token );
100+ final Request r = request .build ();
101+ log .info ("Quboo plugin getting issues from {}" , r .url ().toString ());
102+ final Response response = http .newCall (r ).execute ();
103+ if (response .isSuccessful ()) {
104+ final String body = response .body ().string ();
105+ final Issues issues = mapper .readValue (body , Issues .class );
106+ log .info ("Quboo plugin got {} issues" , issues .getIssues ().size ());
107+ wrapper .filterAndAddIssues (issues , server .getVersion ());
108+ moreData = moreData (issues .getPaging (), issues .getIssues ().size ());
109+ pageNumber ++;
110+ if (pageNumber > 50 ) { // there is a hard limit in Sonar API that doesn't allow querying more than 10K results
111+ log .info ("Reached max number of issues that can be fetched. Skipping remaining ones..." );
112+ break ;
113+ }
114+ } else {
115+ log .error ("Aborting issues fetch. Got an error from server: {}. Request: {}" , response .message (), r .url ().toString ());
116+ break ;
117+ }
118+ } catch (final Exception e ) {
119+ log .error ("Quboo could not fetch issues from the server: " + e .getMessage ());
120+ break ;
121+ }
99122 }
100- log .info ("Sending " + wrapper .getIssues ().size () + " issues to Quboo" );
101123 return wrapper ;
102124 }
103125
104126 private void sendUsersToQuboo (final UsersWrapper allUsers , final String qubooKey , final String qubooSecret ) throws Exception {
105- final Request request = new Request .Builder ()
106- .url (QubooPlugin .QUBOO_SERVER + "/updater/users" )
107- .header (QubooPlugin .QUBOO_HEADER_ACCESS_KEY , qubooKey )
108- .header (QubooPlugin .QUBOO_HEADER_SECRET_KEY , qubooSecret )
109- .post (RequestBody .create (MediaType .get ("application/json" ), mapper .writeValueAsString (allUsers )))
110- .build ();
111- log .info ("Sending users to Quboo, url: {}" , request .url ().toString ());
112- final Response response = http .newCall (request ).execute ();
113- final String body = response .body ().string ();
114- log .info ("Response " + response .code () + " | " + body );
127+ if (!allUsers .getUsers ().isEmpty ()) {
128+ final Request request = new Request .Builder ()
129+ .url (QubooPlugin .QUBOO_SERVER + "/updater/users" )
130+ .header (QubooPlugin .QUBOO_HEADER_ACCESS_KEY , qubooKey )
131+ .header (QubooPlugin .QUBOO_HEADER_SECRET_KEY , qubooSecret )
132+ .post (RequestBody .create (MediaType .get ("application/json" ), mapper .writeValueAsString (allUsers )))
133+ .build ();
134+ log .info ("Sending {} users to Quboo, url: {}" , allUsers .getUsers ().size (), request .url ().toString ());
135+ final Response response = http .newCall (request ).execute ();
136+ final String body = response .body ().string ();
137+ log .info ("Response " + response .code () + " | " + body );
138+ if (response .isSuccessful ()) {
139+ QubooCache .INSTANCE .toCache (allUsers );
140+ }
141+ } else {
142+ log .info ("No new users to send to Quboo. Skipping..." );
143+ }
115144 }
116145
117146 private UsersWrapper getUsers (final String token ) {
@@ -127,18 +156,26 @@ private UsersWrapper getUsers(final String token) {
127156 final Request r = request .build ();
128157 log .info ("Quboo plugin getting users from {}" , r .url ().toString ());
129158 final Response response = http .newCall (r ).execute ();
130- final String body = response .body ().string ();
131- final Users users = mapper .readValue (body , Users .class );
132- log .info ("Quboo plugin got {} users" , users .getUsers ().size ());
133- wrapper .filterAndAddUsers (users , server .getVersion ());
134- moreData = moreData (users .getPaging (), users .getUsers ().size ());
135- pageNumber ++;
159+ if (response .isSuccessful ()) {
160+ final String body = response .body ().string ();
161+ final Users users = mapper .readValue (body , Users .class );
162+ log .info ("Quboo plugin got {} users" , users .getUsers ().size ());
163+ wrapper .filterAndAddUsers (users , server .getVersion ());
164+ moreData = moreData (users .getPaging (), users .getUsers ().size ());
165+ pageNumber ++;
166+ if (pageNumber > 50 ) { // there is a hard limit in Sonar API that doesn't allow querying more than 10K results
167+ log .info ("Reached max number of users that can be fetched. Skipping remaining ones..." );
168+ break ;
169+ }
170+ } else {
171+ log .error ("Aborting users fetch. Got an error from server: {}. Request: {}" , response .message (), r .url ().toString ());
172+ break ;
173+ }
136174 } catch (final Exception e ) {
137- log .error ("Quboo could not fetch data from the server: " + e .getMessage ());
175+ log .error ("Quboo could not fetch users from the server: " + e .getMessage ());
138176 break ;
139177 }
140178 }
141- log .info ("Sending " + wrapper .getUsers ().size () + " users to Quboo" );
142179 return wrapper ;
143180 }
144181
0 commit comments