aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2014-04-25 10:43:02 +0200
committerJulien Lancelot <julien.lancelot@sonarsource.com>2014-04-25 10:43:11 +0200
commit6c95411c7beea684ba428d44fbf0dbd967f96c0e (patch)
tree87569cf1d1267093ae3d8aa1f4a6c0c584c54eaf
parent689c237e415710257a2d48c218f95ab279cd7a8a (diff)
downloadsonarqube-6c95411c7beea684ba428d44fbf0dbd967f96c0e.tar.gz
sonarqube-6c95411c7beea684ba428d44fbf0dbd967f96c0e.zip
SONAR-4764 Restore default profiles from ui
-rw-r--r--sonar-core/src/main/resources/org/sonar/l10n/core.properties5
-rw-r--r--sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackup.java26
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb23
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_restore_default_form.html.erb22
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb7
-rw-r--r--sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackupTest.java36
-rw-r--r--sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesMediumTest.java27
7 files changed, 123 insertions, 23 deletions
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 <pre>Internal.profile_backup</pre>
@@ -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<QProfile> findProvidedProfilesByLanguage(String language) {
- List<QProfile> profiles = newArrayList();
+ public Collection<String> findExistingProvidedProfileNamesByLanguage(String language) {
+ Set<String> profiles = newHashSet();
ListMultimap<String, RulesProfile> 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<String> findProvidedProfileNamesByLanguage(String language) {
+ Set<String> profiles = newHashSet();
+ ListMultimap<String, RulesProfile> 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/<profile id>
+ 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=<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/<id>
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 @@
+<form id="restore-default-profiles-form" action="profiles/restore_default" method="POST">
+ <fieldset>
+ <input type="hidden" name="language" value="<%= @language.getKey() -%>"/>
+
+ <div class="modal-head">
+ <h2><%= h message('quality_profiles.restore_default_profiles_x_language', :params => @language.getName()) -%></h2>
+ </div>
+
+ <div class="modal-body">
+ <% if @existing_default_profiles.empty? %>
+ <%= h message('quality_profiles.restore_default_profiles_confirmation', :params => [@default_profile_names.join('\', \''), @language.getName()]) -%>
+ <% else %>
+ <%= h message('quality_profiles.restore_default_profiles_warning', :params => [@language.getName(), @existing_default_profiles.join('\', \'')]) -%>
+ <% end %>
+ </div>
+
+ <div class="modal-foot">
+ <input type="submit" value="<%= h message('restore') -%>" id="restore-default-profiles-submit" <%= 'disabled=disabled' unless @existing_default_profiles.empty? -%>/>
+ <a href="#" onclick="return closeModalWindow()" id="restore-default-profiles-cancel"><%= h message('cancel') -%></a>
+ </div>
+ </fieldset>
+</form>
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') -%></a>
</li>
</ul>
+ <ul style="float: right" class="horizontal">
+ <li class="marginleft10">
+ <i class="icon-plus"></i>
+ <a id="create-link-<%= language.getKey() -%>" href="<%= ApplicationController.root_context -%>/profiles/restore_default_form?language=<%= u language.getKey() -%>"
+ class="open-modal link-action"><%= message('quality_profiles.restore_default_profiles') -%></a>
+ </li>
+ </ul>
<% end %>
<h2><%= message('quality_profiles.x_language_profiles', :params => language.getName()) -%></h2>
</div>
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<QProfile> result = backup.findProvidedProfilesByLanguage("java");
- assertThat(result).hasSize(1);
+ Collection<String> 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");