class Danger::ShiphawkPlugin

Run Ruby files through Rubocop. Results are passed out in a hash with errors/warnings

@example Specifying custom config file.

shiphawk_plugin.checkup

@see ShipHawk/danger-shiphawk-plugin @tags ruby, rubocop, rubocop-plugin

Public Instance Methods

checkup(options = {}) click to toggle source

Runs Ruby files through Rubocop. Generates a list of errors/warnings

@param [Hash] options @return [void]

# File lib/danger_plugin.rb, line 23
def checkup(options = {})
  @files = options.fetch(:files, 'all')
  @config = options.fetch(:config, nil)
  @limit_of_warnings = options.fetch(:limit_of_warnings, 10)
  @autofix_hint_threshold = options.fetch(:autofix_hint_threshold, 50)

  return if offenses.empty?

  message_detailed_report
  message_compact_report
  message_hint
  message_autofix

  nil
end

Private Instance Methods

detect_offenses() click to toggle source
# File lib/danger_plugin.rb, line 45
def detect_offenses
  rubocop_output = json_parse(rubocop)

  all_offenses = rubocop_output.files.flat_map do |file|
    offenses = file.offenses
    next [] if offenses.empty?

    offenses.map do |offense|
      OpenStruct.new(
        path: file.path,
        message: offense.message,
        serenity: serenity_lvl_for(offense.cop_name)
      )
    end
  end

  all_offenses.sort_by(&:serenity)
end
files_to_lint() click to toggle source
# File lib/danger_plugin.rb, line 74
def files_to_lint
  file_paths = case @files
  when 'all'
    Dir.glob('.')
  when 'diff'
    git.modified_files + git.added_files
  else
    raise ArgumentError, "Incorrect 'files' option"
  end

  Shellwords.join(file_paths)
end
json_parse(data) click to toggle source
# File lib/danger_plugin.rb, line 135
def json_parse(data)
  JSON.parse(data, object_class: OpenStruct)
end
message_autofix() click to toggle source
# File lib/danger_plugin.rb, line 87
def message_autofix
  return if offenses.empty?
  return if offenses.size > @autofix_hint_threshold

  msg = '## Please fix rubocop mistakes: `rubocop --auto-correct`'
  print_message(msg, :fail)
end
message_compact_report() click to toggle source
# File lib/danger_plugin.rb, line 108
def message_compact_report
  compact_report_offenses = offenses[@limit_of_warnings..-1]

  return if compact_report_offenses.nil?

  msg = "...And other #{compact_report_offenses.size} warnings"
  print_message(msg, :warn)
end
message_detailed_report() click to toggle source
# File lib/danger_plugin.rb, line 95
def message_detailed_report
  wide_report_offenses = offenses[0...@limit_of_warnings]

  wide_report_offenses.each do |offense|
    msg = []
    msg << "## Syntax error detected:" if offense.serenity == :fail
    msg << offense.path
    msg << offense.message

    print_message(msg.join(" \n "), offense.serenity)
  end
end
message_hint() click to toggle source
# File lib/danger_plugin.rb, line 117
def message_hint
  file_names = offenses.map(&:path).uniq
  file_paths_sentance = Shellwords.join(file_names)

  msg = "To locally check these files run: \n `rubocop #{file_paths_sentance}`"
  print_message(msg, :warn)
end
offenses() click to toggle source
# File lib/danger_plugin.rb, line 41
def offenses
  @offenses ||= detect_offenses
end
print_message(message, serenity) click to toggle source
rubocop() click to toggle source
# File lib/danger_plugin.rb, line 64
def rubocop
  command = ['bundle', 'exec']
  command += ['rubocop']
  command += ['--force-exclusion']
  command += ['--format', 'json']
  command += ['--config', @config.shellescape] if @config

  `#{command.join(' ')} #{files_to_lint}`
end
serenity_lvl_for(cop_name) click to toggle source
# File lib/danger_plugin.rb, line 129
def serenity_lvl_for(cop_name)
  return :fail if cop_name == 'Lint/Syntax'

  :warn
end