Search in sources :

Example 91 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.

the class QualityProfileChangeEventServiceImpl method distributeRuleChangeEvent.

public void distributeRuleChangeEvent(Collection<QProfileDto> profiles, List<ActiveRuleChange> activeRuleChanges, String language) {
    if (activeRuleChanges.isEmpty()) {
        return;
    }
    Set<RuleChange> activatedRules = new HashSet<>();
    Set<RuleChange> deactivatedRules = new HashSet<>();
    for (ActiveRuleChange arc : activeRuleChanges) {
        ActiveRuleDto activeRule = arc.getActiveRule();
        if (activeRule == null) {
            continue;
        }
        RuleChange ruleChange = new RuleChange();
        ruleChange.setKey(activeRule.getRuleKey().rule());
        ruleChange.setSeverity(arc.getSeverity());
        ruleChange.setLanguage(language);
        Optional<String> templateKey = templateKey(arc);
        templateKey.ifPresent(ruleChange::setTemplateKey);
        // params
        List<ParamChange> paramChanges = new ArrayList<>();
        for (Map.Entry<String, String> entry : arc.getParameters().entrySet()) {
            paramChanges.add(new ParamChange(entry.getKey(), entry.getValue()));
        }
        ruleChange.setParams(paramChanges.toArray(new ParamChange[0]));
        switch(arc.getType()) {
            case ACTIVATED:
            case UPDATED:
                activatedRules.add(ruleChange);
                break;
            case DEACTIVATED:
                deactivatedRules.add(ruleChange);
                break;
        }
    }
    Set<String> projectKeys = getProjectKeys(profiles);
    if (activatedRules.isEmpty() && deactivatedRules.isEmpty()) {
        return;
    }
    RuleSetChangedEvent event = new RuleSetChangedEvent(projectKeys.toArray(new String[0]), activatedRules.toArray(new RuleChange[0]), deactivatedRules.toArray(new RuleChange[0]));
    eventsDistributor.pushEvent(event);
}
Also used : ParamChange(org.sonar.core.util.ParamChange) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) ArrayList(java.util.ArrayList) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) RuleChange(org.sonar.core.util.RuleChange) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) Map(java.util.Map) RuleSetChangedEvent(org.sonar.core.util.RuleSetChangedEvent) HashSet(java.util.HashSet)

Example 92 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.

the class QualityProfileChangeEventServiceImpl method createRuleChanges.

private List<RuleChange> createRuleChanges(@NotNull QProfileDto profileDto) {
    List<RuleChange> ruleChanges = new ArrayList<>();
    try (DbSession dbSession = dbClient.openSession(false)) {
        List<OrgActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByProfile(dbSession, profileDto);
        List<String> activeRuleUuids = activeRuleDtos.stream().map(ActiveRuleDto::getUuid).collect(Collectors.toList());
        Map<String, List<ActiveRuleParamDto>> paramsByActiveRuleUuid = dbClient.activeRuleDao().selectParamsByActiveRuleUuids(dbSession, activeRuleUuids).stream().collect(Collectors.groupingBy(ActiveRuleParamDto::getActiveRuleUuid));
        Map<String, String> activeRuleUuidByRuleUuid = activeRuleDtos.stream().collect(Collectors.toMap(ActiveRuleDto::getRuleUuid, ActiveRuleDto::getUuid));
        List<String> ruleUuids = activeRuleDtos.stream().map(ActiveRuleDto::getRuleUuid).collect(Collectors.toList());
        List<RuleDto> ruleDtos = dbClient.ruleDao().selectByUuids(dbSession, ruleUuids);
        for (RuleDto ruleDto : ruleDtos) {
            String activeRuleUuid = activeRuleUuidByRuleUuid.get(ruleDto.getUuid());
            List<ActiveRuleParamDto> params = paramsByActiveRuleUuid.getOrDefault(activeRuleUuid, new ArrayList<>());
            RuleChange ruleChange = toRuleChange(ruleDto, params);
            ruleChanges.add(ruleChange);
        }
    }
    return ruleChanges;
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ArrayList(java.util.ArrayList) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) RuleChange(org.sonar.core.util.RuleChange) DbSession(org.sonar.db.DbSession) ArrayList(java.util.ArrayList) List(java.util.List)

Example 93 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.

the class QualityProfileChangeEventServiceImplTest method publishRuleActivationToSonarLintClients.

