From: Daniel Schwarz Date: Mon, 3 Jul 2017 09:43:09 +0000 (+0200) Subject: SONAR-9432 let rule search use OR instead of AND when filtering by tags X-Git-Tag: 6.5-M2~27 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=72f20e1066197e64b7e5d348d9f1bc2466073f2e;p=sonarqube.git SONAR-9432 let rule search use OR instead of AND when filtering by tags --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java index 8638e255fa8..7a623e10dd8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java @@ -256,7 +256,7 @@ public class RuleIndex { .filter(QueryBuilders.termQuery(FIELD_RULE_EXTENSION_TAGS, tag)) .filter(termsQuery(FIELD_RULE_EXTENSION_SCOPE, RuleExtensionScope.system().getScope(), RuleExtensionScope.organization(query.getOrganization()).getScope()))) .map(childQuery -> QueryBuilders.hasChildQuery(INDEX_TYPE_RULE_EXTENSION.getType(), childQuery)) - .forEach(q::filter); + .forEach(q::should); filters.put(FIELD_RULE_EXTENSION_TAGS, q); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java index 8b2c610a6dd..1d5507795ea 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java @@ -222,6 +222,26 @@ public class SearchActionTest { verify(request, rule1); } + @Test + public void when_searching_for_several_tags_combine_them_with_OR() throws IOException { + OrganizationDto organization = dbTester.organizations().insert(); + RuleDefinitionDto bothTagsRule = createJavaRule(); + insertMetadata(organization, bothTagsRule, setTags("tag1", "tag2")); + RuleDefinitionDto oneTagRule = createJavaRule(); + insertMetadata(organization, oneTagRule, setTags("tag1")); + RuleDefinitionDto otherTagRule = createJavaRule(); + insertMetadata(organization, otherTagRule, setTags("tag2")); + RuleDefinitionDto noTagRule = createJavaRule(); + insertMetadata(organization, noTagRule, setTags()); + indexRules(); + + Consumer request = r -> r + .setParam("f", "repo,name") + .setParam("tags", "tag1,tag2") + .setParam("organization", organization.getKey()); + verify(request, bothTagsRule, oneTagRule, otherTagRule); + } + @Test public void should_list_tags_in_tags_facet() throws IOException { OrganizationDto organization = dbTester.organizations().insert(); @@ -279,6 +299,29 @@ public class SearchActionTest { .containsExactly(metadata.getTags().toArray(new String[0])); } + @Test + public void should_not_return_tags_of_foreign_organization() throws IOException { + OrganizationDto organizationWithSpecificTags = dbTester.organizations().insert(); + OrganizationDto myOrganization = dbTester.organizations().insert(); + RuleDefinitionDto rule = dbTester.rules().insert(setSystemTags("system1", "system2")); + insertMetadata(organizationWithSpecificTags, rule, setTags("tag1", "tag2")); + indexRules(); + + SearchResponse result = ws.newRequest() + .setParam("facets", "tags") + .setParam("f", "tags") + .setParam("organization", myOrganization.getKey()) + .executeProtobuf(SearchResponse.class); + + assertThat(result.getRulesList()).extracting(Rule::getKey).containsExactly(rule.getKey().toString()); + assertThat(result.getFacets().getFacets(0).getValuesList()) + .extracting(v -> tuple(v.getVal(), v.getCount())) + .containsExactly( + tuple("system1", 1L), + tuple("system2", 1L) + ); + } + @Test public void should_return_specified_fields() throws Exception { RuleDefinitionDto rule = createJavaRule();