From 6c95411c7beea684ba428d44fbf0dbd967f96c0e Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 25 Apr 2014 10:43:02 +0200 Subject: [PATCH] SONAR-4764 Restore default profiles from ui --- .../resources/org/sonar/l10n/core.properties | 5 +++ .../server/qualityprofile/QProfileBackup.java | 26 +++++++++----- .../app/controllers/profiles_controller.rb | 23 ++++++++++++ .../profiles/_restore_default_form.html.erb | 22 ++++++++++++ .../WEB-INF/app/views/profiles/index.html.erb | 7 ++++ .../qualityprofile/QProfileBackupTest.java | 36 ++++++++++++++++--- .../qualityprofile/QProfilesMediumTest.java | 27 ++++++++------ 7 files changed, 123 insertions(+), 23 deletions(-) create mode 100644 sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_restore_default_form.html.erb diff --git a/sonar-core/src/main/resources/org/sonar/l10n/core.properties b/sonar-core/src/main/resources/org/sonar/l10n/core.properties index e9f2973f509..6353deb8990 100644 --- a/sonar-core/src/main/resources/org/sonar/l10n/core.properties +++ b/sonar-core/src/main/resources/org/sonar/l10n/core.properties @@ -116,6 +116,7 @@ rename=Rename reporter=Reporter reset_verb=Reset resolution=Resolution +restore=Restore result=Result results=Results x_results={0} results @@ -1628,6 +1629,10 @@ quality_profiles.remove_projects_confirm_message=Are you sure that you want to d quality_profiles.remove_projects_confirm_button=Remove All quality_profiles.copy_x_title=Copy Profile {0} quality_profiles.copy_new_name=New name +quality_profiles.restore_default_profiles=Restore Default Profiles +quality_profiles.restore_default_profiles_x_language=Restore default profiles for language '{0}' +quality_profiles.restore_default_profiles_confirmation=Are you sure you want to restore '{0}' profile(s) for '{1}' ? +quality_profiles.restore_default_profiles_warning=Before restoring default profiles for '{0}', you have to rename or delete profile(s) '{1}'. #------------------------------------------------------------------------------ # diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackup.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackup.java index 93f2afe8a17..b7276ac7c75 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackup.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackup.java @@ -43,12 +43,10 @@ import org.sonar.server.user.UserSession; import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; import static com.google.common.collect.Lists.newArrayList; +import static com.google.common.collect.Sets.newHashSet; /** * Used through ruby code
Internal.profile_backup
@@ -188,20 +186,32 @@ public class QProfileBackup implements ServerComponent { } /** - * Return the list of existing profiles that match the provided ones for a given language + * Return the list of existing profile names that match the provided ones for a given language */ - public List findProvidedProfilesByLanguage(String language) { - List profiles = newArrayList(); + public Collection findExistingProvidedProfileNamesByLanguage(String language) { + Set profiles = newHashSet(); ListMultimap profilesByName = profilesByName(language, new QProfileResult()); for (RulesProfile rulesProfile : profilesByName.values()) { QProfile profile = qProfileLookup.profile(rulesProfile.getName(), language); if (profile != null) { - profiles.add(profile); + profiles.add(profile.name()); } } return profiles; } + /** + * Return the list of provided profile names for a given language + */ + public Collection findProvidedProfileNamesByLanguage(String language) { + Set profiles = newHashSet(); + ListMultimap profilesByName = profilesByName(language, new QProfileResult()); + for (RulesProfile rulesProfile : profilesByName.values()) { + profiles.add(rulesProfile.getName()); + } + return profiles; + } + private void checkProfileDoesNotExists(RulesProfile importedProfile, boolean deleteExisting, DatabaseSession hibernateSession) { RulesProfile existingProfile = hibernateSession.getSingleResult(RulesProfile.class, "name", importedProfile.getName(), "language", importedProfile.getLanguage()); if (existingProfile != null && !deleteExisting) { diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb index 404e89d1855..a6b590acc3b 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb @@ -59,6 +59,29 @@ class ProfilesController < ApplicationController redirect_to :action => 'index' end + # Modal window to restore default profiles + # GET /profiles/restore_default_form/ + def restore_default_form + verify_ajax_request + require_parameters 'language' + @language = java_facade.getLanguages().find { |l| l.getKey()==params[:language].to_s } + call_backend do + @default_profile_names = Internal.profile_backup.findProvidedProfileNamesByLanguage(@language.getKey()).to_a + @existing_default_profiles = Internal.profile_backup.findExistingProvidedProfileNamesByLanguage(@language.getKey()).to_a + end + render :partial => 'profiles/restore_default_form' + end + + # POST /profiles/restore_default?language= + def restore_default + require_parameters 'language' + call_backend do + @result = Internal.profile_backup.restoreProvidedProfilesFromLanguage(params[:language].to_s) + flash_result(@result) + end + redirect_to :action => 'index' + end + # POST /profiles/delete/ def delete verify_post_request diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_restore_default_form.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_restore_default_form.html.erb new file mode 100644 index 00000000000..f650cfbecaa --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_restore_default_form.html.erb @@ -0,0 +1,22 @@ +
+
+ + + + + + + +
+
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb index 6b307cc7bd2..26cb167d7dd 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb @@ -27,6 +27,13 @@ class="open-modal link-action"><%= message('create') -%> + <% end %>

<%= message('quality_profiles.x_language_profiles', :params => language.getName()) -%>

diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackupTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackupTest.java index dec4f775dd4..ad3dfbd5830 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackupTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackupTest.java @@ -50,6 +50,7 @@ import org.sonar.server.user.UserSession; import java.io.Reader; import java.io.Writer; +import java.util.Collection; import java.util.List; import static com.google.common.collect.Lists.newArrayList; @@ -369,21 +370,46 @@ public class QProfileBackupTest { } @Test - public void find_provided_profiles_by_language() throws Exception { + public void find_existing_provided_profiles_by_language() throws Exception { RulesProfile rulesProfile1 = RulesProfile.create("Basic", "java"); ProfileDefinition profileDefinition1 = mock(ProfileDefinition.class); when(profileDefinition1.createProfile(any(ValidationMessages.class))).thenReturn(rulesProfile1); definitions.add(profileDefinition1); - RulesProfile rulesProfile2 = RulesProfile.create("Default", "java"); + RulesProfile rulesProfile2 = RulesProfile.create("Basic", "java"); ProfileDefinition profileDefinition2 = mock(ProfileDefinition.class); when(profileDefinition2.createProfile(any(ValidationMessages.class))).thenReturn(rulesProfile2); definitions.add(profileDefinition2); + RulesProfile rulesProfile3 = RulesProfile.create("Default", "java"); + ProfileDefinition profileDefinition3 = mock(ProfileDefinition.class); + when(profileDefinition3.createProfile(any(ValidationMessages.class))).thenReturn(rulesProfile3); + definitions.add(profileDefinition3); + // Only basic profile is existing - when(qProfileLookup.profile("Basic", "java")).thenReturn(new QProfile().setId(1)); + when(qProfileLookup.profile("Basic", "java")).thenReturn(new QProfile().setId(1).setName("Basic").setLanguage("java")); + + assertThat(backup.findExistingProvidedProfileNamesByLanguage("java")).containsOnly("Basic"); + } + + @Test + public void find_provided_profile_names_by_language() throws Exception { + RulesProfile rulesProfile1 = RulesProfile.create("Basic", "java"); + ProfileDefinition profileDefinition1 = mock(ProfileDefinition.class); + when(profileDefinition1.createProfile(any(ValidationMessages.class))).thenReturn(rulesProfile1); + definitions.add(profileDefinition1); + + RulesProfile rulesProfile2 = RulesProfile.create("Default", "java"); + ProfileDefinition profileDefinition2 = mock(ProfileDefinition.class); + when(profileDefinition2.createProfile(any(ValidationMessages.class))).thenReturn(rulesProfile2); + definitions.add(profileDefinition2); + + RulesProfile rulesProfile3 = RulesProfile.create("Default", "java"); + ProfileDefinition profileDefinition3 = mock(ProfileDefinition.class); + when(profileDefinition3.createProfile(any(ValidationMessages.class))).thenReturn(rulesProfile3); + definitions.add(profileDefinition3); - List result = backup.findProvidedProfilesByLanguage("java"); - assertThat(result).hasSize(1); + Collection result = backup.findProvidedProfileNamesByLanguage("java"); + assertThat(result).containsOnly("Basic", "Default"); } } diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesMediumTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesMediumTest.java index 26830e2e1f6..ea52ce51645 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesMediumTest.java @@ -62,14 +62,21 @@ public class QProfilesMediumTest { assertThat(qProfiles.searchProfileRules(ProfileRuleQuery.create(profile.id()), Paging.create(10, 1)).rules()).hasSize(2); - QProfileRule qProfileRule = qProfiles.searchProfileRules(ProfileRuleQuery.create(profile.id()).setNameOrKey("x1"), Paging.create(10, 1)).rules().get(0); - assertThat(qProfileRule.key()).isEqualTo("x1"); - assertThat(qProfileRule.severity()).isEqualTo("MAJOR"); - assertThat(qProfileRule.params()).hasSize(1); - - QProfileRuleParam qProfileRuleParam = qProfileRule.params().get(0); - assertThat(qProfileRuleParam.key()).isEqualTo("acceptWhitespace"); - assertThat(qProfileRuleParam.value()).isEqualTo("true"); + // Rule x1 + QProfileRule qProfileRule1 = qProfiles.searchProfileRules(ProfileRuleQuery.create(profile.id()).setNameOrKey("x1"), Paging.create(10, 1)).rules().get(0); + assertThat(qProfileRule1.key()).isEqualTo("x1"); + assertThat(qProfileRule1.severity()).isEqualTo("MAJOR"); + assertThat(qProfileRule1.params()).hasSize(1); + + QProfileRuleParam qProfileRule1Param = qProfileRule1.params().get(0); + assertThat(qProfileRule1Param.key()).isEqualTo("acceptWhitespace"); + assertThat(qProfileRule1Param.value()).isEqualTo("true"); + + // Rule x2 + QProfileRule qProfileRule2 = qProfiles.searchProfileRules(ProfileRuleQuery.create(profile.id()).setNameOrKey("x2"), Paging.create(10, 1)).rules().get(0); + assertThat(qProfileRule2.key()).isEqualTo("x2"); + assertThat(qProfileRule2.severity()).isEqualTo("BLOCKER"); + assertThat(qProfileRule2.params()).isEmpty(); } @Test @@ -91,12 +98,12 @@ public class QProfilesMediumTest { Rule rule2 = rules.find(RuleQuery.builder().searchQuery("x2").build()).results().iterator().next(); qProfiles.deactivateRule(qProfiles.profile("Basic", "xoo").id(), rule2.id()); - assertThat(qProfileBackup.findProvidedProfilesByLanguage("xoo")).hasSize(1); + assertThat(qProfileBackup.findExistingProvidedProfileNamesByLanguage("xoo")).hasSize(1); // Renamed profile qProfiles.renameProfile(profile.id(), "Old Basic"); - assertThat(qProfileBackup.findProvidedProfilesByLanguage("xoo")).isEmpty(); + assertThat(qProfileBackup.findExistingProvidedProfileNamesByLanguage("xoo")).isEmpty(); // Restore default profiles of xoo qProfileBackup.restoreProvidedProfilesFromLanguage("xoo"); -- 2.39.5