aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorDaniel Schwarz <daniel.schwarz@sonarsource.com>2017-05-11 16:33:13 +0200
committerDaniel Schwarz <bartfastiel@users.noreply.github.com>2017-05-12 10:21:55 +0200
commitf0bf6ee66ab748216508fc3bd35c0ba2fb54d41f (patch)
tree53036d3c85dda445b9e957eaaa3d1da4aecb26db /server
parent2ab2fc924295122ed0b334ac481b9d779922cbf0 (diff)
downloadsonarqube-f0bf6ee66ab748216508fc3bd35c0ba2fb54d41f.tar.gz
sonarqube-f0bf6ee66ab748216508fc3bd35c0ba2fb54d41f.zip
SONAR-9078 only include attr. "project" for certain types of suggestions
The attribute will be omitted for views, subviews and projects, but included for modules, files and test-files.
Diffstat (limited to 'server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java16
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/component/ws/SuggestionsActionTest.java32
2 files changed, 42 insertions, 6 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java
index 8b71294a811..cb9adf7fab6 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java
@@ -30,7 +30,9 @@ import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
+import java.util.stream.Stream;
import javax.annotation.Nullable;
+import org.sonar.api.resources.Qualifiers;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
@@ -58,7 +60,6 @@ import static com.google.common.base.Preconditions.checkState;
import static java.util.Arrays.stream;
import static java.util.Collections.emptySet;
import static java.util.Collections.singletonList;
-import static java.util.Optional.ofNullable;
import static org.sonar.api.web.UserRole.USER;
import static org.sonar.core.util.stream.MoreCollectors.toList;
import static org.sonar.core.util.stream.MoreCollectors.toSet;
@@ -78,6 +79,7 @@ public class SuggestionsAction implements ComponentsWsAction {
private static final int MAXIMUM_RECENTLY_BROWSED = 50;
private static final int EXTENDED_LIMIT = 20;
+ private static final Set<String> QUALIFIERS_FOR_WHICH_TO_RETURN_PROJECT = Stream.of(Qualifiers.MODULE, Qualifiers.FILE, Qualifiers.UNIT_TEST_FILE).collect(Collectors.toSet());
private final ComponentIndex index;
private final FavoriteFinder favoriteFinder;
@@ -254,7 +256,7 @@ public class SuggestionsAction implements ComponentsWsAction {
private Map<String, ComponentDto> loadProjects(DbSession dbSession, Collection<ComponentDto> components) {
Set<String> projectUuids = components.stream()
- .filter(c -> !c.projectUuid().equals(c.uuid()))
+ .filter(c -> QUALIFIERS_FOR_WHICH_TO_RETURN_PROJECT.contains(c.qualifier()))
.map(ComponentDto::projectUuid)
.collect(MoreCollectors.toSet());
return dbClient.componentDao().selectByUuids(dbSession, projectUuids).stream()
@@ -305,15 +307,17 @@ public class SuggestionsAction implements ComponentsWsAction {
ComponentDto result = componentsByUuids.get(hit.getUuid());
String organizationKey = organizationByUuids.get(result.getOrganizationUuid()).getKey();
checkState(organizationKey != null, "Organization with uuid '%s' not found", result.getOrganizationUuid());
- String projectKey = ofNullable(result.projectUuid()).map(projectsByUuids::get).map(ComponentDto::getKey).orElse("");
- return Suggestion.newBuilder()
+ Suggestion.Builder builder = Suggestion.newBuilder()
.setOrganization(organizationKey)
- .setProject(projectKey)
.setKey(result.getKey())
.setName(result.name())
.setMatch(hit.getHighlightedText().orElse(HtmlEscapers.htmlEscaper().escape(result.name())))
.setIsRecentlyBrowsed(recentlyBrowsedKeys.contains(result.getKey()))
- .setIsFavorite(favoriteUuids.contains(result.uuid()))
+ .setIsFavorite(favoriteUuids.contains(result.uuid()));
+ if (QUALIFIERS_FOR_WHICH_TO_RETURN_PROJECT.contains(result.qualifier())) {
+ builder.setProject(projectsByUuids.get(result.projectUuid()).getKey());
+ }
+ return builder
.build();
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SuggestionsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SuggestionsActionTest.java
index ed48e8aae29..7173b5c927d 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SuggestionsActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SuggestionsActionTest.java
@@ -34,6 +34,7 @@ import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.System2;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ComponentTesting;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.component.index.ComponentIndex;
import org.sonar.server.component.index.ComponentIndexDefinition;
@@ -59,6 +60,7 @@ import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.joining;
import static java.util.stream.IntStream.range;
import static java.util.stream.Stream.of;
+import static org.apache.commons.lang.RandomStringUtils.random;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.groups.Tuple.tuple;
import static org.mockito.Mockito.doReturn;
@@ -519,6 +521,36 @@ public class SuggestionsActionTest {
}
@Test
+ public void should_only_provide_project_for_certain_qualifiers() throws Exception {
+ String query = random(10);
+
+ ComponentDto view = db.components().insertView(organization, v -> v.setName(query));
+ ComponentDto subView = db.components().insertComponent(ComponentTesting.newSubView(view).setName(query));
+ ComponentDto project = db.components().insertPrivateProject(organization, p -> p.setName(query));
+ ComponentDto module = db.components().insertComponent(ComponentTesting.newModuleDto(project).setName(query));
+ ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(module).setName(query));
+ ComponentDto test = db.components().insertComponent(ComponentTesting.newFileDto(module).setName(query).setQualifier(Qualifiers.UNIT_TEST_FILE));
+ componentIndexer.indexOnStartup(null);
+ authorizationIndexerTester.allowOnlyAnyone(project);
+ authorizationIndexerTester.allowOnlyAnyone(view);
+
+ SuggestionsWsResponse response = ws.newRequest()
+ .setMethod("POST")
+ .setParam(PARAM_QUERY, project.name())
+ .executeProtobuf(SuggestionsWsResponse.class);
+
+ assertThat(response.getResultsList())
+ .extracting(Category::getQ, c -> c.getItemsList().stream().map(Suggestion::hasProject).findFirst().orElse(null))
+ .containsExactlyInAnyOrder(
+ tuple(SuggestionCategory.VIEW.getName(), false),
+ tuple(SuggestionCategory.SUBVIEW.getName(), false),
+ tuple(SuggestionCategory.PROJECT.getName(), false),
+ tuple(SuggestionCategory.MODULE.getName(), true),
+ tuple(SuggestionCategory.FILE.getName(), true),
+ tuple(SuggestionCategory.UNIT_TEST_FILE.getName(), true));
+ }
+
+ @Test
public void should_not_propose_to_show_more_results_if_0_projects_are_found() {
check_proposal_to_show_more_results(0, 0, 0L, null, true);
}