Search in sources :

Example 1 with DebtRemediationFunction

use of org.sonar.api.server.debt.DebtRemediationFunction in project sonarqube by SonarSource.

the class DebtCalculator method calculate.

@CheckForNull
public Duration calculate(DefaultIssue issue) {
    Rule rule = ruleRepository.getByKey(issue.ruleKey());
    DebtRemediationFunction fn = rule.getRemediationFunction();
    if (fn != null) {
        verifyEffortToFix(issue, fn);
        Duration debt = Duration.create(0);
        String gapMultiplier = fn.gapMultiplier();
        if (fn.type().usesGapMultiplier() && !Strings.isNullOrEmpty(gapMultiplier)) {
            int effortToFixValue = MoreObjects.firstNonNull(issue.effortToFix(), 1).intValue();
            // TODO convert to Duration directly in Rule#remediationFunction -> better performance + error handling
            debt = durations.decode(gapMultiplier).multiply(effortToFixValue);
        }
        String baseEffort = fn.baseEffort();
        if (fn.type().usesBaseEffort() && !Strings.isNullOrEmpty(baseEffort)) {
            // TODO convert to Duration directly in Rule#remediationFunction -> better performance + error handling
            debt = debt.add(durations.decode(baseEffort));
        }
        return debt;
    }
    return null;
}
Also used : DebtRemediationFunction(org.sonar.api.server.debt.DebtRemediationFunction) Duration(org.sonar.api.utils.Duration) CheckForNull(javax.annotation.CheckForNull)

Example 2 with DebtRemediationFunction

use of org.sonar.api.server.debt.DebtRemediationFunction in project sonarqube by SonarSource.

the class UpdateAction method readDebt.

private void readDebt(Request request, RuleUpdate update) {
    String value = defaultIfEmpty(request.param(PARAM_REMEDIATION_FN_TYPE), request.param(DEPRECATED_PARAM_REMEDIATION_FN_TYPE));
    if (value != null) {
        if (StringUtils.isBlank(value)) {
            update.setDebtRemediationFunction(null);
        } else {
            DebtRemediationFunction fn = new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.valueOf(value), defaultIfEmpty(request.param(PARAM_REMEDIATION_FN_GAP_MULTIPLIER), request.param(DEPRECATED_PARAM_REMEDIATION_FN_COEFF)), defaultIfEmpty(request.param(PARAM_REMEDIATION_FN_BASE_EFFORT), request.param(DEPRECATED_PARAM_REMEDIATION_FN_OFFSET)));
            update.setDebtRemediationFunction(fn);
        }
    }
}
Also used : DefaultDebtRemediationFunction(org.sonar.api.server.debt.internal.DefaultDebtRemediationFunction) DebtRemediationFunction(org.sonar.api.server.debt.DebtRemediationFunction) DefaultDebtRemediationFunction(org.sonar.api.server.debt.internal.DefaultDebtRemediationFunction)

Example 3 with DebtRemediationFunction

use of org.sonar.api.server.debt.DebtRemediationFunction in project sonarqube by SonarSource.

the class RulesDefinitionXmlLoaderTest method test_linear_with_offset_remediation_function.

@Test
public void test_linear_with_offset_remediation_function() {
    String xml = "" + "<rules>" + "  <rule>" + "    <key>1</key>" + "    <name>One</name>" + "    <description>Desc</description>" + "    <effortToFixDescription>lines</effortToFixDescription>" + "    <remediationFunction>LINEAR_OFFSET</remediationFunction>" + "    <remediationFunctionGapMultiplier>2d 3h</remediationFunctionGapMultiplier>" + "    <remediationFunctionBaseEffort>5min</remediationFunctionBaseEffort>" + "  </rule>" + "</rules>";
    RulesDefinition.Rule rule = load(xml).rule("1");
    assertThat(rule.gapDescription()).isEqualTo("lines");
    DebtRemediationFunction function = rule.debtRemediationFunction();
    assertThat(function).isNotNull();
    assertThat(function.type()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET);
    assertThat(function.gapMultiplier()).isEqualTo("2d3h");
    assertThat(function.baseEffort()).isEqualTo("5min");
}
Also used : DebtRemediationFunction(org.sonar.api.server.debt.DebtRemediationFunction) Test(org.junit.Test)

Example 4 with DebtRemediationFunction

use of org.sonar.api.server.debt.DebtRemediationFunction in project sonarqube by SonarSource.

the class RulesDefinitionXmlLoaderTest method test_constant_remediation_function.

@Test
public void test_constant_remediation_function() {
    String xml = "" + "<rules>" + "  <rule>" + "    <key>1</key>" + "    <name>One</name>" + "    <description>Desc</description>" + "    <remediationFunction>CONSTANT_ISSUE</remediationFunction>" + "    <remediationFunctionBaseEffort>5min</remediationFunctionBaseEffort>" + "  </rule>" + "</rules>";
    RulesDefinition.Rule rule = load(xml).rule("1");
    DebtRemediationFunction function = rule.debtRemediationFunction();
    assertThat(function).isNotNull();
    assertThat(function.type()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE);
    assertThat(function.gapMultiplier()).isNull();
    assertThat(function.baseEffort()).isEqualTo("5min");
}
Also used : DebtRemediationFunction(org.sonar.api.server.debt.DebtRemediationFunction) Test(org.junit.Test)

Example 5 with DebtRemediationFunction

use of org.sonar.api.server.debt.DebtRemediationFunction in project sonarqube by SonarSource.

the class RuleUpdater method updateDebtRemediationFunction.

private static void updateDebtRemediationFunction(RuleUpdate update, Context context) {
    DebtRemediationFunction function = update.getDebtRemediationFunction();
    if (function == null) {
        context.rule.setRemediationFunction(null);
        context.rule.setRemediationGapMultiplier(null);
        context.rule.setRemediationBaseEffort(null);
    } else {
        if (isSameAsDefaultFunction(function, context.rule)) {
            // reset to default
            context.rule.setRemediationFunction(null);
            context.rule.setRemediationGapMultiplier(null);
            context.rule.setRemediationBaseEffort(null);
        } else {
            context.rule.setRemediationFunction(function.type().name());
            context.rule.setRemediationGapMultiplier(function.gapMultiplier());
            context.rule.setRemediationBaseEffort(function.baseEffort());
        }
    }
}
Also used : DebtRemediationFunction(org.sonar.api.server.debt.DebtRemediationFunction)

Aggregations

DebtRemediationFunction (org.sonar.api.server.debt.DebtRemediationFunction)10 Test (org.junit.Test)4 DefaultDebtRemediationFunction (org.sonar.api.server.debt.internal.DefaultDebtRemediationFunction)3 CheckForNull (javax.annotation.CheckForNull)1 Duration (org.sonar.api.utils.Duration)1