class Pronto::Findbugs

Constants

Offence

Public Instance Methods

run() click to toggle source
# File lib/pronto/findbugs.rb, line 6
def run
  return [] unless @patches

  @patches
    .select(&method(:valid_patch?))
    .flat_map(&method(:inspect))
    .compact
end

Private Instance Methods

findbugs_offences() click to toggle source
# File lib/pronto/findbugs.rb, line 37
def findbugs_offences
  @findbugs_offences ||=
    begin
      pattern = File.join(findbugs_reports_dir, '**', '*.xml')
      Dir.glob(pattern).flat_map(&method(:read_findbugs_report))
    end
end
findbugs_reports_dir() click to toggle source
# File lib/pronto/findbugs.rb, line 19
def findbugs_reports_dir
  ENV['PRONTO_FINDBUGS_REPORTS_DIR'] || (raise 'Please set `PRONTO_FINDBUGS_REPORTS_DIR` to use pronto-findbugs')
end
inspect(patch) click to toggle source
# File lib/pronto/findbugs.rb, line 27
def inspect(patch)
  offences = findbugs_offences.select { |offence| offence.path == patch.new_file_full_path.to_s }

  offences.flat_map do |offence|
    patch.added_lines
         .select { |line| line.new_lineno == offence.line }
         .map { |line| new_message(offence, line) }
  end
end
line_from(bug_node) click to toggle source
# File lib/pronto/findbugs.rb, line 64
def line_from(bug_node)
  source_line = REXML::XPath.first(bug_node, 'SourceLine')
  source_line.attribute('start').to_s.to_i
end
message_from(bug_node) click to toggle source
# File lib/pronto/findbugs.rb, line 69
def message_from(bug_node)
  long_message = REXML::XPath.first(bug_node, 'LongMessage')
  return long_message.text if long_message

  bug_type = bug_node.attribute('type').to_s
  bug_category = bug_node.attribute('category').to_s
  "type=#{bug_type} category=#{bug_category}"
end
new_message(offence, line) click to toggle source
# File lib/pronto/findbugs.rb, line 78
def new_message(offence, line)
  path = line.patch.delta.new_file[:path]
  Message.new(path, line, :warning, offence.message, nil, self.class)
end
path_from(bug_node, root) click to toggle source
# File lib/pronto/findbugs.rb, line 58
def path_from(bug_node, root)
  source_line = REXML::XPath.first(bug_node, 'SourceLine')
  path = source_line.attribute('sourcepath').to_s
  File.join(root, path)
end
read_findbugs_report(path) click to toggle source
# File lib/pronto/findbugs.rb, line 45
def read_findbugs_report(path)
  doc = REXML::Document.new(File.read(path))
  src_dirs = REXML::XPath.match(doc, '/BugCollection/Project/SrcDir/text()')
  return [] if src_dirs.empty?

  REXML::XPath.match(doc, '/BugCollection/BugInstance').map do |bug|
    src = src_dirs.find { |s| File.exist?(path_from(bug, s.to_s)) }
    next unless src

    Offence.new(path_from(bug, src.to_s), line_from(bug), message_from(bug))
  end.compact
end
valid_patch?(patch) click to toggle source
# File lib/pronto/findbugs.rb, line 23
def valid_patch?(patch)
  patch.additions > 0
end