import static org.sonar.server.ws.WsUtils.checkFound;
import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
+import static org.sonarqube.ws.client.component.ComponentsWsParameters.ACTION_SEARCH_PROJECTS;
import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_FILTER;
import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_ORGANIZATION;
import static org.sonarqube.ws.client.component.SearchProjectsRequest.DEFAULT_PAGE_SIZE;
@Override
public void define(WebService.NewController context) {
- WebService.NewAction action = context.createAction("search_projects")
+ WebService.NewAction action = context.createAction(ACTION_SEARCH_PROJECTS)
.setSince("6.2")
.setDescription("Search for projects")
.addPagingParams(DEFAULT_PAGE_SIZE, MAX_PAGE_SIZE)
.setPossibleValues(SUPPORTED_FACETS.stream().sorted().collect(MoreCollectors.toList(SUPPORTED_FACETS.size())));
action
.createParam(PARAM_FILTER)
+ .setMinimumLength(2)
.setDescription("Filter of projects on name, key, measure value, quality gate, language, tag or whether a project is a favorite or not.<br>" +
"The filter must be encoded to form a valid URL (for example '=' must be replaced by '%3D').<br>" +
"Examples of use:" +
import org.sonar.server.component.index.ComponentHit;
import org.sonar.server.component.index.ComponentHitsPerQualifier;
import org.sonar.server.component.index.ComponentIndex;
-import org.sonar.server.component.index.SuggestionQuery;
import org.sonar.server.component.index.ComponentIndexResults;
+import org.sonar.server.component.index.SuggestionQuery;
import org.sonar.server.es.DefaultIndexSettings;
import org.sonar.server.favorite.FavoriteFinder;
import org.sonar.server.user.UserSession;
action.createParam(PARAM_QUERY)
.setRequired(false)
- .setDescription("Search query with a minimum of two characters. Can contain several search tokens, separated by spaces. " +
- "Search tokens with only one character will be ignored.")
+ .setMinimumLength(2)
+ .setDescription("Search query: can contain several search tokens separated by spaces.")
.setExampleValue("sonar");
action.createParam(PARAM_MORE)
import org.sonarqube.ws.client.component.TreeWsRequest;
import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.collect.FluentIterable.from;
import static java.lang.String.CASE_INSENSITIVE_ORDER;
import static java.lang.String.format;
import static java.util.Collections.emptyMap;
import static org.sonar.api.utils.Paging.offset;
import static org.sonar.core.util.Uuids.UUID_EXAMPLE_02;
+import static org.sonar.core.util.stream.MoreCollectors.toList;
import static org.sonar.db.component.ComponentTreeQuery.Strategy.CHILDREN;
import static org.sonar.db.component.ComponentTreeQuery.Strategy.LEAVES;
import static org.sonar.server.component.ComponentFinder.ParamNames.COMPONENT_ID_AND_COMPONENT;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
import static org.sonar.server.ws.WsParameterBuilder.createQualifiersParameter;
import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext;
-import static org.sonar.server.ws.WsUtils.checkRequest;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
import static org.sonarqube.ws.client.component.ComponentsWsParameters.ACTION_TREE;
import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_COMPONENT;
.setDescription(format("Limit search to: <ul>" +
"<li>component names that contain the supplied string</li>" +
"<li>component keys that are exactly the same as the supplied string</li>" +
- "</ul>" +
- "Must have at least %d characters", QUERY_MINIMUM_LENGTH))
+ "</ul>"))
+ .setMinimumLength(QUERY_MINIMUM_LENGTH)
.setExampleValue("FILE_NAM");
createQualifiersParameter(action, newQualifierParameterContext(i18n, resourceTypes));
List<String> referenceComponentIds = components.stream()
.map(ComponentDto::getCopyResourceUuid)
.filter(Objects::nonNull)
- .collect(MoreCollectors.toList());
+ .collect(toList());
if (referenceComponentIds.isEmpty()) {
return emptyMap();
}
}
private static TreeWsRequest toTreeWsRequest(Request request) {
- TreeWsRequest treeWsRequest = new TreeWsRequest()
+ return new TreeWsRequest()
.setBaseComponentId(request.param(PARAM_COMPONENT_ID))
.setBaseComponentKey(request.param(PARAM_COMPONENT))
.setBranch(request.param(PARAM_BRANCH))
.setAsc(request.mandatoryParamAsBoolean(Param.ASCENDING))
.setPage(request.mandatoryParamAsInt(Param.PAGE))
.setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE));
- String searchQuery = treeWsRequest.getQuery();
- checkRequest(searchQuery == null || searchQuery.length() >= QUERY_MINIMUM_LENGTH,
- "The '%s' parameter must have at least %d characters", Param.TEXT_QUERY, QUERY_MINIMUM_LENGTH);
-
- return treeWsRequest;
}
private static List<ComponentDto> paginateComponents(List<ComponentDto> components, TreeWsRequest wsRequest) {
- return from(components)
- .skip(offset(wsRequest.getPage(), wsRequest.getPageSize()))
- .limit(wsRequest.getPageSize())
- .toList();
+ return components.stream().skip(offset(wsRequest.getPage(), wsRequest.getPageSize()))
+ .limit(wsRequest.getPageSize()).collect(toList());
}
public static List<ComponentDto> sortComponents(List<ComponentDto> components, TreeWsRequest wsRequest) {
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.tester.UserSessionRule;
@Test
public void fail_when_search_query_has_less_than_3_characters() {
- expectedException.expect(BadRequestException.class);
- expectedException.expectMessage("The 'q' parameter must have at least 3 characters");
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("'q' length (2) is shorter than the minimum authorized (3)");
componentDb.insertComponent(newPrivateProjectDto(db.organizations().insert(), "project-uuid"));
db.commit();