From 733f89f53adb3364b11420a41aca1280149fbfbd Mon Sep 17 00:00:00 2001 From: Godin Date: Sat, 18 Dec 2010 11:23:14 +0000 Subject: [PATCH] SONAR-1722: Allow to override inherited rule --- .../java/org/sonar/api/rules/ActiveRule.java | 18 +++++++++++++ .../server/configuration/ProfilesManager.java | 27 +++++++++++++++---- .../views/rules_configuration/_rule.html.erb | 18 ++++++++----- .../rules_configuration/_rule_param.html.erb | 3 +-- .../shouldSetParent-result.xml | 2 +- .../InheritedProfilesTest/shouldSetParent.xml | 2 -- 6 files changed, 54 insertions(+), 16 deletions(-) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRule.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRule.java index 9b0cd956551..bc3c36c04c3 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRule.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRule.java @@ -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 */ diff --git a/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java b/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java index 2cf094e8ac0..6d692a6e7b7 100644 --- a/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java +++ b/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java @@ -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 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); + } } } diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/rules_configuration/_rule.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/rules_configuration/_rule.html.erb index d8e131ce0d8..d612abf3f3f 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/rules_configuration/_rule.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/rules_configuration/_rule.html.erb @@ -1,10 +1,9 @@ - <% 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 + %>
- <% 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}')" @@ -18,13 +17,20 @@ :loading => "$('levels_#{rule.id}').replace('');", :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?)}) %>
+ <% if inherited %> + <% if overrides %> + Overrides rule from parent profile.
+ <% else %> + Inherited from parent profile.
+ <% end %> + <% end %> <%= link_to_function("#{h rule.name}", nil, :class => "") do |page| page.toggle "desc_#{rule.id}" end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/rules_configuration/_rule_param.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/rules_configuration/_rule_param.html.erb index 15f91b19ed4..c695a0218b7 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/rules_configuration/_rule_param.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/rules_configuration/_rule_param.html.erb @@ -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? %> diff --git a/sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldSetParent-result.xml b/sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldSetParent-result.xml index 2e5ffabf848..a66038a7760 100644 --- a/sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldSetParent-result.xml +++ b/sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldSetParent-result.xml @@ -9,6 +9,6 @@ - + diff --git a/sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldSetParent.xml b/sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldSetParent.xml index a5d827cabbe..0c458b31a8c 100644 --- a/sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldSetParent.xml +++ b/sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldSetParent.xml @@ -9,6 +9,4 @@ - - -- 2.39.5