module Utils

Public Instance Methods

diff_strings(a, b) click to toggle source

Creates a git-format diff of the two strings by writing them to temp files

# File lib/wcc/utils.rb, line 124
def diff_strings(a, b)
  File.write('a.tmp', a)
  File.write('b.tmp', b)
  diff = `git diff --no-index a.tmp b.tmp`
  File.delete('a.tmp', 'b.tmp')
  diff
end
each_addition_in_diff(passed_diff = nil, &block) click to toggle source
# File lib/wcc/utils.rb, line 69
def each_addition_in_diff(passed_diff = nil, &block)
  each_line_in_diff(passed_diff, type: :addition, &block)
end
each_file_in_diff(passed_diff = nil) { |file, diff| ... } click to toggle source
# File lib/wcc/utils.rb, line 60
def each_file_in_diff(passed_diff = nil)
  diffs = passed_diff ? [passed_diff] : parsed_diffs
  diffs&.flat_map do |diff|
    diff.files.flat_map do |file|
      yield(file, diff)
    end
  end
end
each_line_in_diff(passed_diff = nil, type: nil) { |l, hunk, file, diff| ... } click to toggle source
# File lib/wcc/utils.rb, line 73
def each_line_in_diff(passed_diff = nil, type: nil)
  each_file_in_diff(passed_diff) do |file, diff|
    file.hunks.flat_map do |hunk|
      lines = hunk.lines
      lines = lines.select { |l| l.public_send("#{type}?") } if type
      lines = lines.map { |l| yield(l, hunk, file, diff) } if block_given?
      lines
    end
  end
end
find_file_in_diff(filename) click to toggle source
# File lib/wcc/utils.rb, line 35
def find_file_in_diff(filename)
  each_file_in_diff do |file, _diff|
    return file if file.b_path == filename
  end
  nil
end
find_in_diff(regex) { |content.match(regex), l, hunk, file, diff| ... } click to toggle source

Finds lines in the overall diff matching the given regex, and executes a block for each matched line. The results of the yield block are returned as an array.

# File lib/wcc/utils.rb, line 45
def find_in_diff(regex)
  each_file_in_diff do |file, diff|
    file.hunks.flat_map do |hunk|
      lines = hunk.lines.select { |l| l.addition? && l.content =~ regex }
      if block_given?
        lines =
          lines.map do |l|
            yield(l.content.match(regex), l, hunk, file, diff)
          end
      end
      lines
    end
  end
end
issue(message, severity: 'message', file: nil, line: nil) click to toggle source

Adds a message to the Danger report with the given serverity - 'message', 'warn', or 'fail

# File lib/wcc/utils.rb, line 138
def issue(message, severity: 'message', file: nil, line: nil)
  case severity
  when 'message'
    plugin.message(message, file: file, line: line)
  when 'warn'
    plugin.warn(message, file: file, line: line)
  else
    plugin.fail(message, file: file, line: line)
  end
end
logger() click to toggle source
# File lib/wcc/utils.rb, line 13
def logger
  return @logger if @logger

  @logger = Logger.new(STDERR)
  @logger.level = ENV['DANGER_LOG_LEVEL'] ||
    (plugin.verbose ? Logger::INFO : Logger::ERROR)
  @logger
end
parsed_diffs() click to toggle source

All the diffs in the PR parsed into GitDiff objects

# File lib/wcc/utils.rb, line 23
def parsed_diffs
  @parsed_diffs ||=
    plugin.git.diff&.map do |d|
      begin
        GitDiff.from_string(d.patch)
      rescue StandardError
        logger.fatal "Error parsing patch:\n#{d.patch}"
        raise
      end
    end
end
plugin() click to toggle source
# File lib/wcc/utils.rb, line 7
def plugin
  # individual check classes usually set the '@plugin' variable,
  # otherwise this mixin was included on the plugin itself.
  @plugin || self
end
run(command) click to toggle source
# File lib/wcc/utils.rb, line 84
def run(command)
  logger.info "Executing command '#{command}'"
  result = `#{command}`
  logger.debug result
  result
end
run_and_diff(command = nil) { || ... } click to toggle source

Runs a command twice - once on the merge base, and once on the current working directory. Then, returns the git diff of the printed results.

# File lib/wcc/utils.rb, line 93
def run_and_diff(command = nil)
  unless command || block_given?
    raise ArgumentError('Must give command or block')
  end

  logger.info "Executing diff: '#{command}'"
  with_revision(plugin.github.base_commit) do |dir|
    initial = nil
    Dir.chdir(dir) do
      initial = command ? `#{command}` : yield
    end
    final = command ? `#{command}` : yield

    diff = diff_strings(initial, final)
    logger.debug diff
    diff
  end
end
with_revision(revision) { |dir| ... } click to toggle source

Executes a block after checking out the specified revision into a temp directory.

# File lib/wcc/utils.rb, line 114
def with_revision(revision)
  Dir.mktmpdir do |dir|
    logger.debug "Checking out revision #{revision} into #{dir}"
    system "git --work-tree=#{dir} checkout #{revision.strip} -- ."

    yield(dir)
  end
end