Search in sources :

Example 11 with RulesProfile

use of org.sonar.api.profiles.RulesProfile in project sonarqube by SonarSource.

the class QProfileResetMediumTest method reset_language_profile_param_when_rule_definition_has_changed.

@Test
public void reset_language_profile_param_when_rule_definition_has_changed() {
    RulesProfile defProfile = RulesProfile.create("Basic", ServerTester.Xoo.KEY);
    defProfile.activateRule(org.sonar.api.rules.Rule.create("xoo", "x1"), null);
    register(new Rules() {

        @Override
        public void init(RulesDefinition.NewRepository repository) {
            RulesDefinition.NewRule x1 = repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc").setSeverity(MAJOR);
            x1.createParam("acceptWhitespace").setDefaultValue("false").setType(RuleParamType.BOOLEAN).setDescription("Accept whitespaces on the line");
        }
    }, defProfile);
    QualityProfileDto profile = tester.get(QualityProfileDao.class).selectByNameAndLanguage("Basic", ServerTester.Xoo.KEY, dbSession);
    ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), RuleKey.of("xoo", "x1"));
    // Change param in the rule def
    register(new Rules() {

        @Override
        public void init(RulesDefinition.NewRepository repository) {
            RulesDefinition.NewRule x1 = repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc").setSeverity(MAJOR);
            x1.createParam("acceptWhitespace").setDefaultValue("true").setType(RuleParamType.BOOLEAN).setDescription("Accept whitespaces on the line");
        }
    }, defProfile);
    reset.resetLanguage(ServerTester.Xoo.KEY);
    // Parameter value come back to origin after reset
    ActiveRuleDto activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
    List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
    assertThat(params).hasSize(1);
    assertThat(params.get(0).getKey()).isEqualTo("acceptWhitespace");
    assertThat(params.get(0).getValue()).isEqualTo("true");
}
Also used : QualityProfileDao(org.sonar.db.qualityprofile.QualityProfileDao) RulesProfile(org.sonar.api.profiles.RulesProfile) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 12 with RulesProfile

use of org.sonar.api.profiles.RulesProfile in project sonar-web by SonarSource.

the class SonarWayProfileTest method test.

@Test
public void test() {
    ValidationMessages validationMessages = ValidationMessages.create();
    RulesProfile profile = new SonarWayProfile(ruleFinder()).createProfile(validationMessages);
    Assertions.assertThat(profile.getName()).isEqualTo("Sonar way");
    Assertions.assertThat(profile.getLanguage()).isEqualTo(WebConstants.LANGUAGE_KEY);
    Assertions.assertThat(profile.getActiveRules()).onProperty("repositoryKey").containsOnly(WebRulesDefinition.REPOSITORY_KEY);
    Assertions.assertThat(profile.getActiveRules().size()).isGreaterThan(10);
    assertThat(validationMessages.hasErrors(), is(false));
}
Also used : RulesProfile(org.sonar.api.profiles.RulesProfile) ValidationMessages(org.sonar.api.utils.ValidationMessages) Test(org.junit.Test)

Example 13 with RulesProfile

use of org.sonar.api.profiles.RulesProfile in project sonarqube by SonarSource.

the class QProfileReset method loadDefinitionsGroupedByName.

private ListMultimap<QProfileName, RulesProfile> loadDefinitionsGroupedByName(String language) {
    ListMultimap<QProfileName, RulesProfile> profilesByName = ArrayListMultimap.create();
    for (ProfileDefinition definition : definitions) {
        ValidationMessages validation = ValidationMessages.create();
        RulesProfile profile = definition.createProfile(validation);
        if (language.equals(profile.getLanguage())) {
            processValidationMessages(validation);
            profilesByName.put(new QProfileName(profile.getLanguage(), profile.getName()), profile);
        }
    }
    return profilesByName;
}
Also used : RulesProfile(org.sonar.api.profiles.RulesProfile) ProfileDefinition(org.sonar.api.profiles.ProfileDefinition) ValidationMessages(org.sonar.api.utils.ValidationMessages)

Example 14 with RulesProfile

use of org.sonar.api.profiles.RulesProfile 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 15 with RulesProfile

use of org.sonar.api.profiles.RulesProfile 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)

Aggregations

RulesProfile (org.sonar.api.profiles.RulesProfile)21 Test (org.junit.Test)6 ValidationMessages (org.sonar.api.utils.ValidationMessages)5 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)4 ActiveRulesBuilder (org.sonar.api.batch.rule.internal.ActiveRulesBuilder)3 DbSession (org.sonar.db.DbSession)3 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)3 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ProfileDefinition (org.sonar.api.profiles.ProfileDefinition)2 ProfileImporter (org.sonar.api.profiles.ProfileImporter)2 RuleKey (org.sonar.api.rule.RuleKey)2 ActiveRule (org.sonar.api.rules.ActiveRule)2 ActiveRuleParam (org.sonar.api.rules.ActiveRuleParam)2 Rule (org.sonar.api.rules.Rule)2 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)2 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)2 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)2 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)2