From 2f3ccdc9055cf911711bff699849c8f0002eb20f Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 20 Jun 2017 15:25:39 +0200 Subject: [PATCH] SONAR-9442 Send one notification, if several built-in profiles changed --- .../RegisterQualityProfiles.java | 15 +++++++------- ...gisterQualityProfilesNotificationTest.java | 20 +++++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java index f13a0e6efbe..804876db073 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java @@ -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 persistedRuleProfiles = loadPersistedProfiles(dbSession); + List 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 update(DbSession dbSession, BuiltInQProfile builtIn, RulesProfileDto ruleProfile) { LOGGER.info("Update profile {}", builtIn.getQProfileName()); - List activeRuleChanges = builtInQProfileUpdate.update(dbSession, builtIn, ruleProfile); - - if (!activeRuleChanges.isEmpty()) { - builtInQualityProfilesNotification.send(); - } + return builtInQProfileUpdate.update(dbSession, builtIn, ruleProfile); } /** diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesNotificationTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesNotificationTest.java index c9fc944106b..4c2c71cf4d7 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesNotificationTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesNotificationTest.java @@ -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)); -- 2.39.5