use of org.sonar.core.util.RuleChange 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.RuleChange in project sonarqube by SonarSource.
the class SonarLintClientsRegistry method toJson.
private static String toJson(RuleSetChangedEvent ruleSetChangedEvent) {
JSONObject data = new JSONObject();
data.put("projects", ruleSetChangedEvent.getProjects());
JSONArray activatedRulesJson = new JSONArray();
for (RuleChange rule : ruleSetChangedEvent.getActivatedRules()) {
activatedRulesJson.put(toJson(rule));
}
data.put("activatedRules", activatedRulesJson);
JSONArray deactivatedRulesJson = new JSONArray();
for (RuleChange rule : ruleSetChangedEvent.getDeactivatedRules()) {
deactivatedRulesJson.put(toJson(rule));
}
data.put("deactivatedRules", deactivatedRulesJson);
return data.toString();
}
use of org.sonar.core.util.RuleChange 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.core.util.RuleChange in project sonarqube by SonarSource.
the class SonarLintClientsRegistryTest method listen_givenOneClientInterestedInProjA_DontCheckPermissionsForProjB.
@Test
public void listen_givenOneClientInterestedInProjA_DontCheckPermissionsForProjB() throws IOException {
when(defaultAsyncContext.getResponse()).thenReturn(response);
when(response.getOutputStream()).thenReturn(outputStream);
Set<String> clientProjectKeys = Set.of("projA");
Set<String> eventProjectKeys = Set.of("projA", "projB");
SonarLintClient sonarLintClient = new SonarLintClient(defaultAsyncContext, clientProjectKeys, Set.of("java"), USER_UUID);
underTest.registerClient(sonarLintClient);
RuleChange javaRuleChange = createRuleChange();
RuleChange[] activatedRules = {};
RuleChange[] deactivatedRules = { javaRuleChange };
RuleSetChangedEvent ruleSetChangedEvent = new RuleSetChangedEvent(eventProjectKeys.toArray(String[]::new), activatedRules, deactivatedRules);
underTest.listen(ruleSetChangedEvent);
ArgumentCaptor<Set<String>> argument = ArgumentCaptor.forClass(Set.class);
verify(permissionsValidator).validateUserCanReceivePushEventForProjects(anyString(), argument.capture());
assertThat(argument.getValue()).isEqualTo(clientProjectKeys);
}
use of org.sonar.core.util.RuleChange in project sonarqube by SonarSource.
the class SonarLintClientsRegistryTest method listen_givenOneClientInterestedInJsEventsAndJavaEventGenerated_sendZeroEvents.
@Test
public void listen_givenOneClientInterestedInJsEventsAndJavaEventGenerated_sendZeroEvents() throws IOException {
Set<String> jsLanguageKey = Set.of("js");
when(defaultAsyncContext.getResponse()).thenReturn(response);
when(response.getOutputStream()).thenReturn(outputStream);
SonarLintClient sonarLintClient = new SonarLintClient(defaultAsyncContext, exampleKeys, jsLanguageKey, USER_UUID);
underTest.registerClient(sonarLintClient);
RuleChange javaRuleChange = createRuleChange();
RuleChange[] activatedRules = {};
RuleChange[] deactivatedRules = { javaRuleChange };
RuleSetChangedEvent ruleSetChangedEvent = new RuleSetChangedEvent(exampleKeys.toArray(String[]::new), activatedRules, deactivatedRules);
underTest.listen(ruleSetChangedEvent);
verifyNoInteractions(outputStream);
}
Aggregations