]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 21 Jan 2015 14:44:45 +0000 (15:44 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 21 Jan 2015 15:50:42 +0000 (16:50 +0100)
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java
server/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java
server/sonar-server/src/main/java/org/sonar/server/rule/ws/AppAction.java
server/sonar-server/src/main/java/org/sonar/server/rule/ws/RepositoriesAction.java

index f3779a75ec93df1b5e7610fc3379720c3fea2e29..a137d9a95da0e72f19bbafda41b6ecac17464258 100644 (file)
@@ -80,6 +80,7 @@ import static com.google.common.collect.Lists.newArrayList;
 
 public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
 
+  private static final String FILTER_DEBT_CHARACTERISTICS = "debtCharacteristics";
   public static final String FACET_LANGUAGES = "languages";
   public static final String FACET_TAGS = "tags";
   public static final String FACET_REPOSITORIES = "repositories";
@@ -273,7 +274,7 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
     // Construct the debt filter on effective char and subChar
     Collection<String> debtCharacteristics = query.getDebtCharacteristics();
     if (debtCharacteristics != null && !debtCharacteristics.isEmpty()) {
-      filters.put("debtCharacteristics",
+      filters.put(FILTER_DEBT_CHARACTERISTICS,
         FilterBuilders.boolFilter().must(
           FilterBuilders.orFilter(
             // Match only when NONE (overridden)
@@ -376,8 +377,6 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
 
   private void addStatusFacetIfNeeded(RuleQuery query, QueryContext options, Map<String, AggregationBuilder> aggregations, StickyFacetBuilder stickyFacetBuilder) {
     if (options.facets().contains(FACET_STATUSES)) {
-      Collection<RuleStatus> statusesFromQuery = query.getStatuses();
-
       BoolFilterBuilder facetFilter = stickyFacetBuilder.getStickyFacetFilter(RuleNormalizer.RuleField.STATUS.field());
       AggregationBuilder statuses = AggregationBuilders.filter(FACET_STATUSES + "_filter")
         .filter(facetFilter)
@@ -400,7 +399,7 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
       Collection<String> characsFromQuery = query.getDebtCharacteristics();
       Object[] selectedChars = characsFromQuery == null ? new Object[0] : characsFromQuery.toArray();
       AggregationBuilder debtChar = AggregationBuilders.filter(FACET_DEBT_CHARACTERISTICS + "__chars")
-        .filter(stickyFacetBuilder.getStickyFacetFilter("debtCharacteristics"))
+        .filter(stickyFacetBuilder.getStickyFacetFilter(FILTER_DEBT_CHARACTERISTICS))
         .subAggregation(
           AggregationBuilders.terms(FACET_DEBT_CHARACTERISTICS + "__chars_top").field(RuleNormalizer.RuleField.CHARACTERISTIC.field())
             .size(characsSize))
@@ -409,7 +408,7 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
             .include(Joiner.on('|').join(selectedChars))
             .size(characsSize));
       AggregationBuilder debtSubChar = AggregationBuilders.filter(FACET_DEBT_CHARACTERISTICS + "__subchars")
-        .filter(stickyFacetBuilder.getStickyFacetFilter("debtCharacteristics"))
+        .filter(stickyFacetBuilder.getStickyFacetFilter(FILTER_DEBT_CHARACTERISTICS))
         .subAggregation(
           AggregationBuilders.terms(FACET_DEBT_CHARACTERISTICS + "__subchars_top").field(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field())
             .size(subCharacsSize))
index 7180176a1c407fdffabeb3e781c383f516c80d93..8191bc1142928b563ceca7608461cd1972a72085 100644 (file)
@@ -21,6 +21,8 @@ package org.sonar.server.rule.ws;
 
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.sonar.api.ServerComponent;
 import org.sonar.api.resources.Language;
 import org.sonar.api.resources.Languages;
@@ -42,6 +44,9 @@ import java.util.Map;
  * web services.
  */
 public class ActiveRuleCompleter implements ServerComponent {
+
+  private static final Logger LOG = LoggerFactory.getLogger(ActiveRuleCompleter.class);
+
   private final QProfileLoader loader;
   private final Languages languages;
 
@@ -51,10 +56,15 @@ public class ActiveRuleCompleter implements ServerComponent {
   }
 
   void completeSearch(RuleQuery query, Collection<Rule> rules, JsonWriter json) {
-    json.name("actives").beginObject();
+    Collection<String> harvestedProfileKeys = writeActiveRules(json, query, rules);
+
+    writeProfiles(json, harvestedProfileKeys);
+  }
 
+  private Collection<String> writeActiveRules(JsonWriter json, RuleQuery query, Collection<Rule> rules) {
     Collection<String> qProfileKeys = Sets.newHashSet();
 
+    json.name("actives").beginObject();
     String profileKey = query.getQProfileKey();
     if (profileKey != null) {
       // Load details of active rules on the selected profile
@@ -72,29 +82,7 @@ public class ActiveRuleCompleter implements ServerComponent {
     }
     json.endObject();
 
-
-    Map<String, QualityProfileDto> qProfilesByKey = Maps.newHashMap();
-    for (String qProfileKey: qProfileKeys) {
-      if (!qProfilesByKey.containsKey(qProfileKey)) {
-        QualityProfileDto profile = loader.getByKey(qProfileKey);
-        qProfilesByKey.put(qProfileKey, profile);
-        if (profile.getParentKee() != null && !qProfilesByKey.containsKey(profile.getParentKee())) {
-          qProfilesByKey.put(profile.getParentKee(), loader.getByKey(profile.getParentKee()));
-        }
-      }
-    }
-    json.name("qProfiles").beginObject();
-    for (QualityProfileDto profile: qProfilesByKey.values()) {
-      Language language = languages.get(profile.getLanguage());
-      String langName = language == null ? profile.getLanguage() : language.getName();
-      json.name(profile.getKey()).beginObject()
-        .prop("name", profile.getName())
-        .prop("lang", profile.getLanguage())
-        .prop("langName", langName)
-        .prop("parent", profile.getParentKee())
-        .endObject();
-    }
-    json.endObject();
+    return qProfileKeys;
   }
 
   void completeShow(Rule rule, JsonWriter json) {
@@ -139,4 +127,39 @@ public class ActiveRuleCompleter implements ServerComponent {
     json.endArray().endObject();
     return activeRule.key().qProfile();
   }
+
+  private void writeProfiles(JsonWriter json, Collection<String> harvestedProfileKeys) {
+    Map<String, QualityProfileDto> qProfilesByKey = Maps.newHashMap();
+    for (String qProfileKey : harvestedProfileKeys) {
+      if (!qProfilesByKey.containsKey(qProfileKey)) {
+        QualityProfileDto profile = loader.getByKey(qProfileKey);
+        if (profile == null) {
+          LOG.warn("Could not find quality profile with key " + qProfileKey);
+          continue;
+        }
+        qProfilesByKey.put(qProfileKey, profile);
+        String parentKee = profile.getParentKee();
+        if (parentKee != null && !qProfilesByKey.containsKey(parentKee)) {
+          qProfilesByKey.put(parentKee, loader.getByKey(parentKee));
+        }
+      }
+    }
+    json.name("qProfiles").beginObject();
+    for (QualityProfileDto profile : qProfilesByKey.values()) {
+      writeProfile(json, profile);
+    }
+    json.endObject();
+  }
+
+  private void writeProfile(JsonWriter json, QualityProfileDto profile) {
+    Language language = languages.get(profile.getLanguage());
+    String langName = language == null ? profile.getLanguage() : language.getName();
+    json.name(profile.getKey()).beginObject()
+      .prop("name", profile.getName())
+      .prop("lang", profile.getLanguage())
+      .prop("langName", langName)
+      .prop("parent", profile.getParentKee())
+      .endObject();
+  }
+
 }
index 36351a152ae1a01d0b650f7ae0caa1f3f11816cf..307f1c30b6dab0f11434898ac3b4ec3dcd6efb1e 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.server.rule.ws;
 import com.google.common.collect.LinkedListMultimap;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Multimap;
+import org.apache.commons.collections.ComparatorUtils;
 import org.sonar.api.i18n.I18n;
 import org.sonar.api.resources.Language;
 import org.sonar.api.resources.Languages;
@@ -160,7 +161,7 @@ public class AppAction implements RulesAction {
     Collections.sort(rootCharacs, new Comparator<DebtCharacteristic>() {
       @Override
       public int compare(DebtCharacteristic o1, DebtCharacteristic o2) {
-        return o1.order().compareTo(o2.order());
+        return ComparatorUtils.nullHighComparator(ComparatorUtils.naturalComparator()).compare(o1.order(), o2.order());
       }
     });
     return rootCharacs;
index 55ff2345c37a0c121624eb70926038d7b9fe27d5..db05d1ef7dc79c8880c87e3a509caaa852f8835b 100644 (file)
@@ -42,6 +42,7 @@ import java.util.regex.Pattern;
  */
 public class RepositoriesAction implements RulesAction {
 
+  private static final String LANGUAGE = "language";
   private static final String MATCH_ALL = ".*";
   private final RuleRepositories repositories;
 
@@ -52,12 +53,12 @@ public class RepositoriesAction implements RulesAction {
   @Override
   public void handle(Request request, Response response) throws Exception {
     String query = request.param("q");
-    String languageKey = request.param("language");
+    String languageKey = request.param(LANGUAGE);
     int pageSize = request.mandatoryParamAsInt("ps");
 
     JsonWriter json = response.newJsonWriter().beginObject().name("repositories").beginArray();
     for (Repository repo : listMatchingRepositories(query, languageKey, pageSize)) {
-      json.beginObject().prop("key", repo.key()).prop("name", repo.name()).prop("language", repo.language()).endObject();
+      json.beginObject().prop("key", repo.key()).prop("name", repo.name()).prop(LANGUAGE, repo.language()).endObject();
     }
     json.endArray().endObject().close();
   }
@@ -90,7 +91,7 @@ public class RepositoriesAction implements RulesAction {
     action.createParam("q")
       .setDescription("A pattern to match repository keys/names against")
       .setExampleValue("squid");
-    action.createParam("language")
+    action.createParam(LANGUAGE)
       .setDescription("A language key; if provided, only repositories for the given language will be returned")
       .setExampleValue("java");
     action.createParam("ps")