22
33import com .fasterxml .jackson .databind .ObjectMapper ;
44import io .tpd .quboo .sonarplugin .QubooPlugin ;
5+ import io .tpd .quboo .sonarplugin .dtos .IssuesWrapper ;
6+ import io .tpd .quboo .sonarplugin .dtos .UsersWrapper ;
57import io .tpd .quboo .sonarplugin .pojos .Issues ;
8+ import io .tpd .quboo .sonarplugin .pojos .Paging ;
69import io .tpd .quboo .sonarplugin .pojos .Users ;
710import io .tpd .quboo .sonarplugin .settings .QubooProperties ;
811import okhttp3 .*;
1215import org .sonar .api .utils .log .Loggers ;
1316
1417/**
15- * Sends stats to the Quboo server
18+ * Sends stats to the Quboo server after an analysis meeting these conditions:
19+ * 1. On a 'master' branch
20+ * 2. No more than 5 times a day
21+ * 3. Issues created before today
1622 */
1723public class QubooConnector implements PostProjectAnalysisTask {
1824
@@ -33,16 +39,16 @@ public void finished(ProjectAnalysis analysis) {
3339 final String qubooSecret = analysis .getScannerContext ().getProperties ().get (QubooProperties .SECRET_KEY );
3440 log .info ("Connecting to Quboo with quboo key: " + qubooKey );
3541 try {
36- final Users allUsers = getUsers ();
42+ final UsersWrapper allUsers = getUsers ();
3743 sendUsersToQuboo (allUsers , qubooKey , qubooSecret );
38- final Issues allIssues = getIssues ();
44+ final IssuesWrapper allIssues = getIssues ();
3945 sendIssuesToQuboo (allIssues , qubooKey , qubooSecret );
4046 } catch (final Exception e ) {
4147 log .error ("Error while trying to connect to Quboo" , e );
4248 }
4349 }
4450
45- private void sendIssuesToQuboo (final Issues allIssues , final String qubooKey , final String qubooSecret ) throws Exception {
51+ private void sendIssuesToQuboo (final IssuesWrapper allIssues , final String qubooKey , final String qubooSecret ) throws Exception {
4652 final Request request = new Request .Builder ()
4753 .url (QubooPlugin .QUBOO_SERVER + "/updater/issues" )
4854 .header (QubooPlugin .QUBOO_HEADER_ACCESS_KEY , qubooKey )
@@ -54,16 +60,24 @@ private void sendIssuesToQuboo(final Issues allIssues, final String qubooKey, fi
5460 log .info ("Response " + body );
5561 }
5662
57- private Issues getIssues () throws Exception {
58- final Request request = new Request .Builder ().url (server .getPublicRootUrl () + "/api/issues/search" ).get ().build ();
59- final Response response = http .newCall (request ).execute ();
60- final String body = response .body ().string ();
61- final Issues issues = mapper .readValue (body , Issues .class );
62- log .info ("There are " + issues .getIssues ().size () + " issues" );
63- return issues ;
63+ private IssuesWrapper getIssues () throws Exception {
64+ int pageNumber = 1 ;
65+ boolean moreData = true ;
66+ IssuesWrapper wrapper = new IssuesWrapper ();
67+ while (moreData ) {
68+ final Request request = new Request .Builder ().url (server .getPublicRootUrl () + "/api/issues/search?ps=200&p=" + pageNumber ).get ().build ();
69+ final Response response = http .newCall (request ).execute ();
70+ final String body = response .body ().string ();
71+ final Issues issues = mapper .readValue (body , Issues .class );
72+ wrapper .filterAndAddIssues (issues );
73+ moreData = moreData (issues .getPaging (), issues .getIssues ().size ());
74+ pageNumber ++;
75+ }
76+ log .info ("There are " + wrapper .getIssues ().size () + " issues" );
77+ return wrapper ;
6478 }
6579
66- private void sendUsersToQuboo (final Users allUsers , final String qubooKey , final String qubooSecret ) throws Exception {
80+ private void sendUsersToQuboo (final UsersWrapper allUsers , final String qubooKey , final String qubooSecret ) throws Exception {
6781 final Request request = new Request .Builder ()
6882 .url (QubooPlugin .QUBOO_SERVER + "/updater/users" )
6983 .header (QubooPlugin .QUBOO_HEADER_ACCESS_KEY , qubooKey )
@@ -75,13 +89,30 @@ private void sendUsersToQuboo(final Users allUsers, final String qubooKey, final
7589 log .info ("Response " + body );
7690 }
7791
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 ;
92+ private UsersWrapper getUsers () {
93+ int pageNumber = 1 ;
94+ boolean moreData = true ;
95+ UsersWrapper wrapper = new UsersWrapper ();
96+ while (moreData ) {
97+ try {
98+ final Request request = new Request .Builder ().url (server .getPublicRootUrl () + "/api/users/search?ps=200&p=" + pageNumber ).get ().build ();
99+ final Response response = http .newCall (request ).execute ();
100+ final String body = response .body ().string ();
101+ final Users users = mapper .readValue (body , Users .class );
102+ wrapper .filterAndAddUsers (users );
103+ moreData = moreData (users .getPaging (), users .getUsers ().size ());
104+ pageNumber ++;
105+ } catch (final Exception e ) {
106+ log .error ("Quboo could not fetch data from the server: " + e .getMessage ());
107+ break ;
108+ }
109+ }
110+ log .info ("There are " + wrapper .getUsers ().size () + " users" );
111+ return wrapper ;
112+ }
113+
114+ private boolean moreData (final Paging paging , final int elementsInPage ) {
115+ return elementsInPage == paging .getPageSize () && paging .getTotal () > paging .getPageSize () * paging .getPageIndex ();
85116 }
86117
87118}
0 commit comments