]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1722: Allow to override inherited rule
authorGodin <mandrikov@gmail.com>
Sat, 18 Dec 2010 11:23:14 +0000 (11:23 +0000)
committerGodin <mandrikov@gmail.com>
Sat, 18 Dec 2010 11:23:14 +0000 (11:23 +0000)
sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRule.java
sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java
sonar-server/src/main/webapp/WEB-INF/app/views/rules_configuration/_rule.html.erb
sonar-server/src/main/webapp/WEB-INF/app/views/rules_configuration/_rule_param.html.erb
sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldSetParent-result.xml
sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldSetParent.xml

index 9b0cd9565512c86fced0cfa7a22edae0e41c7884..bc3c36c04c343cc0a9ded02f27665e1ca0814ef8 100644 (file)
@@ -107,6 +107,24 @@ public class ActiveRule implements Cloneable {
     this.inherited = inherited;
   }
 
+  /**
+   * For internal use only.
+   * 
+   * @since 2.5
+   */
+  public Boolean isOverrides() {
+    return overrides == null ? false : overrides;
+  }
+
+  /**
+   * For internal use only.
+   * 
+   * @since 2.5
+   */
+  public void setOverrides(Boolean overrides) {
+    this.overrides = overrides;
+  }
+
   /**
    * @deprecated visibility should be decreased to protected or package
    */
