aboutsummaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2018-09-13 15:10:42 +0200
committerSonarTech <sonartech@sonarsource.com>2018-09-24 20:20:58 +0200
commit04963a43f10710b0992c1ef708183f05723c7025 (patch)
treeebc1036d6fd7b569fed30f62591184f1cf30200a /plugins
parent1cacbb1d2ed72d720058580746d678ba8da1b453 (diff)
downloadsonarqube-04963a43f10710b0992c1ef708183f05723c7025.tar.gz
sonarqube-04963a43f10710b0992c1ef708183f05723c7025.zip
SONAR-11210 Display organization specific ad hoc rule info
Diffstat (limited to 'plugins')
-rw-r--r--plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java4
-rw-r--r--plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OnePredefinedAndAdHocRuleExternalIssuePerLineSensor.java82
-rw-r--r--plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java4
3 files changed, 87 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 cc66f2d9c1d..25ee11d763e 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
@@ -49,7 +49,6 @@ 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.OnePredefinedRuleExternalIssuePerLineSensor;
import org.sonar.xoo.rule.OneIssueOnDirPerFileSensor;
import org.sonar.xoo.rule.OneIssuePerDirectorySensor;
import org.sonar.xoo.rule.OneIssuePerFileSensor;
@@ -57,6 +56,8 @@ import org.sonar.xoo.rule.OneIssuePerLineSensor;
import org.sonar.xoo.rule.OneIssuePerModuleSensor;
import org.sonar.xoo.rule.OneIssuePerTestFileSensor;
import org.sonar.xoo.rule.OneIssuePerUnknownFileSensor;
+import org.sonar.xoo.rule.OnePredefinedAndAdHocRuleExternalIssuePerLineSensor;
+import org.sonar.xoo.rule.OnePredefinedRuleExternalIssuePerLineSensor;
import org.sonar.xoo.rule.OneVulnerabilityIssuePerModuleSensor;
import org.sonar.xoo.rule.RandomAccessSensor;
import org.sonar.xoo.rule.SaveDataTwiceSensor;
@@ -169,6 +170,7 @@ public class XooPlugin implements Plugin {
context.addExtensions(
OneExternalIssuePerLineSensor.class,
OnePredefinedRuleExternalIssuePerLineSensor.class,
+ OnePredefinedAndAdHocRuleExternalIssuePerLineSensor.class,
SignificantCodeSensor.class);
}
if (context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(7, 3))) {
diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OnePredefinedAndAdHocRuleExternalIssuePerLineSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OnePredefinedAndAdHocRuleExternalIssuePerLineSensor.java
new file mode 100644
index 00000000000..b522c299f00
--- /dev/null
+++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OnePredefinedAndAdHocRuleExternalIssuePerLineSensor.java
@@ -0,0 +1,82 @@
+/*
+ * 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.rules.RuleType;
+import org.sonar.xoo.Xoo;
+
+public class OnePredefinedAndAdHocRuleExternalIssuePerLineSensor implements Sensor {
+
+ public static final String ACTIVATE = "sonar.onePredefinedAndAdHocRuleExternalIssuePerLine.activate";
+ private static final String NAME = "One External Issue Per Line With A Predefined And An AdHoc Rule";
+
+ @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(OnePredefinedRuleExternalIssuePerLineSensor.ENGINE_ID)
+ .ruleId(OnePredefinedRuleExternalIssuePerLineSensor.RULE_ID)
+ .at(newIssue.newLocation()
+ .on(file)
+ .at(file.selectLine(line))
+ .message("This issue is generated on each line and the rule is predefined"))
+ .severity(Severity.valueOf(OnePredefinedRuleExternalIssuePerLineSensor.SEVERITY))
+ .remediationEffortMinutes(OnePredefinedRuleExternalIssuePerLineSensor.EFFORT)
+ .type(OnePredefinedRuleExternalIssuePerLineSensor.TYPE)
+ .save();
+
+ // Even if the issue is on a predefined rule, the sensor is declaring an adHoc rule => this info should be ignored
+ context.newAdHocRule()
+ .engineId(OnePredefinedRuleExternalIssuePerLineSensor.ENGINE_ID)
+ .ruleId(OnePredefinedRuleExternalIssuePerLineSensor.RULE_ID)
+ .name("An ad hoc rule")
+ .description("blah blah")
+ .severity(Severity.BLOCKER)
+ .type(RuleType.BUG)
+ .save();
+ }
+ }
+}
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 fadb287de47..f515a6859f7 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
@@ -60,7 +60,7 @@ public class XooPluginTest {
Plugin.Context context = new PluginContextImpl.Builder().setSonarRuntime(runtime).build();
new XooPlugin().define(context);
assertThat(getExtensions(context))
- .hasSize(52)
+ .hasSize(53)
.contains(OneExternalIssuePerLineSensor.class);
}
@@ -70,7 +70,7 @@ public class XooPluginTest {
Plugin.Context context = new PluginContextImpl.Builder().setSonarRuntime(runtime).build();
new XooPlugin().define(context);
assertThat(getExtensions(context))
- .hasSize(53)
+ .hasSize(54)
.contains(OneExternalIssuePerLineSensor.class);
}