aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-webserver-pushapi/src
diff options
context:
space:
mode:
authorDamien Urruty <damien.urruty@sonarsource.com>2022-03-01 09:43:18 +0100
committersonartech <sonartech@sonarsource.com>2022-03-03 09:54:42 +0000
commit042825ec38a115983cbdced7a8b5b1d56aa05e74 (patch)
tree2a542ee829f55c4dc0047d7ba2c235bcc35dc069 /server/sonar-webserver-pushapi/src
parentbb4cc98d5f0b52c3caeb52fc4f772248be8d95fa (diff)
downloadsonarqube-042825ec38a115983cbdced7a8b5b1d56aa05e74.tar.gz
sonarqube-042825ec38a115983cbdced7a8b5b1d56aa05e74.zip
SONAR-15919 Send only rule keys for deactivated rules in RuleSetChanged
Diffstat (limited to 'server/sonar-webserver-pushapi/src')
-rw-r--r--server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/qualityprofile/QualityProfileChangeEventServiceImpl.java53
-rw-r--r--server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/sonarlint/SonarLintClientsRegistry.java6
-rw-r--r--server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/sonarlint/SonarLintClientsRegistryTest.java26
-rw-r--r--server/sonar-webserver-pushapi/src/test/java/org/sonar/server/qualityprofile/builtin/QualityProfileChangeEventServiceImplTest.java11
-rw-r--r--server/sonar-webserver-pushapi/src/test/resources/org/sonar/server/pushapi/sonarlint/rule-change-event-data.json13
5 files changed, 56 insertions, 53 deletions
diff --git a/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/qualityprofile/QualityProfileChangeEventServiceImpl.java b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/qualityprofile/QualityProfileChangeEventServiceImpl.java
index c5386a36ef7..75cd84956f5 100644
--- a/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/qualityprofile/QualityProfileChangeEventServiceImpl.java
+++ b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/qualityprofile/QualityProfileChangeEventServiceImpl.java
@@ -24,12 +24,14 @@ import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
+import org.sonar.api.rule.RuleKey;
import org.sonar.api.server.ServerSide;
import org.sonar.core.util.ParamChange;
import org.sonar.core.util.RuleChange;
@@ -45,6 +47,11 @@ import org.sonar.db.qualityprofile.QProfileDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.server.qualityprofile.ActiveRuleChange;
+import static java.util.function.Predicate.not;
+import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.ACTIVATED;
+import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.DEACTIVATED;
+import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.UPDATED;
+
@ServerSide
public class QualityProfileChangeEventServiceImpl implements QualityProfileChangeEventService {
@@ -59,21 +66,23 @@ public class QualityProfileChangeEventServiceImpl implements QualityProfileChang
@Override
public void publishRuleActivationToSonarLintClients(ProjectDto project, @Nullable QProfileDto activatedProfile, @Nullable QProfileDto deactivatedProfile) {
List<RuleChange> activatedRules = new ArrayList<>();
- List<RuleChange> deactivatedRules = new ArrayList<>();
+ Set<String> deactivatedRules = new HashSet<>();
if (activatedProfile != null) {
activatedRules.addAll(createRuleChanges(activatedProfile));
}
if (deactivatedProfile != null) {
- deactivatedRules.addAll(createRuleChanges(deactivatedProfile));
+ deactivatedRules.addAll(getRuleKeys(deactivatedProfile));
}
if (activatedRules.isEmpty() && deactivatedRules.isEmpty()) {
return;
}
- RuleSetChangedEvent event = new RuleSetChangedEvent(new String[] {project.getKey()}, activatedRules.toArray(new RuleChange[0]), deactivatedRules.toArray(new RuleChange[0]));
+ String language = activatedProfile != null ? activatedProfile.getLanguage() : deactivatedProfile.getLanguage();
+ RuleSetChangedEvent event = new RuleSetChangedEvent(new String[] {project.getKey()}, activatedRules.toArray(new RuleChange[0]), deactivatedRules.toArray(new String[0]),
+ language);
eventsDistributor.pushEvent(event);
}
@@ -102,6 +111,22 @@ public class QualityProfileChangeEventServiceImpl implements QualityProfileChang
return ruleChanges;
}
+ private Set<String> getRuleKeys(@NotNull QProfileDto profileDto) {
+ Set<String> ruleKeys = new HashSet<>();
+
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ List<OrgActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByProfile(dbSession, profileDto);
+
+ List<String> ruleUuids = activeRuleDtos.stream().map(ActiveRuleDto::getRuleUuid).collect(Collectors.toList());
+ List<RuleDto> ruleDtos = dbClient.ruleDao().selectByUuids(dbSession, ruleUuids);
+
+ for (RuleDto ruleDto : ruleDtos) {
+ ruleKeys.add(ruleDto.getRuleKey());
+ }
+ }
+ return ruleKeys;
+ }
+
@NotNull
private RuleChange toRuleChange(RuleDto ruleDto, List<ActiveRuleParamDto> activeRuleParamDtos) {
RuleChange ruleChange = new RuleChange();
@@ -133,7 +158,6 @@ public class QualityProfileChangeEventServiceImpl implements QualityProfileChang
}
Set<RuleChange> activatedRules = new HashSet<>();
- Set<RuleChange> deactivatedRules = new HashSet<>();
for (ActiveRuleChange arc : activeRuleChanges) {
ActiveRuleDto activeRule = arc.getActiveRule();
@@ -156,24 +180,27 @@ public class QualityProfileChangeEventServiceImpl implements QualityProfileChang
}
ruleChange.setParams(paramChanges.toArray(new ParamChange[0]));
- switch (arc.getType()) {
- case ACTIVATED:
- case UPDATED:
- activatedRules.add(ruleChange);
- break;
- case DEACTIVATED:
- deactivatedRules.add(ruleChange);
- break;
+ if (ACTIVATED.equals(arc.getType()) || UPDATED.equals(arc.getType())) {
+ activatedRules.add(ruleChange);
}
}
+ Set<String> deactivatedRules = activeRuleChanges.stream()
+ .filter(r -> DEACTIVATED.equals(r.getType()))
+ .map(ActiveRuleChange::getActiveRule)
+ .filter(not(Objects::isNull))
+ .map(ActiveRuleDto::getRuleKey)
+ .map(RuleKey::rule)
+ .collect(Collectors.toSet());
+
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]));
+ RuleSetChangedEvent event = new RuleSetChangedEvent(projectKeys.toArray(new String[0]), activatedRules.toArray(new RuleChange[0]), deactivatedRules.toArray(new String[0]),
+ language);
eventsDistributor.pushEvent(event);
}
diff --git a/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/sonarlint/SonarLintClientsRegistry.java b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/sonarlint/SonarLintClientsRegistry.java
index fa712ace19c..88d87433773 100644
--- a/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/sonarlint/SonarLintClientsRegistry.java
+++ b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/sonarlint/SonarLintClientsRegistry.java
@@ -109,7 +109,7 @@ public class SonarLintClientsRegistry implements RuleActivationListener {
try {
sonarLintClientPermissionsValidator.validateUserCanReceivePushEventForProjects(c.getUserUuid(), projectKeysInterestingForClient);
RuleSetChangedEvent personalizedEvent = new RuleSetChangedEvent(projectKeysInterestingForClient.toArray(String[]::new), event.getActivatedRules(),
- event.getDeactivatedRules());
+ event.getDeactivatedRules(), event.getLanguage());
String message = getMessage(personalizedEvent);
c.writeAndFlush(message);
} catch (ForbiddenException forbiddenException) {
@@ -137,8 +137,8 @@ public class SonarLintClientsRegistry implements RuleActivationListener {
data.put("activatedRules", activatedRulesJson);
JSONArray deactivatedRulesJson = new JSONArray();
- for (RuleChange rule : ruleSetChangedEvent.getDeactivatedRules()) {
- deactivatedRulesJson.put(toJson(rule));
+ for (String ruleKey : ruleSetChangedEvent.getDeactivatedRules()) {
+ deactivatedRulesJson.put(ruleKey);
}
data.put("deactivatedRules", deactivatedRulesJson);
diff --git a/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/sonarlint/SonarLintClientsRegistryTest.java b/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/sonarlint/SonarLintClientsRegistryTest.java
index d82e71a2fcb..f423dd7096b 100644
--- a/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/sonarlint/SonarLintClientsRegistryTest.java
+++ b/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/sonarlint/SonarLintClientsRegistryTest.java
@@ -104,8 +104,8 @@ public class SonarLintClientsRegistryTest {
RuleChange javaRule = createRuleChange();
RuleChange[] activatedRules = {javaRule};
- RuleChange[] deactivatedRules = {javaRule};
- RuleSetChangedEvent ruleSetChangedEvent = new RuleSetChangedEvent(exampleKeys.toArray(String[]::new), activatedRules, deactivatedRules);
+ String[] deactivatedRules = {"rule-key"};
+ RuleSetChangedEvent ruleSetChangedEvent = new RuleSetChangedEvent(exampleKeys.toArray(String[]::new), activatedRules, deactivatedRules, "java");
underTest.listen(ruleSetChangedEvent);
ArgumentCaptor<byte[]> captor = ArgumentCaptor.forClass(byte[].class);
@@ -125,11 +125,9 @@ public class SonarLintClientsRegistryTest {
underTest.registerClient(sonarLintClient);
- RuleChange javaRuleChange = createRuleChange();
-
RuleChange[] activatedRules = {};
- RuleChange[] deactivatedRules = {javaRuleChange};
- RuleSetChangedEvent ruleSetChangedEvent = new RuleSetChangedEvent(exampleKeys.toArray(String[]::new), activatedRules, deactivatedRules);
+ String[] deactivatedRules = {"rule-key"};
+ RuleSetChangedEvent ruleSetChangedEvent = new RuleSetChangedEvent(exampleKeys.toArray(String[]::new), activatedRules, deactivatedRules, "java");
underTest.listen(ruleSetChangedEvent);
verifyNoInteractions(outputStream);
@@ -145,11 +143,9 @@ public class SonarLintClientsRegistryTest {
underTest.registerClient(sonarLintClient);
- RuleChange javaRuleChange = createRuleChange();
-
RuleChange[] activatedRules = {};
- RuleChange[] deactivatedRules = {javaRuleChange};
- RuleSetChangedEvent ruleSetChangedEvent = new RuleSetChangedEvent(eventProjectKeys.toArray(String[]::new), activatedRules, deactivatedRules);
+ String[] deactivatedRules = {"rule-key"};
+ RuleSetChangedEvent ruleSetChangedEvent = new RuleSetChangedEvent(eventProjectKeys.toArray(String[]::new), activatedRules, deactivatedRules, "java");
underTest.listen(ruleSetChangedEvent);
ArgumentCaptor<Set<String>> argument = ArgumentCaptor.forClass(Set.class);
@@ -159,10 +155,9 @@ public class SonarLintClientsRegistryTest {
@Test
public void listen_givenUserNotPermittedToReceiveEvent_closeConnection() {
- RuleChange javaRuleChange = createRuleChange();
RuleChange[] activatedRules = {};
- RuleChange[] deactivatedRules = {javaRuleChange};
- RuleSetChangedEvent ruleSetChangedEvent = new RuleSetChangedEvent(exampleKeys.toArray(String[]::new), activatedRules, deactivatedRules);
+ String[] deactivatedRules = {"rule-key"};
+ RuleSetChangedEvent ruleSetChangedEvent = new RuleSetChangedEvent(exampleKeys.toArray(String[]::new), activatedRules, deactivatedRules, "java");
SonarLintClient sonarLintClient = createSampleSLClient();
underTest.registerClient(sonarLintClient);
@@ -175,10 +170,9 @@ public class SonarLintClientsRegistryTest {
@Test
public void listen_givenUnregisteredClient_closeConnection() throws IOException {
- RuleChange javaRuleChange = createRuleChange();
RuleChange[] activatedRules = {};
- RuleChange[] deactivatedRules = {javaRuleChange};
- RuleSetChangedEvent ruleSetChangedEvent = new RuleSetChangedEvent(exampleKeys.toArray(String[]::new), activatedRules, deactivatedRules);
+ String[] deactivatedRules = {"rule-key"};
+ RuleSetChangedEvent ruleSetChangedEvent = new RuleSetChangedEvent(exampleKeys.toArray(String[]::new), activatedRules, deactivatedRules, "java");
SonarLintClient sonarLintClient = createSampleSLClient();
underTest.registerClient(sonarLintClient);
diff --git a/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/qualityprofile/builtin/QualityProfileChangeEventServiceImplTest.java b/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/qualityprofile/builtin/QualityProfileChangeEventServiceImplTest.java
index b72c6961979..79900642530 100644
--- a/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/qualityprofile/builtin/QualityProfileChangeEventServiceImplTest.java
+++ b/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/qualityprofile/builtin/QualityProfileChangeEventServiceImplTest.java
@@ -115,6 +115,7 @@ public class QualityProfileChangeEventServiceImplTest {
public void publishRuleActivationToSonarLintClients() {
ProjectDto projectDao = new ProjectDto();
QProfileDto activatedQualityProfile = QualityProfileTesting.newQualityProfileDto();
+ activatedQualityProfile.setLanguage("xoo");
db.qualityProfiles().insert(activatedQualityProfile);
RuleDefinitionDto rule1 = db.rules().insert(r -> r.setLanguage("xoo"));
RuleParamDto rule1Param = db.rules().insertRuleParam(rule1);
@@ -159,15 +160,7 @@ public class QualityProfileChangeEventServiceImplTest {
// 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());
+ .containsExactly(rule2.getRuleKey());
}
}
diff --git a/server/sonar-webserver-pushapi/src/test/resources/org/sonar/server/pushapi/sonarlint/rule-change-event-data.json b/server/sonar-webserver-pushapi/src/test/resources/org/sonar/server/pushapi/sonarlint/rule-change-event-data.json
index b70bd0dd2ab..9f157f28479 100644
--- a/server/sonar-webserver-pushapi/src/test/resources/org/sonar/server/pushapi/sonarlint/rule-change-event-data.json
+++ b/server/sonar-webserver-pushapi/src/test/resources/org/sonar/server/pushapi/sonarlint/rule-change-event-data.json
@@ -19,17 +19,6 @@
}
],
"deactivatedRules": [
- {
- "key": "rule-key",
- "templateKey": "template-key",
- "severity": "CRITICAL",
- "language": "java",
- "params": [
- {
- "value": "param-value",
- "key": "param-key"
- }
- ]
- }
+ "rule-key"
]
}