]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9442 Send one notification, if several built-in profiles changed
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 20 Jun 2017 13:25:39 +0000 (15:25 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 29 Jun 2017 15:23:19 +0000 (17:23 +0200)
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesNotificationTest.java

index f13a0e6efbe99dd770399362ba78be5a99263538..804876db07385ea7f6ebf54d1faa7b442e59a6f7 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.qualityprofile;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -69,14 +70,18 @@ public class RegisterQualityProfiles {
 
       Map<QProfileName, RulesProfileDto> persistedRuleProfiles = loadPersistedProfiles(dbSession);
 
+      List<ActiveRuleChange> changes = new ArrayList<>();
       builtInQProfiles.forEach(builtIn -> {
         RulesProfileDto ruleProfile = persistedRuleProfiles.get(builtIn.getQProfileName());
         if (ruleProfile == null) {
           register(dbSession, batchDbSession, builtIn);
         } else {
-          update(dbSession, builtIn, ruleProfile);
+          changes.addAll(update(dbSession, builtIn, ruleProfile));
         }
       });
+      if (!changes.isEmpty()) {
+        builtInQualityProfilesNotification.send();
+      }
     }
     profiler.stopDebug();
   }
@@ -94,14 +99,10 @@ public class RegisterQualityProfiles {
     builtInQProfileInsert.create(dbSession, batchDbSession, builtIn);
   }
 
-  private void update(DbSession dbSession, BuiltInQProfile builtIn, RulesProfileDto ruleProfile) {
+  private List<ActiveRuleChange> update(DbSession dbSession, BuiltInQProfile builtIn, RulesProfileDto ruleProfile) {
     LOGGER.info("Update profile {}", builtIn.getQProfileName());
 
-    List<ActiveRuleChange> activeRuleChanges = builtInQProfileUpdate.update(dbSession, builtIn, ruleProfile);
-
-    if (!activeRuleChanges.isEmpty()) {
-      builtInQualityProfilesNotification.send();
-    }
+    return builtInQProfileUpdate.update(dbSession, builtIn, ruleProfile);
   }
 
   /**
index c9fc944106b455bc479a452c604e842a631b2638..4c2c71cf4d7ee86be68c1ce8730ac00370e9f349 100644 (file)
@@ -115,6 +115,26 @@ public class RegisterQualityProfilesNotificationTest {
     verify(builtInQualityProfilesNotification).send();
   }
 
+  @Test
+  public void only_send_one_notification_when_several_built_in_profiles_contain_new_rules() {
+    String language = newLanguageKey();
+    RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(language));
+    RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(language));
+
+    RulesProfileDto dbProfile1 = insertBuiltInProfile(language);
+    activateRuleInDb(dbProfile1, existingRule, MAJOR);
+    addPluginProfile(dbProfile1, existingRule, newRule);
+
+    RulesProfileDto dbProfile2 = insertBuiltInProfile(language);
+    activateRuleInDb(dbProfile2, existingRule, MAJOR);
+    addPluginProfile(dbProfile2, existingRule, newRule);
+    builtInQProfileRepositoryRule.initialize();
+
+    underTest.start();
+
+    verify(builtInQualityProfilesNotification).send();
+  }
+
   private void addPluginProfile(RulesProfileDto dbProfile, RuleDefinitionDto... dbRules) {
     RulesProfile pluginProfile = RulesProfile.create(dbProfile.getName(), dbProfile.getLanguage());
     Arrays.stream(dbRules).forEach(dbRule -> pluginProfile.activateRule(create(dbRule.getRepositoryKey(), dbRule.getRuleKey()), MAJOR));