import org.sonar.core.qualityprofile.db.ActiveRuleKey;
import org.sonar.server.qualityprofile.ActiveRule;
import org.sonar.server.rule.index.RuleNormalizer;
-import org.sonar.server.search.BaseIndex;
-import org.sonar.server.search.FacetValue;
-import org.sonar.server.search.IndexDefinition;
-import org.sonar.server.search.IndexField;
-import org.sonar.server.search.SearchClient;
+import org.sonar.server.search.*;
import java.io.IOException;
import java.util.ArrayList;
public List<ActiveRule> findByProfile(String key) {
SearchRequestBuilder request = getClient().prepareSearch(getIndexName())
- .setQuery(QueryBuilders.termQuery(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY.field(), key))
+ .setQuery(QueryBuilders.filteredQuery(
+ QueryBuilders.termsQuery(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY.field(), key),
+ FilterBuilders.boolFilter()
+ .mustNot(FilterBuilders.hasParentFilter(this.getParentType(),
+ FilterBuilders.termFilter(RuleNormalizer.RuleField.STATUS.field(), RuleStatus.REMOVED.name())))))
.setRouting(key)
// TODO replace by scrolling
.setSize(Integer.MAX_VALUE);
RuleDto rule1 = RuleTesting.newXooX1().setSeverity(Severity.MAJOR);
RuleDto rule2 = RuleTesting.newXooX2().setSeverity(Severity.MAJOR);
- db.ruleDao().insert(dbSession, rule1, rule2);
+ RuleDto removedRule = RuleTesting.newDto(RuleKey.of("xoo", "removed")).setSeverity(Severity.MAJOR).setStatus(RuleStatus.REMOVED);
+ db.ruleDao().insert(dbSession, rule1, rule2, removedRule);
db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile1, rule1).setSeverity(Severity.MINOR));
db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile1, rule2).setSeverity(Severity.BLOCKER));
db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile2, rule2).setSeverity(Severity.CRITICAL));
+ // Removed rule can still be activated for instance when removing the checkstyle plugin, active rules related on checkstyle are not removed
+ // because if the plugin is re-install, quality profiles using these rule are not changed.
+ db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile2, removedRule).setSeverity(Severity.MAJOR));
dbSession.commit();
// 1. find by rule key