Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

Commit

Permalink
JENKINS-20147: Add include and exclude files patterns
Browse files Browse the repository at this point in the history
Add include patterns option to include files in the report based on
their absolute paths.
Add exclude patterns option to exclude files from the report based on
their absolute paths.

Issue: JENKINS-20147
Change-Id: I0d7742555daeab03c346174d66212203290f5a0a
  • Loading branch information
meg authored and Geraldo Mecja committed Oct 30, 2013
1 parent 5d070b0 commit c19ae4c
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 45 deletions.
Expand Up @@ -40,9 +40,12 @@ public class FindBugsPublisher extends HealthAwarePublisher {
/** Determines whether to use the rank when evaluation the priority. @since 4.26 */
private final boolean isRankActivated;

/** RegEx files patterns to exclude from report. */
/** RegEx patterns of files to exclude from the report. */
private final String excludePattern;

/** RegEx patterns of files to include in the report. */
private final String includePattern;

/**
* Creates a new instance of {@link FindBugsPublisher}.
*
Expand Down Expand Up @@ -109,7 +112,9 @@ public class FindBugsPublisher extends HealthAwarePublisher {
* determines whether new warnings should be computed (with
* respect to baseline)
* @param excludePattern
* RegEx files patterns to exclude from report
* RegEx patterns of files to exclude from the report
* @param includePattern
* RegEx patterns of files to include in the report
*/
// CHECKSTYLE:OFF
@SuppressWarnings("PMD.ExcessiveParameterList")
Expand All @@ -121,7 +126,7 @@ public FindBugsPublisher(final String healthy, final String unHealthy, final Str
final String failedTotalAll, final String failedTotalHigh, final String failedTotalNormal, final String failedTotalLow,
final String failedNewAll, final String failedNewHigh, final String failedNewNormal, final String failedNewLow,
final boolean canRunOnFailed, final boolean useStableBuildAsReference, final boolean shouldDetectModules,
final String pattern, final boolean isRankActivated, final boolean canComputeNew, final String excludePattern) {
final String pattern, final boolean isRankActivated, final boolean canComputeNew, final String excludePattern, final String includePattern) {
super(healthy, unHealthy, thresholdLimit, defaultEncoding, useDeltaValues,
unstableTotalAll, unstableTotalHigh, unstableTotalNormal, unstableTotalLow,
unstableNewAll, unstableNewHigh, unstableNewNormal, unstableNewLow,
Expand All @@ -131,6 +136,7 @@ public FindBugsPublisher(final String healthy, final String unHealthy, final Str
this.pattern = pattern;
this.isRankActivated = isRankActivated;
this.excludePattern= excludePattern;
this.includePattern= includePattern;
}
// CHECKSTYLE:ON

Expand All @@ -155,14 +161,23 @@ public String getPattern() {
}

/**
* Returns the RegEx patterns to exclude from report.
* RegEx patterns of files to exclude from the report.
*
* @return RegEx files patterns to exclude from report
* @return String of concatenated exclude patterns separated by a comma
*/
public String getExcludePattern() {
return excludePattern;
}

/**
* Returns the RegEx patterns to include in the report.
*
* @return String of concatenated include patterns separated by a comma
*/
public String getIncludePattern() {
return includePattern;
}

@Override
public Action getProjectAction(final AbstractProject<?, ?> project) {
return new FindBugsProjectAction(project);
Expand All @@ -174,7 +189,7 @@ public BuildResult perform(final AbstractBuild<?, ?> build, final PluginLogger l

String defaultPattern = isMavenBuild(build) ? MAVEN_DEFAULT_PATTERN : ANT_DEFAULT_PATTERN;
FilesParser collector = new FilesParser(PLUGIN_NAME, StringUtils.defaultIfEmpty(getPattern(), defaultPattern),
new FindBugsParser(isRankActivated, getExcludePattern()), shouldDetectModules(), isMavenBuild(build));
new FindBugsParser(isRankActivated, getExcludePattern(), getIncludePattern()), shouldDetectModules(), isMavenBuild(build));
ParserResult project = build.getWorkspace().act(collector);
logger.logLines(project.getLogMessages());
FindBugsResult result = new FindBugsResult(build, getDefaultEncoding(), project, useOnlyStableBuildsAsReference());
Expand Down
27 changes: 21 additions & 6 deletions plugin/src/main/java/hudson/plugins/findbugs/FindBugsReporter.java
Expand Up @@ -47,9 +47,12 @@ public class FindBugsReporter extends HealthAwareReporter<FindBugsResult> {
/** Determines whether to use the rank when evaluation the priority. @since 4.26 */
private final boolean isRankActivated;

/** RegEx files patterns to exclude from report. */
/** RegEx patterns of files to exclude from the report. */
private final String excludePattern;

/** RegEx patterns of files to include in the report. */
private final String includePattern;

/**
* Creates a new instance of <code>FindBugsReporter</code>.
*
Expand Down Expand Up @@ -109,7 +112,9 @@ public class FindBugsReporter extends HealthAwareReporter<FindBugsResult> {
* determines whether new warnings should be computed (with
* respect to baseline)
* @param excludePattern
* RegEx files patterns to exclude from report
* RegEx patterns of files to exclude from the report
* @param includePattern
* RegEx patterns of files to include in the report
*/
// CHECKSTYLE:OFF
@SuppressWarnings("PMD.ExcessiveParameterList")
Expand All @@ -119,7 +124,7 @@ public FindBugsReporter(final String healthy, final String unHealthy, final Stri
final String unstableNewAll, final String unstableNewHigh, final String unstableNewNormal, final String unstableNewLow,
final String failedTotalAll, final String failedTotalHigh, final String failedTotalNormal, final String failedTotalLow,
final String failedNewAll, final String failedNewHigh, final String failedNewNormal, final String failedNewLow,
final boolean canRunOnFailed, final boolean useStableBuildAsReference, final boolean isRankActivated, final boolean canComputeNew, final String excludePattern) {
final boolean canRunOnFailed, final boolean useStableBuildAsReference, final boolean isRankActivated, final boolean canComputeNew, final String excludePattern, final String includePattern) {
super(healthy, unHealthy, thresholdLimit, useDeltaValues,
unstableTotalAll, unstableTotalHigh, unstableTotalNormal, unstableTotalLow,
unstableNewAll, unstableNewHigh, unstableNewNormal, unstableNewLow,
Expand All @@ -128,6 +133,7 @@ public FindBugsReporter(final String healthy, final String unHealthy, final Stri
canRunOnFailed, useStableBuildAsReference, canComputeNew, PLUGIN_NAME);
this.isRankActivated = isRankActivated;
this.excludePattern = excludePattern;
this.includePattern = includePattern;
}
// CHECKSTYLE:ON

Expand All @@ -143,14 +149,23 @@ public boolean isRankActivated() {
}

/**
* Returns the RegEx patterns to exclude from report.
* RegEx patterns of files to exclude from the report.
*
* @return RegEx files patterns to exclude from report
* @return String of concatenated exclude patterns separated by a comma
*/
public String getExcludePattern() {
return excludePattern;
}

/**
* Returns the RegEx patterns to include in the report.
*
* @return String of concatenated include patterns separated by a comma
*/
public String getIncludePattern() {
return includePattern;
}

@Override
public boolean preExecute(final MavenBuildProxy build, final MavenProject pom, final MojoInfo mojo,
final BuildListener listener) throws InterruptedException, IOException {
Expand Down Expand Up @@ -189,7 +204,7 @@ public ParserResult perform(final MavenBuildProxy build, final MavenProject pom,
sources.addAll(pom.getTestCompileSourceRoots());

FilesParser findBugsCollector = new FilesParser(PLUGIN_NAME, determineFileName(mojo),
new FindBugsParser(sources, isRankActivated, getExcludePattern()), getModuleName(pom));
new FindBugsParser(sources, isRankActivated, getExcludePattern(), getIncludePattern()), getModuleName(pom));

return getOutputPath(mojo, pom).act(findBugsCollector);
}
Expand Down
Expand Up @@ -70,17 +70,20 @@ public class FindBugsParser implements AnnotationParser {
/** Determines whether to use the rank when evaluation the priority. @since 4.26 */
private final boolean isRankActivated;

/** RegEx patterns of files to exclude from report. */
/** RegEx patterns of files to exclude from the report. */
private final Set<Pattern> excludePatterns = Sets.newHashSet();

/** RegEx patterns of files to include in the report. */
private final Set<Pattern> includePatterns = Sets.newHashSet();

/**
* Creates a new instance of {@link FindBugsParser}.
*
* @param isRankActivated
* determines whether to use the rank when evaluation the priority
*/
public FindBugsParser(final boolean isRankActivated) {
this(isRankActivated, null);
this(isRankActivated, null, null);
}

/**
Expand All @@ -89,10 +92,12 @@ public FindBugsParser(final boolean isRankActivated) {
* @param isRankActivated
* determines whether to use the rank when evaluation the priority
* @param excludePattern
* RegEx pattern of files to exclude from report
* RegEx patterns of files to exclude from the report
* @param includePattern
* RegEx patterns of files to include in the report
*/
public FindBugsParser(final boolean isRankActivated, final String excludePattern) {
this(new ArrayList<String>(), isRankActivated, excludePattern);
public FindBugsParser(final boolean isRankActivated, final String excludePattern, final String includePattern) {
this(new ArrayList<String>(), isRankActivated, excludePattern, includePattern);
}

/**
Expand All @@ -103,20 +108,23 @@ public FindBugsParser(final boolean isRankActivated, final String excludePattern
* @param isRankActivated
* determines whether to use the rank when evaluation the priority
* @param excludePattern
* RegEx patterns of files to exclude from report
* RegEx patterns of files to exclude from the report
* @param includePattern
* RegEx patterns of files to include in the report
*/
public FindBugsParser(final Collection<String> sourceFolders, final boolean isRankActivated,
final String excludePattern) {
final String excludePattern, final String includePattern) {
mavenSources.addAll(sourceFolders);
this.isRankActivated = isRankActivated;
addPatterns(includePatterns, includePattern);
addPatterns(excludePatterns, excludePattern);
}

/**
* Extract the RegEx patterns from the string.
* Add RegEx patterns to include/exclude in the report.
*
* @param patterns
* RegEx patterns of files to exclude from report
* RegEx patterns
* @param pattern
* String of RegEx patterns
*/
Expand All @@ -131,6 +139,7 @@ private void addPatterns(final Set<Pattern> patterns, final String pattern) {
}
}


/** {@inheritDoc} */
public Collection<FileAnnotation> parse(final File file, final String moduleName) throws InvocationTargetException {
try {
Expand Down Expand Up @@ -306,6 +315,7 @@ private Collection<FileAnnotation> parse(final InputStream file, final Collectio
return applyExcludeFilter(annotations);
}


/**
* Applies the exclude filter to the found annotations.
*
Expand All @@ -314,12 +324,26 @@ private Collection<FileAnnotation> parse(final InputStream file, final Collectio
* @return the filtered annotations if there is a filter defined
*/
private List<FileAnnotation> applyExcludeFilter(final List<FileAnnotation> allAnnotations) {
if (excludePatterns.isEmpty()) {
return allAnnotations;
List<FileAnnotation> includedAnnotations;
if (includePatterns.isEmpty()) {
includedAnnotations = allAnnotations;
}
else {
List<FileAnnotation> excludedAnnotations = new ArrayList<FileAnnotation>(allAnnotations);
includedAnnotations = new ArrayList<FileAnnotation>();
for (FileAnnotation annotation : allAnnotations) {
for (Pattern include : includePatterns) {
if (include.matcher(annotation.getFileName()).matches()) {
includedAnnotations.add(annotation);
}
}
}
}
if (excludePatterns.isEmpty()) {
return includedAnnotations;
}
else {
List<FileAnnotation> excludedAnnotations = new ArrayList<FileAnnotation>(includedAnnotations);
for (FileAnnotation annotation : includedAnnotations) {
for (Pattern exclude : excludePatterns) {
if (exclude.matcher(annotation.getFileName()).matches()) {
excludedAnnotations.remove(annotation);
Expand Down
13 changes: 0 additions & 13 deletions plugin/src/main/resources/findbugs/exclude_pattern.jelly

This file was deleted.

2 changes: 0 additions & 2 deletions plugin/src/main/resources/findbugs/exclude_pattern.properties

This file was deleted.

Expand Up @@ -5,9 +5,15 @@
description="${%description.pattern('http://ant.apache.org/manual/Types/fileset.html')}">
<f:textbox />
</f:entry>
<fb:exclude_pattern />
<fb:rank />
<f:advanced>
<f:entry title="${%Bugs to include}" field="includePattern" description="${%description.includePattern}">
<f:textbox />
</f:entry>
<f:entry title="${%Bugs to ignore}" field="excludePattern" description="${%description.excludePattern}">
<f:textbox />
</f:entry>

<u:advanced id="findbugs" />
</f:advanced>
</j:jelly>
Expand Up @@ -5,3 +5,10 @@ description.pattern=<a href="{0}">Fileset 'includes'</a> \
If no value is set, then the default '**/findbugsXml.xml' or '**/findbugs.xml' \
are used for maven or ant builds, respectively. Be sure not to include any \
non-report files into this pattern.
description.includePattern=Comma separated list of \
<a href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html">regular expressions</a> \
that specifies the files to include in the report (based on their absolute filename). \
If this field is empty then all files are included.
description.excludePattern=Comma separated list of \
<a href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html">regular expressions</a> \
that specifies the files to exclude from the report (based on their absolute filename).
Expand Up @@ -5,9 +5,15 @@
description="${%description.pattern('http://ant.apache.org/manual/Types/fileset.html')}">
<f:textbox />
</f:entry>
<fb:exclude_pattern />
<fb:rank />
<f:advanced>
<f:entry title="${%Bugs to include}" field="includePattern" description="${%description.includePattern}">
<f:textbox />
</f:entry>
<f:entry title="${%Bugs to ignore}" field="excludePattern" description="${%description.excludePattern}">
<f:textbox />
</f:entry>

<u:advanced id="findbugs" />
</f:advanced>
</j:jelly>

0 comments on commit c19ae4c

Please sign in to comment.