Search in sources :

Example 1 with RuleDto

use of org.sonar.db.rule.RuleDto in project sonarqube by SonarSource.

the class IssueDtoTest method set_rule.

@Test
public void set_rule() {
    IssueDto dto = new IssueDto().setKee("100").setRule(new RuleDto().setId(1).setRuleKey("AvoidCycle").setRepositoryKey("squid")).setLanguage("xoo");
    assertThat(dto.getRuleId()).isEqualTo(1);
    assertThat(dto.getRuleRepo()).isEqualTo("squid");
    assertThat(dto.getRule()).isEqualTo("AvoidCycle");
    assertThat(dto.getRuleKey().toString()).isEqualTo("squid:AvoidCycle");
    assertThat(dto.getLanguage()).isEqualTo("xoo");
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) Test(org.junit.Test)

Example 2 with RuleDto

use of org.sonar.db.rule.RuleDto in project sonarqube by SonarSource.

the class RuleRepositoryImpl method loadRulesFromDb.

private void loadRulesFromDb(DbSession dbSession) {
    ImmutableMap.Builder<RuleKey, Rule> rulesByKeyBuilder = ImmutableMap.builder();
    ImmutableMap.Builder<Integer, Rule> rulesByIdBuilder = ImmutableMap.builder();
    for (RuleDto ruleDto : dbClient.ruleDao().selectAll(dbSession)) {
        Rule rule = new RuleImpl(ruleDto);
        rulesByKeyBuilder.put(ruleDto.getKey(), rule);
        rulesByIdBuilder.put(ruleDto.getId(), rule);
    }
    this.rulesByKey = rulesByKeyBuilder.build();
    this.rulesById = rulesByIdBuilder.build();
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) RuleDto(org.sonar.db.rule.RuleDto) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with RuleDto

use of org.sonar.db.rule.RuleDto in project sonarqube by SonarSource.

the class IssueUpdater method saveIssue.

public void saveIssue(DbSession session, DefaultIssue issue, IssueChangeContext context, @Nullable String comment) {
    issueStorage.save(session, issue);
    Optional<RuleDto> rule = getRuleByKey(session, issue.getRuleKey());
    ComponentDto project = dbClient.componentDao().selectOrFailByUuid(session, issue.projectUuid());
    notificationService.scheduleForSending(new IssueChangeNotification().setIssue(issue).setChangeAuthorLogin(context.login()).setRuleName(rule.isPresent() ? rule.get().getName() : null).setProject(project.getKey(), project.name()).setComponent(dbClient.componentDao().selectOrFailByUuid(session, issue.componentUuid())).setComment(comment));
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) ComponentDto(org.sonar.db.component.ComponentDto) IssueChangeNotification(org.sonar.server.issue.notification.IssueChangeNotification)

Example 4 with RuleDto

use of org.sonar.db.rule.RuleDto in project sonarqube by SonarSource.

the class ActiveRuleCompleter method writeActiveRules.

private Collection<String> writeActiveRules(DbSession dbSession, SearchResponse.Builder response, RuleQuery query, List<RuleDto> rules) {
    Collection<String> qProfileKeys = newHashSet();
    Rules.Actives.Builder activesBuilder = response.getActivesBuilder();
    String profileKey = query.getQProfileKey();
    if (profileKey != null) {
        // Load details of active rules on the selected profile
        List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByProfileKey(dbSession, profileKey);
        Map<RuleKey, ActiveRuleDto> activeRuleByRuleKey = from(activeRuleDtos).uniqueIndex(ActiveRuleToRuleKey.INSTANCE);
        ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = activeRuleDtosToActiveRuleParamDtos(dbSession, activeRuleDtos);
        for (RuleDto rule : rules) {
            ActiveRuleDto activeRule = activeRuleByRuleKey.get(rule.getKey());
            if (activeRule != null) {
                qProfileKeys = writeActiveRules(rule.getKey(), singletonList(activeRule), activeRuleParamsByActiveRuleKey, activesBuilder);
            }
        }
    } else {
        // Load details of all active rules
        List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByRuleIds(dbSession, Lists.transform(rules, RuleDto::getId));
        Multimap<RuleKey, ActiveRuleDto> activeRulesByRuleKey = from(activeRuleDtos).index(ActiveRuleToRuleKey.INSTANCE);
        ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = activeRuleDtosToActiveRuleParamDtos(dbSession, activeRuleDtos);
        for (RuleDto rule : rules) {
            qProfileKeys = writeActiveRules(rule.getKey(), activeRulesByRuleKey.get(rule.getKey()), activeRuleParamsByActiveRuleKey, activesBuilder);
        }
    }
    response.setActives(activesBuilder);
    return qProfileKeys;
}
Also used : ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey)

Example 5 with RuleDto

use of org.sonar.db.rule.RuleDto in project sonarqube by SonarSource.

the class RegisterRules method start.

@Override
public void start() {
    Profiler profiler = Profiler.create(LOG).startInfo("Register rules");
    DbSession session = dbClient.openSession(false);
    try {
        Map<RuleKey, RuleDto> allRules = loadRules(session);
        RulesDefinition.Context context = defLoader.load();
        for (RulesDefinition.ExtendedRepository repoDef : getRepositories(context)) {
            if (languages.get(repoDef.language()) != null) {
                for (RulesDefinition.Rule ruleDef : repoDef.rules()) {
                    registerRule(ruleDef, allRules, session);
                }
                session.commit();
            }
        }
        List<RuleDto> activeRules = processRemainingDbRules(allRules.values(), session);
        List<ActiveRuleChange> changes = removeActiveRulesOnStillExistingRepositories(session, activeRules, context);
        session.commit();
        persistRepositories(session, context.repositories());
        ruleIndexer.index();
        activeRuleIndexer.index(changes);
        profiler.stopDebug();
    } finally {
        session.close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) Profiler(org.sonar.api.utils.log.Profiler) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange)

Aggregations

RuleDto (org.sonar.db.rule.RuleDto)197 Test (org.junit.Test)140 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)80 ComponentDto (org.sonar.db.component.ComponentDto)47 WsTester (org.sonar.server.ws.WsTester)38 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)29 RuleParamDto (org.sonar.db.rule.RuleParamDto)29 IssueDto (org.sonar.db.issue.IssueDto)28 RuleKey (org.sonar.api.rule.RuleKey)24 SearchOptions (org.sonar.server.es.SearchOptions)16 RuleQuery (org.sonar.server.rule.index.RuleQuery)16 BadRequestException (org.sonar.server.exceptions.BadRequestException)15 RuleTesting.newRuleDto (org.sonar.db.rule.RuleTesting.newRuleDto)14 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)12 IssueIndexer (org.sonar.server.issue.index.IssueIndexer)10 Date (java.util.Date)9 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)8 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)8 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)7 ArrayList (java.util.ArrayList)6