]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9181 WS bulk_apply_template accepts several qualifiers
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 8 Sep 2017 08:09:50 +0000 (10:09 +0200)
committerStas Vilchik <stas.vilchik@sonarsource.com>
Mon, 11 Sep 2017 09:28:29 +0000 (11:28 +0200)
server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/BulkApplyTemplateAction.java
server/sonar-server/src/main/java/org/sonar/server/ws/WsParameterBuilder.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/BulkApplyTemplateActionTest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/BaseService.java
sonar-ws/src/main/java/org/sonarqube/ws/client/permission/BulkApplyTemplateWsRequest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsService.java
sonar-ws/src/test/java/org/sonarqube/ws/client/permission/PermissionsServiceTest.java

index e56b103db7bfaf46df96fd2fbc7d2c016e4c4047..5d17a3eb34a10530791abb57607786a3397d93d8 100644 (file)
  */
 package org.sonar.server.permission.ws.template;
 
-import com.google.common.collect.Collections2;
 import java.util.List;
-import javax.annotation.Nullable;
 import org.sonar.api.i18n.I18n;
-import org.sonar.api.resources.ResourceType;
+import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.resources.ResourceTypes;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
@@ -40,15 +38,17 @@ import org.sonar.server.permission.ws.PermissionsWsAction;
 import org.sonar.server.user.UserSession;
 import org.sonarqube.ws.client.permission.BulkApplyTemplateWsRequest;
 
+import static org.sonar.core.util.Protobuf.setNullable;
 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdmin;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
 import static org.sonar.server.permission.ws.template.WsTemplateRef.newTemplateRef;
-import static org.sonar.server.ws.WsParameterBuilder.createRootQualifierParameter;
+import static org.sonar.server.ws.WsParameterBuilder.createRootQualifiersParameter;
 import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext;
 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION;
 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
+import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_QUALIFIERS;
 
 public class BulkApplyTemplateAction implements PermissionsWsAction {
 
@@ -69,15 +69,6 @@ public class BulkApplyTemplateAction implements PermissionsWsAction {
     this.resourceTypes = resourceTypes;
   }
 
-  private static BulkApplyTemplateWsRequest toBulkApplyTemplateWsRequest(Request request) {
-    return new BulkApplyTemplateWsRequest()
-      .setTemplateId(request.param(PARAM_TEMPLATE_ID))
-      .setOrganization(request.param(PARAM_ORGANIZATION))
-      .setTemplateName(request.param(PARAM_TEMPLATE_NAME))
-      .setQualifier(request.param(PARAM_QUALIFIER))
-      .setQuery(request.param(Param.TEXT_QUERY));
-  }
-
   @Override
   public void define(WebService.NewController context) {
     WebService.NewAction action = context.createAction("bulk_apply_template")
@@ -94,7 +85,11 @@ public class BulkApplyTemplateAction implements PermissionsWsAction {
         "<li>project keys that are exactly the same as the supplied string</li>" +
         "</ul>")
       .setExampleValue("apac");
-    createRootQualifierParameter(action, newQualifierParameterContext(i18n, resourceTypes));
+
+    createRootQualifiersParameter(action, newQualifierParameterContext(i18n, resourceTypes))
+      .setDefaultValue(Qualifiers.PROJECT)
+      .setDeprecatedKey(PARAM_QUALIFIER, "6.6");
+
     createTemplateParameters(action);
   }
 
@@ -110,19 +105,28 @@ public class BulkApplyTemplateAction implements PermissionsWsAction {
         request.getTemplateId(), request.getOrganization(), request.getTemplateName()));
       checkGlobalAdmin(userSession, template.getOrganizationUuid());
 
-      ComponentQuery componentQuery = ComponentQuery.builder()
-        .setNameOrKeyQuery(request.getQuery())
-        .setQualifiers(qualifiers(request.getQualifier()))
-        .build();
+      ComponentQuery componentQuery = buildDbQuery(request);
       List<ComponentDto> projects = dbClient.componentDao().selectByQuery(dbSession, template.getOrganizationUuid(), componentQuery, 0, Integer.MAX_VALUE);
 
       permissionTemplateService.applyAndCommit(dbSession, template, projects);
     }
   }
 
