]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9000 Limit number of results for api/organizations/search to 500
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 23 Nov 2017 13:42:45 +0000 (14:42 +0100)
committerTeryk Bellahsene <teryk@users.noreply.github.com>
Wed, 29 Nov 2017 16:27:53 +0000 (17:27 +0100)
server/sonar-server/src/main/java/org/sonar/server/organization/ws/SearchAction.java
server/sonar-server/src/test/java/org/sonar/server/organization/ws/SearchActionTest.java

index cdafe498a10368da4337f4c3aecabca81b8e9dc0..957bf0d71113c08d7890af79503ec572e40503e5 100644 (file)
@@ -34,12 +34,15 @@ import org.sonarqube.ws.Organizations.Organization;
 
 import static org.sonar.db.Pagination.forPage;
 import static org.sonar.db.organization.OrganizationQuery.newOrganizationQueryBuilder;
+import static org.sonar.server.ws.WsUtils.checkRequest;
 import static org.sonar.server.ws.WsUtils.writeProtobuf;
 import static org.sonarqube.ws.Common.Paging;
 
 public class SearchAction implements OrganizationsWsAction {
   private static final String PARAM_ORGANIZATIONS = "organizations";
+  private static final String PARAM_MEMBER = "member";
   private static final String ACTION = "search";
+  private static final int MAX_SIZE = 500;
 
   private final DbClient dbClient;
   private final OrganizationsWsSupport wsSupport;
@@ -67,14 +70,15 @@ public class SearchAction implements OrganizationsWsAction {
       .setRequired(false)
       .setSince("6.3");
 
-    action.addPagingParams(100);
+    action.addPagingParams(100, MAX_SIZE);
   }
 
   @Override
   public void handle(Request request, Response response) throws Exception {
     try (DbSession dbSession = dbClient.openSession(false)) {
+      List<String> organizations = getOrganizationKeys(request);
       OrganizationQuery organizationQuery = newOrganizationQueryBuilder()
-        .setKeys(request.paramAsStrings(PARAM_ORGANIZATIONS))
+        .setKeys(organizations)
         .build();
 
       int total = dbClient.organizationDao().countByQuery(dbSession, organizationQuery);
@@ -103,4 +107,12 @@ public class SearchAction implements OrganizationsWsAction {
       .build();
   }
 
+  private static List<String> getOrganizationKeys(Request request) {
+    List<String> organizations = request.paramAsStrings(PARAM_ORGANIZATIONS);
+    if (organizations != null) {
+      checkRequest(organizations.size() <= MAX_SIZE, "Size of '%s' (%d) must be less than %d", PARAM_ORGANIZATIONS, organizations.size(), MAX_SIZE);
+    }
+    return organizations;
+  }
+
 }
index 1ccaad72e789ada89d4be2259a138e637236fe3f..62b0a59d998cdb02ac3f83a4266de278c871731b 100644 (file)
@@ -83,20 +83,23 @@ public class SearchActionTest {
     assertThat(action.params()).hasSize(3);
     assertThat(action.responseExample()).isEqualTo(getClass().getResource("search-example.json"));
 
-    WebService.Param organizationsParam = action.param("organizations");
-    assertThat(organizationsParam.isRequired()).isFalse();
-    assertThat(organizationsParam.defaultValue()).isNull();
-    assertThat(organizationsParam.description()).isEqualTo("Comma-separated list of organization keys");
-    assertThat(organizationsParam.exampleValue()).isEqualTo("my-org-1,foocorp");
-    assertThat(organizationsParam.since()).isEqualTo("6.3");
-    WebService.Param pParam = action.param("p");
-    assertThat(pParam.isRequired()).isFalse();
-    assertThat(pParam.defaultValue()).isEqualTo("1");
-    assertThat(pParam.description()).isEqualTo("1-based page number");
-    WebService.Param psParam = action.param("ps");
-    assertThat(psParam.isRequired()).isFalse();
-    assertThat(psParam.defaultValue()).isEqualTo("100");
-    assertThat(psParam.description()).isEqualTo("Page size. Must be greater than 0.");
+    WebService.Param organizations = action.param("organizations");
+    assertThat(organizations.isRequired()).isFalse();
+    assertThat(organizations.defaultValue()).isNull();
+    assertThat(organizations.description()).isEqualTo("Comma-separated list of organization keys");
+    assertThat(organizations.exampleValue()).isEqualTo("my-org-1,foocorp");
+    assertThat(organizations.since()).isEqualTo("6.3");
+
+    WebService.Param page = action.param("p");
+    assertThat(page.isRequired()).isFalse();
+    assertThat(page.defaultValue()).isEqualTo("1");
+    assertThat(page.description()).isEqualTo("1-based page number");
+
+    WebService.Param pageSize = action.param("ps");
+    assertThat(pageSize.isRequired()).isFalse();
+    assertThat(pageSize.defaultValue()).isEqualTo("100");
+    assertThat(pageSize.maximumValue()).isEqualTo(500);
+    assertThat(pageSize.description()).isEqualTo("Page size. Must be greater than 0 and less than 500");
   }
 
   @Test