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";
// 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)
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)
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))
.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))
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;
* web services.
*/
public class ActiveRuleCompleter implements ServerComponent {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ActiveRuleCompleter.class);
+
private final QProfileLoader loader;
private final Languages languages;
}
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
}
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) {
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();
+ }
+
}
*/
public class RepositoriesAction implements RulesAction {
+ private static final String LANGUAGE = "language";
private static final String MATCH_ALL = ".*";
private final RuleRepositories repositories;
@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();
}
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")