Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding possibility for sputnik to load additional SpotBugs plugins like find-sec-bugs #229

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1a4f193
Publish workflow testing
SpOOnman Jan 8, 2021
3ecbf13
Publish workflow testing - signing
SpOOnman Jan 8, 2021
5738946
Publish workflow testing - signing 2
SpOOnman Jan 8, 2021
8ca43e5
Publish workflow testing - signing 3
SpOOnman Jan 8, 2021
fd550b6
Publish workflow testing - signing 4
SpOOnman Jan 11, 2021
44a165d
Publish workflow testing - signing 5
SpOOnman Jan 11, 2021
5fb31c0
Publish workflow testing - signing 6
SpOOnman Jan 11, 2021
258c78b
Publish workflow testing - signing 7
SpOOnman Jan 11, 2021
f6aabbc
Publish workflow testing - signing 8
SpOOnman Jan 11, 2021
4ce0796
Adding possibility for sputnik to load additional SpotBugs plugins li…
fipciu1996 Jan 11, 2021
26c009f
Adding possibility for sputnik to load additional SpotBugs plugins li…
fipciu1996 Jan 11, 2021
439ae15
Adding possibility for sputnik to load additional SpotBugs plugins li…
fipciu1996 Jan 11, 2021
85dd88f
Merge remote-tracking branch 'origin/master'
Jan 12, 2021
3654b91
Clear imports
fipciu1996 Jan 12, 2021
f0ae5b0
Improving method to get SpotBugs Plugins
fipciu1996 Jan 12, 2021
2b8a950
Improving method to get SpotBugs Plugins
fipciu1996 Jan 12, 2021
5f9d4d7
Adding possibility for sputnik to load additional SpotBugs plugins li…
fipciu1996 Mar 24, 2021
1f26597
Adding possibility for sputnik to load additional SpotBugs plugins li…
fipciu1996 Mar 24, 2021
dc08e71
Adding possibility for sputnik to load additional SpotBugs plugins li…
fipciu1996 Mar 25, 2021
80a134a
Adding possibility for sputnik to load additional SpotBugs plugins li…
fipciu1996 Mar 25, 2021
ce47479
Merge branch 'master' into master
fipciu1996 Mar 25, 2021
ee51538
Reformat build.gradle to us spaces like before
May 26, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pmd.ruleSets=rulesets/java/android.xml,rulesets/java/basic.xml
spotbugs.enabled=true
spotbugs.includeFilter=
spotbugs.excludeFilter=
spotbugs.plugins.location=/opt/sputnik/SpotBugsPlugins
codenarc.enabled=true
codenarc.ruleSets=
codenarc.excludes=**/*.java
Expand Down
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ shadowJar {
zip64 true
}

shadowJar {
zip64 true
}

jar {
manifest {
attributes("Specification-Title": "Sputnik",
Expand All @@ -209,7 +213,7 @@ publishing {
pom {
groupId = 'pl.touk'
name = 'Sputnik'
description = 'Static code review for your Gerrit and Stash patchsets. Runs Checkstyle, PMD and SpotBugs for you!'
description = 'Static code review for your Gerrit and Stash patchsets. Runs Checkstyle, PMD and SpotBugs for you!'
url = 'https://github.com/TouK/sputnik/'
scm {
url = 'scm:[email protected]:TouK/sputnik.git'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public enum GeneralOption implements ConfigurationOption {
SPOTBUGS_LOAD_PROPERTIES_FROM("spotbugs.loadPropertiesFrom", "SpotBugs properties file", ""),
SPOTBUGS_INCLUDE_FILTER("spotbugs.includeFilter", "SpotBugs include filter file", ""),
SPOTBUGS_EXCLUDE_FILTER("spotbugs.excludeFilter", "SpotBugs exclude filter file", ""),
SPOTBUGS_PLUGINS_LOCATION("spotbugs.plugins.location", "Path to catalog where SpotBugs plugins jar files are located", ""),

SCALASTYLE_ENABLED("scalastyle.enabled", "ScalaStyle enabled", "false"),
SCALASTYLE_CONFIGURATION_FILE("scalastyle.configurationFile", "ScalaStyle configuration file", ""),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
import edu.umd.cs.findbugs.DetectorFactoryCollection;
import edu.umd.cs.findbugs.FindBugs2;
import edu.umd.cs.findbugs.IClassScreener;
import edu.umd.cs.findbugs.Plugin;
import edu.umd.cs.findbugs.PluginException;
import edu.umd.cs.findbugs.Priorities;
import edu.umd.cs.findbugs.Project;
import edu.umd.cs.findbugs.SystemProperties;
import edu.umd.cs.findbugs.config.UserPreferences;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
Expand All @@ -29,9 +35,11 @@

@Slf4j
public class SpotBugsProcessor implements ReviewProcessor {

private static final String SOURCE_NAME = "SpotBugs";

private final CollectorBugReporter collectorBugReporter;

private final Configuration config;

public SpotBugsProcessor(@NotNull Configuration configuration) {
Expand Down Expand Up @@ -70,6 +78,7 @@ public String getName() {
}

public FindBugs2 createFindBugs2(Review review) {
loadAllSpotbugsPlugins(config.getProperty(GeneralOption.SPOTBUGS_PLUGINS_LOCATION));
FindBugs2 findBugs = new FindBugs2();
findBugs.setProject(createProject(review));
findBugs.setBugReporter(collectorBugReporter);
Expand Down Expand Up @@ -145,4 +154,32 @@ private String getExcludeFilterFilename() {
log.info("Using SpotBugs exclude filter file {}", excludeFilterFilename);
return excludeFilterFilename;
}

public void loadAllSpotbugsPlugins(String pluginsLocation) {
File[] fileList = getSpotBugsPluginFiles(pluginsLocation);
for (File file : fileList) {
loadSpotBugsPlugin(file);
}
}

public File[] getSpotBugsPluginFiles(String pluginsLocation) {
if (!StringUtils.isBlank(pluginsLocation)) {
File[] fileList = new File(pluginsLocation).listFiles();
if (fileList != null) {
return Arrays.stream(fileList).filter(file -> file.getName().contains(".jar")).toArray(File[]::new);
}
}
return new File[0];
}

private void loadSpotBugsPlugin(File pluginFile) {
log.info("SpotBugs additional plugin loading: file://{}", pluginFile.getAbsoluteFile());
try {
Plugin.addCustomPlugin(new URI("file://" + pluginFile.getAbsoluteFile()));
} catch (PluginException e) {
log.info("Spotbugs additional plugins not loaded {} plugin not supported", e.getMessage());
} catch (URISyntaxException e) {
log.info("Spotbugs additional plugins not loaded {} check path", e.getMessage());
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/example.properties
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pmd.showViolationDetails=true
spotbugs.enabled=true
spotbugs.includeFilter=
spotbugs.excludeFilter=
spotbugs.plugins.location=

scalastyle.enabled=true
scalastyle.configurationFile=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import edu.umd.cs.findbugs.Plugin;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -13,9 +15,6 @@
import pl.touk.sputnik.review.ReviewFile;
import pl.touk.sputnik.review.ReviewFormatterFactory;
import pl.touk.sputnik.review.ReviewResult;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

class SpotBugsProcessorTest extends TestEnvironment {
Expand Down Expand Up @@ -60,6 +59,19 @@ void shouldReturnEmptyWhenNoFilesToReview() {
}

@Test
void shouldInstantiateIfPluginsCatalogIsPresentButEmpty() {
ReviewResult reviewResult = spotBugsProcessor.process(nonExistentReview());
spotBugsProcessor.loadAllSpotbugsPlugins("src/test/resources/java/empty");
assertThat(reviewResult).isNotNull();
assertThat(reviewResult.getViolations()).isEmpty();
}

@Test
void pluginShouldBeLoaded() {
spotBugsProcessor.loadAllSpotbugsPlugins("src/test/resources/java");
assertThat(Plugin.getByPluginId("com.h3xstream.findsecbugs")).isNotNull();
}

void shouldLoadPropertiesFromExternalLocation() {
ReviewResult reviewResult = spotBugsProcessor.process(nonExistentReview());

Expand Down
Empty file.
Binary file not shown.
1 change: 1 addition & 0 deletions src/test/resources/test.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pmd.ruleSets=rulesets/java/android.xml,rulesets/java/basic.xml,rulesets/java/bra
spotbugs.enabled=true
spotbugs.includeFilter=
spotbugs.excludeFilter=
spotbugs.plugins.location=
jslint.enabled=true
scalastyle.configurationFile=src/test/resources/scalastyle.xml
jshint.enabled=true