]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9432 let rule search use OR instead of AND when filtering by tags
authorDaniel Schwarz <daniel.schwarz@sonarsource.com>
Mon, 3 Jul 2017 09:43:09 +0000 (11:43 +0200)
committerDaniel Schwarz <bartfastiel@users.noreply.github.com>
Tue, 4 Jul 2017 13:53:24 +0000 (15:53 +0200)
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java
server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java

index 8638e255fa8022f7e9decc4460843b45df2b91c2..7a623e10dd811957360c69b107e3ad4c2236b3b7 100644 (file)
@@ -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);
     }
 
index 8b2c610a6dd0a199fc88cf545cc49156c9149e73..1d5507795ead1ae3daec00b17bafab0c62d60e64 100644 (file)
@@ -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<TestRequest> 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();