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;
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;
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;
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()
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();
}
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;
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;
.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);