diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2018-02-20 13:28:34 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2018-02-22 09:23:27 +0100 |
commit | 47634942ff953226dcc9f678117ccf04a0e9c7c3 (patch) | |
tree | 57f5b4dc4a53851a9124f9ac57696ea3ce784083 | |
parent | 8a5b1bfefce83141ca4887466f02247d9e857164 (diff) | |
download | sonarqube-47634942ff953226dcc9f678117ccf04a0e9c7c3.tar.gz sonarqube-47634942ff953226dcc9f678117ccf04a0e9c7c3.zip |
SONAR-10356 clean message when applying template on >1000 projects
2 files changed, 36 insertions, 15 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/BulkApplyTemplateAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/BulkApplyTemplateAction.java index b5cdd5f896e..783e643654c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/BulkApplyTemplateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/BulkApplyTemplateAction.java @@ -25,10 +25,12 @@ import java.util.List; import org.sonar.api.i18n.I18n; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.ResourceTypes; +import org.sonar.api.server.ws.Change; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.server.ws.WebService.Param; +import org.sonar.db.DatabaseUtils; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; @@ -41,6 +43,7 @@ import org.sonar.server.project.Visibility; import org.sonar.server.user.UserSession; import org.sonarqube.ws.client.permission.BulkApplyTemplateWsRequest; +import static java.lang.String.format; import static org.sonar.api.utils.DateUtils.parseDateOrDateTime; import static org.sonar.core.util.Protobuf.setNullable; import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdmin; @@ -87,6 +90,7 @@ public class BulkApplyTemplateAction implements PermissionsWsAction { "Requires the following permission: 'Administer System'.") .setPost(true) .setSince("5.5") + .setChangelog(new Change("6.7.2", format("Parameter %s accepts maximum %d values", PARAM_PROJECTS, DatabaseUtils.PARTITION_SIZE_FOR_ORACLE))) .setHandler(this); action.createParam(Param.TEXT_QUERY) @@ -106,6 +110,9 @@ public class BulkApplyTemplateAction implements PermissionsWsAction { .createParam(PARAM_PROJECTS) .setDescription("Comma-separated list of project keys") .setSince("6.6") + // Limitation of ComponentDao#selectByQuery(), max 1000 values are accepted. + // Restricting size of HTTP parameter allows to not fail with SQL error + .setMaxValuesAllowed(DatabaseUtils.PARTITION_SIZE_FOR_ORACLE) .setExampleValue(String.join(",", KEY_PROJECT_EXAMPLE_001, KEY_PROJECT_EXAMPLE_002)); action.createParam(PARAM_VISIBILITY) diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/BulkApplyTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/BulkApplyTemplateActionTest.java index 0d04a677a5b..d48e48b9e5e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/BulkApplyTemplateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/BulkApplyTemplateActionTest.java @@ -19,7 +19,9 @@ */ package org.sonar.server.permission.ws.template; +import java.util.Collections; import java.util.List; +import org.apache.commons.lang.StringUtils; import org.junit.Before; import org.junit.Test; import org.sonar.api.resources.Qualifiers; @@ -102,7 +104,7 @@ public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyT } @Test - public void bulk_apply_template_by_template_uuid() throws Exception { + public void bulk_apply_template_by_template_uuid() { // this project should not be applied the template OrganizationDto otherOrganization = db.organizations().insert(); db.components().insertPrivateProject(otherOrganization); @@ -120,7 +122,7 @@ public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyT } @Test - public void request_throws_NotFoundException_if_template_with_specified_name_does_not_exist_in_specified_organization() throws Exception { + public void request_throws_NotFoundException_if_template_with_specified_name_does_not_exist_in_specified_organization() { OrganizationDto otherOrganization = db.organizations().insert(); loginAsAdmin(otherOrganization); @@ -135,7 +137,19 @@ public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyT } @Test - public void bulk_apply_template_by_template_name() throws Exception { + public void request_throws_IAE_if_more_than_1000_projects() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("'projects' can contains only 1000 values, got 1001"); + + newRequest() + .setParam(PARAM_ORGANIZATION, organization.getKey()) + .setParam(PARAM_TEMPLATE_NAME, template1.getName()) + .setParam(PARAM_PROJECTS, StringUtils.join(Collections.nCopies(1_001, "foo"), ",")) + .execute(); + } + + @Test + public void bulk_apply_template_by_template_name() { ComponentDto privateProject = db.components().insertPrivateProject(organization); ComponentDto publicProject = db.components().insertPublicProject(organization); loginAsAdmin(organization); @@ -150,7 +164,7 @@ public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyT } @Test - public void apply_template_by_qualifiers() throws Exception { + public void apply_template_by_qualifiers() { ComponentDto publicProject = db.components().insertPublicProject(organization); ComponentDto privateProject = db.components().insertPrivateProject(organization); ComponentDto view = db.components().insertComponent(newView(organization)); @@ -169,7 +183,7 @@ public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyT } @Test - public void apply_template_by_query_on_name_and_key_public_project() throws Exception { + public void apply_template_by_query_on_name_and_key_public_project() { ComponentDto publicProjectFoundByKey = ComponentTesting.newPublicProjectDto(organization).setDbKey("sonar"); db.components().insertProjectAndSnapshot(publicProjectFoundByKey); ComponentDto publicProjectFoundByName = ComponentTesting.newPublicProjectDto(organization).setName("name-sonar-name"); @@ -189,7 +203,7 @@ public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyT } @Test - public void apply_template_by_query_on_name_and_key() throws Exception { + public void apply_template_by_query_on_name_and_key() { // partial match on key ComponentDto privateProjectFoundByKey = ComponentTesting.newPrivateProjectDto(organization).setDbKey("sonarqube"); db.components().insertProjectAndSnapshot(privateProjectFoundByKey); @@ -210,7 +224,7 @@ public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyT } @Test - public void apply_template_by_project_keys() throws Exception { + public void apply_template_by_project_keys() { ComponentDto project1 = db.components().insertPrivateProject(organization); ComponentDto project2 = db.components().insertPrivateProject(organization); ComponentDto untouchedProject = db.components().insertPrivateProject(organization); @@ -227,7 +241,7 @@ public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyT } @Test - public void apply_template_by_provisioned_only() throws Exception { + public void apply_template_by_provisioned_only() { ComponentDto provisionedProject1 = db.components().insertPrivateProject(organization); ComponentDto provisionedProject2 = db.components().insertPrivateProject(organization); ComponentDto analyzedProject = db.components().insertPrivateProject(organization); @@ -245,7 +259,7 @@ public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyT } @Test - public void apply_template_by_analyzed_before() throws Exception { + public void apply_template_by_analyzed_before() { ComponentDto oldProject1 = db.components().insertPrivateProject(organization); ComponentDto oldProject2 = db.components().insertPrivateProject(organization); ComponentDto recentProject = db.components().insertPrivateProject(organization); @@ -265,7 +279,7 @@ public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyT } @Test - public void apply_template_by_visibility() throws Exception { + public void apply_template_by_visibility() { ComponentDto privateProject1 = db.components().insertPrivateProject(organization); ComponentDto privateProject2 = db.components().insertPrivateProject(organization); ComponentDto publicProject = db.components().insertPublicProject(organization); @@ -282,7 +296,7 @@ public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyT } @Test - public void fail_if_no_template_parameter() throws Exception { + public void fail_if_no_template_parameter() { loginAsAdmin(db.getDefaultOrganization()); expectedException.expect(BadRequestException.class); @@ -292,7 +306,7 @@ public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyT } @Test - public void fail_if_template_name_is_incorrect() throws Exception { + public void fail_if_template_name_is_incorrect() { loginAsAdmin(db.getDefaultOrganization()); expectedException.expect(NotFoundException.class); @@ -301,7 +315,7 @@ public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyT newRequest().setParam(PARAM_TEMPLATE_ID, "unknown-template-uuid").execute(); } - private void assertTemplate1AppliedToPublicProject(ComponentDto project) throws Exception { + private void assertTemplate1AppliedToPublicProject(ComponentDto project) { assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).containsExactly(group1.getName()); assertThat(selectProjectPermissionGroups(project, UserRole.USER)).isEmpty(); assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty(); @@ -309,7 +323,7 @@ public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyT assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).containsExactly(user2.getId()); } - private void assertTemplate1AppliedToPrivateProject(ComponentDto project) throws Exception { + private void assertTemplate1AppliedToPrivateProject(ComponentDto project) { assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).containsExactly(group1.getName()); assertThat(selectProjectPermissionGroups(project, UserRole.USER)).containsExactly(group2.getName()); assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty(); @@ -317,7 +331,7 @@ public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyT assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).containsExactly(user2.getId()); } - private void assertNoPermissionOnProject(ComponentDto project) throws Exception { + private void assertNoPermissionOnProject(ComponentDto project) { assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).isEmpty(); assertThat(selectProjectPermissionGroups(project, UserRole.CODEVIEWER)).isEmpty(); assertThat(selectProjectPermissionGroups(project, UserRole.ISSUE_ADMIN)).isEmpty(); |