]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5389 Reintroduce Issue filters
authorJulien HENRY <julien.henry@sonarsource.com>
Thu, 19 Jun 2014 15:59:49 +0000 (17:59 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Thu, 19 Jun 2014 16:01:24 +0000 (18:01 +0200)
sonar-batch/src/main/java/org/sonar/batch/issue/IssueFilters.java
sonar-batch/src/main/java/org/sonar/batch/scan/AnalyzerContextAdaptor.java
sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultAnalyzerContext.java
sonar-batch/src/main/java/org/sonar/batch/scan2/ModuleScanContainer.java
sonar-batch/src/main/java/org/sonar/batch/scan2/ModuleScanExecutor.java [new file with mode: 0644]
sonar-batch/src/main/java/org/sonar/batch/scan2/Phase2Executor.java [deleted file]
sonar-batch/src/main/java/org/sonar/batch/scan2/ProjectScanContainer.java
sonar-batch/src/test/java/org/sonar/batch/mediumtest/xoo/XooMediumTest.java
sonar-batch/src/test/resources/org/sonar/batch/medium/simple-project/sonar-project.properties [deleted file]
sonar-batch/src/test/resources/org/sonar/batch/medium/simple-project/sources/Fake.java [deleted file]
sonar-core/src/main/java/org/sonar/core/issue/DefaultIssueBuilder.java

index b43f98f6d2770dc8fd33897039c6241b3714985d..a6987f2368ebf05a7a35f210662e5b7d8c35ee2b 100644 (file)
@@ -19,7 +19,7 @@
  */
 package org.sonar.batch.issue;
 
-import org.sonar.api.BatchExtension;
+import org.sonar.api.BatchComponent;
 import org.sonar.api.issue.batch.IssueFilter;
 import org.sonar.api.issue.internal.DefaultIssue;
 import org.sonar.api.rules.Violation;
@@ -27,14 +27,15 @@ import org.sonar.batch.ViolationFilters;
 
 import javax.annotation.Nullable;
 
-public class IssueFilters implements BatchExtension {
+public class IssueFilters implements BatchComponent {
 
   private final ViolationFilters deprecatedFilters;
   private final DeprecatedViolations deprecatedViolations;
   private final org.sonar.api.issue.IssueFilter[] exclusionFilters;
   private final IssueFilter[] filters;
 
-  public IssueFilters(@Nullable ViolationFilters deprecatedFilters, @Nullable DeprecatedViolations deprecatedViolations, org.sonar.api.issue.IssueFilter[] exclusionFilters, IssueFilter[] filters) {
+  public IssueFilters(@Nullable ViolationFilters deprecatedFilters, @Nullable DeprecatedViolations deprecatedViolations, org.sonar.api.issue.IssueFilter[] exclusionFilters,
+    IssueFilter[] filters) {
     this.deprecatedFilters = deprecatedFilters;
     this.deprecatedViolations = deprecatedViolations;
     this.exclusionFilters = exclusionFilters;
index b4231b5c252458ea6e40433dcb897eeb772e7a9d..eb76d3a269dc8377b2ae5dafc0980fa527567a8e 100644 (file)
@@ -19,8 +19,6 @@
  */
 package org.sonar.batch.scan;
 
-import org.sonar.api.batch.measure.Metric;
-
 import org.sonar.api.batch.SensorContext;
 import org.sonar.api.batch.analyzer.AnalyzerContext;
 import org.sonar.api.batch.analyzer.issue.AnalyzerIssue;
@@ -31,16 +29,19 @@ import org.sonar.api.batch.analyzer.measure.AnalyzerMeasureBuilder;
 import org.sonar.api.batch.analyzer.measure.internal.DefaultAnalyzerMeasureBuilder;
 import org.sonar.api.batch.fs.FileSystem;
 import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.measure.Metric;
 import org.sonar.api.batch.rule.ActiveRules;
 import org.sonar.api.component.ResourcePerspectives;
 import org.sonar.api.config.Settings;
 import org.sonar.api.issue.Issuable;
+import org.sonar.api.issue.internal.DefaultIssue;
 import org.sonar.api.measures.Measure;
 import org.sonar.api.measures.MetricFinder;
 import org.sonar.api.resources.File;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
 import org.sonar.api.rule.RuleKey;
+import org.sonar.core.issue.DefaultIssueBuilder;
 
 import java.io.Serializable;
 
@@ -194,12 +195,18 @@ public class AnalyzerContextAdaptor implements AnalyzerContext {
       r = project;
     }
     Issuable issuable = perspectives.as(Issuable.class, r);
-    issuable.addIssue(issuable.newIssueBuilder()
+    issuable.addIssue(toDefaultIssue(project.getKey(), r.getKey(), issue));
+  }
+
+  public static DefaultIssue toDefaultIssue(String projectKey, String componentKey, AnalyzerIssue issue) {
+    return new DefaultIssueBuilder()
+      .componentKey(componentKey)
+      .projectKey(projectKey)
       .ruleKey(RuleKey.of(issue.ruleKey().repository(), issue.ruleKey().rule()))
       .effortToFix(issue.effortToFix())
       .line(issue.line())
       .message(issue.message())
-      .build());
+      .build();
   }
 
 }
index d9f03c17e50604a7605bb2d06a0eb0a93497f1bb..cb46ec2204c0d00a090e27093a157b0db4c56eee 100644 (file)
@@ -34,6 +34,8 @@ import org.sonar.api.batch.fs.InputFile;
 import org.sonar.api.batch.measure.Metric;
 import org.sonar.api.batch.rule.ActiveRules;
 import org.sonar.api.config.Settings;
+import org.sonar.batch.issue.IssueFilters;
+import org.sonar.batch.scan.AnalyzerContextAdaptor;
 import org.sonar.core.component.ComponentKeys;
 
 import java.io.Serializable;
@@ -42,19 +44,21 @@ public class DefaultAnalyzerContext implements AnalyzerContext {
 
   private final AnalyzerMeasureCache measureCache;
   private final AnalyzerIssueCache issueCache;
-  private ProjectDefinition def;
-  private Settings settings;
-  private FileSystem fs;
-  private ActiveRules activeRules;
+  private final ProjectDefinition def;
+  private final Settings settings;
+  private final FileSystem fs;
+  private final ActiveRules activeRules;
+  private final IssueFilters issueFilters;
 
   public DefaultAnalyzerContext(ProjectDefinition def, AnalyzerMeasureCache measureCache, AnalyzerIssueCache issueCache,
-    Settings settings, FileSystem fs, ActiveRules activeRules) {
+    Settings settings, FileSystem fs, ActiveRules activeRules, IssueFilters issueFilters) {
     this.def = def;
     this.measureCache = measureCache;
     this.issueCache = issueCache;
     this.settings = settings;
     this.fs = fs;
     this.activeRules = activeRules;
+    this.issueFilters = issueFilters;
   }
 
   @Override
@@ -120,7 +124,8 @@ public class DefaultAnalyzerContext implements AnalyzerContext {
       resourceKey = def.getKey();
     }
 
-    issueCache.put(resourceKey, (DefaultAnalyzerIssue) issue);
+    if (issueFilters.accept(AnalyzerContextAdaptor.toDefaultIssue(def.getKey(), resourceKey, issue), null)) {
+      issueCache.put(resourceKey, (DefaultAnalyzerIssue) issue);
+    }
   }
-
 }
index fc6c0e85b3b8e7f67d71de4aa38ce4764a3b19e6..3ec356536ada76a28464d1622d295e39c992b22a 100644 (file)
@@ -78,8 +78,8 @@ public class ModuleScanContainer extends ComponentContainer {
       ModuleSettings.class,
 
       EventBus.class,
-      Phase2Executor.class,
-      Phase2Executor.getPhaseClasses(),
+      ModuleScanExecutor.class,
+      ModuleScanExecutor.getPhaseClasses(),
       moduleDefinition.getContainerExtensions(),
       AnalyzersExecutor.class,
 
@@ -133,7 +133,7 @@ public class ModuleScanContainer extends ComponentContainer {
 
   @Override
   protected void doAfterStart() {
-    getComponentByType(Phase2Executor.class).execute(moduleDefinition);
+    getComponentByType(ModuleScanExecutor.class).execute(moduleDefinition);
   }
 
 }
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan2/ModuleScanExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/scan2/ModuleScanExecutor.java
new file mode 100644 (file)
index 0000000..55423ee
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.batch.scan2;
+
+import org.sonar.api.batch.analyzer.AnalyzerContext;
+
+import com.google.common.collect.Lists;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.batch.issue.ignore.scanner.IssueExclusionsLoader;
+import org.sonar.batch.phases.SensorsExecutor;
+import org.sonar.batch.rule.QProfileVerifier;
+import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem;
+import org.sonar.batch.scan.filesystem.FileSystemLogger;
+
+import java.util.Collection;
+
+public final class ModuleScanExecutor {
+
+  public static final Logger LOGGER = LoggerFactory.getLogger(ModuleScanExecutor.class);
+
+  private final AnalyzersExecutor analyzersExecutor;
+  private final AnalyzerContext analyzerContext;
+  private final FileSystemLogger fsLogger;
+  private final DefaultModuleFileSystem fs;
+  private final QProfileVerifier profileVerifier;
+  private final IssueExclusionsLoader issueExclusionsLoader;
+
+  public ModuleScanExecutor(AnalyzersExecutor analyzersExecutor,
+    AnalyzerContext analyzerContext,
+    FileSystemLogger fsLogger, DefaultModuleFileSystem fs, QProfileVerifier profileVerifier,
+    IssueExclusionsLoader issueExclusionsLoader) {
+    this.analyzersExecutor = analyzersExecutor;
+    this.analyzerContext = analyzerContext;
+    this.fsLogger = fsLogger;
+    this.fs = fs;
+    this.profileVerifier = profileVerifier;
+    this.issueExclusionsLoader = issueExclusionsLoader;
+  }
+
+  public static Collection<Class> getPhaseClasses() {
+    return Lists.<Class>newArrayList(SensorsExecutor.class);
+  }
+
+  /**
+   * Executed on each module
+   */
+  public void execute(ProjectDefinition moduleDefinition) {
+    fsLogger.log();
+
+    // Index and lock the filesystem
+    fs.index();
+
+    // Log detected languages and their profiles after FS is indexed and languages detected
+    profileVerifier.execute();
+
+    // Initialize issue exclusions
+    issueExclusionsLoader.execute();
+
+    analyzersExecutor.execute(analyzerContext);
+  }
+}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan2/Phase2Executor.java b/sonar-batch/src/main/java/org/sonar/batch/scan2/Phase2Executor.java
deleted file mode 100644 (file)
index 0b6f207..0000000
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.batch.scan2;
-
-import org.sonar.api.batch.analyzer.AnalyzerContext;
-
-import com.google.common.collect.Lists;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonar.api.batch.bootstrap.ProjectDefinition;
-import org.sonar.batch.issue.ignore.scanner.IssueExclusionsLoader;
-import org.sonar.batch.phases.SensorsExecutor;
-import org.sonar.batch.rule.QProfileVerifier;
-import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem;
-import org.sonar.batch.scan.filesystem.FileSystemLogger;
-
-import java.util.Collection;
-
-public final class Phase2Executor {
-
-  public static final Logger LOGGER = LoggerFactory.getLogger(Phase2Executor.class);
-
-  private final AnalyzersExecutor analyzersExecutor;
-  private final AnalyzerContext analyzerContext;
-  private final FileSystemLogger fsLogger;
-  private final DefaultModuleFileSystem fs;
-  private final QProfileVerifier profileVerifier;
-  private final IssueExclusionsLoader issueExclusionsLoader;
-
-  public Phase2Executor(AnalyzersExecutor analyzersExecutor,
-    AnalyzerContext analyzerContext,
-    FileSystemLogger fsLogger, DefaultModuleFileSystem fs, QProfileVerifier profileVerifier,
-    IssueExclusionsLoader issueExclusionsLoader) {
-    this.analyzersExecutor = analyzersExecutor;
-    this.analyzerContext = analyzerContext;
-    this.fsLogger = fsLogger;
-    this.fs = fs;
-    this.profileVerifier = profileVerifier;
-    this.issueExclusionsLoader = issueExclusionsLoader;
-  }
-
-  public static Collection<Class> getPhaseClasses() {
-    return Lists.<Class>newArrayList(SensorsExecutor.class);
-  }
-
-  /**
-   * Executed on each module
-   */
-  public void execute(ProjectDefinition moduleDefinition) {
-    fsLogger.log();
-
-    // Index and lock the filesystem
-    fs.index();
-
-    // Log detected languages and their profiles after FS is indexed and languages detected
-    profileVerifier.execute();
-
-    // Initialize issue exclusions
-    issueExclusionsLoader.execute();
-
-    analyzersExecutor.execute(analyzerContext);
-  }
-}
index 8dad42fd7e6c7546eefe044ea90cba524c7cf35e..28c5df4f76512bff7f699fae1bc82dd990d65b74 100644 (file)
@@ -106,6 +106,7 @@ public class ProjectScanContainer extends ComponentContainer {
   protected void doAfterStart() {
     ProjectReactor tree = getComponentByType(ProjectReactor.class);
     scanRecursively(tree.getRoot());
+
     getComponentByType(ScanTaskObservers.class).notifyEndOfScanTask();
   }
 
index 73c2b5636395e2636733548dc4ed144490552c9c..0b3cc9d77ef5512b506bcf26f55ed1f43e42c0e1 100644 (file)
@@ -65,6 +65,20 @@ public class XooMediumTest {
     assertThat(result.issues()).hasSize(24);
   }
 
+  @Test
+  public void testIssueExclusion() throws Exception {
+    File projectDir = new File(XooMediumTest.class.getResource("/org/sonar/batch/mediumtest/xoo/sample").toURI());
+
+    TaskResult result = tester
+      .newScanTask(new File(projectDir, "sonar-project.properties"))
+      .property("sonar.issue.ignore.allfile", "1")
+      .property("sonar.issue.ignore.allfile.1.fileRegexp", "object")
+      .start();
+
+    assertThat(result.measures()).hasSize(13);
+    assertThat(result.issues()).hasSize(19);
+  }
+
   @Test
   public void mediumTest() throws IOException {
 
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/medium/simple-project/sonar-project.properties b/sonar-batch/src/test/resources/org/sonar/batch/medium/simple-project/sonar-project.properties
deleted file mode 100644 (file)
index 69ccd8d..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-sonar.projectKey=com.foo.project
-sonar.projectName=Foo Project
-sonar.projectVersion=1.0-SNAPSHOT
-sonar.projectDescription=Description of Foo Project
-
-sonar.sources=sources
-sonar.libraries=libs/*.txt
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/medium/simple-project/sources/Fake.java b/sonar-batch/src/test/resources/org/sonar/batch/medium/simple-project/sources/Fake.java
deleted file mode 100644 (file)
index b2e6462..0000000
+++ /dev/null
@@ -1 +0,0 @@
-Fake
index 859ad804ad234819b5ade20c97e8dd7bc4f360fe..3d1eaea338338c96ee93c1299c09a591b5b5f5da 100644 (file)
@@ -53,50 +53,49 @@ public class DefaultIssueBuilder implements Issuable.IssueBuilder {
     return this;
   }
 
-
   public DefaultIssueBuilder projectKey(String projectKey) {
     this.projectKey = projectKey;
     return this;
   }
 
   @Override
-  public Issuable.IssueBuilder ruleKey(RuleKey ruleKey) {
+  public DefaultIssueBuilder ruleKey(RuleKey ruleKey) {
     this.ruleKey = ruleKey;
     return this;
   }
 
   @Override
-  public Issuable.IssueBuilder line(@Nullable Integer line) {
+  public DefaultIssueBuilder line(@Nullable Integer line) {
     this.line = line;
     return this;
   }
 
   @Override
-  public Issuable.IssueBuilder message(@Nullable String s) {
+  public DefaultIssueBuilder message(@Nullable String s) {
     this.message = s;
     return this;
   }
 
   @Override
-  public Issuable.IssueBuilder severity(@Nullable String severity) {
+  public DefaultIssueBuilder severity(@Nullable String severity) {
     this.severity = severity;
     return this;
   }
 
   @Override
-  public Issuable.IssueBuilder effortToFix(@Nullable Double d) {
+  public DefaultIssueBuilder effortToFix(@Nullable Double d) {
     this.effortToFix = d;
     return this;
   }
 
   @Override
-  public Issuable.IssueBuilder reporter(@Nullable String s) {
+  public DefaultIssueBuilder reporter(@Nullable String s) {
     this.reporter = s;
     return this;
   }
 
   @Override
-  public Issuable.IssueBuilder attribute(String key, @Nullable String value) {
+  public DefaultIssueBuilder attribute(String key, @Nullable String value) {
     if (attributes == null) {
       attributes = Maps.newLinkedHashMap();
     }