-  private String[] qualifiers(@Nullable String qualifier) {
-    return qualifier == null
-      ? Collections2.transform(resourceTypes.getRoots(), ResourceType::getQualifier).toArray(new String[resourceTypes.getRoots().size()])
-      : (new String[] {qualifier});
+  private static BulkApplyTemplateWsRequest toBulkApplyTemplateWsRequest(Request request) {
+    return new BulkApplyTemplateWsRequest()
+      .setOrganization(request.param(PARAM_ORGANIZATION))
+      .setTemplateId(request.param(PARAM_TEMPLATE_ID))
+      .setTemplateName(request.param(PARAM_TEMPLATE_NAME))
+      .setQualifiers(request.mandatoryParamAsStrings(PARAM_QUALIFIERS))
+      .setQuery(request.param(Param.TEXT_QUERY));
   }
+
+  private static ComponentQuery buildDbQuery(BulkApplyTemplateWsRequest request) {
+    ComponentQuery.Builder dbQuery = ComponentQuery.builder()
+      .setNameOrKeyQuery(request.getQuery());
+    setNullable(request.getQualifiers(), l -> dbQuery.setQualifiers(l.toArray(new String[0])));
+
+    return dbQuery.build();
+  }
+
 }
index d1cae3abacf37e717657f525e78fbba3d13a38ab..ce05d9354aa399afcb3ccf4a0c00d95d622e4cbb 100644 (file)
@@ -45,6 +45,13 @@ public class WsParameterBuilder {
       .setPossibleValues(getRootQualifiers(context.getResourceTypes()));
   }
 
