Skip to content

Commit

Permalink
Adds several attributes to spotbugs rule
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-mgalindo committed Oct 3, 2024
1 parent 43d61cb commit e8dbd6f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
28 changes: 28 additions & 0 deletions java/private/spotbugs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ Spotbugs integration logic

def _spotbugs_impl(ctx):
info = ctx.attr.config[SpotBugsInfo]
max_rank = info.max_rank
effort = info.effort
fail_on_warning = info.fail_on_warning
exclude_filter = info.exclude_filter
omit_visitors = info.omit_visitors
plugin_list = info.plugin_list

baseline_files = ctx.attr.baseline_file
if len(baseline_files) > 1:
fail("More than one baseline file was specified.")

if ctx.attr.only_output_jars:
deps = []
for target in ctx.attr.deps:
Expand All @@ -30,11 +36,25 @@ def _spotbugs_impl(ctx):
flags.extend(["-exclude", exclude_filter.short_path])
runfiles.append(exclude_filter)

if len(baseline_files) > 0:
baseline_file = baseline_files[0].files.to_list()[0]
print(baseline_file)
flags.extend(["-excludeBugs", baseline_file.short_path])
runfiles.append(baseline_file)

# NOTE: pluginList needs to be specified before specifying any visitor
# otherwise the execution will will fail with detector not found.
if plugin_list:
plugin_list_cli_flag = ":".join([plugin.short_path for plugin in plugin_list])
flags.extend(["-pluginList", plugin_list_cli_flag])
runfiles.extend(plugin_list)

if omit_visitors:
flags.extend(["-omitVisitors", ",".join(omit_visitors)])

if max_rank:
flags.extend(["-maxRank", max_rank])

test = [
"#!/usr/bin/env bash",
"ERRORLOG=$(mktemp)",
Expand Down Expand Up @@ -94,6 +114,14 @@ spotbugs_test = rule(
SpotBugsInfo,
],
),
# NOTE: this will always be just one file, but we use a label_list instead of a label
# so that we can pass a glob. In cases where no baseline file exist, the glob will simply
# be empty. Using a label instead of a label_list would force us to create a baseline file
# for all targets, even if theres no need for one.
"baseline_file": attr.label_list(
allow_files = True,
default = [],
),
"only_output_jars": attr.bool(
doc = "If set to true, only the output jar of the target will be analyzed. Otherwise all transitive runtime dependencies will be analyzed",
default = True,
Expand Down
11 changes: 11 additions & 0 deletions java/private/spotbugs_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def spotbugs_binary(
SpotBugsInfo = provider(
fields = {
"effort": "Effort can be min, less, default, more or max.",
"max_rank": "Only report issues with a bug rank at least as scary as that provided.",
"exclude_filter": "Optional filter file to use.",
"omit_visitors": "Omit named visitors",
"fail_on_warning": "Whether to fail on warning, or just create a report.",
"plugin_list": "Optional list of JARs to load as plugins.",
"binary": "The spotbugs binary to use.",
Expand All @@ -61,7 +63,9 @@ def _spotbugs_config_impl(ctx):
),
SpotBugsInfo(
effort = ctx.attr.effort,
max_rank = ctx.attr.max_rank,
exclude_filter = ctx.file.exclude_filter,
omit_visitors = ctx.attr.omit_visitors,
plugin_list = ctx.files.plugin_list,
fail_on_warning = ctx.attr.fail_on_warning,
binary = ctx.executable.spotbugs_binary,
Expand All @@ -77,10 +81,17 @@ spotbugs_config = rule(
values = ["min", "less", "default", "more", "max"],
default = "default",
),
"max_rank": attr.string(
doc = "Only report issues with a bug rank at least as scary as that provided.",
),
"exclude_filter": attr.label(
doc = "Report all bug instances except those matching the filter specified by this filter file",
allow_single_file = True,
),
"omit_visitors": attr.string_list(
doc = "Omit named visitors.",
default = [],
),
"plugin_list": attr.label_list(
doc = "Specify a list of plugin Jar files to load",
allow_files = True,
Expand Down

0 comments on commit e8dbd6f

Please sign in to comment.