Search in sources :

Example 61 with QualityProfileDto

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

the class QProfilesWsMediumTest method bulk_deactivate_rule_not_all.

@Test
public void bulk_deactivate_rule_not_all() throws Exception {
    QualityProfileDto profile = createProfile("java");
    QualityProfileDto php = createProfile("php");
    RuleDto rule0 = createRule(profile.getLanguage(), "toto1");
    RuleDto rule1 = createRule(profile.getLanguage(), "toto2");
    createActiveRule(rule0, profile);
    createActiveRule(rule1, profile);
    createActiveRule(rule0, php);
    createActiveRule(rule1, php);
    session.commit();
    ruIndexer.index();
    activeRuIndexer.index();
    // 0. Assert No Active Rule for profile
    assertThat(db.activeRuleDao().selectByProfileKey(session, profile.getKey())).hasSize(2);
    // 1. Deactivate Rule
    WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, BulkRuleActivationActions.BULK_DEACTIVATE_ACTION);
    request.setParam(RuleActivationActions.PROFILE_KEY, profile.getKey());
    WsTester.Result result = request.execute();
    session.clearCache();
    // 2. Assert ActiveRule in DAO
    assertThat(db.activeRuleDao().selectByProfileKey(session, profile.getKey())).hasSize(0);
    assertThat(db.activeRuleDao().selectByProfileKey(session, php.getKey())).hasSize(2);
}
Also used : WsTester(org.sonar.server.ws.WsTester) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 62 with QualityProfileDto

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

the class QProfilesWsMediumTest method activate_rule_override_severity.

@Test
public void activate_rule_override_severity() throws Exception {
    QualityProfileDto profile = createProfile("java");
    RuleDto rule = createRule(profile.getLanguage(), "toto");
    session.commit();
    ruIndexer.index();
    // 0. Assert No Active Rule for profile
    assertThat(db.activeRuleDao().selectByProfileKey(session, profile.getKey())).isEmpty();
    // 1. Activate Rule
    WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, RuleActivationActions.ACTIVATE_ACTION);
    request.setParam(RuleActivationActions.PROFILE_KEY, profile.getKey());
    request.setParam(RuleActivationActions.RULE_KEY, rule.getKey().toString());
    request.setParam(RuleActivationActions.SEVERITY, "MINOR");
    WsTester.Result result = request.execute();
    session.clearCache();
    // 2. Assert ActiveRule in DAO
    ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), rule.getKey());
    assertThat(db.activeRuleDao().selectOrFailByKey(session, activeRuleKey).getSeverityString()).isEqualTo("MINOR");
}
Also used : WsTester(org.sonar.server.ws.WsTester) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 63 with QualityProfileDto

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

the class QProfileReset method resetLanguage.

/**
   * Reset built-in profiles for the given language. Missing profiles are created and
   * existing ones are updated.
   */
public void resetLanguage(String language) {
    DbSession dbSession = db.openSession(false);
    try {
        ListMultimap<QProfileName, RulesProfile> profilesByName = loadDefinitionsGroupedByName(language);
        for (Map.Entry<QProfileName, Collection<RulesProfile>> entry : profilesByName.asMap().entrySet()) {
            QProfileName profileName = entry.getKey();
            QualityProfileDto profile = factory.getOrCreate(dbSession, profileName);
            List<RuleActivation> activations = Lists.newArrayList();
            for (RulesProfile def : entry.getValue()) {
                for (ActiveRule activeRule : def.getActiveRules()) {
                    RuleActivation activation = new RuleActivation(RuleKey.of(activeRule.getRepositoryKey(), activeRule.getRuleKey()));
                    activation.setSeverity(activeRule.getSeverity().name());
                    if (!activeRule.getActiveRuleParams().isEmpty()) {
                        for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
                            activation.setParameter(param.getParamKey(), param.getValue());
                        }
                    } else {
                        for (RuleParamDto param : db.ruleDao().selectRuleParamsByRuleKey(dbSession, activeRule.getRule().ruleKey())) {
                            activation.setParameter(param.getName(), param.getDefaultValue());
                        }
                    }
                    activations.add(activation);
                }
            }
            doReset(dbSession, profile, activations);
        }
    } finally {
        dbSession.close();
    }
}
Also used : RulesProfile(org.sonar.api.profiles.RulesProfile) ActiveRule(org.sonar.api.rules.ActiveRule) DbSession(org.sonar.db.DbSession) ActiveRuleParam(org.sonar.api.rules.ActiveRuleParam) Collection(java.util.Collection) RuleParamDto(org.sonar.db.rule.RuleParamDto) Map(java.util.Map) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto)