index 2cf094e8ac036ae8e1b74911d03b41b5e9edab3a..6d692a6e7b7b297529faf8616cd4a109a8b363cb 100644 (file)
@@ -87,7 +87,7 @@ public class ProfilesManager extends BaseDao {
       // Activate all inherited rules
       if (newParent != null) {
         for (ActiveRule activeRule : newParent.getActiveRules()) {
-          activate(profile, activeRule);
+          activateOrChange(profile, activeRule);
         }
       }
       profile.setParentId(parentId);
@@ -102,8 +102,12 @@ public class ProfilesManager extends BaseDao {
   public void activatedOrChanged(int parentProfileId, int activeRuleId) {
     List<RulesProfile> children = getChildren(parentProfileId);
     ActiveRule parentActiveRule = getSession().getEntity(ActiveRule.class, activeRuleId);
+    if (parentActiveRule.isInherited() && !parentActiveRule.isOverrides()) {
+      parentActiveRule.setOverrides(true);
+      getSession().saveWithoutFlush(parentActiveRule);
+    }
     for (RulesProfile child : children) {
-      activate(child, parentActiveRule);
+      activateOrChange(child, parentActiveRule);
     }
     getSession().commit();
   }
@@ -120,10 +124,17 @@ public class ProfilesManager extends BaseDao {
     getSession().commit();
   }
 
-  private void activate(RulesProfile profile, ActiveRule parentActiveRule) {
+  private void activateOrChange(RulesProfile profile, ActiveRule parentActiveRule) {
     ActiveRule activeRule = profile.getActiveRule(parentActiveRule.getRule());
     if (activeRule != null) {
-      removeActiveRule(profile, activeRule);
+      if (activeRule.isInherited() && !activeRule.isOverrides()) {
+        removeActiveRule(profile, activeRule);
+      } else {
+        activeRule.setInherited(true);
+        activeRule.setOverrides(true);
+        getSession().saveWithoutFlush(activeRule);
+        return;
+      }
     }
     activeRule = (ActiveRule) parentActiveRule.clone();
     activeRule.setRulesProfile(profile);
@@ -134,7 +145,13 @@ public class ProfilesManager extends BaseDao {
   private void deactivate(RulesProfile profile, Rule rule) {
     ActiveRule activeRule = profile.getActiveRule(rule);
     if (activeRule != null) {
-      removeActiveRule(profile, activeRule);
+      if (activeRule.isInherited() && !activeRule.isOverrides()) {
+        removeActiveRule(profile, activeRule);
+      } else {
+        activeRule.setInherited(false);
+        activeRule.setOverrides(false);
+        getSession().saveWithoutFlush(activeRule);
+      }
     }
   }
 
index d8e131ce0d81bd794d78b7564843744cdbe3d218..d612abf3f3fb83a261c492dbf692b361a0a8622b 100644 (file)
@@ -1,10 +1,9 @@
 <td nowrap valign="top" class="left" x="<%= active_rule.failure_level if active_rule -%>" width="1%">
-  <% inherited = active_rule.inherited if active_rule %>
-  <% if inherited %>
-  Inherited from parent profile.
-  <% end %>
+  <% inherited = active_rule.inherited if active_rule
+     overrides = active_rule.overrides if active_rule
+  %>
   <form id="levels_<%= rule.id -%>" action="">
-      <% enable_modification = is_admin && !profile.provided? && !inherited
+      <% enable_modification = is_admin && !profile.provided?
          select_box_id = "levels_select_#{rule.id}"
          check_box_id = "levels_check_#{rule.id}"
          rule_select_box = "$('#{select_box_id}')"
                  :loading => "$('levels_#{rule.id}').replace('<img src=\"#{ApplicationController.root_context}/images/loading.gif\"/>');",
                  :with => "'level=' + get_level_for_rule(#{rule_select_box},#{rule_check_box})")
       %>
-      <%= check_box_tag(check_box_id, 'yes', (!active_rule.nil?), :onclick => activate_rule, :disabled => !enable_modification) %>
+      <%= check_box_tag(check_box_id, 'yes', (!active_rule.nil?), :onclick => activate_rule, :disabled => !enable_modification || inherited) %>
       <%= select_tag(select_box_id, options_for_select(RulesConfigurationController::RULE_PRIORITIES, (active_rule.nil? ? rule.priority_text : active_rule.priority_text)),
             {:onchange => changel_level, :disabled => (!(enable_modification) || active_rule.nil?)}) %>
 
   </form>
 </td>
 <td valign="top" class="left">
+  <% if inherited %>
+    <% if overrides %>
+      Overrides rule from parent profile.<br/>
+    <% else %>
+      Inherited from parent profile.<br/>
+    <% end %>
+  <% end %>
   <%= link_to_function("#{h rule.name}", nil, :class => "") do |page|
         page.toggle "desc_#{rule.id}"
       end
index 15f91b19ed4685ad713df8b37de503bcd73cc617..c695a0218b7b8a350fd3c969b7615575c65f4ee0 100644 (file)
@@ -6,8 +6,7 @@
       param_value = active_parameter.value unless active_parameter.value.blank?
     end
     active_rule_id = active_rule.id if active_rule
-    inherited = active_rule.inherited if active_rule
-    enable_modification = is_admin && !profile.provided? && !inherited
+    enable_modification = is_admin && !profile.provided?
 %>
 <tr>
     <td width="10%" nowrap class="left">
index 2e5ffabf8480f1152ffcf3e75ca494e165b4c09f..a66038a776093e86886730fa1312269160deaf91 100644 (file)
@@ -9,6 +9,6 @@
 
   <active_rules id="1" profile_id="1" rule_id="1" failure_level="2" inherited="false" overrides="[null]"/>
 
-  <active_rules id="3" profile_id="2" rule_id="1" failure_level="2" inherited="true" overrides="[null]"/>
+  <active_rules id="2" profile_id="2" rule_id="1" failure_level="2" inherited="true" overrides="[null]"/>
 
 </dataset>
index a5d827cabbef70015d45ac7df99ddc1f53b86d9a..0c458b31a8ca96ea657671f179e45556fecb1236 100644 (file)
@@ -9,6 +9,4 @@
 
   <active_rules id="1" profile_id="1" rule_id="1" failure_level="2" inherited="false" overrides="[null]"/>
 
-  <active_rules id="2" profile_id="2" rule_id="1" failure_level="2" inherited="false" overrides="[null]"/>
-
 </dataset>