*/
package org.sonar.server.project.ws;
+import com.google.common.base.Strings;
import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.server.project.Visibility;
import org.sonar.server.user.UserSession;
+import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.Math.min;
+import static java.lang.String.format;
import static org.sonar.api.resources.Qualifiers.APP;
import static org.sonar.api.resources.Qualifiers.PROJECT;
import static org.sonar.api.resources.Qualifiers.VIEW;
"Requires 'Administer System' permission.")
.setSince("5.2")
.setHandler(this)
- .setChangelog(new Change("6.7.2", "Only the 1'000 first items in project filters are taken into account"));
+ .setChangelog(
+ new Change("7.8", format("parameters are optionals, but at least one is required among %s, %s and %s", PARAM_ANALYZED_BEFORE, PARAM_PROJECTS, Param.TEXT_QUERY)),
+ new Change("6.7.2", "Only the 1'000 first items in project filters are taken into account"));
support.addOrganizationParam(action);
public void handle(Request request, Response response) throws Exception {
SearchRequest searchRequest = toSearchWsRequest(request);
userSession.checkLoggedIn();
+
+
try (DbSession dbSession = dbClient.openSession(false)) {
OrganizationDto organization = support.getOrganization(dbSession, searchRequest.getOrganization());
userSession.checkPermission(OrganizationPermission.ADMINISTER, organization);
+ checkAtLeastOneParameterIsPresent(searchRequest);
ComponentQuery query = buildDbQuery(searchRequest);
List<ComponentDto> componentDtos = dbClient.componentDao().selectByQuery(dbSession, organization.getUuid(), query, 0, Integer.MAX_VALUE);
response.noContent();
}
+ private void checkAtLeastOneParameterIsPresent(SearchRequest searchRequest) {
+ boolean analyzedBeforePresent = !Strings.isNullOrEmpty(searchRequest.getAnalyzedBefore());
+ List<String> projects = searchRequest.getProjects();
+ boolean projectsPresent = projects != null && !projects.isEmpty();
+ List<String> projectIds = searchRequest.getProjectIds();
+ boolean projectIdsPresent = projectIds != null && !projectIds.isEmpty();
+ boolean queryPresent = !Strings.isNullOrEmpty(searchRequest.getQuery());
+ boolean atLeastOneParameterIsPresent = analyzedBeforePresent || projectsPresent || queryPresent || projectIdsPresent;
+
+ checkArgument(atLeastOneParameterIsPresent, format("At lease one parameter among %s, %s, %s, and %s must be provided", PARAM_ANALYZED_BEFORE, PARAM_PROJECTS, PARAM_PROJECT_IDS, Param.TEXT_QUERY));
+ }
+
private static SearchRequest toSearchWsRequest(Request request) {
return SearchRequest.builder()
.setOrganization(request.param(PARAM_ORGANIZATION))
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
+import java.util.stream.Stream;
import org.apache.commons.lang.StringUtils;
import org.junit.Before;
import org.junit.Rule;
verifyListenersOnProjectsDeleted(toDeleteInOrg1, toDeleteInOrg2);
}
+ @Test
+ public void throw_IllegalArgumentException_if_request_without_any_parameters(){
+ userSession.logIn().setRoot();
+ db.components().insertPrivateProject(org1);
+
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("At lease one parameter among analyzedBefore, projects, projectIds, and q must be provided");
+
+ try {
+ ws.newRequest().execute();
+ } finally {
+ verifyNoDeletions();
+ verifyZeroInteractions(projectLifeCycleListeners);
+ }
+ }
+
@Test
public void projects_that_dont_exist_are_ignored_and_dont_break_bulk_deletion() {
userSession.logIn().setRoot();
ComponentDto analyzedProject = db.components().insertPrivateProject();
db.components().insertSnapshot(newAnalysis(analyzedProject));
- ws.newRequest().setParam(PARAM_ON_PROVISIONED_ONLY, "true").execute();
+ ws.newRequest().setParam(PARAM_PROJECTS, provisionedProject.getKey() + "," + analyzedProject.getKey()).setParam(PARAM_ON_PROVISIONED_ONLY, "true").execute();
verifyDeleted(provisionedProject);
verifyListenersOnProjectsDeleted(provisionedProject);
userSession.logIn().addPermission(ADMINISTER, db.getDefaultOrganization());
ComponentDto[] projects = IntStream.range(0, 55).mapToObj(i -> db.components().insertPrivateProject()).toArray(ComponentDto[]::new);
- ws.newRequest().execute();
+ List<String> projectKeys = Stream.of(projects).map(ComponentDto::getKey).collect(Collectors.toList());
+ ws.newRequest().setParam(PARAM_PROJECTS, String.join(",", projectKeys)).execute();
verifyDeleted(projects);
verifyListenersOnProjectsDeleted(projects);
ComponentDto project = db.components().insertPrivateProject();
ComponentDto view = db.components().insertView();
- ws.newRequest().setParam(PARAM_QUALIFIERS, String.join(",", Qualifiers.PROJECT, Qualifiers.VIEW)).execute();
+ ws.newRequest()
+ .setParam(PARAM_PROJECTS, project.getKey() + "," + view.getKey())
+ .setParam(PARAM_QUALIFIERS, String.join(",", Qualifiers.PROJECT, Qualifiers.VIEW))
+ .execute();
verifyDeleted(project, view);
verifyListenersOnProjectsDeleted(project, view);