messages.addWarningText("The parameter '" + entry.getKey() + "' does not exist in the rule: " + ruleToString(repositoryKey, key)); } else { activeRule.setParameter(entry.getKey(), entry.getValue());
private RulesProfile wrap(DbSession dbSession, QProfileDto profile) { RulesProfile target = new RulesProfile(profile.getName(), profile.getLanguage()); List<OrgActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByProfile(dbSession, profile); List<ActiveRuleParamDto> activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleIds(dbSession, Lists.transform(activeRuleDtos, ActiveRuleDto::getId)); ListMultimap<Integer, ActiveRuleParamDto> activeRuleParamsByActiveRuleId = FluentIterable.from(activeRuleParamDtos).index(ActiveRuleParamDto::getActiveRuleId); for (ActiveRuleDto activeRule : activeRuleDtos) { // TODO all rules should be loaded by using one query with all active rule keys as parameter Rule rule = ruleFinder.findByKey(activeRule.getRuleKey()); org.sonar.api.rules.ActiveRule wrappedActiveRule = target.activateRule(rule, RulePriority.valueOf(activeRule.getSeverityString())); List<ActiveRuleParamDto> paramDtos = activeRuleParamsByActiveRuleId.get(activeRule.getId()); for (ActiveRuleParamDto activeRuleParamDto : paramDtos) { wrappedActiveRule.setParameter(activeRuleParamDto.getKey(), activeRuleParamDto.getValue()); } } return target; }
@Test public void exportRuleParameters() throws IOException, SAXException { Writer writer = new StringWriter(); RulesProfile profile = RulesProfile.create("sonar way", "java"); Rule rule = Rule.create("checkstyle", "IllegalRegexp", "illegal regexp"); rule.createParameter("format"); rule.createParameter("message"); rule.createParameter("tokens"); ActiveRule activeRule = profile.activateRule(rule, RulePriority.BLOCKER); activeRule.setParameter("format", "foo"); activeRule.setParameter("message", "with special characters < > &"); // the tokens parameter is not set new XMLProfileSerializer().write(profile, writer); assertSimilarXml("exportRuleParameters.xml", writer.toString()); }
@Override public RulesProfile createProfile(ValidationMessages validation) { RulesProfile profile1 = RulesProfile.create("Profile 1", "xoo"); profile1.activateRule(Rule.create("repo1", "defaultSeverity"), null); profile1.activateRule(Rule.create("repo1", "overrideSeverity"), RulePriority.CRITICAL); Rule ruleWithParam = Rule.create("repo1", "overrideParam"); ruleWithParam.setParams(Arrays.asList(new RuleParam(ruleWithParam, "param", "", ""))); ActiveRule arWithParam = profile1.activateRule(ruleWithParam, null); arWithParam.setParameter("param", "value"); return profile1; } }
private void activateProperties(ActiveRule activeRule, Map<String, String> properties) { for (Map.Entry<String, String> property : properties.entrySet()) { if (StringUtils.equals("severity", property.getKey())) { activeRule.setSeverity(CheckstyleSeverityUtils.fromSeverity(property.getValue())); } else if (!StringUtils.equals("id", property.getKey())) { activeRule.setParameter(property.getKey(), property.getValue()); } } }
private static void activateProperties(ActiveRule activeRule, Map<String, String> properties) { for (Map.Entry<String, String> property : properties.entrySet()) { if (StringUtils.equals("severity", property.getKey())) { activeRule.setSeverity(CheckstyleSeverityUtils.fromSeverity(property.getValue())); } else if (!StringUtils.equals("id", property.getKey())) { activeRule.setParameter(property.getKey(), property.getValue()); } } }
private void activateProperties(ActiveRule activeRule, Map<String, String> properties) { for (Map.Entry<String, String> property : properties.entrySet()) { if (StringUtils.equals("severity", property.getKey())) { activeRule.setSeverity(CheckstyleSeverityUtils.fromSeverity(property.getValue())); } else if (!StringUtils.equals("id", property.getKey())) { activeRule.setParameter(property.getKey(), property.getValue()); } } } }
/** * @param messages * @param pmdRule * @param key * @param rule * @param activeRule */ private void createRule(ValidationMessages messages, PmdRule pmdRule, String key, Rule rule, ActiveRule activeRule) { for (PmdProperty prop : pmdRule.getProperties()) { String name = prop.getName(); if (rule.getParam(name) != null) { String value = prop.getValue(); String ruleValue = prop.isCdataValue() && isBlank(value) ? prop.getCdataValue() : value; activeRule.setParameter(name, ruleValue); } else { StringBuilder message = new StringBuilder("The property '").append(name); message.append("' is not supported in the PhpCodeSniffer rule: ").append(key); messages.addWarningText(message.toString()); } } }
private void addParametersToActiveRules(SMInputCursor rulesCursor, Map<String, ActiveRule> activeRules, String gendarmeCategory) throws XMLStreamException { SMInputCursor parameterCursor = rulesCursor.childElementCursor(); while (parameterCursor.getNext() != null) { ActiveRule currentRule = activeRules.get(parameterCursor.getAttrValue("rule") + "@" + gendarmeCategory); if (currentRule != null) { currentRule.setParameter(parameterCursor.getAttrValue("property"), parameterCursor.getAttrValue("value")); } } }
messages.addWarningText("The parameter '" + entry.getKey() + "' does not exist in the rule: " + ruleToString(repositoryKey, key)); } else { activeRule.setParameter(entry.getKey(), entry.getValue());
/** * @param messages * @param profile * @param pmdRule * @param configKey * @param rule */ private void createRule(ValidationMessages messages, RulesProfile profile, PmdRule pmdRule, String configKey, Rule rule) { PmdRulePriorityMapper mapper = new PmdRulePriorityMapper(); ActiveRule activeRule = profile.activateRule(rule, mapper.from(pmdRule.getPriority())); if (pmdRule.getProperties() != null) { for (PmdProperty prop : pmdRule.getProperties()) { String name = prop.getName(); if (rule.getParam(name) != null) { String value = prop.getValue(); String ruleValue = prop.isCdataValue() && isBlank(value) ? prop.getCdataValue() : value; activeRule.setParameter(name, ruleValue); } else { StringBuilder message = new StringBuilder("The property '").append(name); message.append("' is not supported in the pmd rule: ").append(configKey); messages.addWarningText(message.toString()); } } } }
private void setParameters(ActiveRule activeRule, PmdRule pmdRule, Rule rule, ValidationMessages messages) { for (PmdProperty prop : pmdRule.getProperties()) { String paramName = prop.getName(); if (rule.getParam(paramName) == null) { messages.addWarningText("The property '" + paramName + "' is not supported in the pmd rule: " + pmdRule.getRef()); } else { activeRule.setParameter(paramName, prop.getValue()); } } }
private static void setParameters(ActiveRule activeRule, PmdRule pmdRule, Rule rule, ValidationMessages messages) { for (PmdProperty prop : pmdRule.getProperties()) { String paramName = prop.getName(); if (rule.getParam(paramName) == null) { messages.addWarningText("The property '" + paramName + "' is not supported in the pmd rule: " + pmdRule.getRef()); } else { activeRule.setParameter(paramName, prop.getValue()); } } }
private RulesProfile wrap(DbSession dbSession, QProfileDto profile) { RulesProfile target = new RulesProfile(profile.getName(), profile.getLanguage()); List<OrgActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByProfile(dbSession, profile); List<ActiveRuleParamDto> activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleIds(dbSession, Lists.transform(activeRuleDtos, ActiveRuleDto::getId)); ListMultimap<Integer, ActiveRuleParamDto> activeRuleParamsByActiveRuleId = FluentIterable.from(activeRuleParamDtos).index(ActiveRuleParamDto::getActiveRuleId); for (ActiveRuleDto activeRule : activeRuleDtos) { // TODO all rules should be loaded by using one query with all active rule keys as parameter Rule rule = ruleFinder.findByKey(activeRule.getRuleKey()); org.sonar.api.rules.ActiveRule wrappedActiveRule = target.activateRule(rule, RulePriority.valueOf(activeRule.getSeverityString())); List<ActiveRuleParamDto> paramDtos = activeRuleParamsByActiveRuleId.get(activeRule.getId()); for (ActiveRuleParamDto activeRuleParamDto : paramDtos) { wrappedActiveRule.setParameter(activeRuleParamDto.getKey(), activeRuleParamDto.getValue()); } } return target; }
private RulesProfile select(QProfile qProfile, ActiveRules activeRules) { RulesProfile deprecatedProfile = new RulesProfile(); // TODO deprecatedProfile.setVersion(qProfile.version()); deprecatedProfile.setName(qProfile.getName()); deprecatedProfile.setLanguage(qProfile.getLanguage()); for (org.sonar.api.batch.rule.ActiveRule activeRule : ((DefaultActiveRules) activeRules).findByLanguage(qProfile.getLanguage())) { Rule rule = Rule.create(activeRule.ruleKey().repository(), activeRule.ruleKey().rule()); rule.setConfigKey(activeRule.internalKey()); ActiveRule deprecatedActiveRule = deprecatedProfile.activateRule(rule, RulePriority.valueOf(activeRule.severity())); for (Map.Entry<String, String> param : activeRule.params().entrySet()) { rule.createParameter(param.getKey()); deprecatedActiveRule.setParameter(param.getKey(), param.getValue()); } } return deprecatedProfile; }
messages.addWarningText("The parameter '" + entry.getKey() + "' does not exist in the rule: " + ruleToString(repositoryKey, key)); } else { activeRule.setParameter(entry.getKey(), entry.getValue());
private static RulesProfile select(QProfile qProfile, ActiveRules activeRules) { RulesProfile deprecatedProfile = new RulesProfile(); // TODO deprecatedProfile.setVersion(qProfile.version()); deprecatedProfile.setName(qProfile.getName()); deprecatedProfile.setLanguage(qProfile.getLanguage()); for (org.sonar.api.batch.rule.ActiveRule activeRule : activeRules.findByLanguage(qProfile.getLanguage())) { Rule rule = Rule.create(activeRule.ruleKey().repository(), activeRule.ruleKey().rule()); rule.setConfigKey(activeRule.internalKey()); // SONAR-6706 if (activeRule.templateRuleKey() != null) { rule.setTemplate(Rule.create(activeRule.ruleKey().repository(), activeRule.templateRuleKey())); } ActiveRule deprecatedActiveRule = deprecatedProfile.activateRule(rule, RulePriority.valueOf(activeRule.severity())); for (Map.Entry<String, String> param : activeRule.params().entrySet()) { rule.createParameter(param.getKey()); deprecatedActiveRule.setParameter(param.getKey(), param.getValue()); } } return deprecatedProfile; }
protected RulesProfile createRuleProfile(PmdRuleset pmdRuleset, ValidationMessages messages) { RulesProfile profile = RulesProfile.create(); for (PmdRule pmdRule : pmdRuleset.getPmdRules()) { if (PmdConstants.XPATH_CLASS.equals(pmdRule.getClazz())) { messages.addWarningText("PMD XPath rule '" + pmdRule.getName() + "' can't be imported automatically. The rule must be created manually through the Sonar web interface."); continue; } if (pmdRule.getRef() == null) { messages.addWarningText("A PMD rule without 'ref' attribute can't be imported. see '" + pmdRule.getClazz() + "'"); continue; } Rule rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(PmdConstants.REPOSITORY_KEY).withConfigKey(pmdRule.getRef())); if (rule != null) { ActiveRule activeRule = profile.activateRule(rule, PmdLevelUtils.fromLevel(pmdRule.getPriority())); if (pmdRule.getProperties() != null) { for (PmdProperty prop : pmdRule.getProperties()) { if (rule.getParam(prop.getName()) == null) { messages.addWarningText("The property '" + prop.getName() + "' is not supported in the pmd rule: " + pmdRule.getRef()); continue; } activeRule.setParameter(prop.getName(), prop.getValue()); } } } else { messages.addWarningText("Unable to import unknown PMD rule '" + pmdRule.getRef() + "'"); } } return profile; }