+  public static WebService.NewParam createRootQualifiersParameter(WebService.NewAction action, QualifierParameterContext context) {
+    return action.createParam(PARAM_QUALIFIERS)
+      .setDescription("Comma-separated list of component qualifiers. Filter the results with the specified qualifiers. " +
+        "Possible values are:" + buildRootQualifiersDescription(context))
+      .setPossibleValues(getRootQualifiers(context.getResourceTypes()));
+  }
+
   public static WebService.NewParam createDefaultTemplateQualifierParameter(WebService.NewAction action, QualifierParameterContext context) {
     return action.createParam(PARAM_QUALIFIER)
       .setDescription("Project qualifier. Filter the results with the specified qualifier. Possible values are:" + buildDefaultTemplateQualifiersDescription(context))
index 18a6b2423c747b4bf224cc6131d26dfb3ce748cf..ac6fffb08d55d6f6ca234b5e1c70be509668e84f 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.server.permission.ws.template;
 import java.util.List;
 import org.junit.Before;
 import org.junit.Test;
+import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.api.web.UserRole;
 import org.sonar.db.component.ComponentDto;
@@ -40,11 +41,12 @@ import org.sonar.server.permission.PermissionTemplateService;
 import org.sonar.server.permission.ws.BasePermissionWsTest;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.component.ComponentTesting.newApplication;
 import static org.sonar.db.component.ComponentTesting.newView;
 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION;
-import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
+import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_QUALIFIERS;
 
 public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyTemplateAction> {
 
@@ -104,7 +106,10 @@ public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyT
     ComponentDto view = db.components().insertView(organization);
     loginAsAdmin(organization);
 
-    newRequest().setParam(PARAM_TEMPLATE_ID, template1.getUuid()).execute();
+    newRequest()
+      .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
+      .setParam(PARAM_QUALIFIERS, String.join(",", Qualifiers.PROJECT, Qualifiers.VIEW))
+      .execute();
 
     assertTemplate1AppliedToPrivateProject(privateProject);
     assertTemplate1AppliedToPublicProject(publicProject);
@@ -142,18 +147,21 @@ public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyT
   }
 
   @Test
-  public void apply_template_by_qualifier() throws Exception {
+  public void apply_template_by_qualifiers() throws Exception {
     ComponentDto publicProject = db.components().insertPublicProject(organization);
     ComponentDto privateProject = db.components().insertPrivateProject(organization);
     ComponentDto view = db.components().insertComponent(newView(organization));
+    ComponentDto application = db.components().insertComponent(newApplication(organization));
     loginAsAdmin(organization);
 
     newRequest()
       .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
-      .setParam(PARAM_QUALIFIER, privateProject.qualifier()).execute();
+      .setParam(PARAM_QUALIFIERS, String.join(",", Qualifiers.PROJECT, Qualifiers.APP))
+      .execute();
 
     assertTemplate1AppliedToPrivateProject(privateProject);
     assertTemplate1AppliedToPublicProject(publicProject);
+    assertTemplate1AppliedToPublicProject(application);
     assertNoPermissionOnProject(view);
   }
 
index 9d5f9e9ce398396d9c0517fee1e1a75725079a33..4c6a0e9449e2a6cd4f069ce783a80d394c3dad4b 100644 (file)
@@ -22,7 +22,7 @@ package org.sonarqube.ws.client;
 import com.google.protobuf.Message;
 import com.google.protobuf.Parser;
 import java.io.InputStream;
-import java.util.List;
+import java.util.Collection;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import org.apache.commons.io.IOUtils;
@@ -67,7 +67,7 @@ public abstract class BaseService {
   }
 
   @CheckForNull
-  protected static String inlineMultipleParamValue(@Nullable List<String> values) {
+  protected static String inlineMultipleParamValue(@Nullable Collection<String> values) {
     return values == null ? null : String.join(",", values);
   }
 }
index 5fdc4d31623301d870510a4b0b46cc7940f0dc88..77d7eff64b223376b1027256c22301c08ce113ce 100644 (file)
  */
 package org.sonarqube.ws.client.permission;
 
+import java.util.Collection;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 
+import static java.util.Objects.requireNonNull;
+
 public class BulkApplyTemplateWsRequest {
   private String templateId;
   private String organization;
   private String templateName;
   private String query;
-  private String qualifier;
+  private Collection<String> qualifiers;
 
   @CheckForNull
   public String getTemplateId() {
@@ -70,12 +73,12 @@ public class BulkApplyTemplateWsRequest {
   }
 
   @CheckForNull
-  public String getQualifier() {
-    return qualifier;
+  public Collection<String> getQualifiers() {
+    return qualifiers;
   }
 
-  public BulkApplyTemplateWsRequest setQualifier(@Nullable String qualifier) {
-    this.qualifier = qualifier;
+  public BulkApplyTemplateWsRequest setQualifiers(Collection<String> qualifiers) {
+    this.qualifiers = requireNonNull(qualifiers);
     return this;
   }
 }
index 81cccb39e9fd1a6afa2b4830d947255d53b57f44..97802e65da74b2601b5570f8068827914ac77126 100644 (file)
@@ -45,6 +45,7 @@ import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_Q
 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN;
+import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_QUALIFIERS;
 
 public class PermissionsService extends BaseService {
 
@@ -123,7 +124,7 @@ public class PermissionsService extends BaseService {
       .setParam(PARAM_TEMPLATE_ID, request.getTemplateId())
       .setParam(PARAM_TEMPLATE_NAME, request.getTemplateName())
       .setParam("q", request.getQuery())
-      .setParam(PARAM_QUALIFIER, request.getQualifier()));
+      .setParam(PARAM_QUALIFIERS, inlineMultipleParamValue(request.getQualifiers())));
   }
 
   public CreateTemplateWsResponse createTemplate(CreateTemplateWsRequest request) {
index fd4dbf4ac27c4979ea4bf8289f44aefa868cf837..b83868acfa56727dfb0ad06ad5036c8f870ad055 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonarqube.ws.client.permission;
 
+import java.util.Arrays;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonarqube.ws.WsPermissions;
@@ -43,6 +44,7 @@ import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_Q
 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN;
+import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_QUALIFIERS;
 
 public class PermissionsServiceTest {
   private static final String ORGANIZATION_VALUE = "organization value";
@@ -217,7 +219,7 @@ public class PermissionsServiceTest {
       .setOrganization(ORGANIZATION_VALUE)
       .setTemplateId(TEMPLATE_ID_VALUE)
       .setTemplateName(TEMPLATE_NAME_VALUE)
-      .setQualifier(QUALIFIER_VALUE)
+      .setQualifiers(Arrays.asList("TRK", "VW"))
       .setQuery(QUERY_VALUE));
 
     assertThat(serviceTester.getPostParser()).isNull();
@@ -228,7 +230,7 @@ public class PermissionsServiceTest {
       .hasParam(PARAM_TEMPLATE_ID, TEMPLATE_ID_VALUE)
       .hasParam(PARAM_TEMPLATE_NAME, TEMPLATE_NAME_VALUE)
       .hasParam("q", QUERY_VALUE)
-      .hasParam(PARAM_QUALIFIER, QUALIFIER_VALUE)
+      .hasParam(PARAM_QUALIFIERS, "TRK,VW")
       .andNoOtherParam();
   }