use of org.sonar.core.util.ParamChange in project sonarqube by SonarSource.
the class SonarLintClientsRegistryTest method createRuleChange.
private RuleChange createRuleChange() {
RuleChange javaRule = new RuleChange();
javaRule.setLanguage("java");
javaRule.setParams(new ParamChange[] { new ParamChange("param-key", "param-value") });
javaRule.setTemplateKey("template-key");
javaRule.setSeverity(Severity.CRITICAL);
javaRule.setKey("rule-key");
return javaRule;
}
use of org.sonar.core.util.ParamChange in project sonarqube by SonarSource.
the class QualityProfileChangeEventServiceImplTest method distributeRuleChangeEvent.
@Test
public void distributeRuleChangeEvent() {
QProfileDto qualityProfileDto = QualityProfileTesting.newQualityProfileDto();
// Template rule
RuleDto templateRule = newTemplateRule(RuleKey.of("xoo", "template-key"));
db.rules().insert(templateRule.getDefinition());
// Custom rule
RuleDefinitionDto rule1 = newCustomRule(templateRule.getDefinition()).setLanguage("xoo").setDescription("<div>line1\nline2</div>").setDescriptionFormat(MARKDOWN);
db.rules().insert(rule1);
ActiveRuleDto activeRuleDto = ActiveRuleDto.createFor(qualityProfileDto, rule1);
ActiveRuleChange activeRuleChange = new ActiveRuleChange(ACTIVATED, activeRuleDto, rule1);
activeRuleChange.setParameter("paramChangeKey", "paramChangeValue");
Collection<QProfileDto> profiles = Collections.singleton(qualityProfileDto);
ProjectDto project = db.components().insertPrivateProjectDto();
db.qualityProfiles().associateWithProject(project, qualityProfileDto);
underTest.distributeRuleChangeEvent(profiles, of(activeRuleChange), "xoo");
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[] { project.getKey() });
assertThat(ruleSetChangedEvent.getActivatedRules()).extracting(RuleChange::getKey, RuleChange::getLanguage, RuleChange::getSeverity, RuleChange::getTemplateKey).containsExactly(tuple(rule1.getRuleKey(), "xoo", null, "template-key"));
assertThat(ruleSetChangedEvent.getActivatedRules()[0].getParams()).hasSize(1);
ParamChange actualParamChange = ruleSetChangedEvent.getActivatedRules()[0].getParams()[0];
assertThat(actualParamChange).extracting(ParamChange::getKey, ParamChange::getValue).containsExactly("paramChangeKey", "paramChangeValue");
assertThat(ruleSetChangedEvent.getDeactivatedRules()).isEmpty();
}
use of org.sonar.core.util.ParamChange in project sonarqube by SonarSource.
the class QualityProfileChangeEventServiceImpl method toRuleChange.
@NotNull
private RuleChange toRuleChange(RuleDto ruleDto, List<ActiveRuleParamDto> activeRuleParamDtos) {
RuleChange ruleChange = new RuleChange();
ruleChange.setKey(ruleDto.getRuleKey());
ruleChange.setLanguage(ruleDto.getLanguage());
ruleChange.setSeverity(ruleDto.getSeverityString());
List<ParamChange> paramChanges = new ArrayList<>();
for (ActiveRuleParamDto activeRuleParam : activeRuleParamDtos) {
paramChanges.add(new ParamChange(activeRuleParam.getKey(), activeRuleParam.getValue()));
}
ruleChange.setParams(paramChanges.toArray(new ParamChange[0]));
String templateUuid = ruleDto.getTemplateUuid();
if (templateUuid != null && !"".equals(templateUuid)) {
try (DbSession dbSession = dbClient.openSession(false)) {
RuleDto templateRule = dbClient.ruleDao().selectByUuid(templateUuid, dbSession).orElseThrow(() -> new IllegalStateException(String.format("Unknown Template Rule '%s'", templateUuid)));
ruleChange.setTemplateKey(templateRule.getRuleKey());
}
}
return ruleChange;
}
use of org.sonar.core.util.ParamChange 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.core.util.ParamChange in project sonarqube by SonarSource.
the class SonarLintClientsRegistry method toJson.
private static JSONObject toJson(RuleChange rule) {
JSONObject ruleJson = new JSONObject();
ruleJson.put("key", rule.getKey());
ruleJson.put("language", rule.getLanguage());
ruleJson.put("severity", rule.getSeverity());
ruleJson.put("templateKey", rule.getTemplateKey());
JSONArray params = new JSONArray();
for (ParamChange paramChange : rule.getParams()) {
params.put(toJson(paramChange));
}
ruleJson.put("params", params);
return ruleJson;
}
Aggregations