From a621ae4781fb3692f53f6f14315f142f8d8b705b Mon Sep 17 00:00:00 2001 From: =?utf8?q?L=C3=A9o=20Geoffroy?= Date: Fri, 4 Aug 2023 10:45:14 +0200 Subject: [PATCH] SONAR-20021 Add impact and clean code attribute to xoo plugin --- .../rule/OneBlockerIssuePerFileSensor.java | 2 ++ .../sonar/xoo/rule/OneIssuePerLineSensor.java | 32 +++++++++++++++-- .../sonar/xoo/rule/XooRulesDefinition.java | 36 ++++++++++++++++--- 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneBlockerIssuePerFileSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneBlockerIssuePerFileSensor.java index fd60ad39360..3a5ec0931bf 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneBlockerIssuePerFileSensor.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneBlockerIssuePerFileSensor.java @@ -25,6 +25,7 @@ import org.sonar.api.batch.rule.ActiveRules; import org.sonar.api.batch.rule.Severity; import org.sonar.api.batch.sensor.SensorContext; import org.sonar.api.batch.sensor.issue.NewIssue; +import org.sonar.api.issue.impact.SoftwareQuality; import org.sonar.api.rule.RuleKey; public class OneBlockerIssuePerFileSensor extends AbstractXooRuleSensor { @@ -42,6 +43,7 @@ public class OneBlockerIssuePerFileSensor extends AbstractXooRuleSensor { @Override protected void processFile(InputFile inputFile, SensorContext context, RuleKey ruleKey, String languageKey) { NewIssue newIssue = context.newIssue() + .overrideImpact(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH) .overrideSeverity(Severity.BLOCKER) .forRule(ruleKey); newIssue.at(newIssue.newLocation() diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneIssuePerLineSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneIssuePerLineSensor.java index 1dbb19d339f..363efc0a713 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneIssuePerLineSensor.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneIssuePerLineSensor.java @@ -19,6 +19,8 @@ */ package org.sonar.xoo.rule; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import org.sonar.api.batch.fs.FilePredicates; import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputFile; @@ -28,6 +30,7 @@ import org.sonar.api.batch.sensor.Sensor; import org.sonar.api.batch.sensor.SensorContext; import org.sonar.api.batch.sensor.SensorDescriptor; import org.sonar.api.batch.sensor.issue.NewIssue; +import org.sonar.api.issue.impact.SoftwareQuality; import org.sonar.api.rule.RuleKey; import org.sonar.xoo.Xoo; import org.sonar.xoo.Xoo2; @@ -65,20 +68,45 @@ public class OneIssuePerLineSensor implements Sensor { private void createIssues(InputFile file, SensorContext context, String repo) { RuleKey ruleKey = RuleKey.of(repo, RULE_KEY); - String severity = context.config().get(FORCE_SEVERITY_PROPERTY).orElse(null); + String severityStr = context.config().get(FORCE_SEVERITY_PROPERTY).orElse(null); for (int line = 1; line <= file.lines(); line++) { NewIssue newIssue = context.newIssue(); + Severity severity = severityStr != null ? Severity.valueOf(severityStr) : null; + org.sonar.api.issue.impact.Severity impactSeverity = mapSeverity(severity); newIssue .forRule(ruleKey) .at(newIssue.newLocation() .on(file) .at(file.selectLine(line)) .message("This issue is generated on each line")) - .overrideSeverity(severity != null ? Severity.valueOf(severity) : null) + .overrideSeverity(severity) .setRuleDescriptionContextKey(AVAILABLE_CONTEXTS[0]) .gap(context.config().getDouble(EFFORT_TO_FIX_PROPERTY).orElse(null)); + + if (impactSeverity != null) { + newIssue.overrideImpact(SoftwareQuality.MAINTAINABILITY, impactSeverity); + } newIssue.save(); } } + @CheckForNull + org.sonar.api.issue.impact.Severity mapSeverity(@Nullable Severity severity) { + if (severity == null) { + return null; + } + switch (severity) { + case CRITICAL: + case BLOCKER: + return org.sonar.api.issue.impact.Severity.HIGH; + case MAJOR: + return org.sonar.api.issue.impact.Severity.MEDIUM; + case MINOR: + case INFO: + return org.sonar.api.issue.impact.Severity.LOW; + default: + return null; + } + } + } diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/XooRulesDefinition.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/XooRulesDefinition.java index 4583eb6f7d4..49bc7e82f0c 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/XooRulesDefinition.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/XooRulesDefinition.java @@ -21,7 +21,10 @@ package org.sonar.xoo.rule; import javax.annotation.Nullable; import org.sonar.api.SonarRuntime; +import org.sonar.api.issue.impact.Severity; +import org.sonar.api.issue.impact.SoftwareQuality; import org.sonar.api.rule.RuleScope; +import org.sonar.api.rules.CleanCodeAttribute; import org.sonar.api.rules.RuleType; import org.sonar.api.server.rule.RuleDescriptionSection; import org.sonar.api.server.rule.RuleParamType; @@ -96,6 +99,7 @@ public class XooRulesDefinition implements RulesDefinition { new RulesDefinitionAnnotationLoader().load(repo, Check.ALL); NewRule hasTag = repo.createRule(HasTagSensor.RULE_KEY).setName("Has Tag") + .addDefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM) .setActivatedByDefault(true) .addDescriptionSection(howToFixSectionWithContext("single_context")); addDescriptionSectionsWithoutContexts(hasTag, "Search for a given tag in Xoo files"); @@ -116,6 +120,8 @@ public class XooRulesDefinition implements RulesDefinition { ruleWithParameters.createParam("float").setType(RuleParamType.FLOAT); NewRule oneIssuePerLine = repo.createRule(OneIssuePerLineSensor.RULE_KEY).setName("One Issue Per Line") + .setCleanCodeAttribute(CleanCodeAttribute.COMPLETE) + .addDefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM) .setTags("line"); addDescriptionSectionsWithoutContexts(oneIssuePerLine, "Generate an issue on each line of a file. It requires the metric \"lines\"."); addHowToFixSectionsWithContexts(oneIssuePerLine); @@ -125,6 +131,8 @@ public class XooRulesDefinition implements RulesDefinition { .addEducationPrincipleKeys("defense_in_depth", "never_trust_user_input"); NewRule oneQuickFixPerLine = repo.createRule(OneQuickFixPerLineSensor.RULE_KEY).setName("One Quick Fix Per Line") + .setCleanCodeAttribute(CleanCodeAttribute.DISTINCT) + .addDefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM) .setTags("line"); addAllDescriptionSections(oneQuickFixPerLine, "Generate an issue with quick fix available on each line of a file. It requires the metric \"lines\"."); @@ -147,6 +155,8 @@ public class XooRulesDefinition implements RulesDefinition { NewRule oneBugIssuePerTestLine = repo.createRule(OneBugIssuePerTestLineSensor.RULE_KEY).setName("One Bug Issue Per Test Line") .setScope(RuleScope.TEST) + .setCleanCodeAttribute(CleanCodeAttribute.RESPECTFUL) + .addDefaultImpact(SoftwareQuality.RELIABILITY, Severity.MEDIUM) .setType(RuleType.BUG); addAllDescriptionSections(oneBugIssuePerTestLine, "Generate a bug issue on each line of a test file. It requires the metric \"lines\"."); @@ -155,34 +165,45 @@ public class XooRulesDefinition implements RulesDefinition { NewRule oneCodeSmellIssuePerTestLine = repo.createRule(OneCodeSmellIssuePerTestLineSensor.RULE_KEY).setName("One Code Smell Issue Per Test Line") .setScope(RuleScope.TEST) + .setCleanCodeAttribute(CleanCodeAttribute.TESTED) + .addDefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM) .setType(RuleType.CODE_SMELL); addAllDescriptionSections(oneCodeSmellIssuePerTestLine, "Generate a code smell issue on each line of a test file. It requires the metric \"lines\"."); oneCodeSmellIssuePerTestLine .setDebtRemediationFunction(oneCodeSmellIssuePerTestLine.debtRemediationFunctions().linear("3min")); - NewRule oneIssuePerDirectory = repo.createRule(OneIssuePerDirectorySensor.RULE_KEY).setName("One Issue Per Directory"); + NewRule oneIssuePerDirectory = repo.createRule(OneIssuePerDirectorySensor.RULE_KEY) + .setName("One Issue Per Directory") + .setCleanCodeAttribute(CleanCodeAttribute.CLEAR) + .addDefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM); oneIssuePerDirectory.setDebtRemediationFunction(oneIssuePerDirectory.debtRemediationFunctions().linear(TEN_MIN)); addAllDescriptionSections(oneIssuePerDirectory, "Generate an issue on each non-empty directory"); - NewRule oneDayDebtPerFile = repo.createRule(OneDayDebtPerFileSensor.RULE_KEY).setName("One Day Debt Per File"); + NewRule oneDayDebtPerFile = repo.createRule(OneDayDebtPerFileSensor.RULE_KEY).setName("One Day Debt Per File") + .setCleanCodeAttribute(CleanCodeAttribute.LAWFUL) + .addDefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM); oneDayDebtPerFile.setDebtRemediationFunction(oneDayDebtPerFile.debtRemediationFunctions().linear("1d")); addAllDescriptionSections(oneDayDebtPerFile, "Generate an issue on each file with a debt of one day"); NewRule oneIssuePerModule = repo.createRule(OneIssuePerModuleSensor.RULE_KEY).setName("One Issue Per Module"); oneIssuePerModule + .addDefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM) .setDebtRemediationFunction(oneIssuePerModule.debtRemediationFunctions().linearWithOffset("25min", "1h")) .setGapDescription("A certified architect will need roughly half an hour to start working on removal of modules, " + "then it's about one hour per module."); addAllDescriptionSections(oneIssuePerModule, "Generate an issue on each module"); - NewRule oneBlockerIssuePerFile = repo.createRule(OneBlockerIssuePerFileSensor.RULE_KEY).setName("One Blocker Issue Per File"); + NewRule oneBlockerIssuePerFile = repo.createRule(OneBlockerIssuePerFileSensor.RULE_KEY).setName("One Blocker Issue Per File") + .addDefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM); addAllDescriptionSections(oneBlockerIssuePerFile, "Generate a blocker issue on each file, whatever the severity declared in the Quality profile"); - NewRule issueWithCustomMessage = repo.createRule(CustomMessageSensor.RULE_KEY).setName("Issue With Custom Message"); + NewRule issueWithCustomMessage = repo.createRule(CustomMessageSensor.RULE_KEY).setName("Issue With Custom Message") + .addDefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM); addAllDescriptionSections(issueWithCustomMessage, "Generate an issue on each file with a custom message"); - NewRule oneIssuePerFileWithRandomAccess = repo.createRule(RandomAccessSensor.RULE_KEY).setName("One Issue Per File with Random Access"); + NewRule oneIssuePerFileWithRandomAccess = repo.createRule(RandomAccessSensor.RULE_KEY).setName("One Issue Per File with Random Access") + .addDefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM); addAllDescriptionSections(oneIssuePerFileWithRandomAccess, "This issue is generated on each file"); NewRule issueWithRangeAndMultipleLocations = repo.createRule(MultilineIssuesSensor.RULE_KEY).setName("Creates issues with ranges/multiple locations"); @@ -198,18 +219,23 @@ public class XooRulesDefinition implements RulesDefinition { addAllDescriptionSections(issueOnEachFileWithExtUnknown, "This issue is generated on each file with extenstion 'unknown'"); NewRule oneBugIssuePerLine = repo.createRule(OneBugIssuePerLineSensor.RULE_KEY).setName("One Bug Issue Per Line") + .addDefaultImpact(SoftwareQuality.RELIABILITY, Severity.MEDIUM) .setType(RuleType.BUG); oneBugIssuePerLine .setDebtRemediationFunction(oneBugIssuePerLine.debtRemediationFunctions().linear("5min")); addAllDescriptionSections(oneBugIssuePerLine, "Generate a bug issue on each line of a file. It requires the metric \"lines\"."); NewRule oneCodeSmellIssuePerLine = repo.createRule(OneCodeSmellIssuePerLineSensor.RULE_KEY).setName("One Code Smell Issue Per Line") + .addDefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM) + .addDefaultImpact(SoftwareQuality.RELIABILITY, Severity.LOW) .setType(RuleType.CODE_SMELL); oneCodeSmellIssuePerLine .setDebtRemediationFunction(oneCodeSmellIssuePerLine.debtRemediationFunctions().linear("9min")); addAllDescriptionSections(oneCodeSmellIssuePerLine, "Generate a code smell issue on each line of a file. It requires the metric \"lines\"."); NewRule oneVulnerabilityIssuePerModule = repo.createRule(OneVulnerabilityIssuePerModuleSensor.RULE_KEY).setName("One Vulnerability Issue Per Module") + .addDefaultImpact(SoftwareQuality.SECURITY, Severity.MEDIUM) + .addDefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.HIGH) .setType(RuleType.VULNERABILITY); addAllDescriptionSections(oneVulnerabilityIssuePerModule, "Generate an issue on each module"); -- 2.39.5