@Test
public void publishRuleActivationToSonarLintClients() {
    ProjectDto projectDao = new ProjectDto();
    QProfileDto activatedQualityProfile = QualityProfileTesting.newQualityProfileDto();
    db.qualityProfiles().insert(activatedQualityProfile);
    RuleDefinitionDto rule1 = db.rules().insert(r -> r.setLanguage("xoo"));
    RuleParamDto rule1Param = db.rules().insertRuleParam(rule1);
    ActiveRuleDto activeRule1 = db.qualityProfiles().activateRule(activatedQualityProfile, rule1);
    ActiveRuleParamDto activeRuleParam1 = ActiveRuleParamDto.createFor(rule1Param).setValue(randomAlphanumeric(20));
    db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule1, activeRuleParam1);
    db.getSession().commit();
    QProfileDto deactivatedQualityProfile = QualityProfileTesting.newQualityProfileDto();
    db.qualityProfiles().insert(deactivatedQualityProfile);
    RuleDefinitionDto rule2 = db.rules().insert(r -> r.setLanguage("xoo"));
    RuleParamDto rule2Param = db.rules().insertRuleParam(rule2);
    ActiveRuleDto activeRule2 = db.qualityProfiles().activateRule(deactivatedQualityProfile, rule2);
    ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(rule2Param).setValue(randomAlphanumeric(20));
    db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule2, activeRuleParam2);
    db.getSession().commit();
    underTest.publishRuleActivationToSonarLintClients(projectDao, activatedQualityProfile, deactivatedQualityProfile);
    ArgumentCaptor<RuleSetChangedEvent> eventCaptor = ArgumentCaptor.forClass(RuleSetChangedEvent.class);
    verify(eventsDistributor).pushEvent(eventCaptor.capture());
    RuleSetChangedEvent ruleSetChangedEvent = eventCaptor.getValue();
    assertThat(ruleSetChangedEvent).isNotNull();
    assertThat(ruleSetChangedEvent).extracting(RuleSetChangedEvent::getEvent, RuleSetChangedEvent::getLanguage, RuleSetChangedEvent::getProjects).containsExactly("RuleSetChanged", "xoo", new String[] { null });
    // activated rule
    assertThat(ruleSetChangedEvent.getActivatedRules()).extracting(RuleChange::getKey, RuleChange::getLanguage, RuleChange::getSeverity, RuleChange::getTemplateKey).containsExactly(tuple(rule1.getRuleKey(), "xoo", rule1.getSeverityString(), null));
    assertThat(ruleSetChangedEvent.getActivatedRules()[0].getParams()).hasSize(1);
    ParamChange actualParamChange = ruleSetChangedEvent.getActivatedRules()[0].getParams()[0];
    assertThat(actualParamChange).extracting(ParamChange::getKey, ParamChange::getValue).containsExactly(activeRuleParam1.getKey(), activeRuleParam1.getValue());
    // deactivated rule
    assertThat(ruleSetChangedEvent.getDeactivatedRules()).extracting(RuleChange::getKey, RuleChange::getLanguage, RuleChange::getSeverity, RuleChange::getTemplateKey).containsExactly(tuple(rule2.getRuleKey(), "xoo", rule2.getSeverityString(), null));
    assertThat(ruleSetChangedEvent.getDeactivatedRules()[0].getParams()).hasSize(1);
    ParamChange actualParamChangeDeactivated = ruleSetChangedEvent.getDeactivatedRules()[0].getParams()[0];
    assertThat(actualParamChangeDeactivated).extracting(ParamChange::getKey, ParamChange::getValue).containsExactly(activeRuleParam2.getKey(), activeRuleParam2.getValue());
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ParamChange(org.sonar.core.util.ParamChange) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleSetChangedEvent(org.sonar.core.util.RuleSetChangedEvent) Test(org.junit.Test)

Example 94 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.

the class ActiveRuleIndexerTest method indexOnStartup_indexes_all_data.

@Test
public void indexOnStartup_indexes_all_data() {
    ActiveRuleDto activeRule = db.qualityProfiles().activateRule(profile1, rule1);
    underTest.indexOnStartup(emptySet());
    List<ActiveRuleDoc> docs = es.getDocuments(TYPE_ACTIVE_RULE, ActiveRuleDoc.class);
    assertThat(docs).hasSize(1);
    verify(docs.get(0), profile1, activeRule);
    assertThatEsQueueTableIsEmpty();
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Test(org.junit.Test)

Example 95 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.

the class ActiveRuleIndexerTest method commitAndIndex_deletes_the_documents_that_dont_exist_in_database.

@Test
public void commitAndIndex_deletes_the_documents_that_dont_exist_in_database() {
    ActiveRuleDto ar = db.qualityProfiles().activateRule(profile1, rule1);
    indexAll();
    assertThat(es.countDocuments(TYPE_ACTIVE_RULE)).isOne();
    db.getDbClient().activeRuleDao().delete(db.getSession(), ar.getKey());
    commitAndIndex(rule1, ar);
    assertThat(es.countDocuments(TYPE_ACTIVE_RULE)).isZero();
    assertThatEsQueueTableIsEmpty();
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Test(org.junit.Test)

Aggregations

ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)112 Test (org.junit.Test)51 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)49 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)26 OrgActiveRuleDto (org.sonar.db.qualityprofile.OrgActiveRuleDto)21 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)21 RuleDto (org.sonar.db.rule.RuleDto)21 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)19 RuleParamDto (org.sonar.db.rule.RuleParamDto)19 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)18 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)15 ActiveRuleChange (org.sonar.server.qualityprofile.ActiveRuleChange)14 RuleKey (org.sonar.api.rule.RuleKey)12 RuleQuery (org.sonar.server.rule.index.RuleQuery)11 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)10 ArrayList (java.util.ArrayList)9 BuiltInQualityProfilesDefinition (org.sonar.api.server.profile.BuiltInQualityProfilesDefinition)9 NewBuiltInQualityProfile (org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.NewBuiltInQualityProfile)9 Map (java.util.Map)7 WsTester (org.sonar.server.ws.WsTester)6