Search in sources :

Example 1 with ActiveRuleChange

use of org.sonar.server.qualityprofile.ActiveRuleChange in project sonarqube by SonarSource.

the class RegisterRules method start.

@Override
public void start() {
    Profiler profiler = Profiler.create(LOG).startInfo("Register rules");
    DbSession session = dbClient.openSession(false);
    try {
        Map<RuleKey, RuleDto> allRules = loadRules(session);
        RulesDefinition.Context context = defLoader.load();
        for (RulesDefinition.ExtendedRepository repoDef : getRepositories(context)) {
            if (languages.get(repoDef.language()) != null) {
                for (RulesDefinition.Rule ruleDef : repoDef.rules()) {
                    registerRule(ruleDef, allRules, session);
                }
                session.commit();
            }
        }
        List<RuleDto> activeRules = processRemainingDbRules(allRules.values(), session);
        List<ActiveRuleChange> changes = removeActiveRulesOnStillExistingRepositories(session, activeRules, context);
        session.commit();
        persistRepositories(session, context.repositories());
        ruleIndexer.index();
        activeRuleIndexer.index(changes);
        profiler.stopDebug();
    } finally {
        session.close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) Profiler(org.sonar.api.utils.log.Profiler) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange)

Example 2 with ActiveRuleChange

use of org.sonar.server.qualityprofile.ActiveRuleChange in project sonarqube by SonarSource.

the class RegisterRules method removeActiveRulesOnStillExistingRepositories.

/**
   * SONAR-4642
   * <p/>
   * Remove active rules on repositories that still exists.
   * <p/>
   * For instance, if the javascript repository do not provide anymore some rules, active rules related to this rules will be removed.
   * But if the javascript repository do not exists anymore, then related active rules will not be removed.
   * <p/>
   * The side effect of this approach is that extended repositories will not be managed the same way.
   * If an extended repository do not exists anymore, then related active rules will be removed.
   */
private List<ActiveRuleChange> removeActiveRulesOnStillExistingRepositories(DbSession session, Collection<RuleDto> removedRules, RulesDefinition.Context context) {
    List<String> repositoryKeys = newArrayList(Iterables.transform(context.repositories(), new Function<RulesDefinition.Repository, String>() {

        @Override
        public String apply(@Nonnull RulesDefinition.Repository input) {
            return input.key();
        }
    }));
    List<ActiveRuleChange> changes = new ArrayList<>();
    for (RuleDto rule : removedRules) {
        // SONAR-4642 Remove active rules only when repository still exists
        if (repositoryKeys.contains(rule.getRepositoryKey())) {
            changes.addAll(ruleActivator.deactivate(session, rule));
        }
    }
    return changes;
}
Also used : DebtRemediationFunction(org.sonar.api.server.debt.DebtRemediationFunction) Function(com.google.common.base.Function) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) Nonnull(javax.annotation.Nonnull) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange)

Aggregations

RulesDefinition (org.sonar.api.server.rule.RulesDefinition)2 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)2 RuleDto (org.sonar.db.rule.RuleDto)2 ActiveRuleChange (org.sonar.server.qualityprofile.ActiveRuleChange)2 Function (com.google.common.base.Function)1 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 ArrayList (java.util.ArrayList)1 Nonnull (javax.annotation.Nonnull)1 RuleKey (org.sonar.api.rule.RuleKey)1 DebtRemediationFunction (org.sonar.api.server.debt.DebtRemediationFunction)1 Profiler (org.sonar.api.utils.log.Profiler)1 DbSession (org.sonar.db.DbSession)1