]> source.dussan.org Git - sonarqube.git/commitdiff
Add a parameter 'hideRules' on the Issues search search in order to not return rules
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 7 Feb 2014 15:02:18 +0000 (16:02 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 7 Feb 2014 15:02:28 +0000 (16:02 +0100)
sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueQuery.java
sonar-plugin-api/src/test/java/org/sonar/api/issue/IssueQueryTest.java
sonar-server/src/main/java/org/sonar/server/issue/DefaultIssueFinder.java
sonar-server/src/main/java/org/sonar/server/issue/PublicRubyIssueService.java
sonar-server/src/main/java/org/sonar/server/issue/filter/IssueFilterParameters.java
sonar-server/src/main/webapp/WEB-INF/app/controllers/api/issues_controller.rb
sonar-server/src/test/java/org/sonar/server/issue/DefaultIssueFinderTest.java
sonar-server/src/test/java/org/sonar/server/issue/PublicRubyIssueServiceTest.java
sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueQuery.java
sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueQueryTest.java

index 278050f32dabfaad41e314a2dc3e76c58bf3ec33..fee8c6b8a19bff437722dc4c07564f6582b0b6e3 100644 (file)
@@ -70,6 +70,7 @@ public class IssueQuery {
   private final Boolean assigned;
   private final Boolean planned;
   private final Boolean resolved;
+  private final Boolean hideRules;
   private final Date createdAt;
   private final Date createdAfter;
   private final Date createdBefore;
@@ -97,6 +98,7 @@ public class IssueQuery {
     this.assigned = builder.assigned;
     this.planned = builder.planned;
     this.resolved = builder.resolved;
+    this.hideRules = builder.hideRules;
     this.createdAt = builder.createdAt;
     this.createdAfter = builder.createdAfter;
     this.createdBefore = builder.createdBefore;
@@ -162,6 +164,14 @@ public class IssueQuery {
     return resolved;
   }
 
+  /**
+   * @since 4.2
+   */
+  @CheckForNull
+  public Boolean hideRules() {
+    return hideRules;
+  }
+
   @CheckForNull
   public Date createdAfter() {
     return createdAfter == null ? null : new Date(createdAfter.getTime());
@@ -226,6 +236,7 @@ public class IssueQuery {
     private Boolean assigned = null;
     private Boolean planned = null;
     private Boolean resolved = null;
+    private Boolean hideRules = false;
     private Date createdAt;
     private Date createdAfter;
     private Date createdBefore;
@@ -315,6 +326,18 @@ public class IssueQuery {
       return this;
     }
 
+    /**
+     * If true, rules will not be loaded
+     * If false, rules will be loaded
+     *
+     * @since 4.2
+     *
+     */
+    public Builder hideRules(@Nullable Boolean b) {
+      this.hideRules = b;
+      return this;
+    }
+
     public Builder createdAt(@Nullable Date d) {
       this.createdAt = d == null ? null : new Date(d.getTime());
       return this;
index 4fb8450de139f9c65bd83f64507530b3d6842e9b..2010e1655f89e48a127dc1562597169ad2cd9ced 100644 (file)
@@ -35,7 +35,7 @@ import static org.fest.assertions.Fail.fail;
 public class IssueQueryTest {
 
   @Test
-  public void should_build_query() throws Exception {
+  public void build_query() throws Exception {
     IssueQuery query = IssueQuery.builder()
       .issueKeys(newArrayList("ABCDE"))
       .severities(newArrayList(Severity.BLOCKER))
@@ -48,6 +48,7 @@ public class IssueQueryTest {
       .reporters(newArrayList("crunky"))
       .assignees(newArrayList("gargantua"))
       .assigned(true)
+      .hideRules(true)
       .createdAfter(new Date())
       .createdBefore(new Date())
       .createdAt(new Date())
@@ -68,6 +69,7 @@ public class IssueQueryTest {
     assertThat(query.reporters()).containsOnly("crunky");
     assertThat(query.assignees()).containsOnly("gargantua");
     assertThat(query.assigned()).isTrue();
+    assertThat(query.hideRules()).isTrue();
     assertThat(query.rules()).containsOnly(RuleKey.of("squid", "AvoidCycle"));
     assertThat(query.actionPlans()).containsOnly("AP1", "AP2");
     assertThat(query.createdAfter()).isNotNull();
@@ -83,7 +85,7 @@ public class IssueQueryTest {
   }
 
   @Test
-  public void should_build_query_without_dates() throws Exception {
+  public void build_query_without_dates() throws Exception {
     IssueQuery query = IssueQuery.builder()
       .issueKeys(newArrayList("ABCDE"))
       .createdAfter(null)
@@ -98,7 +100,7 @@ public class IssueQueryTest {
   }
 
   @Test
-  public void should_throw_exception_if_sort_is_not_valid() throws Exception {
+  public void throw_exception_if_sort_is_not_valid() throws Exception {
     try {
       IssueQuery.builder()
         .sort("UNKNOWN")
@@ -161,7 +163,7 @@ public class IssueQueryTest {
   }
 
   @Test
-  public void should_use_max_page_size_if_negative() throws Exception {
+  public void use_max_page_size_if_negative() throws Exception {
     IssueQuery query = IssueQuery.builder().pageSize(0).build();
     assertThat(query.pageSize()).isEqualTo(IssueQuery.MAX_PAGE_SIZE);
 
@@ -177,7 +179,7 @@ public class IssueQueryTest {
   }
 
   @Test
-  public void should_reset_to_max_page_size() throws Exception {
+  public void reset_to_max_page_size() throws Exception {
     IssueQuery query = IssueQuery.builder()
       .pageSize(IssueQuery.MAX_PAGE_SIZE + 100)
       .build();
index 9601bf4f8ea9bc3908c1da0a89a0243baab921f8..eeeae4fdf1875b5afc393afc412919993f338969 100644 (file)
@@ -25,11 +25,7 @@ import org.apache.ibatis.session.SqlSession;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonar.api.component.Component;
-import org.sonar.api.issue.ActionPlan;
-import org.sonar.api.issue.Issue;
-import org.sonar.api.issue.IssueFinder;
-import org.sonar.api.issue.IssueQuery;
-import org.sonar.api.issue.IssueQueryResult;
+import org.sonar.api.issue.*;
 import org.sonar.api.issue.internal.DefaultIssue;
 import org.sonar.api.issue.internal.DefaultIssueComment;
 import org.sonar.api.rules.Rule;
@@ -45,10 +41,7 @@ import org.sonar.core.resource.ResourceDao;
 import org.sonar.core.rule.DefaultRuleFinder;
 import org.sonar.server.user.UserSession;
 
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static com.google.common.collect.Maps.newHashMap;
@@ -144,9 +137,10 @@ public class DefaultIssueFinder implements IssueFinder {
         }
       }
 
+
       return new DefaultIssueQueryResult(issues)
         .setMaxResultsReached(authorizedIssues.size() == query.maxResults())
-        .addRules(findRules(ruleIds))
+        .addRules(hideRules(query) ? Collections.<Rule>emptyList() : findRules(ruleIds))
         .addComponents(findComponents(componentIds))
         .addProjects(findComponents(projectIds))
         .addActionPlans(findActionPlans(actionPlanKeys))
@@ -158,6 +152,11 @@ public class DefaultIssueFinder implements IssueFinder {
     }
   }
 
+  private boolean hideRules(IssueQuery query){
+    Boolean hideRules = query.hideRules();
+    return hideRules != null ? hideRules : false;
+  }
+
   private List<IssueDto> sort(List<IssueDto> issues, IssueQuery query, int allIssuesSize) {
     if (allIssuesSize < query.maxResults()) {
       return new IssuesFinderSort(issues, query).sort();
index f288bbe227ccda7669e36f522b01d215b5187123..dc9dfd40654e40f32882791a6365472770aae669 100644 (file)
@@ -89,6 +89,7 @@ public class PublicRubyIssueService implements RubyIssueService {
       .assignees(RubyUtils.toStrings(props.get(IssueFilterParameters.ASSIGNEES)))
       .assigned(RubyUtils.toBoolean(props.get(IssueFilterParameters.ASSIGNED)))
       .planned(RubyUtils.toBoolean(props.get(IssueFilterParameters.PLANNED)))
+      .hideRules(RubyUtils.toBoolean(props.get(IssueFilterParameters.HIDE_RULES)))
       .createdAt(RubyUtils.toDate(props.get(IssueFilterParameters.CREATED_AT)))
       .createdAfter(RubyUtils.toDate(props.get(IssueFilterParameters.CREATED_AFTER)))
       .createdBefore(RubyUtils.toDate(props.get(IssueFilterParameters.CREATED_BEFORE)))
index a3078f75e3658597320b889255b30205f4e44185..42b565afdeb6881466989f4ed359e2d0c276f1b2 100644 (file)
@@ -46,6 +46,7 @@ public class IssueFilterParameters {
   public static final String ASSIGNEES = "assignees";
   public static final String ASSIGNED = "assigned";
   public static final String PLANNED = "planned";
+  public static final String HIDE_RULES = "hideRules";
   public static final String CREATED_AFTER = "createdAfter";
   public static final String CREATED_AT = "createdAt";
   public static final String CREATED_BEFORE = "createdBefore";
@@ -55,7 +56,7 @@ public class IssueFilterParameters {
   public static final String ASC = "asc";
 
   public static final List<String> ALL = ImmutableList.of(ISSUES, SEVERITIES, STATUSES, RESOLUTIONS, RESOLVED, COMPONENTS, COMPONENT_ROOTS, RULES, ACTION_PLANS, REPORTERS,
-    ASSIGNEES, ASSIGNED, PLANNED, CREATED_AT, CREATED_AFTER, CREATED_BEFORE, PAGE_SIZE, PAGE_INDEX, SORT, ASC);
+    ASSIGNEES, ASSIGNED, PLANNED, HIDE_RULES, CREATED_AT, CREATED_AFTER, CREATED_BEFORE, PAGE_SIZE, PAGE_INDEX, SORT, ASC);
 
   public static final List<String> ALL_WITHOUT_PAGINATION = newArrayList(Iterables.filter(ALL, new Predicate<String>() {
     @Override
index 7a935b0cbae69b8289f4eb3d922060aa69ca50de..21d65503aeb2e61b8874641f0a3409e16c0d77fd 100644 (file)
@@ -32,7 +32,7 @@ class Api::IssuesController < Api::ApiController
     hash = {
       :maxResultsReached => results.maxResultsReached,
       :paging => paging_to_hash(results.paging),
-      :issues => results.issues.map { |issue| Issue.to_hash(issue, results.rule(issue).name) },
+      :issues => results.issues.map { |issue| Issue.to_hash(issue, results.rule(issue) ? results.rule(issue).name : nil) },
       :components => results.components.map { |component| component_to_hash(component) },
       :projects => results.projects.map { |project| component_to_hash(project) },
       :rules => results.rules.map { |rule| Rule.to_hash(rule) },
index aecd54dd836ec141132e4d27c17524d3c8398c29..9b43022c7d1b73a72c6080b29a6c6b7d574d4e3f 100644 (file)
@@ -53,9 +53,7 @@ import static org.mockito.Matchers.anyCollection;
 import static org.mockito.Matchers.anyInt;
 import static org.mockito.Matchers.anyListOf;
 import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
 
 public class DefaultIssueFinderTest {
 
@@ -69,7 +67,7 @@ public class DefaultIssueFinderTest {
   DefaultIssueFinder finder = new DefaultIssueFinder(mybatis, issueDao, issueChangeDao, ruleFinder, userFinder, resourceDao, actionPlanService);
 
   @Test
-  public void should_find_issues() {
+  public void find_issues() {
     IssueQuery query = IssueQuery.builder().build();
 
     IssueDto issue1 = new IssueDto().setId(1L).setRuleId(50).setComponentId(123l).setRootComponentId(100l)
@@ -96,7 +94,7 @@ public class DefaultIssueFinderTest {
   }
 
   @Test
-  public void should_find_paginate_result() {
+  public void find_paginate_result() {
     IssueQuery query = IssueQuery.builder().pageSize(1).pageIndex(1).build();
 
     IssueDto issue1 = new IssueDto().setId(1L).setRuleId(50).setComponentId(123l).setRootComponentId(100l)
@@ -123,7 +121,7 @@ public class DefaultIssueFinderTest {
   }
 
   @Test
-  public void should_find_by_key() {
+  public void find_by_key() {
     IssueDto issueDto = new IssueDto().setId(1L).setRuleId(1).setComponentId(1l).setRootComponentId(100l)
       .setComponentKey_unit_test_only("Action.java")
       .setRootComponentKey_unit_test_only("struts")
@@ -138,7 +136,7 @@ public class DefaultIssueFinderTest {
   }
 
   @Test
-  public void should_get_rule_from_result() {
+  public void get_rule_from_result() {
     Rule rule = Rule.create().setRepositoryKey("squid").setKey("AvoidCycle");
     when(ruleFinder.findByIds(anyCollection())).thenReturn(newArrayList(rule));
 
@@ -166,7 +164,27 @@ public class DefaultIssueFinderTest {
   }
 
   @Test
-  public void should_get_component_from_result() {
+  public void get_no_rule_from_result_with_hide_rules_param() {
+    Rule rule = Rule.create().setRepositoryKey("squid").setKey("AvoidCycle");
+    when(ruleFinder.findByIds(anyCollection())).thenReturn(newArrayList(rule));
+
+    IssueQuery query = IssueQuery.builder().hideRules(true).build();
+
+    IssueDto issue = new IssueDto().setId(1L).setRuleId(50).setComponentId(123l).setRootComponentId(100l)
+      .setComponentKey_unit_test_only("Action.java")
+      .setRootComponentKey_unit_test_only("struts")
+      .setRuleKey_unit_test_only("squid", "AvoidCycle")
+      .setStatus("OPEN").setResolution("OPEN");
+    when(issueDao.selectByIds(anyCollection(), any(SqlSession.class))).thenReturn(newArrayList(issue));
+
+    IssueQueryResult results = finder.find(query);
+    Issue result = results.issues().iterator().next();
+    assertThat(results.rule(result)).isNull();
+    assertThat(results.rules()).isEmpty();
+  }
+
+  @Test
+  public void get_component_from_result() {
     Component component = new ComponentDto().setKey("Action.java");
     when(resourceDao.findByIds(anyCollection())).thenReturn(newArrayList(component));
 
@@ -193,7 +211,7 @@ public class DefaultIssueFinderTest {
   }
 
   @Test
-  public void should_get_project_from_result() {
+  public void get_project_from_result() {
     Component project = new ComponentDto().setKey("struts");
     when(resourceDao.findByIds(anyCollection())).thenReturn(newArrayList(project));
 
@@ -220,7 +238,7 @@ public class DefaultIssueFinderTest {
   }
 
   @Test
-  public void should_get_action_plans_from_result() {
+  public void get_action_plans_from_result() {
     ActionPlan actionPlan1 = DefaultActionPlan.create("Short term").setKey("A");
     ActionPlan actionPlan2 = DefaultActionPlan.create("Long term").setKey("B");
 
@@ -248,7 +266,7 @@ public class DefaultIssueFinderTest {
   }
 
   @Test
-  public void should_get_user_from_result() {
+  public void get_user_from_result() {
     when(userFinder.findByLogins(anyListOf(String.class))).thenReturn(Lists.<User>newArrayList(
       new DefaultUser().setLogin("perceval").setName("Perceval"),
       new DefaultUser().setLogin("arthur").setName("Roi Arthur")
@@ -274,7 +292,7 @@ public class DefaultIssueFinderTest {
   }
 
   @Test
-  public void should_get_empty_result_when_no_issue() {
+  public void get_empty_result_when_no_issue() {
     IssueQuery query = IssueQuery.builder().build();
     when(issueDao.selectIssueIds(eq(query), anyInt(), any(SqlSession.class))).thenReturn(Collections.<IssueDto>emptyList());
     when(issueDao.selectByIds(anyCollection(), any(SqlSession.class))).thenReturn(Collections.<IssueDto>emptyList());
@@ -287,7 +305,7 @@ public class DefaultIssueFinderTest {
   }
 
   @Test
-  public void should_find_issue_with_technical_debt() {
+  public void find_issue_with_technical_debt() {
     IssueQuery query = IssueQuery.builder().build();
 
     IssueDto issue = new IssueDto().setId(1L).setRuleId(50).setComponentId(123l).setRootComponentId(100l)
index 3a99e736e438f0e38b038ee6ae9f14eccf11c57a..dd26b94474d0538c3c0054ece0e9f1e00f8d2ec8 100644 (file)
@@ -68,7 +68,7 @@ public class PublicRubyIssueServiceTest {
   }
 
   @Test
-  public void should_create_query_from_parameters() {
+  public void create_query_from_parameters() {
     Map<String, Object> map = newHashMap();
     map.put("issues", newArrayList("ABCDE1234"));
     map.put("severities", newArrayList("MAJOR", "MINOR"));
@@ -81,6 +81,7 @@ public class PublicRubyIssueServiceTest {
     map.put("assignees", newArrayList("joanna"));
     map.put("assigned", true);
     map.put("planned", true);
+    map.put("hideRules", true);
     map.put("createdAfter", "2013-04-16T09:08:24+0200");
     map.put("createdBefore", "2013-04-17T09:08:24+0200");
     map.put("rules", "squid:AvoidCycle,findbugs:NullReference");
@@ -101,6 +102,7 @@ public class PublicRubyIssueServiceTest {
     assertThat(query.assignees()).containsOnly("joanna");
     assertThat(query.assigned()).isTrue();
     assertThat(query.planned()).isTrue();
+    assertThat(query.hideRules()).isTrue();
     assertThat(query.rules()).hasSize(2);
     assertThat(query.createdAfter()).isEqualTo(DateUtils.parseDateTime("2013-04-16T09:08:24+0200"));
     assertThat(query.createdBefore()).isEqualTo(DateUtils.parseDateTime("2013-04-17T09:08:24+0200"));
index 23f200f6053cf1d298e2422015233909c784db6d..affb381ea8a3eedaa5500088598d9719f8c5bb12 100644 (file)
@@ -101,6 +101,14 @@ public class IssueQuery {
     return this;
   }
 
+  /**
+   * @since 4.2
+   */
+  public IssueQuery hideRules(Boolean hideRules) {
+    params.put("hideRules", hideRules);
+    return this;
+  }
+
   /**
    * Require second precision.
    * @since 3.7
index 14b352d5eae4b39b4ea964853f090b4840705b7d..2ebd1aa6e06d4b00d39cd759d27d2c0f4cf5ef5a 100644 (file)
@@ -46,6 +46,7 @@ public class IssueQueryTest {
       .componentRoots("struts")
       .resolutions("FIXED", "FALSE-POSITIVE")
       .resolved(true)
+      .hideRules(true)
       .rules("squid:AvoidCycle")
       .actionPlans("ABC")
       .statuses("OPEN", "CLOSED")
@@ -59,7 +60,7 @@ public class IssueQueryTest {
       .pageSize(5)
       .pageIndex(4);
 
-    assertThat(query.urlParams()).hasSize(20);
+    assertThat(query.urlParams()).hasSize(21);
     assertThat(query.urlParams()).includes(entry("issues", "ABCDE,FGHIJ"));
     assertThat(query.urlParams()).includes(entry("assignees", "arthur,perceval"));
     assertThat(query.urlParams()).includes(entry("assigned", true));
@@ -70,6 +71,7 @@ public class IssueQueryTest {
     assertThat(query.urlParams()).includes(entry("actionPlans", "ABC"));
     assertThat(query.urlParams()).includes(entry("resolutions", "FIXED,FALSE-POSITIVE"));
     assertThat(query.urlParams()).includes(entry("resolved", true));
+    assertThat(query.urlParams()).includes(entry("hideRules", true));
     assertThat(query.urlParams()).includes(entry("statuses", "OPEN,CLOSED"));
     assertThat(query.urlParams()).includes(entry("severities", "BLOCKER,INFO"));
     assertThat(query.urlParams()).includes(entry("reporters", "login1,login2"));