diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2018-05-30 10:45:00 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2018-06-01 20:20:47 +0200 |
commit | c72965048f5cf06bdf3414846a347402f2b8dddf (patch) | |
tree | 103f9f4142dd4f508a91d876906c00128bf53c8d /plugins/sonar-xoo-plugin | |
parent | a33103206a6787a4f85fb821cff8b5333f4babb3 (diff) | |
download | sonarqube-c72965048f5cf06bdf3414846a347402f2b8dddf.tar.gz sonarqube-c72965048f5cf06bdf3414846a347402f2b8dddf.zip |
SONAR-10809 Analyzers need to declare whether a rule is external
Diffstat (limited to 'plugins/sonar-xoo-plugin')
6 files changed, 112 insertions, 3 deletions
diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java index 3b626b8ed9f..639d51c2172 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java @@ -51,6 +51,7 @@ import org.sonar.xoo.rule.OneBlockerIssuePerFileSensor; import org.sonar.xoo.rule.OneBugIssuePerLineSensor; import org.sonar.xoo.rule.OneDayDebtPerFileSensor; import org.sonar.xoo.rule.OneExternalIssuePerLineSensor; +import org.sonar.xoo.rule.OneExternalIssueWithDetailsPerLineSensor; import org.sonar.xoo.rule.OneIssueOnDirPerFileSensor; import org.sonar.xoo.rule.OneIssuePerDirectorySensor; import org.sonar.xoo.rule.OneIssuePerFileSensor; @@ -176,6 +177,7 @@ public class XooPlugin implements Plugin { if (context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(7, 2))) { context.addExtensions( OneExternalIssuePerLineSensor.class, + OneExternalIssueWithDetailsPerLineSensor.class, SignificantCodeSensor.class); } } diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneExternalIssuePerLineSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneExternalIssuePerLineSensor.java index d9fe4b59e82..c8d09704371 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneExternalIssuePerLineSensor.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneExternalIssuePerLineSensor.java @@ -38,7 +38,7 @@ public class OneExternalIssuePerLineSensor implements Sensor { public static final String ENGINE_KEY = "XooEngine"; public static final String SEVERITY = "MAJOR"; public static final Long EFFORT = 10l; - public static final RuleType type = RuleType.BUG; + public static final RuleType TYPE = RuleType.BUG; public static final String ACTIVATE_EXTERNAL_ISSUES = "sonar.oneExternalIssuePerLine.activate"; private static final String NAME = "One External Issue Per Line"; @@ -76,7 +76,7 @@ public class OneExternalIssuePerLineSensor implements Sensor { .message("This issue is generated on each line")) .severity(Severity.valueOf(SEVERITY)) .remediationEffortMinutes(EFFORT) - .type(type) + .type(TYPE) .save(); } } diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneExternalIssueWithDetailsPerLineSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneExternalIssueWithDetailsPerLineSensor.java new file mode 100644 index 00000000000..0598b7a8d40 --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneExternalIssueWithDetailsPerLineSensor.java @@ -0,0 +1,83 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.xoo.rule; + +import org.sonar.api.batch.fs.FilePredicates; +import org.sonar.api.batch.fs.FileSystem; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.InputFile.Type; +import org.sonar.api.batch.rule.Severity; +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.NewExternalIssue; +import org.sonar.api.rule.RuleKey; +import org.sonar.api.rules.RuleType; +import org.sonar.xoo.Xoo; +import org.sonar.xoo.Xoo2; + +public class OneExternalIssueWithDetailsPerLineSensor implements Sensor { + public static final String RULE_KEY = "OneExternalIssueWithDetailsPerLine"; + public static final String ENGINE_KEY = "XooEngine"; + public static final String SEVERITY = "MAJOR"; + public static final Long EFFORT = 10l; + public static final RuleType TYPE = RuleType.BUG; + public static final String ACTIVATE_EXTERNAL_ISSUES = "sonar.oneExternalIssueWithDetailsPerLine.activate"; + private static final String NAME = "One External Issue Per Line"; + + @Override + public void describe(SensorDescriptor descriptor) { + descriptor + .name(NAME) + .onlyOnLanguages(Xoo.KEY, Xoo2.KEY) + .onlyWhenConfiguration(c -> c.getBoolean(ACTIVATE_EXTERNAL_ISSUES).orElse(false)); + } + + @Override + public void execute(SensorContext context) { + analyse(context, Xoo.KEY, XooRulesDefinition.XOO_REPOSITORY); + analyse(context, Xoo2.KEY, XooRulesDefinition.XOO2_REPOSITORY); + } + + private void analyse(SensorContext context, String language, String repo) { + FileSystem fs = context.fileSystem(); + FilePredicates p = fs.predicates(); + for (InputFile file : fs.inputFiles(p.and(p.hasLanguages(language), p.hasType(Type.MAIN)))) { + createIssues(file, context, repo); + } + } + + private void createIssues(InputFile file, SensorContext context, String repo) { + RuleKey ruleKey = RuleKey.of(repo, RULE_KEY); + for (int line = 1; line <= file.lines(); line++) { + NewExternalIssue newIssue = context.newExternalIssue(); + newIssue + .forRule(ruleKey) + .at(newIssue.newLocation() + .on(file) + .at(file.selectLine(line)) + .message("This issue is generated on each line and the rule contains details")) + .severity(Severity.valueOf(SEVERITY)) + .remediationEffortMinutes(EFFORT) + .type(TYPE) + .save(); + } + } +} 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 13c238cae7a..4f119f9ebe0 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 @@ -35,6 +35,7 @@ public class XooRulesDefinition implements RulesDefinition { public static final String XOO_REPOSITORY = "xoo"; public static final String XOO2_REPOSITORY = "xoo2"; + public static final String XOO_EXTERNAL_REPOSITORY = "xoo"; private static final String TEN_MIN = "10min"; @@ -42,6 +43,7 @@ public class XooRulesDefinition implements RulesDefinition { public void define(Context context) { defineRulesXoo(context); defineRulesXoo2(context); + defineRulesXooExternal(context); } private static void defineRulesXoo2(Context context) { @@ -156,4 +158,17 @@ public class XooRulesDefinition implements RulesDefinition { } + private static void defineRulesXooExternal(Context context) { + NewRepository repo = context.createExternalRepository(XOO_EXTERNAL_REPOSITORY, Xoo.KEY).setName("XooExternal"); + + repo.createRule(OneExternalIssueWithDetailsPerLineSensor.RULE_KEY) + .setSeverity(OneExternalIssueWithDetailsPerLineSensor.SEVERITY) + .setType(OneExternalIssueWithDetailsPerLineSensor.TYPE) + .setScope(RuleScope.ALL) + .setHtmlDescription("Generates one external issue in each line") + .setName("One external issue per line"); + + repo.done(); + } + } diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java index dacd5b3f575..e66d56e45c8 100644 --- a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java @@ -62,6 +62,6 @@ public class XooPluginTest { SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.parse("7.2"), SonarQubeSide.SCANNER); Plugin.Context context = new PluginContextImpl.Builder().setSonarRuntime(runtime).build(); new XooPlugin().define(context); - assertThat(context.getExtensions()).hasSize(54).contains(OneExternalIssuePerLineSensor.class); + assertThat(context.getExtensions()).hasSize(55).contains(OneExternalIssuePerLineSensor.class); } } diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/XooRulesDefinitionTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/XooRulesDefinitionTest.java index 7de2430f0c1..9658ab8e3a4 100644 --- a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/XooRulesDefinitionTest.java +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/XooRulesDefinitionTest.java @@ -51,6 +51,15 @@ public class XooRulesDefinitionTest { assertThat(rule.debtRemediationFunction().baseEffort()).isNull(); assertThat(rule.gapDescription()).isNotEmpty(); } + + @Test + public void define_xooExternal_rules() { + RulesDefinition.Repository repo = context.repository("external_xoo"); + assertThat(repo).isNotNull(); + assertThat(repo.name()).isEqualTo("XooExternal"); + assertThat(repo.language()).isEqualTo("xoo"); + assertThat(repo.rules()).hasSize(1); + } @Test public void define_xoo2_rules() { |