Search in sources :

Example 1 with ActiveRule

use of org.sonarsource.sonarlint.core.proto.Sonarlint.ActiveRules.ActiveRule in project sonarlint-core by SonarSource.

the class SonarQubeActiveRulesProvider method createNewActiveRule.

private static void createNewActiveRule(ActiveRulesBuilder builder, ActiveRule activeRule, Sonarlint.Rules storageRules, String language, Rules rules) {
    RuleKey ruleKey = RuleKey.of(activeRule.getRepo(), activeRule.getKey());
    Rule rule = rules.find(ruleKey);
    Sonarlint.Rules.Rule storageRule;
    try {
        storageRule = storageRules.getRulesByKeyOrThrow(ruleKey.toString());
    } catch (IllegalArgumentException e) {
        throw new MessageException("Unknown active rule in the quality profile of the project. Please update the SonarQube server binding.");
    }
    NewActiveRule newActiveRule = builder.create(ruleKey).setLanguage(language).setName(rule.name()).setInternalKey(rule.internalKey()).setSeverity(activeRule.getSeverity());
    if (!StringUtils.isEmpty(storageRule.getTemplateKey())) {
        RuleKey templateRuleKey = RuleKey.parse(storageRule.getTemplateKey());
        newActiveRule.setTemplateRuleKey(templateRuleKey.rule());
    }
    for (Map.Entry<String, String> param : activeRule.getParamsMap().entrySet()) {
        newActiveRule.setParam(param.getKey(), param.getValue());
    }
    newActiveRule.activate();
}
Also used : NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) RuleKey(org.sonar.api.rule.RuleKey) MessageException(org.sonarsource.sonarlint.core.client.api.exceptions.MessageException) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) Rule(org.sonar.api.batch.rule.Rule) ActiveRule(org.sonarsource.sonarlint.core.proto.Sonarlint.ActiveRules.ActiveRule) ActiveRules(org.sonar.api.batch.rule.ActiveRules) Rules(org.sonar.api.batch.rule.Rules) Map(java.util.Map)

Example 2 with ActiveRule

use of org.sonarsource.sonarlint.core.proto.Sonarlint.ActiveRules.ActiveRule in project sonarlint-core by SonarSource.

the class SonarQubeActiveRulesProvider method provide.

public ActiveRules provide(Sonarlint.Rules storageRules, Sonarlint.QProfiles qProfiles, StorageReader storageReader, Rules rules, ConnectedAnalysisConfiguration analysisConfiguration, Languages languages) {
    if (activeRules == null) {
        Map<String, String> qProfilesByLanguage = loadQualityProfilesFromStorage(qProfiles, storageReader, analysisConfiguration);
        ActiveRulesBuilder builder = new ActiveRulesBuilder();
        for (Map.Entry<String, String> entry : qProfilesByLanguage.entrySet()) {
            String language = entry.getKey();
            if (languages.get(language) == null) {
                continue;
            }
            String qProfileKey = entry.getValue();
            QProfile qProfile = qProfiles.getQprofilesByKeyOrThrow(qProfileKey);
            if (qProfile.getActiveRuleCount() == 0) {
                LOG.debug("  * {}: {} (0 rules)", language, qProfileKey);
                continue;
            }
            Sonarlint.ActiveRules activeRulesFromStorage = storageReader.readActiveRules(qProfileKey);
            LOG.debug("  * {}: {} ({} rules)", language, qProfileKey, activeRulesFromStorage.getActiveRulesByKeyMap().size());
            for (ActiveRule activeRule : activeRulesFromStorage.getActiveRulesByKeyMap().values()) {
                createNewActiveRule(builder, activeRule, storageRules, language, rules);
            }
        }
        activeRules = builder.build();
    }
    return activeRules;
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) ActiveRule(org.sonarsource.sonarlint.core.proto.Sonarlint.ActiveRules.ActiveRule) Sonarlint(org.sonarsource.sonarlint.core.proto.Sonarlint) Map(java.util.Map) QProfile(org.sonarsource.sonarlint.core.proto.Sonarlint.QProfiles.QProfile)

Aggregations

Map (java.util.Map)2 NewActiveRule (org.sonar.api.batch.rule.internal.NewActiveRule)2 ActiveRule (org.sonarsource.sonarlint.core.proto.Sonarlint.ActiveRules.ActiveRule)2 ActiveRules (org.sonar.api.batch.rule.ActiveRules)1 Rule (org.sonar.api.batch.rule.Rule)1 Rules (org.sonar.api.batch.rule.Rules)1 ActiveRulesBuilder (org.sonar.api.batch.rule.internal.ActiveRulesBuilder)1 RuleKey (org.sonar.api.rule.RuleKey)1 MessageException (org.sonarsource.sonarlint.core.client.api.exceptions.MessageException)1 Sonarlint (org.sonarsource.sonarlint.core.proto.Sonarlint)1 QProfile (org.sonarsource.sonarlint.core.proto.Sonarlint.QProfiles.QProfile)1