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);
}
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;
}
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());
}
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();
}
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();
}
Aggregations