class Danger::DangerBrakeman

Run Ruby files through Brakeman. Results are passed out as a table in markdown.

@example Lint changed files

brakeman.lint

Public Instance Methods

lint(config = nil) click to toggle source

Runs Ruby files through Brakeman. Generates a `markdown` list of warnings.

# File lib/brakeman/plugin.rb, line 13
def lint(config = nil)
  files_to_lint = _fetch_files_to_lint
  brakeman_result = _brakeman(files_to_lint)

  return if brakeman_result.nil?

  _add_warning_for_each_line(brakeman_result)
end

Private Instance Methods

_add_warning_for_each_line(brakeman_result) click to toggle source
# File lib/brakeman/plugin.rb, line 34
def _add_warning_for_each_line(brakeman_result)
  brakeman_result.each do |warning|
    arguments = [
      "[brakeman] #{warning['message']}",
      {
        file: warning['file'],
        line: warning['line']
      }
    ]
    warn(*arguments)
  end
end
_brakeman(files_to_lint) click to toggle source
# File lib/brakeman/plugin.rb, line 24
def _brakeman(files_to_lint)
  base_command = 'brakeman -q -f json --only-files'

  brakeman_output = `#{'bundle exec ' if File.exist?('Gemfile')}#{base_command} #{files_to_lint}`

  return [] if brakeman_output.empty?

  JSON.parse(brakeman_output)['warnings']
end
_fetch_files_to_lint() click to toggle source
# File lib/brakeman/plugin.rb, line 47
def _fetch_files_to_lint
  to_lint = git.modified_files + git.added_files
  Shellwords.join(to_lint).gsub(" ", ",")
end