Search in sources :

Example 66 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class RegisterRules method registerRule.

private void registerRule(RulesDefinition.Rule ruleDef, Map<RuleKey, RuleDto> allRules, DbSession session) {
    RuleKey ruleKey = RuleKey.of(ruleDef.repository().key(), ruleDef.key());
    RuleDto rule = allRules.containsKey(ruleKey) ? allRules.remove(ruleKey) : createRuleDto(ruleDef, session);
    boolean executeUpdate = false;
    if (mergeRule(ruleDef, rule)) {
        executeUpdate = true;
    }
    if (mergeDebtDefinitions(ruleDef, rule)) {
        executeUpdate = true;
    }
    if (mergeTags(ruleDef, rule)) {
        executeUpdate = true;
    }
    if (executeUpdate) {
        update(session, rule);
    }
    mergeParams(ruleDef, rule, session);
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto)

Example 67 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class IssuePatternTest method match_rule.

@Test
public void match_rule() {
    RuleKey rule = Rule.create("checkstyle", "IllegalRegexp", "").ruleKey();
    assertThat(new IssuePattern("*", "*").matchRule(rule)).isTrue();
    assertThat(new IssuePattern("*", "checkstyle:*").matchRule(rule)).isTrue();
    assertThat(new IssuePattern("*", "checkstyle:IllegalRegexp").matchRule(rule)).isTrue();
    assertThat(new IssuePattern("*", "checkstyle:Illegal*").matchRule(rule)).isTrue();
    assertThat(new IssuePattern("*", "*:*Illegal*").matchRule(rule)).isTrue();
    assertThat(new IssuePattern("*", "pmd:IllegalRegexp").matchRule(rule)).isFalse();
    assertThat(new IssuePattern("*", "pmd:*").matchRule(rule)).isFalse();
    assertThat(new IssuePattern("*", "*:Foo*IllegalRegexp").matchRule(rule)).isFalse();
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) Test(org.junit.Test)

Example 68 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class RuleActivatorMediumTest method verifyHasActiveRuleInIndex.

private void verifyHasActiveRuleInIndex(ActiveRuleKey activeRuleKey, String expectedSeverity, @Nullable String expectedInheritance) {
    // verify es
    List<RuleKey> ruleKeys = newArrayList(tester.get(RuleIndex.class).searchAll(new RuleQuery().setKey(activeRuleKey.ruleKey().toString()).setQProfileKey(activeRuleKey.qProfile()).setActivation(true).setInheritance(singleton(expectedInheritance == null ? ActiveRule.Inheritance.NONE.name() : ActiveRule.Inheritance.valueOf(expectedInheritance).name())).setActiveSeverities(singleton(expectedSeverity))));
    assertThat(ruleKeys).as("Rule is not activated in index").hasSize(1);
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleQuery(org.sonar.server.rule.index.RuleQuery)

Example 69 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class UpdateActionMediumTest method update_custom_rule.

@Test
public void update_custom_rule() throws Exception {
    // Template rule
    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001"));
    ruleDao.insert(session, templateRule);
    RuleParamDto param = RuleParamDto.createFor(templateRule).setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*");
    ruleDao.insertRuleParam(session, templateRule, param);
    session.commit();
    // Custom rule
    NewCustomRule newRule = NewCustomRule.createForCustomRule("MY_CUSTOM", templateRule.getKey()).setName("Old custom").setHtmlDescription("Old description").setSeverity(Severity.MINOR).setStatus(RuleStatus.BETA).setParameters(ImmutableMap.of("regex", "a"));
    RuleKey customRuleKey = tester.get(RuleCreator.class).create(newRule);
    session.clearCache();
    WsTester.TestRequest request = wsTester.newPostRequest("api/rules", "update").setParam("key", customRuleKey.toString()).setParam("name", "My custom rule").setParam("markdown_description", "Description").setParam("severity", "MAJOR").setParam("status", "BETA").setParam("params", "regex=a.*");
    request.execute().assertJson(getClass(), "update_custom_rule.json");
}
Also used : WsTester(org.sonar.server.ws.WsTester) RuleDto(org.sonar.db.rule.RuleDto) RuleKey(org.sonar.api.rule.RuleKey) NewCustomRule(org.sonar.server.rule.NewCustomRule) RuleParamDto(org.sonar.db.rule.RuleParamDto) RuleCreator(org.sonar.server.rule.RuleCreator) Test(org.junit.Test)

Example 70 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class OneIssuePerLineSensor method createIssues.

private void createIssues(InputFile file, SensorContext context, String repo) {
    RuleKey ruleKey = RuleKey.of(repo, RULE_KEY);
    String severity = context.settings().getString(FORCE_SEVERITY_PROPERTY);
    for (int line = 1; line <= file.lines(); line++) {
        NewIssue newIssue = context.newIssue();
        newIssue.forRule(ruleKey).at(newIssue.newLocation().on(file).at(file.selectLine(line)).message("This issue is generated on each line")).overrideSeverity(severity != null ? Severity.valueOf(severity) : null);
        if (context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(5, 5))) {
            newIssue.gap(context.settings().getDouble(EFFORT_TO_FIX_PROPERTY));
        } else {
            newIssue.effortToFix(context.settings().getDouble(EFFORT_TO_FIX_PROPERTY));
        }
        newIssue.save();
    }
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue)

Aggregations

RuleKey (org.sonar.api.rule.RuleKey)95 Test (org.junit.Test)48 RuleDto (org.sonar.db.rule.RuleDto)24 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)22 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)17 SearchOptions (org.sonar.server.es.SearchOptions)14 RuleParamDto (org.sonar.db.rule.RuleParamDto)10 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)9 ArrayList (java.util.ArrayList)5 DbSession (org.sonar.db.DbSession)5 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)5 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)4 ComponentDto (org.sonar.db.component.ComponentDto)4 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)4 Rule (org.sonar.api.batch.rule.Rule)3 WildcardPattern (org.sonar.api.utils.WildcardPattern)3 DefaultIssue (org.sonar.core.issue.DefaultIssue)3 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)3 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)3 IssuePattern (org.sonar.scanner.issue.ignore.pattern.IssuePattern)3