From 66041bc94f8c7c8e0a2ab0aafe3895e3b1b98df1 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 18 Sep 2013 16:11:11 +0200 Subject: SONAR-4689 Batch API to get issues of the current module --- .../java/org/sonar/batch/index/DefaultIndex.java | 10 +- .../org/sonar/batch/issue/DefaultIssuable.java | 8 +- .../org/sonar/batch/issue/DefaultModuleIssues.java | 119 +++++++++++++++++++++ .../org/sonar/batch/issue/IssuableFactory.java | 8 +- .../java/org/sonar/batch/issue/ScanIssues.java | 91 ---------------- .../org/sonar/batch/scan/ModuleScanContainer.java | 6 +- 6 files changed, 135 insertions(+), 107 deletions(-) create mode 100644 sonar-batch/src/main/java/org/sonar/batch/issue/DefaultModuleIssues.java delete mode 100644 sonar-batch/src/main/java/org/sonar/batch/issue/ScanIssues.java (limited to 'sonar-batch/src/main/java/org') diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java index caa4a98b0b2..ca8cf60059c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java @@ -49,7 +49,7 @@ import org.sonar.batch.DefaultResourceCreationLock; import org.sonar.batch.ProjectTree; import org.sonar.batch.ResourceFilters; import org.sonar.batch.issue.DeprecatedViolations; -import org.sonar.batch.issue.ScanIssues; +import org.sonar.batch.issue.DefaultModuleIssues; import org.sonar.core.component.ComponentKeys; import org.sonar.core.component.ScanGraph; @@ -82,7 +82,7 @@ public class DefaultIndex extends SonarIndex { private Map> incomingDependenciesByResource = Maps.newHashMap(); private ProjectTree projectTree; private final DeprecatedViolations deprecatedViolations; - private ScanIssues scanIssues; + private DefaultModuleIssues moduleIssues; public DefaultIndex(PersistenceManager persistence, DefaultResourceCreationLock lock, ProjectTree projectTree, MetricFinder metricFinder, ScanGraph graph, DeprecatedViolations deprecatedViolations) { @@ -124,12 +124,12 @@ public class DefaultIndex extends SonarIndex { return currentProject; } - public void setCurrentProject(Project project, ResourceFilters resourceFilters, ScanIssues scanIssues) { + public void setCurrentProject(Project project, ResourceFilters resourceFilters, DefaultModuleIssues moduleIssues) { this.currentProject = project; // the following components depend on the current module, so they need to be reloaded. this.resourceFilters = resourceFilters; - this.scanIssues = scanIssues; + this.moduleIssues = moduleIssues; } /** @@ -378,7 +378,7 @@ public class DefaultIndex extends SonarIndex { violation.setSeverity(null); violation.setResource(bucket.getResource()); - scanIssues.initAndAddViolation(violation); + moduleIssues.initAndAddViolation(violation); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssuable.java b/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssuable.java index 0be18b5dff9..5b7c4c8092c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssuable.java +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssuable.java @@ -33,13 +33,13 @@ import java.util.List; */ public class DefaultIssuable implements Issuable { - private final ScanIssues scanIssues; + private final DefaultModuleIssues moduleIssues; private final IssueCache cache; private final Component component; - DefaultIssuable(Component component, ScanIssues scanIssues, IssueCache cache) { + DefaultIssuable(Component component, DefaultModuleIssues moduleIssues, IssueCache cache) { this.component = component; - this.scanIssues = scanIssues; + this.moduleIssues = moduleIssues; this.cache = cache; } @@ -50,7 +50,7 @@ public class DefaultIssuable implements Issuable { @Override public boolean addIssue(Issue issue) { - return scanIssues.initAndAddIssue((DefaultIssue) issue); + return moduleIssues.initAndAddIssue((DefaultIssue) issue); } @SuppressWarnings("unchecked") diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultModuleIssues.java b/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultModuleIssues.java new file mode 100644 index 00000000000..cdd9f22223a --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultModuleIssues.java @@ -0,0 +1,119 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 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.issue; + +import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; +import org.sonar.api.issue.Issue; +import org.sonar.api.issue.ModuleIssues; +import org.sonar.api.issue.internal.DefaultIssue; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.resources.Project; +import org.sonar.api.rule.RuleKey; +import org.sonar.api.rules.ActiveRule; +import org.sonar.api.rules.Violation; +import org.sonar.core.issue.DefaultIssueBuilder; + +import javax.annotation.Nullable; + +/** + * Initialize the issues raised during scan. + */ +public class DefaultModuleIssues implements ModuleIssues { + + private final RulesProfile qProfile; + private final IssueCache cache; + private final Project project; + private final IssueFilters filters; + + public DefaultModuleIssues(RulesProfile qProfile, IssueCache cache, Project project, IssueFilters filters) { + this.qProfile = qProfile; + this.cache = cache; + this.project = project; + this.filters = filters; + } + + public boolean initAndAddIssue(DefaultIssue issue) { + return initAndAddIssue(issue, null); + } + + public boolean initAndAddViolation(Violation violation) { + DefaultIssue issue = newIssue(violation); + return initAndAddIssue(issue, violation); + } + + private DefaultIssue newIssue(Violation violation) { + return (DefaultIssue) new DefaultIssueBuilder() + .componentKey(violation.getResource().getEffectiveKey()) + .ruleKey(RuleKey.of(violation.getRule().getRepositoryKey(), violation.getRule().getKey())) + .effortToFix(violation.getCost()) + .line(violation.getLineId()) + .message(violation.getMessage()) + .severity(violation.getSeverity() != null ? violation.getSeverity().name() : null) + .build(); + } + + private boolean initAndAddIssue(DefaultIssue issue, @Nullable Violation violation) { + // TODO fail fast : if rule does not exist + + ActiveRule activeRule = qProfile.getActiveRule(issue.ruleKey().repository(), issue.ruleKey().rule()); + if (activeRule == null || activeRule.getRule() == null) { + // rule does not exist or is not enabled -> ignore the issue + return false; + } + issue.setCreationDate(project.getAnalysisDate()); + issue.setUpdateDate(project.getAnalysisDate()); + if (issue.severity() == null) { + issue.setSeverity(activeRule.getSeverity().name()); + } + + if (filters.accept(issue, violation)) { + cache.put(issue); + return true; + } + return false; + } + + @Override + public Iterable issues() { + return (Iterable) Iterables.filter(cache.all(), new ModulePredicate(false)); + } + + @Override + public Iterable resolvedIssues() { + return (Iterable) Iterables.filter(cache.all(), new ModulePredicate(true)); + } + + private class ModulePredicate implements Predicate { + private final boolean resolved; + + private ModulePredicate(boolean resolved) { + this.resolved = resolved; + } + + @Override + public boolean apply(@Nullable DefaultIssue issue) { + if (issue != null && (issue.componentKey().equals(project.getEffectiveKey()) || issue.componentKey().startsWith(project.getEffectiveKey() + ":"))) { + return resolved ? issue.resolution() != null : issue.resolution()==null; + } + return false; + } + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/IssuableFactory.java b/sonar-batch/src/main/java/org/sonar/batch/issue/IssuableFactory.java index 3e13158a761..cd34582c24c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/IssuableFactory.java +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/IssuableFactory.java @@ -33,12 +33,12 @@ import javax.annotation.CheckForNull; */ public class IssuableFactory extends PerspectiveBuilder { - private final ScanIssues scanIssues; + private final DefaultModuleIssues moduleIssues; private final IssueCache cache; - public IssuableFactory(ScanIssues scanIssues, IssueCache cache) { + public IssuableFactory(DefaultModuleIssues moduleIssues, IssueCache cache) { super(Issuable.class); - this.scanIssues = scanIssues; + this.moduleIssues = moduleIssues; this.cache = cache; } @@ -49,6 +49,6 @@ public class IssuableFactory extends PerspectiveBuilder { if (component instanceof ResourceComponent) { supported = Scopes.isHigherThanOrEquals(((ResourceComponent) component).scope(), Scopes.FILE); } - return supported ? new DefaultIssuable(component, scanIssues, cache) : null; + return supported ? new DefaultIssuable(component, moduleIssues, cache) : null; } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ScanIssues.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ScanIssues.java deleted file mode 100644 index a3b53260897..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/ScanIssues.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2013 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.issue; - -import org.sonar.api.BatchComponent; -import org.sonar.api.issue.internal.DefaultIssue; -import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.resources.Project; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.rules.ActiveRule; -import org.sonar.api.rules.Violation; -import org.sonar.core.issue.DefaultIssueBuilder; - -import javax.annotation.Nullable; - -/** - * Initialize the issues raised during scan. - */ -public class ScanIssues implements BatchComponent { - - private final RulesProfile qProfile; - private final IssueCache cache; - private final Project project; - private final IssueFilters filters; - - public ScanIssues(RulesProfile qProfile, IssueCache cache, Project project, IssueFilters filters) { - this.qProfile = qProfile; - this.cache = cache; - this.project = project; - this.filters = filters; - } - - public boolean initAndAddIssue(DefaultIssue issue) { - return initAndAddIssue(issue, null); - } - - public boolean initAndAddViolation(Violation violation) { - DefaultIssue issue = newIssue(violation); - return initAndAddIssue(issue, violation); - } - - private DefaultIssue newIssue(Violation violation) { - return (DefaultIssue) new DefaultIssueBuilder() - .componentKey(violation.getResource().getEffectiveKey()) - .ruleKey(RuleKey.of(violation.getRule().getRepositoryKey(), violation.getRule().getKey())) - .effortToFix(violation.getCost()) - .line(violation.getLineId()) - .message(violation.getMessage()) - .severity(violation.getSeverity() != null ? violation.getSeverity().name() : null) - .build(); - } - - private boolean initAndAddIssue(DefaultIssue issue, @Nullable Violation violation) { - // TODO fail fast : if rule does not exist - - ActiveRule activeRule = qProfile.getActiveRule(issue.ruleKey().repository(), issue.ruleKey().rule()); - if (activeRule == null || activeRule.getRule() == null) { - // rule does not exist or is not enabled -> ignore the issue - return false; - } - issue.setCreationDate(project.getAnalysisDate()); - issue.setUpdateDate(project.getAnalysisDate()); - if (issue.severity() == null) { - issue.setSeverity(activeRule.getSeverity().name()); - } - - if (filters.accept(issue, violation)) { - cache.put(issue); - return true; - } - return false; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java index 0075238fd4e..543f01e2940 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java @@ -42,7 +42,7 @@ import org.sonar.batch.index.DefaultIndex; import org.sonar.batch.index.ResourcePersister; import org.sonar.batch.issue.IssuableFactory; import org.sonar.batch.issue.IssueFilters; -import org.sonar.batch.issue.ScanIssues; +import org.sonar.batch.issue.DefaultModuleIssues; import org.sonar.batch.phases.PhaseExecutor; import org.sonar.batch.phases.PhasesTimeProfiler; import org.sonar.batch.scan.filesystem.*; @@ -113,7 +113,7 @@ public class ModuleScanContainer extends ComponentContainer { new ProfileProvider(), // issues - ScanIssues.class, + DefaultModuleIssues.class, IssuableFactory.class, ScanPerspectives.class @@ -140,7 +140,7 @@ public class ModuleScanContainer extends ComponentContainer { DefaultIndex index = getComponentByType(DefaultIndex.class); index.setCurrentProject(module, getComponentByType(ResourceFilters.class), - getComponentByType(ScanIssues.class)); + getComponentByType(DefaultModuleIssues.class)); getComponentByType(PhaseExecutor.class).execute(module); } -- cgit v1.2.3