*/
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;
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 {
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")
"<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);
}
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();
+ }
+
}
.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))
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;
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> {
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);
}
@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);
}
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;
}
@CheckForNull
- protected static String inlineMultipleParamValue(@Nullable List<String> values) {
+ protected static String inlineMultipleParamValue(@Nullable Collection<String> values) {
return values == null ? null : String.join(",", values);
}
}
*/
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() {
}
@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;
}
}
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 {
.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) {
*/
package org.sonarqube.ws.client.permission;
+import java.util.Arrays;
import org.junit.Rule;
import org.junit.Test;
import org.sonarqube.ws.WsPermissions;
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";
.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();
.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();
}