3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.issue.filter;
22 import com.google.common.base.Splitter;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26 import org.sonar.api.ce.ComputeEngineSide;
27 import org.sonar.api.config.Settings;
28 import org.sonar.api.utils.log.Logger;
29 import org.sonar.api.utils.log.Loggers;
30 import org.sonar.core.issue.DefaultIssue;
31 import org.sonar.server.computation.task.projectanalysis.component.Component;
32 import org.sonar.server.computation.task.projectanalysis.component.SettingsRepository;
33 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
35 import static com.google.common.base.Preconditions.checkArgument;
36 import static com.google.common.base.Strings.isNullOrEmpty;
37 import static java.lang.String.format;
38 import static org.apache.commons.lang.StringUtils.defaultIfBlank;
39 import static org.sonar.core.config.IssueExclusionProperties.PATTERNS_MULTICRITERIA_EXCLUSION_KEY;
40 import static org.sonar.core.config.IssueExclusionProperties.PATTERNS_MULTICRITERIA_INCLUSION_KEY;
41 import static org.sonar.core.config.IssueExclusionProperties.RESOURCE_KEY;
42 import static org.sonar.core.config.IssueExclusionProperties.RULE_KEY;
43 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.FILE;
46 public class IssueFilter {
48 private static final Logger LOG = Loggers.get(IssueFilter.class);
50 private final List<IssuePattern> exclusionPatterns;
51 private final List<IssuePattern> inclusionPatterns;
53 public IssueFilter(TreeRootHolder treeRootHolder, SettingsRepository settingsRepository) {
54 Settings settings = settingsRepository.getSettings(treeRootHolder.getRoot());
55 this.exclusionPatterns = loadPatterns(PATTERNS_MULTICRITERIA_EXCLUSION_KEY, settings);
56 this.inclusionPatterns = loadPatterns(PATTERNS_MULTICRITERIA_INCLUSION_KEY, settings);
59 public boolean accept(DefaultIssue issue, Component component) {
60 if (component.getType() != FILE || (exclusionPatterns.isEmpty() && inclusionPatterns.isEmpty())) {
63 if (isExclude(issue, component)) {
66 return isInclude(issue, component);
69 private boolean isExclude(DefaultIssue issue, Component component) {
70 IssuePattern matchingPattern = null;
71 Iterator<IssuePattern> patternIterator = exclusionPatterns.iterator();
72 while (matchingPattern == null && patternIterator.hasNext()) {
73 IssuePattern nextPattern = patternIterator.next();
74 if (nextPattern.match(issue, component)) {
75 matchingPattern = nextPattern;
78 if (matchingPattern != null) {
79 LOG.debug("Issue {} ignored by exclusion pattern {}", issue, matchingPattern);
85 private boolean isInclude(DefaultIssue issue, Component component) {
86 boolean atLeastOneRuleMatched = false;
87 boolean atLeastOnePatternFullyMatched = false;
88 IssuePattern matchingPattern = null;
90 for (IssuePattern pattern : inclusionPatterns) {
91 if (pattern.getRulePattern().match(issue.ruleKey().toString())) {
92 atLeastOneRuleMatched = true;
93 String componentPath = component.getReportAttributes().getPath();
94 if (componentPath != null && pattern.getComponentPattern().match(componentPath)) {
95 atLeastOnePatternFullyMatched = true;
96 matchingPattern = pattern;
101 if (atLeastOneRuleMatched) {
102 if (atLeastOnePatternFullyMatched) {
103 LOG.debug("Issue {} enforced by pattern {}", issue, matchingPattern);
105 return atLeastOnePatternFullyMatched;
111 private static List<IssuePattern> loadPatterns(String propertyKey, Settings settings) {
112 List<IssuePattern> patterns = new ArrayList<>();
113 String patternConf = defaultIfBlank(settings.getString(propertyKey), "");
114 for (String id : Splitter.on(",").omitEmptyStrings().split(patternConf)) {
115 String propPrefix = propertyKey + "." + id + ".";
116 String componentPathPattern = settings.getString(propPrefix + RESOURCE_KEY);
117 checkArgument(!isNullOrEmpty(componentPathPattern), format("File path pattern cannot be empty. Please check '%s' settings", propertyKey));
118 String ruleKeyPattern = settings.getString(propPrefix + RULE_KEY);
119 checkArgument(!isNullOrEmpty(ruleKeyPattern), format("Rule key pattern cannot be empty. Please check '%s' settings", propertyKey));
120 patterns.add(new IssuePattern(componentPathPattern, ruleKeyPattern));