return null;
}
+ /**
+ * @deprecated since 4.3. Always return null
+ */
+ @Deprecated
@CheckForNull
public Characteristic findRequirementByRule(Rule rule) {
return null;
import javax.annotation.Nullable;
import javax.persistence.*;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Set;
@Entity
@Table(name = "rules")
@JoinColumn(name = "parent_id", updatable = true, nullable = true)
private Rule parent = null;
+ @Column(name = "characteristic_id", updatable = true, nullable = true)
+ private Integer characteristicId;
+
+ @Column(name = "default_characteristic_id", updatable = true, nullable = true)
+ private Integer defaultCharacteristicId;
+
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
@OneToMany(mappedBy = "rule")
private List<RuleParam> params = new ArrayList<RuleParam>();
this.tags = tags;
}
+ /**
+ * For internal use only.
+ *
+ * @since 4.3
+ */
+ @CheckForNull
+ public Integer getCharacteristicId() {
+ return characteristicId;
+ }
+
+ /**
+ * For internal use only.
+ *
+ * @since 4.3
+ */
+ public Rule setCharacteristicId(@Nullable Integer characteristicId) {
+ this.characteristicId = characteristicId;
+ return this;
+ }
+
+
+ /**
+ * For internal use only.
+ *
+ * @since 4.3
+ */
+ @CheckForNull
+ public Integer getDefaultCharacteristicId() {
+ return defaultCharacteristicId;
+ }
+
+ /**
+ * For internal use only.
+ *
+ * @since 4.3
+ */
+ public Rule setDefaultCharacteristicId(@Nullable Integer defaultCharacteristicId) {
+ this.defaultCharacteristicId = defaultCharacteristicId;
+ return this;
+ }
+
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Rule)) {
@CheckForNull
Integer parentId();
+ /**
+ * @deprecated since 4.3. return null
+ */
+ @Deprecated
@CheckForNull
Integer rootId();
+ /**
+ * @deprecated since 4.3. return null
+ */
+ @Deprecated
RuleKey ruleKey();
+ /**
+ * @deprecated since 4.3. return null
+ */
+ @Deprecated
String function();
/**
}
private void addCharacteristics(IssueQueryResult result, DefaultIssue issue, JsonWriter json) {
- Characteristic requirement = technicalDebtManager.findRequirementByRule(result.rule(issue));
- // Requirement can be null if it has been disabled
- if (requirement != null) {
- Characteristic characteristic = findCharacteristicById(requirement.rootId());
+ Integer subCharacteristicId = result.rule(issue).getCharacteristicId() != null ? result.rule(issue).getCharacteristicId() : result.rule(issue).getDefaultCharacteristicId();
+ Characteristic subCharacteristic = findCharacteristicById(subCharacteristicId);
+ if (subCharacteristic != null) {
+ json.prop("subCharacteristic", subCharacteristic.name());
+ Characteristic characteristic = findCharacteristicById(subCharacteristic.parentId());
json.prop("characteristic", characteristic != null ? characteristic.name() : null);
- Characteristic subCharacteristic = findCharacteristicById(requirement.parentId());
- json.prop("subCharacteristic", subCharacteristic != null ? subCharacteristic.name() : null);
}
}
require_parameters :id
rule_key = params[:id].split(':')
@rule = Rule.first(:conditions => ['plugin_name=? and plugin_rule_key=?', rule_key[0], rule_key[1]])
- @requirement = Internal.debt.findRequirementByRuleId(@rule.id)
- # Requirement can be null if it's disabled or if there's no requirement on this rule
- if @requirement
- @characteristic = Internal.debt.findCharacteristic(@requirement.parentId)
- @root_characteristic = Internal.debt.findCharacteristic(@requirement.rootId)
+ characteristic_id = @rule.characteristic_id || @rule.default_characteristic_id
+ if characteristic_id
+ @characteristic = Internal.debt.findCharacteristic(characteristic_id)
+ @root_characteristic = Internal.debt.findCharacteristic(@characteristic.parentId())
end
render :partial => 'issue/rule'
end
<p class="note">
<span class="spacer-right"><%= h @rule.plugin_name -%>:<%= h @rule.plugin_rule_key -%></span>
<%= image_tag 'sep12.png', :class => 'spacer-right' -%>
- <% if @requirement %>
+ <% if @characteristic %>
<%= @root_characteristic.name -%> > <%= @characteristic.name -%>
<% else %>
<%= message 'issue.technical_debt_deleted' %>
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.Rule;
import org.sonar.api.server.ws.WsTester;
-import org.sonar.api.technicaldebt.server.Characteristic;
import org.sonar.api.technicaldebt.server.internal.DefaultCharacteristic;
import org.sonar.api.user.User;
import org.sonar.api.utils.DateUtils;
}
@Test
- public void show_issue_with_characteristics() throws Exception {
+ public void show_issue_with_user_characteristics() throws Exception {
Issue issue = createStandardIssue().setDebt(Duration.create(7260L));
issues.add(issue);
- Characteristic requirement = new DefaultCharacteristic().setId(5).setParentId(2).setRootId(1);
- Characteristic characteristic = new DefaultCharacteristic().setId(1).setName("Maintainability");
- Characteristic subCharacteristic = new DefaultCharacteristic().setId(2).setName("Readability");
- when(technicalDebtManager.findRequirementByRule(result.rule(issue))).thenReturn(requirement);
- when(technicalDebtManager.findCharacteristicById(1)).thenReturn(characteristic);
- when(technicalDebtManager.findCharacteristicById(2)).thenReturn(subCharacteristic);
+ result.rule(issue).setCharacteristicId(2);
+ when(technicalDebtManager.findCharacteristicById(1)).thenReturn(new DefaultCharacteristic().setId(1).setName("Maintainability"));
+ when(technicalDebtManager.findCharacteristicById(2)).thenReturn(new DefaultCharacteristic().setId(2).setName("Readability").setParentId(1));
+
+ MockUserSession.set();
+ WsTester.TestRequest request = tester.newRequest("show").setParam("key", issue.key());
+ request.execute().assertJson(getClass(), "show_issue_with_characteristics.json");
+ }
+
+ @Test
+ public void show_issue_with_default_characteristics() throws Exception {
+ Issue issue = createStandardIssue().setDebt(Duration.create(7260L));
+ issues.add(issue);
+
+ result.rule(issue).setDefaultCharacteristicId(2);
+ when(technicalDebtManager.findCharacteristicById(1)).thenReturn(new DefaultCharacteristic().setId(1).setName("Maintainability"));
+ when(technicalDebtManager.findCharacteristicById(2)).thenReturn(new DefaultCharacteristic().setId(2).setName("Readability").setParentId(1));
+
+ MockUserSession.set();
+ WsTester.TestRequest request = tester.newRequest("show").setParam("key", issue.key());
+ request.execute().assertJson(getClass(), "show_issue_with_characteristics.json");
+ }
+
+ @Test
+ public void use_user_characteristic_if_both_characteristic_ids_are_defined() throws Exception {
+ Issue issue = createStandardIssue().setDebt(Duration.create(7260L));
+ issues.add(issue);
+
+ result.rule(issue).setCharacteristicId(2);
+ when(technicalDebtManager.findCharacteristicById(1)).thenReturn(new DefaultCharacteristic().setId(1).setName("Maintainability"));
+ when(technicalDebtManager.findCharacteristicById(2)).thenReturn(new DefaultCharacteristic().setId(2).setName("Readability").setParentId(1));
+
+ result.rule(issue).setDefaultCharacteristicId(20);
+ when(technicalDebtManager.findCharacteristicById(10)).thenReturn(new DefaultCharacteristic().setId(10).setName("Default Maintainability"));
+ when(technicalDebtManager.findCharacteristicById(20)).thenReturn(new DefaultCharacteristic().setId(20).setName("Default Readability").setParentId(10));
MockUserSession.set();
WsTester.TestRequest request = tester.newRequest("show").setParam("key", issue.key());