This repository has been archived by the owner on Apr 6, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[FIXED JENKINS-24011] Apply include/exclude filters after path expans…
…ion. Also use JenkinsRule for all integration tests.
- Loading branch information
Showing
11 changed files
with
181 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/java/hudson/plugins/warnings/parser/WarningsFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package hudson.plugins.warnings.parser; | ||
|
||
import javax.annotation.CheckForNull; | ||
import java.util.Collection; | ||
import java.util.Set; | ||
import java.util.regex.Pattern; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
|
||
import com.google.common.collect.Sets; | ||
|
||
import hudson.plugins.analysis.util.PluginLogger; | ||
import hudson.plugins.analysis.util.model.FileAnnotation; | ||
|
||
/** | ||
* Filters warnings by exclude and include patterns. | ||
* | ||
* @author Ullrich Hafner | ||
*/ | ||
public class WarningsFilter { | ||
private final Set<Pattern> includePatterns = Sets.newHashSet(); | ||
private final Set<Pattern> excludePatterns = Sets.newHashSet(); | ||
|
||
private Set<Pattern> addPatterns(final @CheckForNull String pattern) { | ||
Set<Pattern> patterns = Sets.newHashSet(); | ||
if (StringUtils.isNotBlank(pattern)) { | ||
String[] split = StringUtils.split(pattern, ','); | ||
for (String singlePattern : split) { | ||
String trimmed = StringUtils.trim(singlePattern); | ||
String directoriesReplaced = StringUtils.replace(trimmed, "**", "*"); // NOCHECKSTYLE | ||
patterns.add(Pattern.compile(StringUtils.replace(directoriesReplaced, "*", ".*"))); // NOCHECKSTYLE | ||
} | ||
} | ||
return patterns; | ||
} | ||
|
||
/** | ||
* Filters te specified warnings by exclude and include patterns. | ||
* | ||
* @param allAnnotations | ||
* all annotations | ||
* @param includePattern | ||
* regexp pattern of files to include in report, | ||
* <code>null</code> or an empty string do not filter the output | ||
* @param excludePattern | ||
* regexp pattern of files to exclude from report, | ||
* <code>null</code> or an empty string do not filter the output | ||
* @return the filtered annotations if there is a filter defined | ||
*/ | ||
public Collection<FileAnnotation> apply(final Collection<FileAnnotation> allAnnotations, | ||
final @CheckForNull String includePattern, | ||
final @CheckForNull String excludePattern, | ||
final PluginLogger logger) { | ||
Collection<Pattern> includePatterns = addPatterns(includePattern); | ||
Collection<Pattern> excludePatterns = addPatterns(excludePattern); | ||
|
||
Collection<FileAnnotation> includedAnnotations; | ||
if (includePatterns.isEmpty()) { | ||
includedAnnotations = allAnnotations; | ||
} | ||
else { | ||
includedAnnotations = Sets.newHashSet(); | ||
for (FileAnnotation annotation : allAnnotations) { | ||
for (Pattern include : includePatterns) { | ||
if (include.matcher(annotation.getFileName()).matches()) { | ||
includedAnnotations.add(annotation); | ||
} | ||
} | ||
} | ||
} | ||
if (excludePatterns.isEmpty()) { | ||
return includedAnnotations; | ||
} | ||
else { | ||
Set<FileAnnotation> excludedAnnotations = Sets.newHashSet(includedAnnotations); | ||
for (FileAnnotation annotation : includedAnnotations) { | ||
for (Pattern exclude : excludePatterns) { | ||
if (exclude.matcher(annotation.getFileName()).matches()) { | ||
excludedAnnotations.remove(annotation); | ||
} | ||
} | ||
} | ||
logger.log(String.format("Found %d warnings after exclusion.", excludedAnnotations.size())); | ||
return excludedAnnotations; | ||
} | ||
} | ||
|
||
public boolean isActive(final String includePattern, final String excludePattern) { | ||
return StringUtils.isNotBlank(includePattern) || StringUtils.isNotBlank(excludePattern); | ||
} | ||
} |
12 changes: 8 additions & 4 deletions
12
src/test/java/hudson/plugins/warnings/ConsoleParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 8 additions & 4 deletions
12
src/test/java/hudson/plugins/warnings/ParserConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.