Example 64 with QualityProfileDto

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

the class RegisterQualityProfiles method register.

private List<ActiveRuleChange> register(QProfileName name, Collection<RulesProfile> profiles, DbSession session) {
    LOGGER.info("Register profile " + name);
    List<ActiveRuleChange> changes = new ArrayList<>();
    QualityProfileDto profileDto = dbClient.qualityProfileDao().selectByNameAndLanguage(name.getName(), name.getLanguage(), session);
    if (profileDto != null) {
        changes.addAll(profileFactory.delete(session, profileDto.getKey(), true));
    }
    profileFactory.create(session, name);
    for (RulesProfile profile : profiles) {
        for (org.sonar.api.rules.ActiveRule activeRule : profile.getActiveRules()) {
            RuleKey ruleKey = RuleKey.of(activeRule.getRepositoryKey(), activeRule.getRuleKey());
            RuleActivation activation = new RuleActivation(ruleKey);
            activation.setSeverity(activeRule.getSeverity() != null ? activeRule.getSeverity().name() : null);
            for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
                activation.setParameter(param.getKey(), param.getValue());
            }
            changes.addAll(ruleActivator.activate(session, activation, name));
        }
    }
    LoadedTemplateDto template = new LoadedTemplateDto(templateKey(name), LoadedTemplateDto.QUALITY_PROFILE_TYPE);
    dbClient.loadedTemplateDao().insert(template, session);
    session.commit();
    return changes;
}
Also used : RulesProfile(org.sonar.api.profiles.RulesProfile) ActiveRuleParam(org.sonar.api.rules.ActiveRuleParam) RuleKey(org.sonar.api.rule.RuleKey) ArrayList(java.util.ArrayList) LoadedTemplateDto(org.sonar.db.loadedtemplate.LoadedTemplateDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto)

Example 65 with QualityProfileDto

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

the class RuleActivator method updateProfileDates.

private void updateProfileDates(DbSession dbSession, RuleActivatorContext context) {
    QualityProfileDto profile = context.profile();
    profile.setRulesUpdatedAtAsDate(context.getInitDate());
    if (userSession.isLoggedIn()) {
        profile.setUserUpdatedAt(context.getInitDate().getTime());
    }
    db.qualityProfileDao().update(dbSession, profile);
}
Also used : QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto)

Aggregations

QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)126 Test (org.junit.Test)76 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)37 RuleDto (org.sonar.db.rule.RuleDto)29 WsTester (org.sonar.server.ws.WsTester)21 RuleQuery (org.sonar.server.rule.index.RuleQuery)14 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)13 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)12 DbSession (org.sonar.db.DbSession)11 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)9 SearchOptions (org.sonar.server.es.SearchOptions)9 ComponentDto (org.sonar.db.component.ComponentDto)8 OrganizationDto (org.sonar.db.organization.OrganizationDto)7 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)6 Date (java.util.Date)5 QualityProfileTesting.newQualityProfileDto (org.sonar.db.qualityprofile.QualityProfileTesting.newQualityProfileDto)5 RuleParamDto (org.sonar.db.rule.RuleParamDto)5 RulesProfile (org.sonar.api.profiles.RulesProfile)4 Language (org.sonar.api.resources.Language)4 RuleKey (org.sonar.api.rule.RuleKey)4