use of org.sonar.api.rules.Rule in project sonarqube by SonarSource.
the class CachingRuleFinder method toRule.
private static Rule toRule(RuleDefinitionDto ruleDefinition, List<RuleParamDto> params) {
String severity = ruleDefinition.getSeverityString();
String description = ruleDefinition.getDescription();
RuleDto.Format descriptionFormat = ruleDefinition.getDescriptionFormat();
Rule apiRule = new Rule();
apiRule.setName(ruleDefinition.getName()).setLanguage(ruleDefinition.getLanguage()).setKey(ruleDefinition.getRuleKey()).setConfigKey(ruleDefinition.getConfigKey()).setIsTemplate(ruleDefinition.isTemplate()).setCreatedAt(new Date(ruleDefinition.getCreatedAt())).setUpdatedAt(new Date(ruleDefinition.getUpdatedAt())).setRepositoryKey(ruleDefinition.getRepositoryKey()).setSeverity(severity != null ? RulePriority.valueOf(severity) : null).setStatus(ruleDefinition.getStatus().name()).setSystemTags(ruleDefinition.getSystemTags().toArray(new String[ruleDefinition.getSystemTags().size()])).setTags(new String[0]);
if (description != null && descriptionFormat != null) {
if (RuleDto.Format.HTML.equals(descriptionFormat)) {
apiRule.setDescription(description);
} else {
apiRule.setDescription(Markdown.convertToHtml(description));
}
}
List<org.sonar.api.rules.RuleParam> apiParams = new ArrayList<>();
for (RuleParamDto param : params) {
apiParams.add(new org.sonar.api.rules.RuleParam(apiRule, param.getName(), param.getDescription(), param.getType()).setDefaultValue(param.getDefaultValue()));
}
apiRule.setParams(apiParams);
return apiRule;
}
use of org.sonar.api.rules.Rule in project sonarqube by SonarSource.
the class CachingRuleFinder method buildRulesByRuleDefinitionDto.
private static Map<RuleDefinitionDto, Rule> buildRulesByRuleDefinitionDto(DbClient dbClient, DbSession dbSession, List<RuleDefinitionDto> dtos) {
Map<String, List<RuleParamDto>> ruleParamsByRuleUuid = retrieveRuleParameters(dbClient, dbSession);
Map<RuleDefinitionDto, Rule> rulesByDefinition = new HashMap<>(dtos.size());
for (RuleDefinitionDto definition : dtos) {
rulesByDefinition.put(definition, toRule(definition, ruleParamsByRuleUuid.getOrDefault(definition.getUuid(), emptyList())));
}
return unmodifiableMap(rulesByDefinition);
}
use of org.sonar.api.rules.Rule in project sonarqube by SonarSource.
the class CachingRuleFinderTest method find_searches_by_exact_match_of_repository_key_and_returns_most_recent_rule.
@Test
public void find_searches_by_exact_match_of_repository_key_and_returns_most_recent_rule() {
String repoKey = "ABCD";
RuleDefinitionDto[] sameRepoKey = { dbTester.rules().insert(rule -> rule.setRepositoryKey(repoKey).setUpdatedAt(system2.now())), dbTester.rules().insert(rule -> rule.setRepositoryKey(repoKey).setUpdatedAt(system2.now())) };
RuleDefinitionDto otherRule = dbTester.rules().insert(rule -> rule.setUpdatedAt(system2.now()));
CachingRuleFinder underTest = new CachingRuleFinder(dbClient);
assertThat(toRuleKey(underTest.find(RuleQuery.create().withRepositoryKey(repoKey)))).isEqualTo(sameRepoKey[1].getKey());
assertThat(toRuleKey(underTest.find(RuleQuery.create().withRepositoryKey(otherRule.getRepositoryKey())))).isEqualTo(otherRule.getKey());
assertThat(underTest.find(RuleQuery.create().withRepositoryKey(repoKey.toLowerCase()))).isNull();
assertThat(underTest.find(RuleQuery.create().withRepositoryKey(randomAlphabetic(3)))).isNull();
}
use of org.sonar.api.rules.Rule in project sonarqube by SonarSource.
the class QProfileExporters method wrap.
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().selectParamsByActiveRuleUuids(dbSession, Lists.transform(activeRuleDtos, ActiveRuleDto::getUuid));
ListMultimap<String, ActiveRuleParamDto> activeRuleParamsByActiveRuleUuid = FluentIterable.from(activeRuleParamDtos).index(ActiveRuleParamDto::getActiveRuleUuid);
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 = activeRuleParamsByActiveRuleUuid.get(activeRule.getUuid());
for (ActiveRuleParamDto activeRuleParamDto : paramDtos) {
wrappedActiveRule.setParameter(activeRuleParamDto.getKey(), activeRuleParamDto.getValue());
}
}
return target;
}
use of org.sonar.api.rules.Rule in project sonarqube by SonarSource.
the class RulesProfileTest method activateRuleWithSpecificPriority.
@Test
public void activateRuleWithSpecificPriority() {
RulesProfile profile = RulesProfile.create();
Rule rule = Rule.create("repo", "key1", "name1").setSeverity(RulePriority.CRITICAL);
profile.activateRule(rule, RulePriority.MINOR);
assertThat(profile.getActiveRule("repo", "key1").getSeverity()).isEqualTo(RulePriority.MINOR);
}
Aggregations