Search in sources :

Example 31 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.

the class ActiveRuleIndexerTest method indexAll_indexes_all_data.

@Test
public void indexAll_indexes_all_data() {
    ActiveRuleDto activeRule = db.qualityProfiles().activateRule(profile1, rule1);
    underTest.indexAll();
    List<ActiveRuleDoc> docs = es.getDocuments(TYPE_ACTIVE_RULE, ActiveRuleDoc.class);
    assertThat(docs).hasSize(1);
    verify(docs.get(0), profile1, activeRule);
    assertThatEsQueueTableIsEmpty();
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Test(org.junit.Test)

Example 32 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.

the class ActiveRuleIndexerTest method commitDeletionOfProfiles.

@Test
public void commitDeletionOfProfiles() {
    ActiveRuleDto ar1 = db.qualityProfiles().activateRule(profile1, rule1);
    db.qualityProfiles().activateRule(profile2, rule1);
    db.qualityProfiles().activateRule(profile2, rule2);
    indexAll();
    db.getDbClient().qualityProfileDao().deleteRulesProfilesByUuids(db.getSession(), singletonList(profile2.getRulesProfileUuid()));
    underTest.commitDeletionOfProfiles(db.getSession(), singletonList(profile2));
    verifyOnlyIndexed(ar1);
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Test(org.junit.Test)

Example 33 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.

the class QProfileResetImpl method reset.

@Override
public BulkChangeResult reset(DbSession dbSession, QProfileDto profile, Collection<RuleActivation> activations) {
    requireNonNull(profile.getRulesProfileUuid(), "Quality profile must be persisted");
    checkArgument(!profile.isBuiltIn(), "Operation forbidden for built-in Quality Profile '%s'", profile.getKee());
    BulkChangeResult result = new BulkChangeResult();
    Set<String> rulesToBeDeactivated = new HashSet<>();
    // Keep reference to all the activated rules before backup restore
    for (ActiveRuleDto activeRuleDto : db.activeRuleDao().selectByProfile(dbSession, profile)) {
        if (activeRuleDto.getInheritance() == null) {
            // inherited rules can't be deactivated
            rulesToBeDeactivated.add(activeRuleDto.getRuleUuid());
        }
    }
    Set<String> ruleUuids = new HashSet<>(rulesToBeDeactivated.size() + activations.size());
    ruleUuids.addAll(rulesToBeDeactivated);
    activations.forEach(a -> ruleUuids.add(a.getRuleUuid()));
    RuleActivationContext context = activator.createContextForUserProfile(dbSession, profile, ruleUuids);
    for (RuleActivation activation : activations) {
        try {
            List<ActiveRuleChange> changes = activator.activate(dbSession, activation, context);
            rulesToBeDeactivated.remove(activation.getRuleUuid());
            result.incrementSucceeded();
            result.addChanges(changes);
        } catch (BadRequestException e) {
            result.incrementFailed();
            result.getErrors().addAll(e.errors());
        }
    }
    List<ActiveRuleChange> changes = new ArrayList<>(result.getChanges());
    for (String ruleUuid : rulesToBeDeactivated) {
        try {
            changes.addAll(activator.deactivate(dbSession, context, ruleUuid, false));
        } catch (BadRequestException e) {
        // ignore, probably a rule inherited from parent that can't be deactivated
        }
    }
    qualityProfileChangeEventService.distributeRuleChangeEvent(List.of(profile), changes, profile.getLanguage());
    activeRuleIndexer.commitAndIndex(dbSession, changes);
    return result;
}
Also used : RuleActivationContext(org.sonar.server.qualityprofile.builtin.RuleActivationContext) ArrayList(java.util.ArrayList) BadRequestException(org.sonar.server.exceptions.BadRequestException) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) HashSet(java.util.HashSet)

Example 34 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.

the class CompareAction method writeRules.

private void writeRules(JsonWriter json, Map<RuleKey, ActiveRuleDto> activeRules, Map<RuleKey, RuleDefinitionDto> rulesByKey, Map<String, RuleRepositoryDto> repositoriesByKey) {
    json.beginArray();
    for (Entry<RuleKey, ActiveRuleDto> activeRule : activeRules.entrySet()) {
        RuleKey key = activeRule.getKey();
        ActiveRuleDto value = activeRule.getValue();
        json.beginObject();
        RuleDefinitionDto rule = rulesByKey.get(key);
        writeRule(json, rule, repositoriesByKey.get(rule.getRepositoryKey()));
        json.prop(ATTRIBUTE_SEVERITY, value.getSeverityString());
        json.endObject();
    }
    json.endArray();
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 35 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.

the class RuleActivator method getSeverityForNonBuiltInProfile.

private static String getSeverityForNonBuiltInProfile(RuleActivation request, RuleWrapper rule, @Nullable ActiveRuleWrapper activeRule, @Nullable ActiveRuleWrapper parentActiveRule) {
    String severity;
    if (activeRule != null) {
        ActiveRuleDto activeRuleDto = activeRule.get();
        // load severity from request, else keep existing one (if overridden), else from parent if rule inherited, else from default
        severity = firstNonNull(request.getSeverity(), activeRuleDto.doesOverride() ? activeRuleDto.getSeverityString() : null, parentActiveRule != null ? parentActiveRule.get().getSeverityString() : activeRuleDto.getSeverityString(), rule.get().getSeverityString());
    } else {
        // load severity from request, else from parent, else from default
        severity = firstNonNull(request.getSeverity(), parentActiveRule != null ? parentActiveRule.get().getSeverityString() : null, rule.get().getSeverityString());
    }
    return severity;
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Aggregations

ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)112 Test (org.junit.Test)51 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)49 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)26 OrgActiveRuleDto (org.sonar.db.qualityprofile.OrgActiveRuleDto)21 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)21 RuleDto (org.sonar.db.rule.RuleDto)21 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)19 RuleParamDto (org.sonar.db.rule.RuleParamDto)19 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)18 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)15 ActiveRuleChange (org.sonar.server.qualityprofile.ActiveRuleChange)14 RuleKey (org.sonar.api.rule.RuleKey)12 RuleQuery (org.sonar.server.rule.index.RuleQuery)11 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)10 ArrayList (java.util.ArrayList)9 BuiltInQualityProfilesDefinition (org.sonar.api.server.profile.BuiltInQualityProfilesDefinition)9 NewBuiltInQualityProfile (org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.NewBuiltInQualityProfile)9 Map (java.util.Map)7 WsTester (org.sonar.server.ws.WsTester)6