]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9078 only include attr. "project" for certain types of suggestions
authorDaniel Schwarz <daniel.schwarz@sonarsource.com>
Thu, 11 May 2017 14:33:13 +0000 (16:33 +0200)
committerDaniel Schwarz <bartfastiel@users.noreply.github.com>
Fri, 12 May 2017 08:21:55 +0000 (10:21 +0200)
The attribute will be omitted for views, subviews and projects, but included for modules, files and test-files.

server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java
server/sonar-server/src/test/java/org/sonar/server/component/ws/SuggestionsActionTest.java

index 8b71294a8118160c4612169bc49ae7a17939e00e..cb9adf7fab61d9a7040aa8a8829b68119364b96b 100644 (file)
@@ -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();
   }
 
index ed48e8aae291c1b4c99ddd6b774a45c2525b85b2..7173b5c927d2a933f3be726d457f309f6a7755b3 100644 (file)
@@ -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;
@@ -518,6 +520,36 @@ public class SuggestionsActionTest {
       .containsExactlyInAnyOrder(tuple("VW", 0), tuple("SVW", 0), tuple("TRK", 1), tuple("BRC", 0), tuple("FIL", 0), tuple("UTS", 0));
   }
 
+  @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);