From a3e3f905dc3231d48beb5b9abb5ae27120ef7b61 Mon Sep 17 00:00:00 2001 From: Alain Kermis Date: Wed, 27 Sep 2023 16:41:05 +0200 Subject: SONAR-20552 Introduce new generic issue import format in scanner --- .../xoo/rule/OneExternalIssueCctPerLineSensor.java | 87 ++++++++++++++++++++++ ...redefinedRuleExternalIssueCctPerLineSensor.java | 70 +++++++++++++++++ .../org/sonar/xoo/rule/XooRulesDefinition.java | 13 ++++ .../org/sonar/xoo/rule/XooRulesDefinitionTest.java | 2 +- 4 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneExternalIssueCctPerLineSensor.java create mode 100644 plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OnePredefinedRuleExternalIssueCctPerLineSensor.java (limited to 'plugins') diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneExternalIssueCctPerLineSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneExternalIssueCctPerLineSensor.java new file mode 100644 index 00000000000..d1ea092440f --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneExternalIssueCctPerLineSensor.java @@ -0,0 +1,87 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 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.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.xoo.Xoo; + +import static org.sonar.api.issue.impact.Severity.LOW; +import static org.sonar.api.issue.impact.Severity.MEDIUM; +import static org.sonar.api.issue.impact.SoftwareQuality.MAINTAINABILITY; +import static org.sonar.api.issue.impact.SoftwareQuality.RELIABILITY; +import static org.sonar.api.rules.CleanCodeAttribute.CLEAR; + +public class OneExternalIssueCctPerLineSensor implements Sensor { + public static final String RULE_ID = "OneExternalIssueWithPerLineSensor"; + public static final String ENGINE_ID = "XooEngine"; + public static final Long EFFORT = 10L; + public static final String ACTIVATE = "sonar.oneExternalIssueWithPerLineSensor.activate"; + public static final String REGISTER_AD_HOC_RULE = "sonar.oneExternalIssueWithPerLineSensor.adhocRule"; + private static final String NAME = "One External Issue Per Line in Clean Code Taxonomy format"; + + @Override + public void describe(SensorDescriptor descriptor) { + descriptor + .name(NAME) + .onlyOnLanguages(Xoo.KEY) + .onlyWhenConfiguration(c -> c.getBoolean(ACTIVATE).orElse(false)); + } + + @Override + public void execute(SensorContext context) { + FileSystem fs = context.fileSystem(); + FilePredicates p = fs.predicates(); + for (InputFile file : fs.inputFiles(p.and(p.hasLanguages(Xoo.KEY), p.hasType(InputFile.Type.MAIN)))) { + createIssues(file, context); + } + if (context.config().getBoolean(REGISTER_AD_HOC_RULE).orElse(false)) { + context.newAdHocRule() + .engineId(ENGINE_ID) + .ruleId(RULE_ID) + .name("An ad hoc rule") + .description("blah blah") + .cleanCodeAttribute(CLEAR) + .addDefaultImpact(MAINTAINABILITY, MEDIUM) + .addDefaultImpact(RELIABILITY, LOW) + .save(); + } + } + + private static void createIssues(InputFile file, SensorContext context) { + for (int line = 1; line <= file.lines(); line++) { + NewExternalIssue newIssue = context.newExternalIssue(); + newIssue + .engineId(ENGINE_ID) + .ruleId(RULE_ID) + .at(newIssue.newLocation() + .on(file) + .at(file.selectLine(line)) + .message("This issue is generated on each line")) + .remediationEffortMinutes(EFFORT) + .save(); + } + } +} diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OnePredefinedRuleExternalIssueCctPerLineSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OnePredefinedRuleExternalIssueCctPerLineSensor.java new file mode 100644 index 00000000000..772c272cf58 --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OnePredefinedRuleExternalIssueCctPerLineSensor.java @@ -0,0 +1,70 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 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.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.xoo.Xoo; + +public class OnePredefinedRuleExternalIssueCctPerLineSensor implements Sensor { + public static final String RULE_ID = "OnePredefinedRuleExternalIssueCctPerLine"; + public static final String ENGINE_ID = "XooEngine"; + public static final Long EFFORT = 10L; + public static final String ACTIVATE = "sonar.onePredefinedRuleExternalIssueCctPerLine.activate"; + private static final String NAME = "One External Issue Per Line With A Predefined Rule in Clean Code Taxonomy format"; + + @Override + public void describe(SensorDescriptor descriptor) { + descriptor + .name(NAME) + .onlyOnLanguages(Xoo.KEY) + .onlyWhenConfiguration(c -> c.getBoolean(ACTIVATE).orElse(false)); + } + + @Override + public void execute(SensorContext context) { + FileSystem fs = context.fileSystem(); + FilePredicates p = fs.predicates(); + for (InputFile file : fs.inputFiles(p.and(p.hasLanguages(Xoo.KEY), p.hasType(Type.MAIN)))) { + createIssues(file, context); + } + } + + private static void createIssues(InputFile file, SensorContext context) { + for (int line = 1; line <= file.lines(); line++) { + NewExternalIssue newIssue = context.newExternalIssue(); + newIssue + .engineId(ENGINE_ID) + .ruleId(RULE_ID) + .at(newIssue.newLocation() + .on(file) + .at(file.selectLine(line)) + .message("This issue is generated on each line and the rule is predefined")) + .remediationEffortMinutes(EFFORT) + .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 c7315d4d991..6f5b2549ee0 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 @@ -76,6 +76,7 @@ public class XooRulesDefinition implements RulesDefinition { defineRulesXoo(context); defineRulesXoo2(context); defineRulesXooExternal(context); + defineRulesXooExternalWithCct(context); } private static void defineRulesXoo2(Context context) { @@ -319,6 +320,18 @@ public class XooRulesDefinition implements RulesDefinition { repo.done(); } + private static void defineRulesXooExternalWithCct(Context context) { + NewRepository repo = context.createExternalRepository(OneExternalIssueCctPerLineSensor.ENGINE_ID, Xoo.KEY).setName(OneExternalIssueCctPerLineSensor.ENGINE_ID); + + repo.createRule(OnePredefinedRuleExternalIssueCctPerLineSensor.RULE_ID) + .setScope(RuleScope.ALL) + .setHtmlDescription("Generates one external issue in each line") + .addDescriptionSection(descriptionSection(INTRODUCTION_SECTION_KEY, "Generates one external issue in each line")) + .setName("One external issue per line"); + + repo.done(); + } + private static void defineRulesXooExternal(Context context) { NewRepository repo = context.createExternalRepository(OneExternalIssuePerLineSensor.ENGINE_ID, Xoo.KEY).setName(OneExternalIssuePerLineSensor.ENGINE_ID); 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 fa287bcd82c..679292b1fbe 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 @@ -102,7 +102,7 @@ public class XooRulesDefinitionTest { assertThat(repo).isNotNull(); assertThat(repo.name()).isEqualTo("XooEngine"); assertThat(repo.language()).isEqualTo("xoo"); - assertThat(repo.rules()).hasSize(1); + assertThat(repo.rules()).hasSize(2); } @Test -- cgit v1.2.3