diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2018-09-04 16:08:53 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2018-09-24 20:20:58 +0200 |
commit | cfba7fcb6500d8217bd81ecfcb8f47ec48ad55f2 (patch) | |
tree | 81398a80d0269b523e630495a580daf8ac144228 /plugins | |
parent | 326b30334f0f5c0bb6a9565a3f6b367695bb1087 (diff) | |
download | sonarqube-cfba7fcb6500d8217bd81ecfcb8f47ec48ad55f2.tar.gz sonarqube-cfba7fcb6500d8217bd81ecfcb8f47ec48ad55f2.zip |
SONAR-11209 Allow sensors to provide ad hoc rule metadata for external issues
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java | 4 | ||||
-rw-r--r-- | plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneExternalIssuePerLineSensor.java | 39 | ||||
-rw-r--r-- | plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OnePredefinedRuleExternalIssuePerLineSensor.java (renamed from plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneExternalIssueWithDetailsPerLineSensor.java) | 33 | ||||
-rw-r--r-- | plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/XooRulesDefinition.java | 9 | ||||
-rw-r--r-- | plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/XooRulesDefinitionTest.java | 5 |
5 files changed, 42 insertions, 48 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 50907147b20..cc66f2d9c1d 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,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.OnePredefinedRuleExternalIssuePerLineSensor; import org.sonar.xoo.rule.OneIssueOnDirPerFileSensor; import org.sonar.xoo.rule.OneIssuePerDirectorySensor; import org.sonar.xoo.rule.OneIssuePerFileSensor; @@ -168,7 +168,7 @@ public class XooPlugin implements Plugin { if (context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(7, 2))) { context.addExtensions( OneExternalIssuePerLineSensor.class, - OneExternalIssueWithDetailsPerLineSensor.class, + OnePredefinedRuleExternalIssuePerLineSensor.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/OneExternalIssuePerLineSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneExternalIssuePerLineSensor.java index c8d09704371..c75a8c7a169 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 @@ -22,54 +22,57 @@ 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 OneExternalIssuePerLineSensor implements Sensor { - public static final String RULE_KEY = "OneExternalIssuePerLine"; - public static final String ENGINE_KEY = "XooEngine"; + public static final String RULE_ID = "OneExternalIssuePerLine"; + public static final String ENGINE_ID = "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.oneExternalIssuePerLine.activate"; + public static final String ACTIVATE = "sonar.oneExternalIssuePerLine.activate"; + public static final String REGISTER_AD_HOC_RULE = "sonar.oneExternalIssuePerLine.adhocRule"; 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)); + .onlyOnLanguages(Xoo.KEY) + .onlyWhenConfiguration(c -> c.getBoolean(ACTIVATE).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); + 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") + .severity(Severity.BLOCKER) + .type(RuleType.BUG) + .save(); } } - private void createIssues(InputFile file, SensorContext context, String repo) { - RuleKey ruleKey = RuleKey.of(repo, RULE_KEY); + private static void createIssues(InputFile file, SensorContext context) { for (int line = 1; line <= file.lines(); line++) { NewExternalIssue newIssue = context.newExternalIssue(); newIssue - .forRule(ruleKey) + .engineId(ENGINE_ID) + .ruleId(RULE_ID) .at(newIssue.newLocation() .on(file) .at(file.selectLine(line)) 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/OnePredefinedRuleExternalIssuePerLineSensor.java index 0598b7a8d40..779674808cc 100644 --- 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/OnePredefinedRuleExternalIssuePerLineSensor.java @@ -28,52 +28,45 @@ 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 class OnePredefinedRuleExternalIssuePerLineSensor implements Sensor { + public static final String RULE_ID = "OnePredefinedRuleExternalIssuePerLine"; + public static final String ENGINE_ID = "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"; + public static final String ACTIVATE = "sonar.onePredefinedRuleExternalIssuePerLine.activate"; + private static final String NAME = "One External Issue Per Line With A Predefined Rule"; @Override public void describe(SensorDescriptor descriptor) { descriptor .name(NAME) - .onlyOnLanguages(Xoo.KEY, Xoo2.KEY) - .onlyWhenConfiguration(c -> c.getBoolean(ACTIVATE_EXTERNAL_ISSUES).orElse(false)); + .onlyOnLanguages(Xoo.KEY) + .onlyWhenConfiguration(c -> c.getBoolean(ACTIVATE).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); + for (InputFile file : fs.inputFiles(p.and(p.hasLanguages(Xoo.KEY), p.hasType(Type.MAIN)))) { + createIssues(file, context); } } - private void createIssues(InputFile file, SensorContext context, String repo) { - RuleKey ruleKey = RuleKey.of(repo, RULE_KEY); + private static void createIssues(InputFile file, SensorContext context) { for (int line = 1; line <= file.lines(); line++) { NewExternalIssue newIssue = context.newExternalIssue(); newIssue - .forRule(ruleKey) + .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 contains details")) + .message("This issue is generated on each line and the rule is predefined")) .severity(Severity.valueOf(SEVERITY)) .remediationEffortMinutes(EFFORT) .type(TYPE) 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 8991b04bc28..aee1ba3c3fa 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 @@ -38,7 +38,6 @@ 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"; @@ -183,11 +182,11 @@ public class XooRulesDefinition implements RulesDefinition { } private static void defineRulesXooExternal(Context context) { - NewRepository repo = context.createExternalRepository(XOO_EXTERNAL_REPOSITORY, Xoo.KEY).setName("XooExternal"); + NewRepository repo = context.createExternalRepository(OneExternalIssuePerLineSensor.ENGINE_ID, Xoo.KEY).setName(OneExternalIssuePerLineSensor.ENGINE_ID); - repo.createRule(OneExternalIssueWithDetailsPerLineSensor.RULE_KEY) - .setSeverity(OneExternalIssueWithDetailsPerLineSensor.SEVERITY) - .setType(OneExternalIssueWithDetailsPerLineSensor.TYPE) + repo.createRule(OnePredefinedRuleExternalIssuePerLineSensor.RULE_ID) + .setSeverity(OnePredefinedRuleExternalIssuePerLineSensor.SEVERITY) + .setType(OnePredefinedRuleExternalIssuePerLineSensor.TYPE) .setScope(RuleScope.ALL) .setHtmlDescription("Generates one external issue in each line") .setName("One external issue per line"); 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 0ded44031fc..d5df1600b56 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 @@ -21,7 +21,6 @@ package org.sonar.xoo.rule; import org.junit.Before; import org.junit.Test; -import org.sonar.api.SonarProduct; import org.sonar.api.SonarQubeSide; import org.sonar.api.internal.SonarRuntimeImpl; import org.sonar.api.server.debt.DebtRemediationFunction; @@ -73,9 +72,9 @@ public class XooRulesDefinitionTest { @Test public void define_xooExternal_rules() { - RulesDefinition.Repository repo = context.repository("external_xoo"); + RulesDefinition.Repository repo = context.repository("external_XooEngine"); assertThat(repo).isNotNull(); - assertThat(repo.name()).isEqualTo("XooExternal"); + assertThat(repo.name()).isEqualTo("XooEngine"); assertThat(repo.language()).isEqualTo("xoo"); assertThat(repo.rules()).hasSize(1); } |