22
33import com .fasterxml .jackson .databind .ObjectMapper ;
44import io .tpd .quboo .sonarplugin .QubooPlugin ;
5- import io .tpd .quboo .sonarplugin .pojos .Issue ;
65import io .tpd .quboo .sonarplugin .pojos .Issues ;
6+ import io .tpd .quboo .sonarplugin .pojos .Users ;
77import io .tpd .quboo .sonarplugin .settings .QubooProperties ;
88import okhttp3 .*;
99import org .sonar .api .ce .posttask .PostProjectAnalysisTask ;
1010import org .sonar .api .platform .Server ;
1111import org .sonar .api .utils .log .Logger ;
1212import org .sonar .api .utils .log .Loggers ;
1313
14- import java .util .List ;
15-
1614/**
1715 * Sends stats to the Quboo server
1816 */
@@ -21,8 +19,10 @@ public class QubooConnector implements PostProjectAnalysisTask {
2119 private final Server server ;
2220 private final Logger log = Loggers .get (getClass ());
2321 private final ObjectMapper mapper ;
22+ private final OkHttpClient http ;
2423
2524 public QubooConnector (final Server server ) {
25+ this .http = new OkHttpClient ();
2626 this .server = server ;
2727 this .mapper = new ObjectMapper ();
2828 }
@@ -33,15 +33,16 @@ public void finished(ProjectAnalysis analysis) {
3333 final String qubooSecret = analysis .getScannerContext ().getProperties ().get (QubooProperties .SECRET_KEY );
3434 log .info ("Connecting to Quboo with quboo key: " + qubooKey );
3535 try {
36- final List <Issue > allIssues = getIssues ();
36+ final Users allUsers = getUsers ();
37+ sendUsersToQuboo (allUsers , qubooKey , qubooSecret );
38+ final Issues allIssues = getIssues ();
3739 sendIssuesToQuboo (allIssues , qubooKey , qubooSecret );
3840 } catch (final Exception e ) {
3941 log .error ("Error while trying to connect to Quboo" , e );
4042 }
4143 }
4244
43- private void sendIssuesToQuboo (final List <Issue > allIssues , final String qubooKey , final String qubooSecret ) throws Exception {
44- final OkHttpClient http = new OkHttpClient ();
45+ private void sendIssuesToQuboo (final Issues allIssues , final String qubooKey , final String qubooSecret ) throws Exception {
4546 final Request request = new Request .Builder ()
4647 .url (QubooPlugin .QUBOO_SERVER + "/updater/issues" )
4748 .header (QubooPlugin .QUBOO_HEADER_ACCESS_KEY , qubooKey )
@@ -53,13 +54,34 @@ private void sendIssuesToQuboo(final List<Issue> allIssues, final String qubooKe
5354 log .info ("Response " + body );
5455 }
5556
56- private List <Issue > getIssues () throws Exception {
57- final OkHttpClient http = new OkHttpClient ();
57+ private Issues getIssues () throws Exception {
5858 final Request request = new Request .Builder ().url (server .getPublicRootUrl () + "/api/issues/search" ).get ().build ();
5959 final Response response = http .newCall (request ).execute ();
6060 final String body = response .body ().string ();
6161 final Issues issues = mapper .readValue (body , Issues .class );
6262 log .info ("There are " + issues .getIssues ().size () + " issues" );
63- return issues . getIssues () ;
63+ return issues ;
6464 }
65+
66+ private void sendUsersToQuboo (final Users allUsers , final String qubooKey , final String qubooSecret ) throws Exception {
67+ final Request request = new Request .Builder ()
68+ .url (QubooPlugin .QUBOO_SERVER + "/updater/users" )
69+ .header (QubooPlugin .QUBOO_HEADER_ACCESS_KEY , qubooKey )
70+ .header (QubooPlugin .QUBOO_HEADER_SECRET_KEY , qubooSecret )
71+ .post (RequestBody .create (MediaType .get ("application/json" ), mapper .writeValueAsString (allUsers )))
72+ .build ();
73+ final Response response = http .newCall (request ).execute ();
74+ final String body = response .body ().string ();
75+ log .info ("Response " + body );
76+ }
77+
78+ private Users getUsers () throws Exception {
79+ final Request request = new Request .Builder ().url (server .getPublicRootUrl () + "/api/users/search" ).get ().build ();
80+ final Response response = http .newCall (request ).execute ();
81+ final String body = response .body ().string ();
82+ final Users users = mapper .readValue (body , Users .class );
83+ log .info ("There are " + users .getUsers ().size () + " users" );
84+ return users ;
85+ }
86+
6587}
0 commit comments