Search in sources :

Example 26 with Rule

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;
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) ArrayList(java.util.ArrayList) RuleParamDto(org.sonar.db.rule.RuleParamDto) Rule(org.sonar.api.rules.Rule) Date(java.util.Date)

Example 27 with Rule

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);
}
Also used : HashMap(java.util.HashMap) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Rule(org.sonar.api.rules.Rule)

Example 28 with Rule

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();
}
Also used : Rule(org.sonar.api.rules.Rule) Arrays(java.util.Arrays) RuleQuery(org.sonar.api.rules.RuleQuery) RuleDao(org.sonar.db.rule.RuleDao) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) DbSession(org.sonar.db.DbSession) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Nullable(javax.annotation.Nullable) Before(org.junit.Before) DbTester(org.sonar.db.DbTester) RuleTesting(org.sonar.db.rule.RuleTesting) System2(org.sonar.api.utils.System2) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) AlwaysIncreasingSystem2(org.sonar.api.impl.utils.AlwaysIncreasingSystem2) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) DbClient(org.sonar.db.DbClient) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) RuleParamDto(org.sonar.db.rule.RuleParamDto) RuleKey(org.sonar.api.rule.RuleKey) Mockito.mock(org.mockito.Mockito.mock) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 29 with Rule

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;
}
Also used : ActiveRule(org.sonar.api.rules.ActiveRule) RulesProfile(org.sonar.api.profiles.RulesProfile) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) Rule(org.sonar.api.rules.Rule) ActiveRule(org.sonar.api.rules.ActiveRule) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto)

Example 30 with Rule

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);
}
Also used : Rule(org.sonar.api.rules.Rule) Test(org.junit.Test)

Aggregations

Rule (org.sonar.api.rules.Rule)31 Test (org.junit.Test)22 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)11 RuleParamDto (org.sonar.db.rule.RuleParamDto)10 List (java.util.List)9 DbSession (org.sonar.db.DbSession)9 Arrays (java.util.Arrays)8 Consumer (java.util.function.Consumer)8 Collectors.toList (java.util.stream.Collectors.toList)8 Nullable (javax.annotation.Nullable)8 RandomStringUtils.randomAlphabetic (org.apache.commons.lang.RandomStringUtils.randomAlphabetic)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 Before (org.junit.Before)8 ArgumentMatchers.anyBoolean (org.mockito.ArgumentMatchers.anyBoolean)8 Mockito.mock (org.mockito.Mockito.mock)8 Mockito.verify (org.mockito.Mockito.verify)8 Mockito.verifyNoMoreInteractions (org.mockito.Mockito.verifyNoMoreInteractions)8 Mockito.when (org.mockito.Mockito.when)8 AlwaysIncreasingSystem2 (org.sonar.api.impl.utils.AlwaysIncreasingSystem2)8 RuleKey (org.sonar.api.rule.RuleKey)8