]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5593 Better handling of profile overwrite on copy
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Thu, 9 Oct 2014 14:38:22 +0000 (16:38 +0200)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Tue, 14 Oct 2014 12:51:32 +0000 (14:51 +0200)
server/sonar-web/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb
server/sonar-web/src/main/webapp/WEB-INF/app/views/profiles/_copy_form.html.erb
sonar-core/src/main/resources/org/sonar/l10n/core.properties

index 6956a1287ebb143f733185c7805cea136dcb560e..3b00a8a16b0b4bff665ebabe116287148e81a3b1 100644 (file)
@@ -140,19 +140,37 @@ class ProfilesController < ApplicationController
     render :partial => 'profiles/copy_form'
   end
 
-  # POST /profiles/copy/<id>?name=<name of new profile>
+  # POST /profiles/copy/<id>?name=<name of new profile>[&overwrite=<name of overwritten profile>]
   def copy
     verify_post_request
     verify_ajax_request
     require_parameters 'id'
 
-    source_key=profile_id_to_key(params[:id].to_i)
+    source_id = params[:id].to_i
+    source_profile = Internal.quality_profiles.profile(source_id)
+
+    source_key=profile_id_to_key(source_id)
     target_name = params['name']
 
-    call_backend do
-      Internal.qprofile_service.copyToName(source_key, target_name)
-      flash[:notice]= message('quality_profiles.profile_x_not_activated', :params => target_name)
-      render :text => 'ok', :status => 200
+    overwrite = (params['overwrite'] == target_name)
+    target_profile = nil
+
+    unless overwrite
+      target_profile = Internal.quality_profiles.profile(target_name, source_profile.language())
+    end
+
+    if target_profile.nil? || overwrite
+      call_backend do
+        Internal.qprofile_service.copyToName(source_key, target_name)
+        if overwrite
+          flash[:notice] = message('quality_profiles.copy_x_overwritten', :params => target_name)
+        else
+          flash[:notice] = message('quality_profiles.profile_x_not_activated', :params => target_name)
+        end
+        render :text => 'ok', :status => 200
+      end
+    else
+      render :text => message('quality_profiles.copy_overwrite_x', :params => target_name), :status => 409
     end
   end
 
index ab8a79f42473af172e47c96ea638457396cf0f3f..71520b058402acfeba4c24aace4e3095cd62f618 100644 (file)
@@ -6,17 +6,37 @@
     </div>
     <div class="modal-body">
       <div class="modal-error"/>
+      <div class="modal-warning"/>
       <div class="modal-field">
         <label for="name"><%= message 'quality_profiles.copy_new_name' -%> <em class="mandatory">*</em></label>
         <input id="copy-name" name="name" type="text" size="50" maxlength="100" autofocus="autofocus"/>
       </div>
     </div>
     <div class="modal-foot">
+      <input type="hidden" value="" name="overwrite" id="copy-overwrite"/>
       <input type="submit" value="<%= h message('copy') -%>" id="copy-submit"/>
       <a href="#" onclick="return closeModalWindow()" id="copy-cancel"><%= h message('cancel') -%></a>
     </div>
   </fieldset>
 </form>
 <script>
-  $j("#copy-profile-form").modalForm();
+  $j("#copy-profile-form").modalForm({
+    error: function (xhr) {
+      if (xhr.status == 409) {
+        $j('#copy-profile-form .modal-error').hide();
+        var warningElt = $j('#copy-profile-form .modal-warning');
+        $j('#copy-overwrite').val($j('#copy-name').val());
+        $j('#copy-profile-form input[type=submit]').removeAttr('disabled');
+        warningElt.html(xhr.responseText);
+        warningElt.show();
+      } else {
+        $j('#copy-profile-form .modal-warning').hide();
+        var errorElt = $j('#copy-profile-form .modal-error');
+        $j('.loading-image').addClass("hidden");
+        $j('#copy-profile-form input[type=submit]').removeAttr('disabled');
+        errorElt.html(xhr.responseText);
+        errorElt.show();
+      }
+    }
+  });
 </script>
index 5c7a8ea65633a616cdf0e6841c7ed4437fda8445..bd72fe78a4434d8481585685ec61b921fe8d48c5 100644 (file)
@@ -1640,6 +1640,8 @@ 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.copy_overwrite_x=You are about to copy this quality profile into the existing "{0}" profile, which will fully overwrite it. Please confirm the name if you want to overwrite, or choose another name.
+quality_profiles.copy_x_overwritten=Profile '{0}' has been overwritten.
 quality_profiles.restore_built_in_profiles=Restore Built-in Profiles
 quality_profiles.restore_built_in_profiles_confirmation=Are you sure you want to restore '{0}' profile(s) for '{1}' ?
 quality_profiles.including=including